memcpy for mutable byte arrays
authorRoman Leshchinskiy <rl@cse.unsw.edu.au>
Sun, 4 Apr 2010 07:09:11 +0000 (07:09 +0000)
committerRoman Leshchinskiy <rl@cse.unsw.edu.au>
Sun, 4 Apr 2010 07:09:11 +0000 (07:09 +0000)
Data/Primitive/ByteArray.hs
cbits/memops.c [new file with mode: 0644]
cbits/memops.h [new file with mode: 0644]
primitive.cabal

index 626cc94..541d205 100644 (file)
@@ -1,4 +1,5 @@
-{-# LANGUAGE MagicHash, UnboxedTuples #-}
+{-# LANGUAGE MagicHash, UnboxedTuples, ForeignFunctionInterface,
+             UnliftedFFITypes #-}
 
 -- |
 -- Module      : Data.Primitive.ByteArray
@@ -24,6 +25,7 @@ module Data.Primitive.ByteArray (
 import Control.Monad.Primitive
 import Data.Primitive.Types
 
+import Foreign.C.Types
 import GHC.Base ( Int(..) )
 import GHC.Prim
 
@@ -111,3 +113,17 @@ writeByteArray
 writeByteArray (MutableByteArray arr#) (I# i#) x
   = primitive_ (writeByteArray# arr# i# x)
 
+memcpyMutableByteArray
+  :: PrimMonad m => MutableByteArray s -> Int -> MutableByteArray s -> Int
+                 -> Int -> m ()
+memcpyMutableByteArray (MutableByteArray dst#) doff
+                       (MutableByteArray src#) soff sz
+  = unsafePrimToPrim
+  $ memcpy_off dst# (fromIntegral doff) src# (fromIntegral soff)
+                    (fromIntegral sz)
+
+foreign import ccall unsafe "memops.h memcpy_off"
+  memcpy_off :: MutableByteArray# s -> CInt
+             -> MutableByteArray# s -> CInt
+             -> CSize -> IO ()
+
diff --git a/cbits/memops.c b/cbits/memops.c
new file mode 100644 (file)
index 0000000..809282c
--- /dev/null
@@ -0,0 +1,7 @@
+#include <string.h>
+
+void memcpy_off( void *dst, int doff, void *src, int soff, size_t len )
+{
+  memcpy( (char *)dst + doff, (char *)src + soff, len );
+}
+
diff --git a/cbits/memops.h b/cbits/memops.h
new file mode 100644 (file)
index 0000000..f7aa9a6
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef haskell_primitive_memops_h
+#define haskell_primitive_memops_h
+
+#include <stdlib.h>
+
+void memcpy_off( void *dst, int doff, void *src, int soff, size_t len );
+
+#endif
+
index 86fe0fc..e1796aa 100644 (file)
@@ -29,3 +29,10 @@ Library
 
   Build-Depends: base >= 2 && < 5, ghc-prim
 
+  Ghc-Options: -O2
+
+  Include-Dirs: cbits
+  Install-Includes: memops.h
+  includes: memops.h
+  c-sources: cbits/memops.c
+