1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.Marshal.Array
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- Marshalling support: routines allocating, storing, and retrieving Haskell
13 -- lists that are represented as arrays in the foreign language
15 -----------------------------------------------------------------------------
17 module Foreign.Marshal.Array (
18 -- * Marshalling arrays
22 mallocArray, -- :: Storable a => Int -> IO (Ptr a)
23 mallocArray0, -- :: Storable a => Int -> IO (Ptr a)
25 allocaArray, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
26 allocaArray0, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
28 reallocArray, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
29 reallocArray0, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
33 peekArray, -- :: Storable a => Int -> Ptr a -> IO [a]
34 peekArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
36 pokeArray, -- :: Storable a => Ptr a -> [a] -> IO ()
37 pokeArray0, -- :: Storable a => a -> Ptr a -> [a] -> IO ()
39 -- ** Combined allocation and marshalling
41 newArray, -- :: Storable a => [a] -> IO (Ptr a)
42 newArray0, -- :: Storable a => a -> [a] -> IO (Ptr a)
44 withArray, -- :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
45 withArray0, -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
47 withArrayLen, -- :: Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
48 withArrayLen0, -- :: Storable a => a -> [a] -> (Int -> Ptr a -> IO b) -> IO b
52 -- | (argument order: destination, source)
53 copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
54 moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
56 -- ** Finding the length
58 lengthArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
62 advancePtr, -- :: Storable a => Ptr a -> Int -> Ptr a
65 import Foreign.Ptr (Ptr, plusPtr)
66 import Foreign.Storable (Storable(sizeOf,peekElemOff,pokeElemOff))
67 import Foreign.Marshal.Alloc (mallocBytes, allocaBytes, reallocBytes)
68 import Foreign.Marshal.Utils (copyBytes, moveBytes)
70 #ifdef __GLASGOW_HASKELL__
76 import Control.Monad (zipWithM_)
82 -- |Allocate storage for the given number of elements of a storable type
83 -- (like 'Foreign.Marshal.Alloc.malloc', but for multiple elements).
85 mallocArray :: Storable a => Int -> IO (Ptr a)
86 mallocArray = doMalloc undefined
88 doMalloc :: Storable a' => a' -> Int -> IO (Ptr a')
89 doMalloc dummy size = mallocBytes (size * sizeOf dummy)
91 -- |Like 'mallocArray', but add an extra position to hold a special
92 -- termination element.
94 mallocArray0 :: Storable a => Int -> IO (Ptr a)
95 mallocArray0 size = mallocArray (size + 1)
97 -- |Temporarily allocate space for the given number of elements
98 -- (like 'Foreign.Marshal.Alloc.alloca', but for multiple elements).
100 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
101 allocaArray = doAlloca undefined
103 doAlloca :: Storable a' => a' -> Int -> (Ptr a' -> IO b') -> IO b'
104 doAlloca dummy size = allocaBytes (size * sizeOf dummy)
106 -- |Like 'allocaArray', but add an extra position to hold a special
107 -- termination element.
109 allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
110 allocaArray0 size = allocaArray (size + 1)
111 {-# INLINE allocaArray0 #-}
112 -- needed to get allocaArray to inline into withCString, for unknown
113 -- reasons --SDM 23/4/2010, see #4004 for benchmark
115 -- |Adjust the size of an array
117 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
118 reallocArray = doRealloc undefined
120 doRealloc :: Storable a' => a' -> Ptr a' -> Int -> IO (Ptr a')
121 doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy)
123 -- |Adjust the size of an array including an extra position for the end marker.
125 reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
126 reallocArray0 ptr size = reallocArray ptr (size + 1)
132 -- |Convert an array of given length into a Haskell list. The implementation
133 -- is tail-recursive and so uses constant stack space.
135 peekArray :: Storable a => Int -> Ptr a -> IO [a]
136 peekArray size ptr | size <= 0 = return []
137 | otherwise = f (size-1) []
139 f 0 acc = do e <- peekElemOff ptr 0; return (e:acc)
140 f n acc = do e <- peekElemOff ptr n; f (n-1) (e:acc)
142 -- |Convert an array terminated by the given end marker into a Haskell list
144 peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
145 peekArray0 marker ptr = do
146 size <- lengthArray0 marker ptr
149 -- |Write the list elements consecutive into memory
151 pokeArray :: Storable a => Ptr a -> [a] -> IO ()
152 #ifndef __GLASGOW_HASKELL__
153 pokeArray ptr vals = zipWithM_ (pokeElemOff ptr) [0..] vals
155 pokeArray ptr vals0 = go vals0 0#
156 where go [] _ = return ()
157 go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#)
160 -- |Write the list elements consecutive into memory and terminate them with the
161 -- given marker element
163 pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
164 #ifndef __GLASGOW_HASKELL__
165 pokeArray0 marker ptr vals = do
167 pokeElemOff ptr (length vals) marker
169 pokeArray0 marker ptr vals0 = go vals0 0#
170 where go [] n# = pokeElemOff ptr (I# n#) marker
171 go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#)
175 -- combined allocation and marshalling
176 -- -----------------------------------
178 -- |Write a list of storable elements into a newly allocated, consecutive
179 -- sequence of storable values
180 -- (like 'Foreign.Marshal.Utils.new', but for multiple elements).
182 newArray :: Storable a => [a] -> IO (Ptr a)
184 ptr <- mallocArray (length vals)
188 -- |Write a list of storable elements into a newly allocated, consecutive
189 -- sequence of storable values, where the end is fixed by the given end marker
191 newArray0 :: Storable a => a -> [a] -> IO (Ptr a)
192 newArray0 marker vals = do
193 ptr <- mallocArray0 (length vals)
194 pokeArray0 marker ptr vals
197 -- |Temporarily store a list of storable values in memory
198 -- (like 'Foreign.Marshal.Utils.with', but for multiple elements).
200 withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
201 withArray vals = withArrayLen vals . const
203 -- |Like 'withArray', but the action gets the number of values
204 -- as an additional parameter
206 withArrayLen :: Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
207 withArrayLen vals f =
208 allocaArray len $ \ptr -> do
215 -- |Like 'withArray', but a terminator indicates where the array ends
217 withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
218 withArray0 marker vals = withArrayLen0 marker vals . const
220 -- |Like 'withArrayLen', but a terminator indicates where the array ends
222 withArrayLen0 :: Storable a => a -> [a] -> (Int -> Ptr a -> IO b) -> IO b
223 withArrayLen0 marker vals f =
224 allocaArray0 len $ \ptr -> do
225 pokeArray0 marker ptr vals
232 -- copying (argument order: destination, source)
235 -- |Copy the given number of elements from the second array (source) into the
236 -- first array (destination); the copied areas may /not/ overlap
238 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
239 copyArray = doCopy undefined
241 doCopy :: Storable a' => a' -> Ptr a' -> Ptr a' -> Int -> IO ()
242 doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy)
244 -- |Copy the given number of elements from the second array (source) into the
245 -- first array (destination); the copied areas /may/ overlap
247 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
248 moveArray = doMove undefined
250 doMove :: Storable a' => a' -> Ptr a' -> Ptr a' -> Int -> IO ()
251 doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy)
254 -- finding the length
255 -- ------------------
257 -- |Return the number of elements in an array, excluding the terminator
259 lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int
260 lengthArray0 marker ptr = loop 0
263 val <- peekElemOff ptr i
264 if val == marker then return i else loop (i+1)
270 -- |Advance a pointer into an array by the given number of elements
272 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
273 advancePtr = doAdvance undefined
275 doAdvance :: Storable a' => a' -> Ptr a' -> Int -> Ptr a'
276 doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy)