X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Foreign%2FMarshal%2FArray.hs;h=9e8d08287da8174200729c7218dff9936277696b;hb=98daa4a448e019083d52ccec219a912021a0bfb0;hp=a28ccc80276152b5c6687d5a806024da210941b8;hpb=0290e82d74f9aa9f0ae06f96fd71ff93ec91c602;p=ghc-base.git diff --git a/Foreign/Marshal/Array.hs b/Foreign/Marshal/Array.hs index a28ccc8..9e8d082 100644 --- a/Foreign/Marshal/Array.hs +++ b/Foreign/Marshal/Array.hs @@ -1,4 +1,4 @@ -{-# OPTIONS -fno-implicit-prelude #-} +{-# OPTIONS_GHC -XNoImplicitPrelude #-} ----------------------------------------------------------------------------- -- | -- Module : Foreign.Marshal.Array @@ -62,18 +62,18 @@ module Foreign.Marshal.Array ( advancePtr, -- :: Storable a => Ptr a -> Int -> Ptr a ) where -import Control.Monad -import Foreign.Ptr (Ptr, plusPtr) -import Foreign.Storable (Storable(sizeOf,peekElemOff,pokeElemOff)) -import Foreign.Marshal.Alloc (mallocBytes, allocaBytes, reallocBytes) +import Foreign.Ptr (Ptr, plusPtr) +import Foreign.Storable (Storable(alignment,sizeOf,peekElemOff,pokeElemOff)) +import Foreign.Marshal.Alloc (mallocBytes, allocaBytesAligned, reallocBytes) import Foreign.Marshal.Utils (copyBytes, moveBytes) #ifdef __GLASGOW_HASKELL__ -import GHC.IOBase import GHC.Num import GHC.List import GHC.Err import GHC.Base +#else +import Control.Monad (zipWithM_) #endif -- allocation @@ -85,7 +85,7 @@ import GHC.Base mallocArray :: Storable a => Int -> IO (Ptr a) mallocArray = doMalloc undefined where - doMalloc :: Storable a => a -> Int -> IO (Ptr a) + doMalloc :: Storable a' => a' -> Int -> IO (Ptr a') doMalloc dummy size = mallocBytes (size * sizeOf dummy) -- |Like 'mallocArray', but add an extra position to hold a special @@ -100,21 +100,25 @@ mallocArray0 size = mallocArray (size + 1) allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b allocaArray = doAlloca undefined where - doAlloca :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b - doAlloca dummy size = allocaBytes (size * sizeOf dummy) + doAlloca :: Storable a' => a' -> Int -> (Ptr a' -> IO b') -> IO b' + doAlloca dummy size = allocaBytesAligned (size * sizeOf dummy) + (alignment dummy) -- |Like 'allocaArray', but add an extra position to hold a special -- termination element. -- allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b allocaArray0 size = allocaArray (size + 1) +{-# INLINE allocaArray0 #-} + -- needed to get allocaArray to inline into withCString, for unknown + -- reasons --SDM 23/4/2010, see #4004 for benchmark -- |Adjust the size of an array -- reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a) reallocArray = doRealloc undefined where - doRealloc :: Storable a => a -> Ptr a -> Int -> IO (Ptr a) + doRealloc :: Storable a' => a' -> Ptr a' -> Int -> IO (Ptr a') doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy) -- |Adjust the size of an array including an extra position for the end marker. @@ -126,10 +130,8 @@ reallocArray0 ptr size = reallocArray ptr (size + 1) -- marshalling -- ----------- --- |Convert an array of given length into a Haskell list. This version --- traverses the array backwards using an accumulating parameter, --- which uses constant stack space. The previous version using mapM --- needed linear stack space. +-- |Convert an array of given length into a Haskell list. The implementation +-- is tail-recursive and so uses constant stack space. -- peekArray :: Storable a => Int -> Ptr a -> IO [a] peekArray size ptr | size <= 0 = return [] @@ -151,9 +153,9 @@ pokeArray :: Storable a => Ptr a -> [a] -> IO () #ifndef __GLASGOW_HASKELL__ pokeArray ptr vals = zipWithM_ (pokeElemOff ptr) [0..] vals #else -pokeArray ptr vals = go vals 0# - where go [] n# = return () - go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#) +pokeArray ptr vals0 = go vals0 0# + where go [] _ = return () + go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#) #endif -- |Write the list elements consecutive into memory and terminate them with the @@ -165,9 +167,9 @@ pokeArray0 marker ptr vals = do pokeArray ptr vals pokeElemOff ptr (length vals) marker #else -pokeArray0 marker ptr vals = go vals 0# +pokeArray0 marker ptr vals0 = go vals0 0# where go [] n# = pokeElemOff ptr (I# n#) marker - go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#) + go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#) #endif @@ -237,7 +239,7 @@ withArrayLen0 marker vals f = copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO () copyArray = doCopy undefined where - doCopy :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO () + doCopy :: Storable a' => a' -> Ptr a' -> Ptr a' -> Int -> IO () doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy) -- |Copy the given number of elements from the second array (source) into the @@ -246,7 +248,7 @@ copyArray = doCopy undefined moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO () moveArray = doMove undefined where - doMove :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO () + doMove :: Storable a' => a' -> Ptr a' -> Ptr a' -> Int -> IO () doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy) @@ -271,5 +273,5 @@ lengthArray0 marker ptr = loop 0 advancePtr :: Storable a => Ptr a -> Int -> Ptr a advancePtr = doAdvance undefined where - doAdvance :: Storable a => a -> Ptr a -> Int -> Ptr a + doAdvance :: Storable a' => a' -> Ptr a' -> Int -> Ptr a' doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy)