1 % -----------------------------------------------------------------------------
2 % $Id: PrelMarshalArray.lhs,v 1.4 2001/08/15 09:18:06 simonmar Exp $
4 % (c) The FFI task force, 2000
7 Marshalling support: routines allocating, storing, and retrieving Haskell
8 lists that are represented as arrays in the foreign language
11 {-# OPTIONS -fno-implicit-prelude #-}
13 module PrelMarshalArray (
17 mallocArray, -- :: Storable a => Int -> IO (Ptr a)
18 mallocArray0, -- :: Storable a => Int -> IO (Ptr a)
20 allocaArray, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
21 allocaArray0, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
23 reallocArray, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
24 reallocArray0, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
28 peekArray, -- :: Storable a => Int -> Ptr a -> IO [a]
29 peekArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
31 pokeArray, -- :: Storable a => Ptr a -> [a] -> IO ()
32 pokeArray0, -- :: Storable a => a -> Ptr a -> [a] -> IO ()
34 -- combined allocation and marshalling
36 newArray, -- :: Storable a => [a] -> IO (Ptr a)
37 newArray0, -- :: Storable a => a -> [a] -> IO (Ptr a)
39 withArray, -- :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
40 withArray0, -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
44 destructArray, -- :: Storable a => Int -> Ptr a -> IO ()
45 destructArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO ()
47 -- copying (argument order: destination, source)
49 copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
50 moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
54 lengthArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
58 advancePtr -- :: Storable a => Ptr a -> Int -> Ptr a
63 #ifdef __GLASGOW_HASKELL__
64 import PrelPtr (Ptr, plusPtr)
65 import PrelStorable (Storable(sizeOf,peekElemOff,pokeElemOff,destruct))
66 import PrelMarshalAlloc (mallocBytes, allocaBytes, reallocBytes)
67 import PrelMarshalUtils (copyBytes, moveBytes)
78 -- allocate storage for the given number of elements of a storable type
80 mallocArray :: Storable a => Int -> IO (Ptr a)
81 mallocArray = doMalloc undefined
83 doMalloc :: Storable a => a -> Int -> IO (Ptr a)
84 doMalloc dummy size = mallocBytes (size * sizeOf dummy)
86 -- like `mallocArray', but add an extra element to signal the end of the array
88 mallocArray0 :: Storable a => Int -> IO (Ptr a)
89 mallocArray0 size = mallocArray (size + 1)
91 -- temporarily allocate space for the given number of elements
93 -- * see `MarshalAlloc.alloca' for the storage lifetime constraints
95 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
96 allocaArray = doAlloca undefined
98 doAlloca :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b
99 doAlloca dummy size = allocaBytes (size * sizeOf dummy)
101 -- like `allocaArray', but add an extra element to signal the end of the array
103 allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
104 allocaArray0 size = allocaArray (size + 1)
106 -- adjust the size of an array
108 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
109 reallocArray = doRealloc undefined
111 doRealloc :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
112 doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy)
114 -- adjust the size of an array while adding an element for the end marker
116 reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
117 reallocArray0 ptr size = reallocArray ptr (size + 1)
123 -- convert an array of given length into a Haskell list. This version
124 -- traverses the array backwards using an accumulating parameter, which
125 -- uses constant stack space. The previous version using mapM
127 peekArray :: Storable a => Int -> Ptr a -> IO [a]
128 peekArray size ptr = f (size-1) []
130 f 0 acc = do e <- peekElemOff ptr 0; return (e:acc)
131 f n acc = do e <- peekElemOff ptr n; f n (e:acc)
133 -- convert an array terminated by the given end marker into a Haskell list
135 peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
136 peekArray0 marker ptr = loop 0
139 val <- peekElemOff ptr i
140 if val == marker then return [] else do
144 -- write the list elements consecutive into memory
146 pokeArray :: Storable a => Ptr a -> [a] -> IO ()
147 pokeArray ptr vals = zipWithM_ (pokeElemOff ptr) [0..] vals
149 -- write the list elements consecutive into memory and terminate them with the
150 -- given marker element
152 pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
153 pokeArray0 marker ptr vals = do
155 pokeElemOff ptr (length vals) marker
158 -- combined allocation and marshalling
159 -- -----------------------------------
161 -- write a list of storable elements into a newly allocated, consecutive
162 -- sequence of storable values
164 newArray :: Storable a => [a] -> IO (Ptr a)
166 ptr <- mallocArray (length vals)
170 -- write a list of storable elements into a newly allocated, consecutive
171 -- sequence of storable values, where the end is fixed by the given end marker
173 newArray0 :: Storable a => a -> [a] -> IO (Ptr a)
174 newArray0 marker vals = do
175 ptr <- mallocArray0 (length vals)
176 pokeArray0 marker ptr vals
179 -- temporarily store a list of storable values in memory
181 withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
183 allocaArray len $ \ptr -> do
186 destructArray len ptr
191 -- like `withArray', but a terminator indicates where the array ends
193 withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
194 withArray0 marker vals f =
195 allocaArray0 len $ \ptr -> do
196 pokeArray0 marker ptr vals
198 destructArray (len+1) ptr
207 -- destruct each element of an array (in reverse order)
209 destructArray :: Storable a => Int -> Ptr a -> IO ()
210 destructArray size ptr =
211 sequence_ [destruct (ptr `advancePtr` i)
212 | i <- [size-1, size-2 .. 0]]
214 -- like `destructArray', but a terminator indicates where the array ends
216 destructArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO ()
217 destructArray0 marker ptr = do
218 size <- lengthArray0 marker ptr
219 sequence_ [destruct (ptr `advancePtr` i)
220 | i <- [size, size-1 .. 0]]
223 -- copying (argument order: destination, source)
226 -- copy the given number of elements from the second array (source) into the
227 -- first array (destination); the copied areas may *not* overlap
229 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
230 copyArray = doCopy undefined
232 doCopy :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
233 doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy)
235 -- copy the given number of elements from the second array (source) into the
236 -- first array (destination); the copied areas *may* overlap
238 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
239 moveArray = doMove undefined
241 doMove :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
242 doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy)
245 -- finding the length
246 -- ------------------
248 -- return the number of elements in an array, excluding the terminator
250 lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int
251 lengthArray0 marker ptr = loop 0
254 val <- peekElemOff ptr i
255 if val == marker then return i else loop (i+1)
261 -- advance a pointer into an array by the given number of elements
263 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
264 advancePtr = doAdvance undefined
266 doAdvance :: Storable a => a -> Ptr a -> Int -> Ptr a
267 doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy)