I am experimenting with the Haskell mmap package and I am quite new to Haskell, so I am trying to get started by writing a little program to write a small amount of data to a memory mapped file.
This code correctly creates and file size but doesn't seem to flush the data from the vector to the memory mapped file; I verified this using hexdump - it's just all 0s.
What is going wrong?
import Control.Monad
import Data.Vector.Storable
import Foreign.Marshal.Array
import System.Directory
import System.IO
import System.IO.MMap
createFile :: FilePath -> Integer -> IO ()
createFile path size = do
h <- openBinaryFile path WriteMode
hSetFileSize h size
n = 10
size = 10 * 8
path = "test.dat" :: FilePath
main :: IO ()
main = do
createFile "signal.ml" size
let v = generate n (\i -> i) :: Vector Int
putStrLn $ show v
(ptr, s, _, _) <- mmapFilePtr path ReadWrite Nothing
unsafeWith v (\srcPtr -> copyArray ptr srcPtr n)
munmapFilePtr ptr s
Many thanks.
Looks like a typo. If I replace this:
createFile "signal.ml" size
with this:
createFile path size
I get correct result:
$ xxd test.dat
0000000: 0000 0000 0000 0000 0100 0000 0000 0000 ................
0000010: 0200 0000 0000 0000 0300 0000 0000 0000 ................
0000020: 0400 0000 0000 0000 0500 0000 0000 0000 ................
0000030: 0600 0000 0000 0000 0700 0000 0000 0000 ................
0000040: 0800 0000 0000 0000 0900 0000 0000 0000 ................
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With