1 {-# OPTIONS -fno-implicit-prelude #-}
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
49 -- | (argument order: destination, source)
50 copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
51 moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
53 -- ** Finding the length
55 lengthArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
59 advancePtr, -- :: Storable a => Ptr a -> Int -> Ptr a
63 import Foreign.Ptr (Ptr, plusPtr)
64 import Foreign.Storable (Storable(sizeOf,peekElemOff,pokeElemOff))
65 import Foreign.Marshal.Alloc (alloca, mallocBytes, allocaBytes, reallocBytes)
66 import Foreign.Marshal.Utils (copyBytes, moveBytes)
68 #ifdef __GLASGOW_HASKELL__
79 -- |Allocate storage for the given number of elements of a storable type.
81 mallocArray :: Storable a => Int -> IO (Ptr a)
82 mallocArray = doMalloc undefined
84 doMalloc :: Storable a => a -> Int -> IO (Ptr a)
85 doMalloc dummy size = mallocBytes (size * sizeOf dummy)
87 -- |Like 'mallocArray', but add an extra element to signal the end of the array
89 mallocArray0 :: Storable a => Int -> IO (Ptr a)
90 mallocArray0 size = mallocArray (size + 1)
92 -- |Temporarily allocate space for the given number of elements.
94 -- * see 'Foreign.Marshal.Alloc.alloca' for the storage lifetime constraints
96 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
97 allocaArray = doAlloca undefined
99 doAlloca :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b
100 doAlloca dummy size = allocaBytes (size * sizeOf dummy)
102 -- |Like 'allocaArray', but add an extra element to signal the end of the array
104 allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
105 allocaArray0 size = allocaArray (size + 1)
107 -- |Adjust the size of an array
109 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
110 reallocArray = doRealloc undefined
112 doRealloc :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
113 doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy)
115 -- |Adjust the size of an array while adding an element for the end marker
117 reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
118 reallocArray0 ptr size = reallocArray ptr (size + 1)
124 -- |Convert an array of given length into a Haskell list. This version
125 -- traverses the array backwards using an accumulating parameter,
126 -- which uses constant stack space. The previous version using mapM
127 -- needed linear stack space.
129 peekArray :: Storable a => Int -> Ptr a -> IO [a]
130 peekArray size ptr | size <= 0 = return []
131 | otherwise = f (size-1) []
133 f 0 acc = do e <- peekElemOff ptr 0; return (e:acc)
134 f n acc = do e <- peekElemOff ptr n; f (n-1) (e:acc)
136 -- |Convert an array terminated by the given end marker into a Haskell list
138 peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
139 peekArray0 marker ptr = loop 0
142 val <- peekElemOff ptr i
143 if val == marker then return [] else do
147 -- |Write the list elements consecutive into memory
149 pokeArray :: Storable a => Ptr a -> [a] -> IO ()
150 #ifndef __GLASGOW_HASKELL__
151 pokeArray ptr vals = zipWithM_ (pokeElemOff ptr) [0..] vals
153 pokeArray ptr vals = go vals 0#
154 where go [] n# = return ()
155 go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#)
158 -- |Write the list elements consecutive into memory and terminate them with the
159 -- given marker element
161 pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
162 #ifndef __GLASGOW_HASKELL__
163 pokeArray0 marker ptr vals = do
165 pokeElemOff ptr (length vals) marker
167 pokeArray0 marker ptr vals = go vals 0#
168 where go [] n# = pokeElemOff ptr (I# n#) marker
169 go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#)
173 -- combined allocation and marshalling
174 -- -----------------------------------
176 -- |Write a list of storable elements into a newly allocated, consecutive
177 -- sequence of storable values
179 newArray :: Storable a => [a] -> IO (Ptr a)
181 ptr <- mallocArray (length vals)
185 -- |Write a list of storable elements into a newly allocated, consecutive
186 -- sequence of storable values, where the end is fixed by the given end marker
188 newArray0 :: Storable a => a -> [a] -> IO (Ptr a)
189 newArray0 marker vals = do
190 ptr <- mallocArray0 (length vals)
191 pokeArray0 marker ptr vals
194 -- |Temporarily store a list of storable values in memory
196 withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
198 allocaArray len $ \ptr -> do
205 -- |Like 'withArray', but a terminator indicates where the array ends
207 withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
208 withArray0 marker vals f =
209 allocaArray0 len $ \ptr -> do
210 pokeArray0 marker ptr vals
217 -- copying (argument order: destination, source)
220 -- |Copy the given number of elements from the second array (source) into the
221 -- first array (destination); the copied areas may /not/ overlap
223 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
224 copyArray = doCopy undefined
226 doCopy :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
227 doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy)
229 -- |Copy the given number of elements from the second array (source) into the
230 -- first array (destination); the copied areas /may/ overlap
232 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
233 moveArray = doMove undefined
235 doMove :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
236 doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy)
239 -- finding the length
240 -- ------------------
242 -- |Return the number of elements in an array, excluding the terminator
244 lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int
245 lengthArray0 marker ptr = loop 0
248 val <- peekElemOff ptr i
249 if val == marker then return i else loop (i+1)
255 -- |Advance a pointer into an array by the given number of elements
257 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
258 advancePtr = doAdvance undefined
260 doAdvance :: Storable a => a -> Ptr a -> Int -> Ptr a
261 doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy)