1 % -----------------------------------------------------------------------------
2 % $Id: PrelMarshalArray.lhs,v 1.1 2001/01/11 17:25:57 simonmar Exp $
4 % (c) The FFI task force, 2000
7 Marshalling support: routines allocating, storing, and retrieving Haskell
8 lists that are represented as arrays in the foreign language
11 module PrelMarshalArray (
15 mallocArray, -- :: Storable a => Int -> IO (Ptr a)
16 mallocArray0, -- :: Storable a => Int -> IO (Ptr a)
18 allocaArray, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
19 allocaArray0, -- :: Storable a => Int -> (Ptr a -> IO b) -> IO b
21 reallocArray, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
22 reallocArray0, -- :: Storable a => Ptr a -> Int -> IO (Ptr a)
26 peekArray, -- :: Storable a => Int -> Ptr a -> IO [a]
27 peekArray0, -- :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
29 pokeArray, -- :: Storable a => Ptr a -> [a] -> IO ()
30 pokeArray0, -- :: Storable a => a -> Ptr a -> [a] -> IO ()
32 -- combined allocation and marshalling
34 newArray, -- :: Storable a => [a] -> IO (Ptr a)
35 newArray0, -- :: Storable a => a -> [a] -> IO (Ptr a)
37 withArray, -- :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
38 withArray0, -- :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
40 -- copying (argument order: destination, source)
42 copyArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
43 moveArray, -- :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
47 advancePtr -- :: Storable a => Ptr a -> Int -> Ptr a
50 import Monad (zipWithM_)
52 import PrelPtr (Ptr, plusPtr)
53 import PrelStorable (Storable(sizeOf, peekElemOff, pokeElemOff))
54 import PrelMarshalAlloc (mallocBytes, allocaBytes, reallocBytes)
55 import PrelMarshalUtils (copyBytes, moveBytes)
61 -- allocate storage for the given number of elements of a storable type
63 mallocArray :: Storable a => Int -> IO (Ptr a)
64 mallocArray = doMalloc undefined
66 doMalloc :: Storable a => a -> Int -> IO (Ptr a)
67 doMalloc dummy size = mallocBytes (size * sizeOf dummy)
69 -- like `mallocArray', but add an extra element to signal the end of the array
71 mallocArray0 :: Storable a => Int -> IO (Ptr a)
72 mallocArray0 size = mallocArray (size + 1)
74 -- temporarily allocate space for the given number of elements
76 -- * see `MarshalAlloc.alloca' for the storage lifetime constraints
78 allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b
79 allocaArray = doAlloca undefined
81 doAlloca :: Storable a => a -> Int -> (Ptr a -> IO b) -> IO b
82 doAlloca dummy size = allocaBytes (size * sizeOf dummy)
84 -- like `allocaArray', but add an extra element to signal the end of the array
86 allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b
87 allocaArray0 size = allocaArray (size + 1)
89 -- adjust the size of an array
91 reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)
92 reallocArray = doRealloc undefined
94 doRealloc :: Storable a => a -> Ptr a -> Int -> IO (Ptr a)
95 doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy)
97 -- adjust the size of an array while adding an element for the end marker
99 reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)
100 reallocArray0 ptr size = reallocArray ptr (size + 1)
106 -- convert an array of given length into a Haskell list
108 peekArray :: Storable a => Int -> Ptr a -> IO [a]
109 peekArray size ptr = mapM (peekElemOff ptr) [0..size-1]
111 -- convert an array terminated by the given end marker into a Haskell list
113 peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
114 peekArray0 marker ptr = loop 0
117 val <- peekElemOff ptr i
118 if val == marker then return [] else do
122 -- write the list elements consecutive into memory
124 pokeArray :: Storable a => Ptr a -> [a] -> IO ()
125 pokeArray ptr vals = zipWithM_ (pokeElemOff ptr) [0..] vals
127 -- write the list elements consecutive into memory and terminate them with the
128 -- given marker element
130 pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
131 pokeArray0 marker ptr vals = do
133 pokeElemOff ptr (length vals) marker
136 -- combined allocation and marshalling
137 -- -----------------------------------
139 -- write a list of storable elements into a newly allocated, consecutive
140 -- sequence of storable values
142 newArray :: Storable a => [a] -> IO (Ptr a)
144 ptr <- mallocArray (length vals)
148 -- write a list of storable elements into a newly allocated, consecutive
149 -- sequence of storable values, where the end is fixed by the given end maker
151 newArray0 :: Storable a => a -> [a] -> IO (Ptr a)
152 newArray0 marker vals = do
153 ptr <- mallocArray0 (length vals)
154 pokeArray0 marker ptr vals
157 -- temporarily store a list of storable values in memory
159 withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
160 withArray vals f = allocaArray (length vals) $ \ptr -> do
164 -- `like withArray', but a terminator indicates where the array ends
166 withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b
167 withArray0 marker vals f = allocaArray0 (length vals) $ \ptr -> do
168 pokeArray0 marker ptr vals
175 -- copies the given number of elements from the second array (source) into the
176 -- first array (destination); the copied areas may *not* overlap
178 copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
179 copyArray = doCopy undefined
181 doCopy :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
182 doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy)
184 -- copies the given number of elements from the second array (source) into the
185 -- first array (destination); the copied areas *may* overlap
187 moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
188 moveArray = doMove undefined
190 doMove :: Storable a => a -> Ptr a -> Ptr a -> Int -> IO ()
191 doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy)
197 -- advance a pointer into an array by the given number of elements
199 advancePtr :: Storable a => Ptr a -> Int -> Ptr a
200 advancePtr = doAdvance undefined
202 doAdvance :: Storable a => a -> Ptr a -> Int -> Ptr a
203 doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy)