) where
import Control.Monad
-
-#ifdef __GLASGOW_HASKELL__
-import Foreign.Ptr (Ptr, plusPtr)
-import GHC.Storable (Storable(sizeOf,peekElemOff,pokeElemOff))
+import Foreign.Ptr (Ptr, plusPtr)
+import Foreign.Storable (Storable(sizeOf,peekElemOff,pokeElemOff))
import Foreign.Marshal.Alloc (alloca, mallocBytes, allocaBytes, reallocBytes)
import Foreign.Marshal.Utils (copyBytes, moveBytes)
+
+#ifdef __GLASGOW_HASKELL__
import GHC.IOBase
import GHC.Num
import GHC.List
-- |Write the list elements consecutive into memory
--
-pokeArray :: Storable a => Ptr a -> [a] -> IO ()
-pokeArray ptr vals = zipWithM_ (pokeElemOff ptr) [0..] vals
+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#)
+#endif
-- |Write the list elements consecutive into memory and terminate them with the
-- given marker element
--
-pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
+pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
+#ifndef __GLASGOW_HASKELL__
pokeArray0 marker ptr vals = do
pokeArray ptr vals
pokeElemOff ptr (length vals) marker
+#else
+pokeArray0 marker ptr vals = go vals 0#
+ where go [] n# = pokeElemOff ptr (I# n#) marker
+ go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#)
+#endif
-- combined allocation and marshalling