Chapter 34
Foreign.Marshal.Utils

module Foreign.Marshal.Utils (  
    with,  new,  fromBool,  toBool,  maybeNew,  maybeWith,  maybePeek,  
    withMany,  copyBytes,  moveBytes  
  ) where

34.1 一般的なマーシャリングユーティリティ

34.1.1 アロケーションとマーシャリングの組み合わせ

with :: Storable a => a -> (Ptr a -> IO b) -> IO b
with val f executes the computation f, passing as argument a pointer to a temporarily allocated block of memory into which val has been marshalled (the combination of alloca および poke).

The memory is freed when f terminates (either normally or via an exception), so the pointer passed to f must not be used after this.

new :: Storable a => a -> IO (Ptr a)
Allocate a block of memory and marshal a value into it (the combination of malloc および poke). The size of the area allocated is determined by the Foreign.Storable.sizeOf method from the instance of Storable for the appropriate type.

The memory may be deallocated using Foreign.Marshal.Alloc.free or Foreign.Marshal.Alloc.finalizerFree when no longer required.

34.1.2 真理値のマーシャリング(非ゼロが Trueに対応)

fromBool :: Num a => Bool -> a
Convert a Haskell Bool to its numeric representation

toBool :: Num a => a -> Bool
Convert a Boolean in numeric representation to a Haskell value

34.1.3 Maybe 値のマーシャリング

maybeNew :: (a -> IO (Ptr a)) -> Maybe a -> IO (Ptr a)
Allocate storage and marshal a storable value wrapped into a Maybe

maybeWith :: (a -> (Ptr b -> IO c) -> IO c)
             -> Maybe a -> (Ptr b -> IO c) -> IO c
Converts a withXXX combinator into one marshalling a value wrapped into a Maybe, using nullPtr to represent Nothing.

maybePeek :: (Ptr a -> IO b) -> Ptr a -> IO (Maybe b)
Convert a peek combinator into a one returning Nothing if applied to a nullPtr

34.1.4 格納可能オブジェクトのリストのマーシャリング

withMany :: (a -> (b -> res) -> res) -> [a] -> ([b] -> res) -> res
Replicates a withXXX combinator over a list of objects, yielding a list of marshalled objects

34.1.5 memcpy および memmove に対する Haskell 流のインターフェイス

(argument order: destination, source)

copyBytes :: Ptr a -> Ptr a -> Int -> IO ()
Copies the given number of bytes from the second area (source) into the first (destination); the copied areas may not overlap

moveBytes :: Ptr a -> Ptr a -> Int -> IO ()
Copies the given number of bytes from the second area (source) into the first (destination); the copied areas may overlap