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/core/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- $Id: Array.hs,v 1.3 2001/08/17 12:50:34 simonmar Exp $
14 -- Marshalling support: routines allocating, storing, and retrieving Haskell
15 -- lists that are represented as arrays in the foreign language
17 -----------------------------------------------------------------------------
19 module Foreign.Marshal.Array (
23 mallocArray, -- :: Storable a => Int -> IO (Ptr a)
24 mallocArray0, -- :: Storable a => Int -> IO (Ptr a)
26 allocaArray, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
27 allocaArray0, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
29 reallocArray, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
30 reallocArray0, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
34 peekArray, -- :: Storable a => Int -> Ptr a -> IO [a]
35 peekArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
37 pokeArray, -- :: Storable a => Ptr a -> [a] -> IO ()
38 pokeArray0, -- :: Storable a => a -> Ptr a -> [a] -> IO ()
40 -- combined allocation and marshalling
42 newArray, -- :: Storable a => [a] -> IO (Ptr a)
43 newArray0, -- :: Storable a => a -> [a] -> IO (Ptr a)
45 withArray, -- :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
46 withArray0, -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
50 destructArray, -- :: Storable a => Int -> Ptr a -> IO ()
51 destructArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO ()
53 -- copying (argument order: destination, source)
55 copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
56 moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
60 lengthArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
64 advancePtr -- :: Storable a => Ptr a -> Int -> Ptr a
69 #ifdef __GLASGOW_HASKELL__
70 import Foreign.Ptr (Ptr, plusPtr)
71 import GHC.Storable (Storable(sizeOf,peekElemOff,pokeElemOff,destruct))
72 import Foreign.Marshal.Alloc (mallocBytes, allocaBytes, reallocBytes)
73 import Foreign.Marshal.Utils (copyBytes, moveBytes)
84 -- allocate storage for the given number of elements of a storable type
86 mallocArray :: Storable a => Int -> IO (Ptr a)
87 mallocArray = doMalloc undefined
89 doMalloc :: Storable a => a -> Int -> IO (Ptr a)
90 doMalloc dummy size = mallocBytes (size * sizeOf dummy)
92 -- like `mallocArray', but add an extra element to signal the end of the array
94 mallocArray0 :: Storable a => Int -> IO (Ptr a)
95 mallocArray0 size = mallocArray (size + 1)
97 -- temporarily allocate space for the given number of elements
99 -- * see `MarshalAlloc.alloca' for the storage lifetime constraints
101 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
102 allocaArray = doAlloca undefined
104 doAlloca :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b
105 doAlloca dummy size = allocaBytes (size * sizeOf dummy)
107 -- like `allocaArray', but add an extra element to signal the end of the array
109 allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
110 allocaArray0 size = allocaArray (size + 1)
112 -- adjust the size of an array
114 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
115 reallocArray = doRealloc undefined
117 doRealloc :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
118 doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy)
120 -- adjust the size of an array while adding an element for the end marker
122 reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
123 reallocArray0 ptr size = reallocArray ptr (size + 1)
129 -- convert an array of given length into a Haskell list. This version
130 -- traverses the array backwards using an accumulating parameter,
131 -- which uses constant stack space. The previous version using mapM
132 -- needed linear stack space.
134 peekArray :: Storable a => Int -> Ptr a -> IO [a]
135 peekArray size ptr | size <= 0 = return []
136 | otherwise = f (size-1) []
138 f 0 acc = do e <- peekElemOff ptr 0; return (e:acc)
139 f n acc = do e <- peekElemOff ptr n; f (n-1) (e:acc)
141 -- convert an array terminated by the given end marker into a Haskell list
143 peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
144 peekArray0 marker ptr = loop 0
147 val <- peekElemOff ptr i
148 if val == marker then return [] else do
152 -- write the list elements consecutive into memory
154 pokeArray :: Storable a => Ptr a -> [a] -> IO ()
155 pokeArray ptr vals = zipWithM_ (pokeElemOff ptr) [0..] vals
157 -- write the list elements consecutive into memory and terminate them with the
158 -- given marker element
160 pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
161 pokeArray0 marker ptr vals = do
163 pokeElemOff ptr (length vals) marker
166 -- combined allocation and marshalling
167 -- -----------------------------------
169 -- write a list of storable elements into a newly allocated, consecutive
170 -- sequence of storable values
172 newArray :: Storable a => [a] -> IO (Ptr a)
174 ptr <- mallocArray (length vals)
178 -- write a list of storable elements into a newly allocated, consecutive
179 -- sequence of storable values, where the end is fixed by the given end marker
181 newArray0 :: Storable a => a -> [a] -> IO (Ptr a)
182 newArray0 marker vals = do
183 ptr <- mallocArray0 (length vals)
184 pokeArray0 marker ptr vals
187 -- temporarily store a list of storable values in memory
189 withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
191 allocaArray len $ \ptr -> do
194 destructArray len ptr
199 -- like `withArray', but a terminator indicates where the array ends
201 withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
202 withArray0 marker vals f =
203 allocaArray0 len $ \ptr -> do
204 pokeArray0 marker ptr vals
206 destructArray (len+1) ptr
215 -- destruct each element of an array (in reverse order)
217 destructArray :: Storable a => Int -> Ptr a -> IO ()
218 destructArray size ptr =
219 sequence_ [destruct (ptr `advancePtr` i)
220 | i <- [size-1, size-2 .. 0]]
222 -- like `destructArray', but a terminator indicates where the array ends
224 destructArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO ()
225 destructArray0 marker ptr = do
226 size <- lengthArray0 marker ptr
227 sequence_ [destruct (ptr `advancePtr` i)
228 | i <- [size, size-1 .. 0]]
231 -- copying (argument order: destination, source)
234 -- copy the given number of elements from the second array (source) into the
235 -- first array (destination); the copied areas may *not* overlap
237 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
238 copyArray = doCopy undefined
240 doCopy :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
241 doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy)
243 -- copy the given number of elements from the second array (source) into the
244 -- first array (destination); the copied areas *may* overlap
246 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
247 moveArray = doMove undefined
249 doMove :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
250 doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy)
253 -- finding the length
254 -- ------------------
256 -- return the number of elements in an array, excluding the terminator
258 lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int
259 lengthArray0 marker ptr = loop 0
262 val <- peekElemOff ptr i
263 if val == marker then return i else loop (i+1)
269 -- advance a pointer into an array by the given number of elements
271 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
272 advancePtr = doAdvance undefined
274 doAdvance :: Storable a => a -> Ptr a -> Int -> Ptr a
275 doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy)