1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.Marshal.Array
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- Marshalling support: routines allocating, storing, and retrieving Haskell
13 -- lists that are represented as arrays in the foreign language
15 -----------------------------------------------------------------------------
17 module Foreign.Marshal.Array (
21 mallocArray, -- :: Storable a => Int -> IO (Ptr a)
22 mallocArray0, -- :: Storable a => Int -> IO (Ptr a)
24 allocaArray, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
25 allocaArray0, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
27 reallocArray, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
28 reallocArray0, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
32 peekArray, -- :: Storable a => Int -> Ptr a -> IO [a]
33 peekArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
35 pokeArray, -- :: Storable a => Ptr a -> [a] -> IO ()
36 pokeArray0, -- :: Storable a => a -> Ptr a -> [a] -> IO ()
38 -- combined allocation and marshalling
40 newArray, -- :: Storable a => [a] -> IO (Ptr a)
41 newArray0, -- :: Storable a => a -> [a] -> IO (Ptr a)
43 withArray, -- :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
44 withArray0, -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
46 -- copying (argument order: destination, source)
48 copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
49 moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
53 lengthArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO Int
57 advancePtr, -- :: Storable a => Ptr a -> Int -> Ptr a
62 #ifdef __GLASGOW_HASKELL__
63 import Foreign.Ptr (Ptr, plusPtr)
64 import GHC.Storable (Storable(sizeOf,peekElemOff,pokeElemOff))
65 import Foreign.Marshal.Alloc (mallocBytes, allocaBytes, reallocBytes)
66 import Foreign.Marshal.Utils (copyBytes, moveBytes)
77 -- allocate storage for the given number of elements of a storable type
79 mallocArray :: Storable a => Int -> IO (Ptr a)
80 mallocArray = doMalloc undefined
82 doMalloc :: Storable a => a -> Int -> IO (Ptr a)
83 doMalloc dummy size = mallocBytes (size * sizeOf dummy)
85 -- like `mallocArray', but add an extra element to signal the end of the array
87 mallocArray0 :: Storable a => Int -> IO (Ptr a)
88 mallocArray0 size = mallocArray (size + 1)
90 -- temporarily allocate space for the given number of elements
92 -- * see `MarshalAlloc.alloca' for the storage lifetime constraints
94 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
95 allocaArray = doAlloca undefined
97 doAlloca :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b
98 doAlloca dummy size = allocaBytes (size * sizeOf dummy)
100 -- like `allocaArray', but add an extra element to signal the end of the array
102 allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
103 allocaArray0 size = allocaArray (size + 1)
105 -- adjust the size of an array
107 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
108 reallocArray = doRealloc undefined
110 doRealloc :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
111 doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy)
113 -- adjust the size of an array while adding an element for the end marker
115 reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
116 reallocArray0 ptr size = reallocArray ptr (size + 1)
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.
127 peekArray :: Storable a => Int -> Ptr a -> IO [a]
128 peekArray size ptr | size <= 0 = return []
129 | otherwise = f (size-1) []
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)
134 -- convert an array terminated by the given end marker into a Haskell list
136 peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
137 peekArray0 marker ptr = loop 0
140 val <- peekElemOff ptr i
141 if val == marker then return [] else do
145 -- write the list elements consecutive into memory
147 pokeArray :: Storable a => Ptr a -> [a] -> IO ()
148 pokeArray ptr vals = zipWithM_ (pokeElemOff ptr) [0..] vals
150 -- write the list elements consecutive into memory and terminate them with the
151 -- given marker element
153 pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
154 pokeArray0 marker ptr vals = do
156 pokeElemOff ptr (length vals) marker
159 -- combined allocation and marshalling
160 -- -----------------------------------
162 -- write a list of storable elements into a newly allocated, consecutive
163 -- sequence of storable values
165 newArray :: Storable a => [a] -> IO (Ptr a)
167 ptr <- mallocArray (length vals)
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
174 newArray0 :: Storable a => a -> [a] -> IO (Ptr a)
175 newArray0 marker vals = do
176 ptr <- mallocArray0 (length vals)
177 pokeArray0 marker ptr vals
180 -- temporarily store a list of storable values in memory
182 withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
184 allocaArray len $ \ptr -> do
191 -- like `withArray', but a terminator indicates where the array ends
193 withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
194 withArray0 marker vals f =
195 allocaArray0 len $ \ptr -> do
196 pokeArray0 marker ptr vals
203 -- copying (argument order: destination, source)
206 -- copy the given number of elements from the second array (source) into the
207 -- first array (destination); the copied areas may *not* overlap
209 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
210 copyArray = doCopy undefined
212 doCopy :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
213 doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy)
215 -- copy the given number of elements from the second array (source) into the
216 -- first array (destination); the copied areas *may* overlap
218 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
219 moveArray = doMove undefined
221 doMove :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
222 doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy)
225 -- finding the length
226 -- ------------------
228 -- return the number of elements in an array, excluding the terminator
230 lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int
231 lengthArray0 marker ptr = loop 0
234 val <- peekElemOff ptr i
235 if val == marker then return i else loop (i+1)
241 -- advance a pointer into an array by the given number of elements
243 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
244 advancePtr = doAdvance undefined
246 doAdvance :: Storable a => a -> Ptr a -> Int -> Ptr a
247 doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy)