[project @ 2004-03-30 07:05:46 by panne]
[ghc-base.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/base/LICENSE)
7 -- 
8 -- Maintainer  :  ffi@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  portable
11 --
12 -- Marshalling support: routines allocating, storing, and retrieving Haskell
13 -- lists that are represented as arrays in the foreign language
14 --
15 -----------------------------------------------------------------------------
16
17 module Foreign.Marshal.Array (
18   -- * Marshalling arrays
19
20   -- ** Allocation
21   --
22   mallocArray,    -- :: Storable a => Int -> IO (Ptr a)
23   mallocArray0,   -- :: Storable a => Int -> IO (Ptr a)
24
25   allocaArray,    -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
26   allocaArray0,   -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
27
28   reallocArray,   -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
29   reallocArray0,  -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
30
31   -- ** Marshalling
32   --
33   peekArray,      -- :: Storable a =>         Int -> Ptr a -> IO [a]
34   peekArray0,     -- :: (Storable a, Eq a) => a   -> Ptr a -> IO [a]
35
36   pokeArray,      -- :: Storable a =>      Ptr a -> [a] -> IO ()
37   pokeArray0,     -- :: Storable a => a -> Ptr a -> [a] -> IO ()
38
39   -- ** Combined allocation and marshalling
40   --
41   newArray,       -- :: Storable a =>      [a] -> IO (Ptr a)
42   newArray0,      -- :: Storable a => a -> [a] -> IO (Ptr a)
43
44   withArray,      -- :: Storable a =>      [a] -> (Ptr a -> IO b) -> IO b
45   withArray0,     -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
46
47   withArrayLen,   -- :: Storable a =>      [a] -> (Int -> Ptr a -> IO b) -> IO b
48   withArrayLen0,  -- :: Storable a => a -> [a] -> (Int -> Ptr a -> IO b) -> IO b
49
50   -- ** Copying
51
52   -- | (argument order: destination, source)
53   copyArray,      -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
54   moveArray,      -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
55
56   -- ** Finding the length
57   --
58   lengthArray0,   -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
59
60   -- ** Indexing
61   --
62   advancePtr,     -- :: Storable a => Ptr a -> Int -> Ptr a
63 ) where
64
65 import Control.Monad
66 import Foreign.Ptr      (Ptr, plusPtr)
67 import Foreign.Storable (Storable(sizeOf,peekElemOff,pokeElemOff))
68 import Foreign.Marshal.Alloc (mallocBytes, allocaBytes, reallocBytes)
69 import Foreign.Marshal.Utils (copyBytes, moveBytes)
70
71 #ifdef __GLASGOW_HASKELL__
72 import GHC.IOBase
73 import GHC.Num
74 import GHC.List
75 import GHC.Err
76 import GHC.Base
77 #endif
78
79 -- allocation
80 -- ----------
81
82 -- |Allocate storage for the given number of elements of a storable type.
83 --
84 mallocArray :: Storable a => Int -> IO (Ptr a)
85 mallocArray  = doMalloc undefined
86   where
87     doMalloc            :: Storable a => a -> Int -> IO (Ptr a)
88     doMalloc dummy size  = mallocBytes (size * sizeOf dummy)
89
90 -- |Like 'mallocArray', but add an extra element to signal the end of the array
91 --
92 mallocArray0      :: Storable a => Int -> IO (Ptr a)
93 mallocArray0 size  = mallocArray (size + 1)
94
95 -- |Temporarily allocate space for the given number of elements.
96 --
97 -- * see 'Foreign.Marshal.Alloc.alloca' for the storage lifetime constraints
98 --
99 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
100 allocaArray  = doAlloca undefined
101   where
102     doAlloca            :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b
103     doAlloca dummy size  = allocaBytes (size * sizeOf dummy)
104
105 -- |Like 'allocaArray', but add an extra element to signal the end of the array
106 --
107 allocaArray0      :: Storable a => Int -> (Ptr a -> IO b) -> IO b
108 allocaArray0 size  = allocaArray (size + 1)
109
110 -- |Adjust the size of an array
111 --
112 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
113 reallocArray  = doRealloc undefined
114   where
115     doRealloc                :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
116     doRealloc dummy ptr size  = reallocBytes ptr (size * sizeOf dummy)
117
118 -- |Adjust the size of an array while adding an element for the end marker
119 --
120 reallocArray0          :: Storable a => Ptr a -> Int -> IO (Ptr a)
121 reallocArray0 ptr size  = reallocArray ptr (size + 1)
122
123
124 -- marshalling
125 -- -----------
126
127 -- |Convert an array of given length into a Haskell list.  This version
128 -- traverses the array backwards using an accumulating parameter,
129 -- which uses constant stack space.  The previous version using mapM
130 -- needed linear stack space.
131 --
132 peekArray          :: Storable a => Int -> Ptr a -> IO [a]
133 peekArray size ptr | size <= 0 = return []
134                  | otherwise = f (size-1) []
135   where
136     f 0 acc = do e <- peekElemOff ptr 0; return (e:acc)
137     f n acc = do e <- peekElemOff ptr n; f (n-1) (e:acc)
138   
139 -- |Convert an array terminated by the given end marker into a Haskell list
140 --
141 peekArray0            :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
142 peekArray0 marker ptr  = do
143   size <- lengthArray0 marker ptr
144   peekArray size ptr
145
146 -- |Write the list elements consecutive into memory
147 --
148 pokeArray :: Storable a => Ptr a -> [a] -> IO ()
149 #ifndef __GLASGOW_HASKELL__
150 pokeArray ptr vals =  zipWithM_ (pokeElemOff ptr) [0..] vals
151 #else
152 pokeArray ptr vals = go vals 0#
153   where go [] n#         = return ()
154         go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#)
155 #endif
156
157 -- |Write the list elements consecutive into memory and terminate them with the
158 -- given marker element
159 --
160 pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
161 #ifndef __GLASGOW_HASKELL__
162 pokeArray0 marker ptr vals  = do
163   pokeArray ptr vals
164   pokeElemOff ptr (length vals) marker
165 #else
166 pokeArray0 marker ptr vals = go vals 0#
167   where go [] n#         = pokeElemOff ptr (I# n#) marker
168         go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#)
169 #endif
170
171
172 -- combined allocation and marshalling
173 -- -----------------------------------
174
175 -- |Write a list of storable elements into a newly allocated, consecutive
176 -- sequence of storable values
177 --
178 newArray      :: Storable a => [a] -> IO (Ptr a)
179 newArray vals  = do
180   ptr <- mallocArray (length vals)
181   pokeArray ptr vals
182   return ptr
183
184 -- |Write a list of storable elements into a newly allocated, consecutive
185 -- sequence of storable values, where the end is fixed by the given end marker
186 --
187 newArray0             :: Storable a => a -> [a] -> IO (Ptr a)
188 newArray0 marker vals  = do
189   ptr <- mallocArray0 (length vals)
190   pokeArray0 marker ptr vals
191   return ptr
192
193 -- |Temporarily store a list of storable values in memory
194 --
195 withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
196 withArray vals = withArrayLen vals . const
197
198 -- |Like 'withArray', but the action gets the number of values
199 -- as an additional parameter
200 --
201 withArrayLen :: Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
202 withArrayLen vals f  =
203   allocaArray len $ \ptr -> do
204       pokeArray ptr vals
205       res <- f len ptr
206       return res
207   where
208     len = length vals
209
210 -- |Like 'withArray', but a terminator indicates where the array ends
211 --
212 withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
213 withArray0 marker vals = withArrayLen0 marker vals . const
214
215 -- |Like 'withArrayLen', but a terminator indicates where the array ends
216 --
217 withArrayLen0 :: Storable a => a -> [a] -> (Int -> Ptr a -> IO b) -> IO b
218 withArrayLen0 marker vals f  =
219   allocaArray0 len $ \ptr -> do
220       pokeArray0 marker ptr vals
221       res <- f len ptr
222       return res
223   where
224     len = length vals
225
226
227 -- copying (argument order: destination, source)
228 -- -------
229
230 -- |Copy the given number of elements from the second array (source) into the
231 -- first array (destination); the copied areas may /not/ overlap
232 --
233 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
234 copyArray  = doCopy undefined
235   where
236     doCopy                     :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
237     doCopy dummy dest src size  = copyBytes dest src (size * sizeOf dummy)
238
239 -- |Copy the given number of elements from the second array (source) into the
240 -- first array (destination); the copied areas /may/ overlap
241 --
242 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
243 moveArray  = doMove undefined
244   where
245     doMove                     :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
246     doMove dummy dest src size  = moveBytes dest src (size * sizeOf dummy)
247
248
249 -- finding the length
250 -- ------------------
251
252 -- |Return the number of elements in an array, excluding the terminator
253 --
254 lengthArray0            :: (Storable a, Eq a) => a -> Ptr a -> IO Int
255 lengthArray0 marker ptr  = loop 0
256   where
257     loop i = do
258         val <- peekElemOff ptr i
259         if val == marker then return i else loop (i+1)
260
261
262 -- indexing
263 -- --------
264
265 -- |Advance a pointer into an array by the given number of elements
266 --
267 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
268 advancePtr  = doAdvance undefined
269   where
270     doAdvance             :: Storable a => a -> Ptr a -> Int -> Ptr a
271     doAdvance dummy ptr i  = ptr `plusPtr` (i * sizeOf dummy)