Remove unused imports
[ghc-base.git] / Foreign / Marshal / Array.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
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 Foreign.Ptr      (Ptr, plusPtr)
66 import Foreign.Storable (Storable(sizeOf,peekElemOff,pokeElemOff))
67 import Foreign.Marshal.Alloc (mallocBytes, allocaBytes, reallocBytes)
68 import Foreign.Marshal.Utils (copyBytes, moveBytes)
69
70 #ifdef __GLASGOW_HASKELL__
71 import GHC.IOBase
72 import GHC.Num
73 import GHC.List
74 import GHC.Err
75 import GHC.Base
76 #endif
77
78 -- allocation
79 -- ----------
80
81 -- |Allocate storage for the given number of elements of a storable type
82 -- (like 'Foreign.Marshal.Alloc.malloc', but for multiple elements).
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 position to hold a special
91 -- termination element.
92 --
93 mallocArray0      :: Storable a => Int -> IO (Ptr a)
94 mallocArray0 size  = mallocArray (size + 1)
95
96 -- |Temporarily allocate space for the given number of elements
97 -- (like 'Foreign.Marshal.Alloc.alloca', but for multiple elements).
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 position to hold a special
106 -- termination element.
107 --
108 allocaArray0      :: Storable a => Int -> (Ptr a -> IO b) -> IO b
109 allocaArray0 size  = allocaArray (size + 1)
110
111 -- |Adjust the size of an array
112 --
113 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
114 reallocArray  = doRealloc undefined
115   where
116     doRealloc                :: Storable a' => a' -> Ptr a' -> Int -> IO (Ptr a')
117     doRealloc dummy ptr size  = reallocBytes ptr (size * sizeOf dummy)
118
119 -- |Adjust the size of an array including an extra position for the end marker.
120 --
121 reallocArray0          :: Storable a => Ptr a -> Int -> IO (Ptr a)
122 reallocArray0 ptr size  = reallocArray ptr (size + 1)
123
124
125 -- marshalling
126 -- -----------
127
128 -- |Convert an array of given length into a Haskell list.  This version
129 -- traverses the array backwards using an accumulating parameter,
130 -- which uses constant stack space.  The previous version using mapM
131 -- needed linear stack space.
132 --
133 peekArray          :: Storable a => Int -> Ptr a -> IO [a]
134 peekArray size ptr | size <= 0 = return []
135                  | otherwise = f (size-1) []
136   where
137     f 0 acc = do e <- peekElemOff ptr 0; return (e:acc)
138     f n acc = do e <- peekElemOff ptr n; f (n-1) (e:acc)
139   
140 -- |Convert an array terminated by the given end marker into a Haskell list
141 --
142 peekArray0            :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
143 peekArray0 marker ptr  = do
144   size <- lengthArray0 marker ptr
145   peekArray size ptr
146
147 -- |Write the list elements consecutive into memory
148 --
149 pokeArray :: Storable a => Ptr a -> [a] -> IO ()
150 #ifndef __GLASGOW_HASKELL__
151 pokeArray ptr vals =  zipWithM_ (pokeElemOff ptr) [0..] vals
152 #else
153 pokeArray ptr vals = go vals 0#
154   where go [] n#         = return ()
155         go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#)
156 #endif
157
158 -- |Write the list elements consecutive into memory and terminate them with the
159 -- given marker element
160 --
161 pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
162 #ifndef __GLASGOW_HASKELL__
163 pokeArray0 marker ptr vals  = do
164   pokeArray ptr vals
165   pokeElemOff ptr (length vals) marker
166 #else
167 pokeArray0 marker ptr vals = go vals 0#
168   where go [] n#         = pokeElemOff ptr (I# n#) marker
169         go (val:vals) n# = do pokeElemOff ptr (I# n#) val; go vals (n# +# 1#)
170 #endif
171
172
173 -- combined allocation and marshalling
174 -- -----------------------------------
175
176 -- |Write a list of storable elements into a newly allocated, consecutive
177 -- sequence of storable values
178 -- (like 'Foreign.Marshal.Utils.new', but for multiple elements).
179 --
180 newArray      :: Storable a => [a] -> IO (Ptr a)
181 newArray vals  = do
182   ptr <- mallocArray (length vals)
183   pokeArray ptr vals
184   return ptr
185
186 -- |Write a list of storable elements into a newly allocated, consecutive
187 -- sequence of storable values, where the end is fixed by the given end marker
188 --
189 newArray0             :: Storable a => a -> [a] -> IO (Ptr a)
190 newArray0 marker vals  = do
191   ptr <- mallocArray0 (length vals)
192   pokeArray0 marker ptr vals
193   return ptr
194
195 -- |Temporarily store a list of storable values in memory
196 -- (like 'Foreign.Marshal.Utils.with', but for multiple elements).
197 --
198 withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
199 withArray vals = withArrayLen vals . const
200
201 -- |Like 'withArray', but the action gets the number of values
202 -- as an additional parameter
203 --
204 withArrayLen :: Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
205 withArrayLen vals f  =
206   allocaArray len $ \ptr -> do
207       pokeArray ptr vals
208       res <- f len ptr
209       return res
210   where
211     len = length vals
212
213 -- |Like 'withArray', but a terminator indicates where the array ends
214 --
215 withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
216 withArray0 marker vals = withArrayLen0 marker vals . const
217
218 -- |Like 'withArrayLen', but a terminator indicates where the array ends
219 --
220 withArrayLen0 :: Storable a => a -> [a] -> (Int -> Ptr a -> IO b) -> IO b
221 withArrayLen0 marker vals f  =
222   allocaArray0 len $ \ptr -> do
223       pokeArray0 marker ptr vals
224       res <- f len ptr
225       return res
226   where
227     len = length vals
228
229
230 -- copying (argument order: destination, source)
231 -- -------
232
233 -- |Copy the given number of elements from the second array (source) into the
234 -- first array (destination); the copied areas may /not/ overlap
235 --
236 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
237 copyArray  = doCopy undefined
238   where
239     doCopy                     :: Storable a' => a' -> Ptr a' -> Ptr a' -> Int -> IO ()
240     doCopy dummy dest src size  = copyBytes dest src (size * sizeOf dummy)
241
242 -- |Copy the given number of elements from the second array (source) into the
243 -- first array (destination); the copied areas /may/ overlap
244 --
245 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
246 moveArray  = doMove undefined
247   where
248     doMove                     :: Storable a' => a' -> Ptr a' -> Ptr a' -> Int -> IO ()
249     doMove dummy dest src size  = moveBytes dest src (size * sizeOf dummy)
250
251
252 -- finding the length
253 -- ------------------
254
255 -- |Return the number of elements in an array, excluding the terminator
256 --
257 lengthArray0            :: (Storable a, Eq a) => a -> Ptr a -> IO Int
258 lengthArray0 marker ptr  = loop 0
259   where
260     loop i = do
261         val <- peekElemOff ptr i
262         if val == marker then return i else loop (i+1)
263
264
265 -- indexing
266 -- --------
267
268 -- |Advance a pointer into an array by the given number of elements
269 --
270 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
271 advancePtr  = doAdvance undefined
272   where
273     doAdvance             :: Storable a' => a' -> Ptr a' -> Int -> Ptr a'
274     doAdvance dummy ptr i  = ptr `plusPtr` (i * sizeOf dummy)