[project @ 2001-06-28 14:15:04 by simonmar]
[packages/old-time.git] / Foreign / Marshal / Array.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 --
4 -- Module : Foreign.Marshal.Array
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
7 --
8 -- Maintainer : ffi@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- $Id: Array.hs,v 1.1 2001/06/28 14:15:03 simonmar Exp $
13 --
14 -- Marshalling support: routines allocating, storing, and retrieving Haskell
15 -- lists that are represented as arrays in the foreign language
16 --
17 -----------------------------------------------------------------------------
18
19 module Foreign.Marshal.Array (
20
21 -- allocation
22 --
23 mallocArray, -- :: Storable a => Int -> IO (Ptr a)
24 mallocArray0, -- :: Storable a => Int -> IO (Ptr a)
25
26 allocaArray, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
27 allocaArray0, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
28
29 reallocArray, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
30 reallocArray0, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
31
32 -- marshalling
33 --
34 peekArray, -- :: Storable a => Int -> Ptr a -> IO [a]
35 peekArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
36
37 pokeArray, -- :: Storable a => Ptr a -> [a] -> IO ()
38 pokeArray0, -- :: Storable a => a -> Ptr a -> [a] -> IO ()
39
40 -- combined allocation and marshalling
41 --
42 newArray, -- :: Storable a => [a] -> IO (Ptr a)
43 newArray0, -- :: Storable a => a -> [a] -> IO (Ptr a)
44
45 withArray, -- :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
46 withArray0, -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
47
48 -- destruction
49 --
50 destructArray, -- :: Storable a => Int -> Ptr a -> IO ()
51 destructArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO ()
52
53 -- copying (argument order: destination, source)
54 --
55 copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
56 moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
57
58 -- finding the length
59 --
60 lengthArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
61
62 -- indexing
63 --
64 advancePtr -- :: Storable a => Ptr a -> Int -> Ptr a
65 ) where
66
67 import Control.Monad
68
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)
74 import GHC.IOBase
75 import GHC.Num
76 import GHC.List
77 import GHC.Err
78 import GHC.Base
79 #endif
80
81 -- allocation
82 -- ----------
83
84 -- allocate storage for the given number of elements of a storable type
85 --
86 mallocArray :: Storable a => Int -> IO (Ptr a)
87 mallocArray = doMalloc undefined
88 where
89 doMalloc :: Storable a => a -> Int -> IO (Ptr a)
90 doMalloc dummy size = mallocBytes (size * sizeOf dummy)
91
92 -- like `mallocArray', but add an extra element to signal the end of the array
93 --
94 mallocArray0 :: Storable a => Int -> IO (Ptr a)
95 mallocArray0 size = mallocArray (size + 1)
96
97 -- temporarily allocate space for the given number of elements
98 --
99 -- * see `MarshalAlloc.alloca' for the storage lifetime constraints
100 --
101 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
102 allocaArray = doAlloca undefined
103 where
104 doAlloca :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b
105 doAlloca dummy size = allocaBytes (size * sizeOf dummy)
106
107 -- like `allocaArray', but add an extra element to signal the end of the array
108 --
109 allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
110 allocaArray0 size = allocaArray (size + 1)
111
112 -- adjust the size of an array
113 --
114 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
115 reallocArray = doRealloc undefined
116 where
117 doRealloc :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
118 doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy)
119
120 -- adjust the size of an array while adding an element for the end marker
121 --
122 reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
123 reallocArray0 ptr size = reallocArray ptr (size + 1)
124
125
126 -- marshalling
127 -- -----------
128
129 -- convert an array of given length into a Haskell list
130 --
131 peekArray :: Storable a => Int -> Ptr a -> IO [a]
132 peekArray size ptr = mapM (peekElemOff ptr) [0..size-1]
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 destructArray size ptr =
212 sequence_ [destruct (ptr `advancePtr` i)
213 | i <- [size-1, size-2 .. 0]]
214
215 -- like `destructArray', but a terminator indicates where the array ends
216 --
217 destructArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO ()
218 destructArray0 marker ptr = do
219 size <- lengthArray0 marker ptr
220 sequence_ [destruct (ptr `advancePtr` i)
221 | i <- [size, size-1 .. 0]]
222
223
224 -- copying (argument order: destination, source)
225 -- -------
226
227 -- copy the given number of elements from the second array (source) into the
228 -- first array (destination); the copied areas may *not* overlap
229 --
230 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
231 copyArray = doCopy undefined
232 where
233 doCopy :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
234 doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy)
235
236 -- copy the given number of elements from the second array (source) into the
237 -- first array (destination); the copied areas *may* overlap
238 --
239 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
240 moveArray = doMove undefined
241 where
242 doMove :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
243 doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy)
244
245
246 -- finding the length
247 -- ------------------
248
249 -- return the number of elements in an array, excluding the terminator
250 --
251 lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int
252 lengthArray0 marker ptr = loop 0
253 where
254 loop i = do
255 val <- peekElemOff ptr i
256 if val == marker then return i else loop (i+1)
257
258
259 -- indexing
260 -- --------
261
262 -- advance a pointer into an array by the given number of elements
263 --
264 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
265 advancePtr = doAdvance undefined
266 where
267 doAdvance :: Storable a => a -> Ptr a -> Int -> Ptr a
268 doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy)