b80b3de9eaf6b30283a23c78706df089e90ac814
[ghc-hetmet.git] / ghc / lib / std / PrelMarshalArray.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelMarshalArray.lhs,v 1.8 2001/08/15 09:54:38 qrczak Exp $
3 %
4 % (c) The FFI task force, 2000
5 %
6
7 Marshalling support: routines allocating, storing, and retrieving Haskell
8 lists that are represented as arrays in the foreign language
9
10 \begin{code}
11 {-# OPTIONS -fno-implicit-prelude #-}
12
13 module PrelMarshalArray (
14
15   -- allocation
16   --
17   mallocArray,    -- :: Storable a => Int -> IO (Ptr a)
18   mallocArray0,   -- :: Storable a => Int -> IO (Ptr a)
19
20   allocaArray,    -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
21   allocaArray0,   -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
22
23   reallocArray,   -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
24   reallocArray0,  -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
25
26   -- marshalling
27   --
28   peekArray,      -- :: Storable a =>         Int -> Ptr a -> IO [a]
29   peekArray0,     -- :: (Storable a, Eq a) => a   -> Ptr a -> IO [a]
30
31   pokeArray,      -- :: Storable a =>      Ptr a -> [a] -> IO ()
32   pokeArray0,     -- :: Storable a => a -> Ptr a -> [a] -> IO ()
33
34   -- combined allocation and marshalling
35   --
36   newArray,       -- :: Storable a =>      [a] -> IO (Ptr a)
37   newArray0,      -- :: Storable a => a -> [a] -> IO (Ptr a)
38
39   withArray,      -- :: Storable a =>      [a] -> (Ptr a -> IO b) -> IO b
40   withArray0,     -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
41
42   -- destruction
43   --
44   destructArray,  -- :: Storable a =>         Int -> Ptr a -> IO ()
45   destructArray0, -- :: (Storable a, Eq a) => a   -> Ptr a -> IO ()
46
47   -- copying (argument order: destination, source)
48   --
49   copyArray,      -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
50   moveArray,      -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
51
52   -- finding the length
53   --
54   lengthArray0,   -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
55
56   -- indexing
57   --
58   advancePtr      -- :: Storable a => Ptr a -> Int -> Ptr a
59 ) where
60
61 import Monad
62
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)
68 import PrelIOBase
69 import PrelNum
70 import PrelList
71 import PrelErr
72 import PrelBase
73 #endif
74
75 -- allocation
76 -- ----------
77
78 -- allocate storage for the given number of elements of a storable type
79 --
80 mallocArray :: Storable a => Int -> IO (Ptr a)
81 mallocArray  = doMalloc undefined
82   where
83     doMalloc            :: Storable a => a -> Int -> IO (Ptr a)
84     doMalloc dummy size  = mallocBytes (size * sizeOf dummy)
85
86 -- like `mallocArray', but add an extra element to signal the end of the array
87 --
88 mallocArray0      :: Storable a => Int -> IO (Ptr a)
89 mallocArray0 size  = mallocArray (size + 1)
90
91 -- temporarily allocate space for the given number of elements
92 --
93 -- * see `MarshalAlloc.alloca' for the storage lifetime constraints
94 --
95 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
96 allocaArray  = doAlloca undefined
97   where
98     doAlloca            :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b
99     doAlloca dummy size  = allocaBytes (size * sizeOf dummy)
100
101 -- like `allocaArray', but add an extra element to signal the end of the array
102 --
103 allocaArray0      :: Storable a => Int -> (Ptr a -> IO b) -> IO b
104 allocaArray0 size  = allocaArray (size + 1)
105
106 -- adjust the size of an array
107 --
108 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
109 reallocArray  = doRealloc undefined
110   where
111     doRealloc                :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
112     doRealloc dummy ptr size  = reallocBytes ptr (size * sizeOf dummy)
113
114 -- adjust the size of an array while adding an element for the end marker
115 --
116 reallocArray0          :: Storable a => Ptr a -> Int -> IO (Ptr a)
117 reallocArray0 ptr size  = reallocArray ptr (size + 1)
118
119
120 -- marshalling
121 -- -----------
122
123 -- convert an array of given length into a Haskell list.  This version
124 -- traverses the array backwards using an accumulating parameter,
125 -- which uses constant stack space.  The previous version using mapM
126 -- needed linear stack space.
127 --
128 peekArray          :: Storable a => Int -> Ptr a -> IO [a]
129 peekArray size ptr | size <= 0 = return []
130                    | otherwise = f (size-1) []
131   where
132     f 0 acc = do e <- peekElemOff ptr 0; return (e:acc)
133     f n acc = do e <- peekElemOff ptr n; f (n-1) (e:acc)
134
135 -- convert an array terminated by the given end marker into a Haskell list
136 --
137 peekArray0            :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
138 peekArray0 marker ptr  = loop 0
139   where
140     loop i = do
141         val <- peekElemOff ptr i
142         if val == marker then return [] else do
143             rest <- loop (i+1)
144             return (val:rest)
145
146 -- write the list elements consecutive into memory
147 --
148 pokeArray          :: Storable a => Ptr a -> [a] -> IO ()
149 pokeArray ptr vals  = zipWithM_ (pokeElemOff ptr) [0..] vals
150
151 -- write the list elements consecutive into memory and terminate them with the
152 -- given marker element
153 --
154 pokeArray0                 :: Storable a => a -> Ptr a -> [a] -> IO ()
155 pokeArray0 marker ptr vals  = do
156   pokeArray ptr vals
157   pokeElemOff ptr (length vals) marker
158
159
160 -- combined allocation and marshalling
161 -- -----------------------------------
162
163 -- write a list of storable elements into a newly allocated, consecutive
164 -- sequence of storable values
165 --
166 newArray      :: Storable a => [a] -> IO (Ptr a)
167 newArray vals  = do
168   ptr <- mallocArray (length vals)
169   pokeArray ptr vals
170   return ptr
171
172 -- write a list of storable elements into a newly allocated, consecutive
173 -- sequence of storable values, where the end is fixed by the given end marker
174 --
175 newArray0             :: Storable a => a -> [a] -> IO (Ptr a)
176 newArray0 marker vals  = do
177   ptr <- mallocArray0 (length vals)
178   pokeArray0 marker ptr vals
179   return ptr
180
181 -- temporarily store a list of storable values in memory
182 --
183 withArray        :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
184 withArray vals f  =
185   allocaArray len $ \ptr -> do
186       pokeArray ptr vals
187       res <- f ptr
188       destructArray len ptr
189       return res
190   where
191     len = length vals
192
193 -- like `withArray', but a terminator indicates where the array ends
194 --
195 withArray0               :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
196 withArray0 marker vals f  =
197   allocaArray0 len $ \ptr -> do
198       pokeArray0 marker ptr vals
199       res <- f ptr
200       destructArray (len+1) ptr
201       return res
202   where
203     len = length vals
204
205
206 -- destruction
207 -- -----------
208
209 -- destruct each element of an array (in reverse order)
210 --
211 destructArray          :: Storable a => Int -> Ptr a -> IO ()
212 destructArray size ptr  =
213   sequence_ [destruct (ptr `advancePtr` i)
214     | i <- [size-1, size-2 .. 0]]
215
216 -- like `destructArray', but a terminator indicates where the array ends
217 --
218 destructArray0            :: (Storable a, Eq a) => a -> Ptr a -> IO ()
219 destructArray0 marker ptr  = do
220   size <- lengthArray0 marker ptr
221   sequence_ [destruct (ptr `advancePtr` i)
222     | i <- [size, size-1 .. 0]]
223
224
225 -- copying (argument order: destination, source)
226 -- -------
227
228 -- copy the given number of elements from the second array (source) into the
229 -- first array (destination); the copied areas may *not* overlap
230 --
231 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
232 copyArray  = doCopy undefined
233   where
234     doCopy                     :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
235     doCopy dummy dest src size  = copyBytes dest src (size * sizeOf dummy)
236
237 -- copy the given number of elements from the second array (source) into the
238 -- first array (destination); the copied areas *may* overlap
239 --
240 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
241 moveArray  = doMove undefined
242   where
243     doMove                     :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
244     doMove dummy dest src size  = moveBytes dest src (size * sizeOf dummy)
245
246
247 -- finding the length
248 -- ------------------
249
250 -- return the number of elements in an array, excluding the terminator
251 --
252 lengthArray0            :: (Storable a, Eq a) => a -> Ptr a -> IO Int
253 lengthArray0 marker ptr  = loop 0
254   where
255     loop i = do
256         val <- peekElemOff ptr i
257         if val == marker then return i else loop (i+1)
258
259
260 -- indexing
261 -- --------
262
263 -- advance a pointer into an array by the given number of elements
264 --
265 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
266 advancePtr  = doAdvance undefined
267   where
268     doAdvance             :: Storable a => a -> Ptr a -> Int -> Ptr a
269     doAdvance dummy ptr i  = ptr `plusPtr` (i * sizeOf dummy)
270
271 \end{code}