10

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.

1
  • 2
    The System.IO.MMap manual says that in case of changes "After some time in will be written to disk. It is unspecified when this happens", and that's a good question if it is possible to force it... Commented Mar 18, 2014 at 18:28

1 Answer 1

10

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  ................
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.