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