HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulebase-4.20.2.0Haskell2010

Foreign.Storable

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.

  • 1 class
  • Packagebase-4.20.2.0
  • Exports1
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceStorable.hs
classclass Storable a where
#

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.

Methods

  • sizeOf :: a -> Int

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

  • alignment :: a -> Int

    Computes the alignment constraint of the argument. An alignment constraint x is fulfilled by any address divisible by x. The alignment must be a power of two if this instance is to be used with alloca or allocaArray. The value of the argument is not used.

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

    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 ()

    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

    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 ()

    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

    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 ()

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

Instances82Storable, …
  • Storable EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.EPoll
  • Storable EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Poll
  • Storable PollFdDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Poll
  • Storable FingerprintDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable CBoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CClockDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CDoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CFloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CLLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CPtrdiffDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CSCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CSUSecondsDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CSigAtomicDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CTimeDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CULLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CULongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUSecondsDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CWcharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Storable WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Storable FLockDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.Handle.Lock.LinuxOFD
  • Storable Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable IoSubSystemDefined in ghc-internal-9.1003.0 · GHC.Internal.RTS.Flags
  • Storable CBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CBlkSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CCcDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CClockIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CDevDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CFsBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CFsFilCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CGidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CInoDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CKeyDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CModeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CNfdsDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CNlinkDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable COffDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CPidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CRLimDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CSocklenDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CSpeedDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CSsizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CTcflagDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CTimerDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CUidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable FdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable BoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable CharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable IntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable WordDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable ()Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable (ConstPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable (FunPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable (Ptr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable (StablePtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable a => Storable (Complex a)Defined in base-4.20.2.0 · Data.Complex
  • Storable a => Storable (Identity a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Identity
  • Storable a => Storable (Down a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Ord
  • (Storable a, Integral a) => Storable (Ratio a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable a => Storable (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const