Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Mapping Large File Haskell

Tags:

haskell

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.

like image 688
user3199023 Avatar asked Mar 18 '14 17:03

user3199023


1 Answers

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  ................
like image 198
Yuras Avatar answered Sep 22 '22 00:09

Yuras