mmap 0.1 → 0.2
raw patch · 4 files changed
+111/−4 lines, 4 files
Files
- cbits/posix.c +2/−2
- cbits/win32.c +5/−1
- mmap.cabal +1/−1
- tests/CpTest.hs +103/−0
cbits/posix.c view
@@ -22,7 +22,7 @@ access = O_RDONLY; break; case 1: - access = O_RDWR; + access = O_RDWR|O_CREAT; break; case 2: access = O_RDONLY; @@ -30,7 +30,7 @@ default: return NULL; } - handle = (void *)open(filepath,access|O_SHLOCK,0666); + handle = (void *)open(filepath,access,0666); if( handle==(void*)(-1) ) { //fprintf(stderr,"open errno %d\n",errno); return NULL;
cbits/win32.c view
@@ -20,17 +20,21 @@ */ void *handle = NULL; DWORD dwDesiredAccess; + DWORD dwCreationDisposition; if( !filepath ) return NULL; switch(mode) { case 0: dwDesiredAccess = GENERIC_READ; + dwCreationDisposition = OPEN_ALWAYS; break; case 1: dwDesiredAccess = GENERIC_WRITE|GENERIC_READ; + dwCreationDisposition = CREATE_ALWAYS; break; case 2: dwDesiredAccess = GENERIC_READ; + dwCreationDisposition = OPEN_ALWAYS; break; default: return NULL; @@ -39,7 +43,7 @@ dwDesiredAccess, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, - OPEN_EXISTING, + dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL); if( handle==INVALID_HANDLE_VALUE )
mmap.cabal view
@@ -1,5 +1,5 @@ Name: mmap-Version: 0.1+Version: 0.2 Stability: alpha License: BSD3 License-File: LICENSE
+ tests/CpTest.hs view
@@ -0,0 +1,103 @@+ + +module Main where + +import System.IO.MMap +import qualified Data.ByteString.Char8 as BSC +import qualified Data.ByteString.Lazy as BSL +import Data.Word +import Foreign.ForeignPtr +import Foreign.Storable +import Foreign.Ptr +import System.Mem +import Control.Concurrent +import Control.Exception as E +import Text.Printf +import Control.Exception +import System.CPUTime +import System.Environment +import System.IO +import Data.ByteString.Internal + + +time :: String -> IO t -> IO t +time txt a = do + printf "%s: " txt + start <- getCPUTime + v <- a + v `seq` return () + end <- getCPUTime + let diff = (fromIntegral (end - start)) / (10^12) + printf "%0.3f sec\n" (diff :: Double) + return v + + +main = do + [filename] <- getArgs + + time "Prelude copy" $ do + hin <- openBinaryFile filename ReadMode + hout <- openBinaryFile (filename ++ ".prelude") WriteMode + c <- hGetContents hin + hPutStr hout c + hClose hin + hClose hout + + time "ByteString copy" $ do + hin <- openBinaryFile filename ReadMode + hout <- openBinaryFile (filename ++ ".bytestring") WriteMode + c <- BSC.hGetContents hin + BSC.hPutStr hout c + hClose hin + hClose hout + + time "ByteString.Lazy copy" $ do + hin <- openBinaryFile filename ReadMode + hout <- openBinaryFile (filename ++ ".bytestringlazy") WriteMode + c <- BSL.hGetContents hin + BSL.hPutStr hout c + hClose hin + hClose hout + + time "MMap copy" $ do + (mmapin,fin1,size) <- mmapFilePtr filename ReadOnly Nothing + let newname = filename ++ ".mmap" + writeFile newname "" + (mmapout,fin2,size) <- mmapFilePtr newname ReadWrite (Just (0,size)) + memcpy mmapout mmapin (fromIntegral size) + fin1 + fin2 + + time "MMap copy lazy" $ do + chunksin <- mmapFileByteStringLazy filename Nothing + let newname = filename ++ ".mmaplazy" + hout <- openBinaryFile newname WriteMode + BSL.hPutStr hout chunksin + + {- + BSC.writeFile "test.bin" content + bs <- mmapFileByteString "test.bin" Nothing + BSC.putStrLn bs + print (bs == content) + bs2 <- mmapFileByteString "test.bin" (Just (5,5)) + print (bs2 == BSC.take 5 (BSC.drop 5 content)) + + -- create 5 gigabyte file + let l = 1024*1024*1024*5 + (f,s) <- mmapFileForeignPtr "test.bin" ReadWrite (Just (l,5)) + withForeignPtr f $ \f -> poke (castPtr f) (64::Word8) + + E.catch (do + bs3 <- mmapFileByteString "test.bin" Nothing + print (fromIntegral l==BSC.length bs3 + 5 )) + (\e -> print True -- exception here is also ok + ) + bs4 <- mmapFileByteStringLazy "test.bin" Nothing + print (BSL.fromChunks [content] == BSL.take (fromIntegral $ BSC.length content) bs4) + bs5 <- mmapFileByteStringLazy "test.bin" (Just (5,5)) + print (BSC.take 5 (BSC.drop 5 content) == BSC.concat (BSL.toChunks bs5)) + + System.Mem.performGC + threadDelay 10000 + + -}