Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE NoImplicitPrelude #-}------------------------------------------------------------------------------- |-- Module : Foreign.Marshal.Utils-- Copyright : (c) The FFI task force 2001-- License : BSD-style (see the file libraries/base/LICENSE)---- Maintainer : ffi@haskell.org-- Stability : provisional-- Portability : portable---- Utilities for primitive marshaling-------------------------------------------------------------------------------moduleForeign.Marshal.Utils(-- * General marshalling utilities-- ** Combined allocation and marshalling--with,new,-- ** Marshalling of Boolean values (non-zero corresponds to 'True')--fromBool,toBool,-- ** Marshalling of Maybe values--maybeNew,maybeWith,maybePeek,-- ** Marshalling lists of storable objects--withMany,-- ** Haskellish interface to memcpy and memmove-- | (argument order: destination, source)--copyBytes,moveBytes,-- ** Filling up memory area with required values--fillBytes,)whereimportData.MaybeimportForeign.Ptr(Ptr,nullPtr)importForeign.Storable(Storable(poke))importForeign.C.Types(CSize(..),CInt(..))importForeign.Marshal.Alloc(malloc,alloca)importData.Word(Word8)importGHC.Real(fromIntegral)importGHC.NumimportGHC.Base-- combined allocation and marshalling-- ------------------------------------- |Allocate a block of memory and marshal a value into it-- (the combination of 'malloc' and '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.--new::Storablea=>a->IO(Ptra)newval=doptr<-mallocpokeptrvalreturnptr-- |@'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' and '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.--with::Storablea=>a->(Ptra->IOb)->IObwithvalf=alloca$\ptr->dopokeptrvalres<-fptrreturnres-- marshalling of Boolean values (non-zero corresponds to 'True')-- ------------------------------- |Convert a Haskell 'Bool' to its numeric representation--fromBool::Numa=>Bool->afromBoolFalse=0fromBoolTrue=1-- |Convert a Boolean in numeric representation to a Haskell value--toBool::(Eqa,Numa)=>a->BooltoBool=(/=0)-- marshalling of Maybe values-- ----------------------------- |Allocate storage and marshal a storable value wrapped into a 'Maybe'---- * the 'nullPtr' is used to represent 'Nothing'--maybeNew::(a->IO(Ptrb))->(Maybea->IO(Ptrb))maybeNew=maybe(returnnullPtr)-- |Converts a @withXXX@ combinator into one marshalling a value wrapped-- into a 'Maybe', using 'nullPtr' to represent 'Nothing'.--maybeWith::(a->(Ptrb->IOc)->IOc)->(Maybea->(Ptrb->IOc)->IOc)maybeWith=maybe($nullPtr)-- |Convert a peek combinator into a one returning 'Nothing' if applied to a-- 'nullPtr'--maybePeek::(Ptra->IOb)->Ptra->IO(Maybeb)maybePeekpeekptr|ptr==nullPtr=returnNothing|otherwise=doa<-peekptr;return(Justa)-- marshalling lists of storable objects-- --------------------------------------- |Replicates a @withXXX@ combinator over a list of objects, yielding a list of-- marshalled objects--withMany::(a->(b->res)->res)-- withXXX combinator for one object->[a]-- storable objects->([b]->res)-- action on list of marshalled obj.s->reswithMany_[]f=f[]withManywithFoo(x:xs)f=withFoox$\x'->withManywithFooxs(\xs'->f(x':xs'))-- Haskellish interface to memcpy and memmove-- -------------------------------------------- |Copies the given number of bytes from the second area (source) into the-- first (destination); the copied areas may /not/ overlap--copyBytes::Ptra->Ptra->Int->IO()copyBytesdestsrcsize=do_<-memcpydestsrc(fromIntegralsize)return()-- |Copies the given number of bytes from the second area (source) into the-- first (destination); the copied areas /may/ overlap--moveBytes::Ptra->Ptra->Int->IO()moveBytesdestsrcsize=do_<-memmovedestsrc(fromIntegralsize)return()-- Filling up memory area with required values-- --------------------------------------------- |Fill a given number of bytes in memory area with a byte value.---- @since 4.8.0.0fillBytes::Ptra->Word8->Int->IO()fillBytesdestcharsize=do_<-memsetdest(fromIntegralchar)(fromIntegralsize)return()-- auxiliary routines-- --------------------- |Basic C routines needed for memory copying--foreignimportccallunsafe"string.h"memcpy::Ptra->Ptra->CSize->IO(Ptra)foreignimportccallunsafe"string.h"memmove::Ptra->Ptra->CSize->IO(Ptra)foreignimportccallunsafe"string.h"memset::Ptra->CInt->CSize->IO(Ptra)
[8]ページ先頭