Safe Haskell | None |
---|---|
Language | Haskell2010 |
This is a drop-in replacement for Foreign and Foreign.C. The
difference is that it uses a CStorable
class instead of
Storable
, and only C types are in CStorable. Otherwise, it's
easy to corrupt memory by accidentally marshalling a haskell type into a
C struct.
It tries to export all the same things that Foreign and Foreign.C do, but because I only copied the things I need, it's not complete.
Synopsis
- class CStorable a where
- alloca :: forall a b. CStorable a => (Ptr a -> IO b) -> IO b
- malloc :: CStorable a => IO (Ptr a)
- mallocArray :: CStorable a => Int -> IO (Ptr a)
- allocaArray :: CStorable a => Int -> (Ptr a -> IO b) -> IO b
- pokeArray :: CStorable a => Ptr a -> [a] -> IO ()
- peekArray :: CStorable a => Int -> Ptr a -> IO [a]
- newArray :: CStorable a => [a] -> IO (Ptr a)
- withArray :: CStorable a => [a] -> (Ptr a -> IO b) -> IO b
- withArrayLen :: CStorable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
- withArrayLenNull :: CStorable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
- copyArray :: CStorable a => Ptr a -> Ptr a -> Int -> IO ()
- with :: CStorable a => a -> (Ptr a -> IO b) -> IO b
- new :: CStorable a => a -> IO (Ptr a)
- module Data.Int
- module Data.Word
- module Foreign.C
- module Foreign.Ptr
- module Foreign.StablePtr
- module Foreign.ForeignPtr
- free :: Ptr a -> IO ()
- reallocBytes :: Ptr a -> Int -> IO (Ptr a)
- allocaBytesAligned :: Int -> Int -> (Ptr a -> IO b) -> IO b
- allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
- mallocBytes :: Int -> IO (Ptr a)
- finalizerFree :: FinalizerPtr a
- fillBytes :: Ptr a -> Word8 -> Int -> IO ()
- moveBytes :: Ptr a -> Ptr a -> Int -> IO ()
- copyBytes :: Ptr a -> Ptr a -> Int -> IO ()
- withMany :: (a -> (b -> res) -> res) -> [a] -> ([b] -> res) -> res
- maybePeek :: (Ptr a -> IO b) -> Ptr a -> IO (Maybe b)
- maybeWith :: (a -> (Ptr b -> IO c) -> IO c) -> Maybe a -> (Ptr b -> IO c) -> IO c
- maybeNew :: (a -> IO (Ptr b)) -> Maybe a -> IO (Ptr b)
- toBool :: (Eq a, Num a) => a -> Bool
- fromBool :: Num a => Bool -> a
Documentation
class CStorable a where Source #
alignment :: a -> Int Source #
peekElemOff :: Ptr a -> Int -> IO a Source #
pokeElemOff :: Ptr a -> Int -> a -> IO () Source #
peekByteOff :: Ptr b -> Int -> IO a Source #
pokeByteOff :: Ptr b -> Int -> a -> IO () Source #
Instances
withArrayLenNull :: CStorable a => [a] -> (Int -> Ptr a -> IO b) -> IO b Source #
Like withArrayLen
, except if the list is null, then pass (0, nullPtr).
module Data.Int
module Data.Word
module Foreign.C
module Foreign.Ptr
module Foreign.StablePtr
module Foreign.ForeignPtr
Free a block of memory that was allocated with malloc
,
mallocBytes
, realloc
, reallocBytes
, new
or any of the new
X functions in Foreign.Marshal.Array or
Foreign.C.String.
reallocBytes :: Ptr a -> Int -> IO (Ptr a) #
Resize a memory area that was allocated with malloc
or mallocBytes
to the given size. The returned pointer may refer to an entirely
different memory area, but will be sufficiently aligned for any of the
basic foreign types that fits into a memory block of the given size.
The contents of the referenced memory area will be the same as of
the original pointer up to the minimum of the original size and the
given size.
If the pointer argument to reallocBytes
is nullPtr
, reallocBytes
behaves like malloc
. If the requested size is 0, reallocBytes
behaves like free
.
allocaBytes :: Int -> (Ptr a -> IO b) -> IO b #
executes the computation allocaBytes
n ff
, passing as argument
a pointer to a temporarily allocated block of memory of n
bytes.
The block of memory is sufficiently aligned for any of the basic
foreign types that fits into a memory block of the allocated size.
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.
mallocBytes :: Int -> IO (Ptr a) #
Allocate a block of memory of the given number of bytes. The block of memory is sufficiently aligned for any of the basic foreign types that fits into a memory block of the allocated size.
The memory may be deallocated using free
or finalizerFree
when
no longer required.
finalizerFree :: FinalizerPtr a #
A pointer to a foreign function equivalent to free
, which may be
used as a finalizer (cf ForeignPtr
) for storage
allocated with malloc
, mallocBytes
, realloc
or reallocBytes
.
fillBytes :: Ptr a -> Word8 -> Int -> IO () #
Fill a given number of bytes in memory area with a byte value.
Since: base-4.8.0.0
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
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
withMany :: (a -> (b -> res) -> res) -> [a] -> ([b] -> res) -> res #
Replicates a withXXX
combinator over a list of objects, yielding a list of
marshalled objects