projects
/
ghc-base.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
[project @ 2003-05-27 16:57:09 by malcolm]
[ghc-base.git]
/
Foreign
/
Marshal
/
Array.hs
diff --git
a/Foreign/Marshal/Array.hs
b/Foreign/Marshal/Array.hs
index
152b475
..
0cd959d
100644
(file)
--- a/
Foreign/Marshal/Array.hs
+++ b/
Foreign/Marshal/Array.hs
@@
-3,22
+3,21
@@
-- |
-- Module : Foreign.Marshal.Array
-- Copyright : (c) The FFI task force 2001
-- |
-- Module : Foreign.Marshal.Array
-- Copyright : (c) The FFI task force 2001
--- License : BSD-style (see the file libraries/core/LICENSE)
+-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : ffi@haskell.org
-- Stability : provisional
-- Portability : portable
--
--
-- Maintainer : ffi@haskell.org
-- Stability : provisional
-- Portability : portable
--
--- $Id: Array.hs,v 1.5 2002/04/24 16:31:44 simonmar Exp $
---
-- Marshalling support: routines allocating, storing, and retrieving Haskell
-- lists that are represented as arrays in the foreign language
--
-----------------------------------------------------------------------------
module Foreign.Marshal.Array (
-- Marshalling support: routines allocating, storing, and retrieving Haskell
-- lists that are represented as arrays in the foreign language
--
-----------------------------------------------------------------------------
module Foreign.Marshal.Array (
+ -- * Marshalling arrays
- -- allocation
+ -- ** Allocation
--
mallocArray, -- :: Storable a => Int -> IO (Ptr a)
mallocArray0, -- :: Storable a => Int -> IO (Ptr a)
--
mallocArray, -- :: Storable a => Int -> IO (Ptr a)
mallocArray0, -- :: Storable a => Int -> IO (Ptr a)
@@
-29,7
+28,7
@@
module Foreign.Marshal.Array (
reallocArray, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
reallocArray0, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
reallocArray, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
reallocArray0, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
- -- marshalling
+ -- ** Marshalling
--
peekArray, -- :: Storable a => Int -> Ptr a -> IO [a]
peekArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
--
peekArray, -- :: Storable a => Int -> Ptr a -> IO [a]
peekArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
@@
-37,7
+36,7
@@
module Foreign.Marshal.Array (
pokeArray, -- :: Storable a => Ptr a -> [a] -> IO ()
pokeArray0, -- :: Storable a => a -> Ptr a -> [a] -> IO ()
pokeArray, -- :: Storable a => Ptr a -> [a] -> IO ()
pokeArray0, -- :: Storable a => a -> Ptr a -> [a] -> IO ()
- -- combined allocation and marshalling
+ -- ** Combined allocation and marshalling
--
newArray, -- :: Storable a => [a] -> IO (Ptr a)
newArray0, -- :: Storable a => a -> [a] -> IO (Ptr a)
--
newArray, -- :: Storable a => [a] -> IO (Ptr a)
newArray0, -- :: Storable a => a -> [a] -> IO (Ptr a)
@@
-45,27
+44,28
@@
module Foreign.Marshal.Array (
withArray, -- :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray0, -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
withArray, -- :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray0, -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
- -- copying (argument order: destination, source)
- --
+ -- ** Copying
+
+ -- | (argument order: destination, source)
copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
- -- finding the length
+ -- ** Finding the length
--
lengthArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
--
lengthArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
- -- indexing
+ -- ** Indexing
--
advancePtr, -- :: Storable a => Ptr a -> Int -> Ptr a
) where
import Control.Monad
--
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 (alloca, mallocBytes, allocaBytes, reallocBytes)
+import Foreign.Marshal.Utils (copyBytes, moveBytes)
#ifdef __GLASGOW_HASKELL__
#ifdef __GLASGOW_HASKELL__
-import Foreign.Ptr (Ptr, plusPtr)
-import GHC.Storable (Storable(sizeOf,peekElemOff,pokeElemOff))
-import Foreign.Marshal.Alloc (mallocBytes, allocaBytes, reallocBytes)
-import Foreign.Marshal.Utils (copyBytes, moveBytes)
import GHC.IOBase
import GHC.Num
import GHC.List
import GHC.IOBase
import GHC.Num
import GHC.List
@@
-76,7
+76,7
@@
import GHC.Base
-- allocation
-- ----------
-- allocation
-- ----------
--- allocate storage for the given number of elements of a storable type
+-- |Allocate storage for the given number of elements of a storable type.
--
mallocArray :: Storable a => Int -> IO (Ptr a)
mallocArray = doMalloc undefined
--
mallocArray :: Storable a => Int -> IO (Ptr a)
mallocArray = doMalloc undefined
@@
-84,14
+84,14
@@
mallocArray = doMalloc undefined
doMalloc :: Storable a => a -> Int -> IO (Ptr a)
doMalloc dummy size = mallocBytes (size * sizeOf dummy)
doMalloc :: Storable a => a -> Int -> IO (Ptr a)
doMalloc dummy size = mallocBytes (size * sizeOf dummy)
--- like `mallocArray', but add an extra element to signal the end of the array
+-- |Like 'mallocArray', but add an extra element to signal the end of the array
--
mallocArray0 :: Storable a => Int -> IO (Ptr a)
mallocArray0 size = mallocArray (size + 1)
--
mallocArray0 :: Storable a => Int -> IO (Ptr a)
mallocArray0 size = mallocArray (size + 1)
--- temporarily allocate space for the given number of elements
+-- |Temporarily allocate space for the given number of elements.
--
--
--- * see `MarshalAlloc.alloca' for the storage lifetime constraints
+-- * see 'Foreign.Marshal.Alloc.alloca' for the storage lifetime constraints
--
allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray = doAlloca undefined
--
allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray = doAlloca undefined
@@
-99,12
+99,12
@@
allocaArray = doAlloca undefined
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 = allocaBytes (size * sizeOf dummy)
--- like `allocaArray', but add an extra element to signal the end of the array
+-- |Like 'allocaArray', but add an extra element to signal the end of the array
--
allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray0 size = allocaArray (size + 1)
--
allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray0 size = allocaArray (size + 1)
--- adjust the size of an array
+-- |Adjust the size of an array
--
reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
reallocArray = doRealloc undefined
--
reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
reallocArray = doRealloc undefined
@@
-112,7
+112,7
@@
reallocArray = doRealloc undefined
doRealloc :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy)
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 while adding an element for the end marker
+-- |Adjust the size of an array while adding an element for the end marker
--
reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
reallocArray0 ptr size = reallocArray ptr (size + 1)
--
reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
reallocArray0 ptr size = reallocArray ptr (size + 1)
@@
-121,7
+121,7
@@
reallocArray0 ptr size = reallocArray ptr (size + 1)
-- marshalling
-- -----------
-- marshalling
-- -----------
--- convert an array of given length into a Haskell list. This version
+-- |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.
-- traverses the array backwards using an accumulating parameter,
-- which uses constant stack space. The previous version using mapM
-- needed linear stack space.
@@
-133,7
+133,7
@@
peekArray size ptr | size <= 0 = return []
f 0 acc = do e <- peekElemOff ptr 0; return (e:acc)
f n acc = do e <- peekElemOff ptr n; f (n-1) (e:acc)
f 0 acc = do e <- peekElemOff ptr 0; return (e:acc)
f n acc = do e <- peekElemOff ptr n; f (n-1) (e:acc)
--- convert an array terminated by the given end marker into a Haskell list
+-- |Convert an array terminated by the given end marker into a Haskell list
--
peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
peekArray0 marker ptr = loop 0
--
peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
peekArray0 marker ptr = loop 0
@@
-144,24
+144,36
@@
peekArray0 marker ptr = loop 0
rest <- loop (i+1)
return (val:rest)
rest <- loop (i+1)
return (val:rest)
--- write the list elements consecutive into memory
+-- |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
+-- |Write the list elements consecutive into memory and terminate them with the
-- given marker element
--
-- 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
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
-- -----------------------------------
-- combined allocation and marshalling
-- -----------------------------------
--- write a list of storable elements into a newly allocated, consecutive
+-- |Write a list of storable elements into a newly allocated, consecutive
-- sequence of storable values
--
newArray :: Storable a => [a] -> IO (Ptr a)
-- sequence of storable values
--
newArray :: Storable a => [a] -> IO (Ptr a)
@@
-170,7
+182,7
@@
newArray vals = do
pokeArray ptr vals
return ptr
pokeArray ptr vals
return ptr
--- write a list of storable elements into a newly allocated, consecutive
+-- |Write a list of storable elements into a newly allocated, consecutive
-- sequence of storable values, where the end is fixed by the given end marker
--
newArray0 :: Storable a => a -> [a] -> IO (Ptr a)
-- sequence of storable values, where the end is fixed by the given end marker
--
newArray0 :: Storable a => a -> [a] -> IO (Ptr a)
@@
-179,7
+191,7
@@
newArray0 marker vals = do
pokeArray0 marker ptr vals
return ptr
pokeArray0 marker ptr vals
return ptr
--- temporarily store a list of storable values in memory
+-- |Temporarily store a list of storable values in memory
--
withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray vals f =
--
withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray vals f =
@@
-190,7
+202,7
@@
withArray vals f =
where
len = length vals
where
len = length vals
--- like `withArray', but a terminator indicates where the array ends
+-- |Like 'withArray', but a terminator indicates where the array ends
--
withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
withArray0 marker vals f =
--
withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
withArray0 marker vals f =
@@
-205,8
+217,8
@@
withArray0 marker vals f =
-- copying (argument order: destination, source)
-- -------
-- copying (argument order: destination, source)
-- -------
--- copy the given number of elements from the second array (source) into the
--- first array (destination); the copied areas may *not* overlap
+-- |Copy the given number of elements from the second array (source) into the
+-- first array (destination); the copied areas may /not/ overlap
--
copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray = doCopy undefined
--
copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray = doCopy undefined
@@
-214,8
+226,8
@@
copyArray = doCopy undefined
doCopy :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy)
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
--- first array (destination); the copied areas *may* overlap
+-- |Copy the given number of elements from the second array (source) into the
+-- first array (destination); the copied areas /may/ overlap
--
moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
moveArray = doMove undefined
--
moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
moveArray = doMove undefined
@@
-227,7
+239,7
@@
moveArray = doMove undefined
-- finding the length
-- ------------------
-- finding the length
-- ------------------
--- return the number of elements in an array, excluding the terminator
+-- |Return the number of elements in an array, excluding the terminator
--
lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int
lengthArray0 marker ptr = loop 0
--
lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int
lengthArray0 marker ptr = loop 0
@@
-240,7
+252,7
@@
lengthArray0 marker ptr = loop 0
-- indexing
-- --------
-- indexing
-- --------
--- advance a pointer into an array by the given number of elements
+-- |Advance a pointer into an array by the given number of elements
--
advancePtr :: Storable a => Ptr a -> Int -> Ptr a
advancePtr = doAdvance undefined
--
advancePtr :: Storable a => Ptr a -> Int -> Ptr a
advancePtr = doAdvance undefined