1 % -----------------------------------------------------------------------------
2 % $Id: PrelMarshalUtils.lhs,v 1.2 2001/03/15 20:35:49 qrczak Exp $
4 % (c) The FFI task force, 2000
7 Utilities for primitive marshaling
10 module PrelMarshalUtils (
12 -- combined allocation and marshalling
14 withObject, -- :: Storable a => a -> (Ptr a -> IO b) -> IO b
15 {- FIXME: should be `with' -}
16 new, -- :: Storable a => a -> IO (Ptr a)
18 -- marshalling of Boolean values (non-zero corresponds to `True')
20 fromBool, -- :: Num a => Bool -> a
21 toBool, -- :: Num a => a -> Bool
23 -- marshalling of Maybe values
25 maybeNew, -- :: ( a -> IO (Ptr a))
26 -- -> (Maybe a -> IO (Ptr a))
27 maybeWith, -- :: ( a -> (Ptr b -> IO c) -> IO c)
28 -- -> (Maybe a -> (Ptr b -> IO c) -> IO c)
29 maybePeek, -- :: (Ptr a -> IO b )
30 -- -> (Ptr a -> IO (Maybe b))
32 -- marshalling lists of storable objects
34 withMany, -- :: (a -> (b -> res) -> res) -> [a] -> ([b] -> res) -> res
36 -- Haskellish interface to memcpy and memmove
37 -- (argument order: destination, source)
39 copyBytes, -- :: Ptr a -> Ptr a -> Int -> IO ()
40 moveBytes -- :: Ptr a -> Ptr a -> Int -> IO ()
43 import Monad ( liftM )
45 import PrelPtr ( Ptr, nullPtr )
46 import PrelStorable ( Storable(poke,destruct) )
47 import PrelCTypesISO ( CSize )
48 import PrelMarshalAlloc ( malloc, alloca )
51 -- combined allocation and marshalling
52 -- -----------------------------------
54 -- allocate storage for a value and marshal it into this storage
56 new :: Storable a => a -> IO (Ptr a)
63 -- allocate temporary storage for a value and marshal it into this storage
65 -- * see the life time constraints imposed by `alloca'
67 {- FIXME: should be called `with' -}
68 withObject :: Storable a => a -> (Ptr a -> IO b) -> IO b
77 -- marshalling of Boolean values (non-zero corresponds to `True')
78 -- -----------------------------
80 -- convert a Haskell Boolean to its numeric representation
82 fromBool :: Num a => Bool -> a
86 -- convert a Boolean in numeric representation to a Haskell value
88 toBool :: Num a => a -> Bool
92 -- marshalling of Maybe values
93 -- ---------------------------
95 -- allocate storage and marshall a storable value wrapped into a `Maybe'
97 -- * the `nullPtr' is used to represent `Nothing'
99 maybeNew :: ( a -> IO (Ptr a))
100 -> (Maybe a -> IO (Ptr a))
101 maybeNew = maybe (return nullPtr)
103 -- converts a withXXX combinator into one marshalling a value wrapped into a
106 maybeWith :: ( a -> (Ptr b -> IO c) -> IO c)
107 -> (Maybe a -> (Ptr b -> IO c) -> IO c)
108 maybeWith = maybe ($ nullPtr)
110 -- convert a peek combinator into a one returning `Nothing' if applied to a
113 maybePeek :: (Ptr a -> IO b) -> Ptr a -> IO (Maybe b)
114 maybePeek peek ptr | ptr == nullPtr = return Nothing
115 | otherwise = liftM Just $ peek ptr
118 -- marshalling lists of storable objects
119 -- -------------------------------------
121 -- replicates a withXXX combinator over a list of objects, yielding a list of
122 -- marshalled objects
124 withMany :: (a -> (b -> res) -> res) -- withXXX combinator for one object
125 -> [a] -- storable objects
126 -> ([b] -> res) -- action on list of marshalled obj.s
128 withMany _ [] f = f []
129 withMany withFoo (x:xs) f = withFoo x $ \x' ->
130 withMany withFoo xs (\xs' -> f (x':xs'))
133 -- Haskellish interface to memcpy and memmove
134 -- ------------------------------------------
136 -- copies the given number of bytes from the second area (source) into the
137 -- first (destination); the copied areas may *not* overlap
139 copyBytes :: Ptr a -> Ptr a -> Int -> IO ()
140 copyBytes dest src size = memcpy dest src (fromIntegral size)
142 -- copies the given number of elements from the second area (source) into the
143 -- first (destination); the copied areas *may* overlap
145 moveBytes :: Ptr a -> Ptr a -> Int -> IO ()
146 moveBytes dest src size = memmove dest src (fromIntegral size)
149 -- auxilliary routines
150 -- -------------------
152 -- basic C routines needed for memory copying
154 foreign import unsafe memcpy :: Ptr a -> Ptr a -> CSize -> IO ()
155 foreign import unsafe memmove :: Ptr a -> Ptr a -> CSize -> IO ()