base-4.8.2.0: Basic libraries

Copyright(c) The FFI task force 2001
Licensesee libraries/base/LICENSE
Maintainerffi@haskell.org
Stabilityprovisional
Portabilityportable
Safe HaskellTrustworthy
LanguageHaskell2010

Foreign.Storable

Description

The module Foreign.Storable provides most elementary support for marshalling and is part of the language-independent portion of the Foreign Function Interface (FFI), and will normally be imported via the Foreign module.

Synopsis

Documentation

class Storable a where Source

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.

Memory addresses are represented as values of type Ptr a, for some a which is an instance of class Storable. The type argument to Ptr helps provide some valuable type safety in FFI code (you can't mix pointers of different types without an explicit cast), while helping the Haskell type system figure out which marshalling method is needed for a given pointer.

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 primitive 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 Int types (Int8, Int16, Int32, Int64), the fixed size Word types (Word8, Word16, Word32, Word64), StablePtr, all types from Foreign.C.Types, as well as Ptr.

Minimal complete definition

sizeOf, alignment, (peek | peekElemOff | peekByteOff), (poke | pokeElemOff | pokeByteOff)

Methods

sizeOf :: a -> Int Source

Computes the storage requirements (in bytes) of the argument. The value of the argument is not used.

alignment :: a -> Int Source

Computes the alignment constraint of the argument. An alignment constraint x is fulfilled by any address divisible by x. The value of the argument is not used.

peekElemOff :: Ptr a -> Int -> IO a Source

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 ->
  peek (addr `plusPtr` (idx * sizeOf result))

Note that this is only a specification, not necessarily the concrete implementation of the function.

pokeElemOff :: Ptr a -> Int -> a -> IO () Source

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 = 
  poke (addr `plusPtr` (idx * sizeOf x)) x

peekByteOff :: Ptr b -> Int -> IO a Source

Read a value from a memory location given by a base address and offset. The following equality holds:

peekByteOff addr off = peek (addr `plusPtr` off)

pokeByteOff :: Ptr b -> Int -> a -> IO () Source

Write a value to a memory location given by a base address and offset. The following equality holds:

pokeByteOff addr off x = poke (addr `plusPtr` off) x

peek :: Ptr a -> IO a Source

Read a value from 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.

poke :: Ptr a -> a -> IO () Source

Write the given value to the given memory location. Alignment restrictions might apply; see peek.

Instances

Storable Bool Source 
Storable Char Source 
Storable Double Source 
Storable Float Source 
Storable Int Source 
Storable Int8 Source 
Storable Int16 Source 
Storable Int32 Source 
Storable Int64 Source 
Storable Word Source 
Storable Word8 Source 
Storable Word16 Source 
Storable Word32 Source 
Storable Word64 Source 
Storable Fingerprint Source 
Storable CUIntMax Source 
Storable CIntMax Source 
Storable CUIntPtr Source 
Storable CIntPtr Source 
Storable CSUSeconds Source 
Storable CUSeconds Source 
Storable CTime Source 
Storable CClock Source 
Storable CSigAtomic Source 
Storable CWchar Source 
Storable CSize Source 
Storable CPtrdiff Source 
Storable CDouble Source 
Storable CFloat Source 
Storable CULLong Source 
Storable CLLong Source 
Storable CULong Source 
Storable CLong Source 
Storable CUInt Source 
Storable CInt Source 
Storable CUShort Source 
Storable CShort Source 
Storable CUChar Source 
Storable CSChar Source 
Storable CChar Source 
Storable IntPtr Source 
Storable WordPtr Source 
Storable Fd Source 

Methods

sizeOf :: Fd -> Int Source

alignment :: Fd -> Int Source

peekElemOff :: Ptr Fd -> Int -> IO Fd Source

pokeElemOff :: Ptr Fd -> Int -> Fd -> IO () Source

peekByteOff :: Ptr b -> Int -> IO Fd Source

pokeByteOff :: Ptr b -> Int -> Fd -> IO () Source

peek :: Ptr Fd -> IO Fd Source

poke :: Ptr Fd -> Fd -> IO () Source

Storable CRLim Source 
Storable CTcflag Source 
Storable CSpeed Source 
Storable CCc Source 
Storable CUid Source 
Storable CNlink Source 
Storable CGid Source 
Storable CSsize Source 
Storable CPid Source 
Storable COff Source 
Storable CMode Source 
Storable CIno Source 
Storable CDev Source 
(Storable a, Integral a) => Storable (Ratio a) Source 

Methods

sizeOf :: Ratio a -> Int Source

alignment :: Ratio a -> Int Source

peekElemOff :: Ptr (Ratio a) -> Int -> IO (Ratio a) Source

pokeElemOff :: Ptr (Ratio a) -> Int -> Ratio a -> IO () Source

peekByteOff :: Ptr b -> Int -> IO (Ratio a) Source

pokeByteOff :: Ptr b -> Int -> Ratio a -> IO () Source

peek :: Ptr (Ratio a) -> IO (Ratio a) Source

poke :: Ptr (Ratio a) -> Ratio a -> IO () Source

Storable (StablePtr a) Source 
Storable (Ptr a) Source 

Methods

sizeOf :: Ptr a -> Int Source

alignment :: Ptr a -> Int Source

peekElemOff :: Ptr (Ptr a) -> Int -> IO (Ptr a) Source

pokeElemOff :: Ptr (Ptr a) -> Int -> Ptr a -> IO () Source

peekByteOff :: Ptr b -> Int -> IO (Ptr a) Source

pokeByteOff :: Ptr b -> Int -> Ptr a -> IO () Source

peek :: Ptr (Ptr a) -> IO (Ptr a) Source

poke :: Ptr (Ptr a) -> Ptr a -> IO () Source

Storable (FunPtr a) Source 

Methods

sizeOf :: FunPtr a -> Int Source

alignment :: FunPtr a -> Int Source

peekElemOff :: Ptr (FunPtr a) -> Int -> IO (FunPtr a) Source

pokeElemOff :: Ptr (FunPtr a) -> Int -> FunPtr a -> IO () Source

peekByteOff :: Ptr b -> Int -> IO (FunPtr a) Source

pokeByteOff :: Ptr b -> Int -> FunPtr a -> IO () Source

peek :: Ptr (FunPtr a) -> IO (FunPtr a) Source

poke :: Ptr (FunPtr a) -> FunPtr a -> IO () Source

Storable a => Storable (Complex a) Source 

Methods

sizeOf :: Complex a -> Int Source

alignment :: Complex a -> Int Source

peekElemOff :: Ptr (Complex a) -> Int -> IO (Complex a) Source

pokeElemOff :: Ptr (Complex a) -> Int -> Complex a -> IO () Source

peekByteOff :: Ptr b -> Int -> IO (Complex a) Source

pokeByteOff :: Ptr b -> Int -> Complex a -> IO () Source

peek :: Ptr (Complex a) -> IO (Complex a) Source

poke :: Ptr (Complex a) -> Complex a -> IO () Source