1 % -----------------------------------------------------------------------------
2 % $Id: PrelMarshalArray.lhs,v 1.2 2001/03/15 20:35:49 qrczak 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 module PrelMarshalArray (
15 mallocArray, -- :: Storable a => Int -> IO (Ptr a)
16 mallocArray0, -- :: Storable a => Int -> IO (Ptr a)
18 allocaArray, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
19 allocaArray0, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
21 reallocArray, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
22 reallocArray0, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
26 peekArray, -- :: Storable a => Int -> Ptr a -> IO [a]
27 peekArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
29 pokeArray, -- :: Storable a => Ptr a -> [a] -> IO ()
30 pokeArray0, -- :: Storable a => a -> Ptr a -> [a] -> IO ()
32 -- combined allocation and marshalling
34 newArray, -- :: Storable a => [a] -> IO (Ptr a)
35 newArray0, -- :: Storable a => a -> [a] -> IO (Ptr a)
37 withArray, -- :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
38 withArray0, -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
42 destructArray, -- :: Storable a => Int -> Ptr a -> IO ()
43 destructArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO ()
45 -- copying (argument order: destination, source)
47 copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
48 moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
52 lengthArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
56 advancePtr -- :: Storable a => Ptr a -> Int -> Ptr a
59 import Monad (zipWithM_)
61 import PrelPtr (Ptr, plusPtr)
62 import PrelStorable (Storable(sizeOf,peekElemOff,pokeElemOff,destruct))
63 import PrelMarshalAlloc (mallocBytes, allocaBytes, reallocBytes)
64 import PrelMarshalUtils (copyBytes, moveBytes)
70 -- allocate storage for the given number of elements of a storable type
72 mallocArray :: Storable a => Int -> IO (Ptr a)
73 mallocArray = doMalloc undefined
75 doMalloc :: Storable a => a -> Int -> IO (Ptr a)
76 doMalloc dummy size = mallocBytes (size * sizeOf dummy)
78 -- like `mallocArray', but add an extra element to signal the end of the array
80 mallocArray0 :: Storable a => Int -> IO (Ptr a)
81 mallocArray0 size = mallocArray (size + 1)
83 -- temporarily allocate space for the given number of elements
85 -- * see `MarshalAlloc.alloca' for the storage lifetime constraints
87 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
88 allocaArray = doAlloca undefined
90 doAlloca :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b
91 doAlloca dummy size = allocaBytes (size * sizeOf dummy)
93 -- like `allocaArray', but add an extra element to signal the end of the array
95 allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
96 allocaArray0 size = allocaArray (size + 1)
98 -- adjust the size of an array
100 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
101 reallocArray = doRealloc undefined
103 doRealloc :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
104 doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy)
106 -- adjust the size of an array while adding an element for the end marker
108 reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
109 reallocArray0 ptr size = reallocArray ptr (size + 1)
115 -- convert an array of given length into a Haskell list
117 peekArray :: Storable a => Int -> Ptr a -> IO [a]
118 peekArray size ptr = mapM (peekElemOff ptr) [0..size-1]
120 -- convert an array terminated by the given end marker into a Haskell list
122 peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
123 peekArray0 marker ptr = loop 0
126 val <- peekElemOff ptr i
127 if val == marker then return [] else do
131 -- write the list elements consecutive into memory
133 pokeArray :: Storable a => Ptr a -> [a] -> IO ()
134 pokeArray ptr vals = zipWithM_ (pokeElemOff ptr) [0..] vals
136 -- write the list elements consecutive into memory and terminate them with the
137 -- given marker element
139 pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
140 pokeArray0 marker ptr vals = do
142 pokeElemOff ptr (length vals) marker
145 -- combined allocation and marshalling
146 -- -----------------------------------
148 -- write a list of storable elements into a newly allocated, consecutive
149 -- sequence of storable values
151 newArray :: Storable a => [a] -> IO (Ptr a)
153 ptr <- mallocArray (length vals)
157 -- write a list of storable elements into a newly allocated, consecutive
158 -- sequence of storable values, where the end is fixed by the given end marker
160 newArray0 :: Storable a => a -> [a] -> IO (Ptr a)
161 newArray0 marker vals = do
162 ptr <- mallocArray0 (length vals)
163 pokeArray0 marker ptr vals
166 -- temporarily store a list of storable values in memory
168 withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
170 allocaArray len $ \ptr -> do
173 destructArray len ptr
178 -- like `withArray', but a terminator indicates where the array ends
180 withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
181 withArray0 marker vals f =
182 allocaArray0 len $ \ptr -> do
183 pokeArray0 marker ptr vals
185 destructArray (len+1) ptr
194 -- destruct each element of an array (in reverse order)
196 destructArray :: Storable a => Int -> Ptr a -> IO ()
197 destructArray size ptr =
198 sequence_ [destruct (ptr `advancePtr` i)
199 | i <- [size-1, size-2 .. 0]]
201 -- like `destructArray', but a terminator indicates where the array ends
203 destructArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO ()
204 destructArray0 marker ptr = do
205 size <- lengthArray0 marker ptr
206 sequence_ [destruct (ptr `advancePtr` i)
207 | i <- [size, size-1 .. 0]]
210 -- copying (argument order: destination, source)
213 -- copy the given number of elements from the second array (source) into the
214 -- first array (destination); the copied areas may *not* overlap
216 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
217 copyArray = doCopy undefined
219 doCopy :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
220 doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy)
222 -- copy the given number of elements from the second array (source) into the
223 -- first array (destination); the copied areas *may* overlap
225 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
226 moveArray = doMove undefined
228 doMove :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
229 doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy)
232 -- finding the length
233 -- ------------------
235 -- return the number of elements in an array, excluding the terminator
237 lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int
238 lengthArray0 marker ptr = loop 0
241 val <- peekElemOff ptr i
242 if val == marker then return i else loop (i+1)
248 -- advance a pointer into an array by the given number of elements
250 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
251 advancePtr = doAdvance undefined
253 doAdvance :: Storable a => a -> Ptr a -> Int -> Ptr a
254 doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy)