The module Storable provides most elementary support for marshalling and is part of the low-level portion of the Foreign Function Interface (FFI) - see also Section 4.9. It serves two functions:
It provides operations to allocate and deallocate blocks of raw memory (i.e., unstructured chunks of memory outside of the area maintained by the Haskell storage manager). These memory blocks are commonly used to pass compound data structures to foreign functions or to provide space in which compound result values are obtained from foreign functions. For example, Haskell lists are typically passed as C arrays to C functions; the storage space for such an array can be allocated by functions (such as malloc) contained in the present module.
It provides a class Storable and instances of this class for all primitive types that can be stored in raw memory. The member functions of this class facilitate writing values of primitive types to raw memory (which may have been allocated with the above mentioned routines) and reading values from blocks of raw memory. The class, furthermore, includes support for computing the storage requirements and alignment restrictions of storable types.
All marshalling between Haskell and a foreign language ultimately boils down to translating Haskell data structures into the binary representation of a corresponding data structure of the foreign language and vice versa. To code this marshalling in Haskell, it is necessary to manipulate primtive data types stored in unstructured memory blocks. The class Storable facilitates this manipulation on all types for which it is instantiated, which are the standard basic types of Haskell, the fixed size IntXX (Section 4.13) and WordXX (Section 4.27) types, stable pointers (Section 4.24), all types from CTypes (Section 4.5) and CTypesISO (Section 4.6), as well as addresses (Section 4.1).
class Storable a where sizeOf :: a -> Int alignment :: a -> Int peekElemOff :: Addr -> Int -> IO a pokeElemOff :: Addr -> Int -> a -> IO () peekByteOff :: Addr -> AddrOff -> IO a pokeByteOff :: Addr -> AddrOff -> a -> IO () peek :: Addr -> IO a poke :: Addr -> a -> IO ()
The behaviour of the member functions is as follows:
The function sizeOf computes the storage requirements (in bytes) of the argument, and alignment computes the alignment constraint of the argument. An alignment constraint x is fulfilled by any address divisible by x. Both functions do not evaluate their argument, but compute the result on the basis of the type of the argument alone.
Read a value from a memory area regarded as an array of values of the same kind. The first argument specifies the start address of the array and the second the index into the array (the first element of the array has index 0). The following equality holds,
peekElemOff addr idx = IOExts.fixIO $ \result -> let off = fromIntegral (idx * sizeOf result) in peek (addr `plusAddr` off)
Note that this is only a specification, but not necessarily the concrete implementation of the function.
Write a value to a memory area regarded as an array of values of the same kind. The following equality holds,
pokeElemOff addr idx x = let off = fromIntegral (idx * sizeOf x) in poke (addr `plusAddr` off) x
Read a value from a memory location given by a base address and offset. The following equality holds,
peekByteOff addr off = peek (addr `plusAddr` off)
Write a value to a memory location given by a base address and offset. The following equality holds,
pokeByteOff addr off x = poke (addr `plusAddr` off) x
Read a value from the given memory location
Write the given value to the given memory location.
Note that the peek and poke functions might require properly aligned addresses to function correctly. This is architecture dependent; thus, portable code should ensure that when peeking or poking values of some type a, the alignment constraint for a, as given by the function alignment is fulfilled.
malloc :: Int -> IO Addr mallocElem :: Storable a => a -> IO Addr mallocElems :: Storable a => a -> Int -> IO Addr realloc :: Addr -> Int -> IO Addr free :: Addr -> IO () alloca :: Int -> (Addr -> IO a) -> IO a allocaElem :: Storable a => a -> (Addr -> IO b) -> IO b allocaElems :: Storable a => a -> Int -> (Addr -> IO b) -> IO b
The functions malloc, realloc, and free correspond to the standard C functions malloc(), realloc(), and free(), respectively. The function mallocElem essentially behaves like malloc, but allocates a block of memory that exactly holds values of its argument type. The function mallocElems is similar, but allocates storage for an array of values, where the size of the array is given in the second argument. More precisely, these last two functions behave as if defined as follows:
mallocElem = malloc . sizeOf mallocElems x n = malloc (n * sizeOf x)
Note that, due to the definition of sizeOf, the first argument of both mallocElem and mallocElems is not evaluated.
The remaining three functions sandwich a given operation between allocation and deallocation of one block of memory in an exception-safe way. They behave as if defined as
alloca n op = bracket (malloc n) free op allocaElem x op = alloca (sizeOf x) op allocaElems x n op = alloca (n * sizeOf x) op