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