Foreign.Storable

Copyright (c) The FFI task force 2001
License see libraries/base/LICENSE
Maintainer [email protected]
Stability provisional
Portability portable
Safe Haskell Trustworthy
Language Haskell2010

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.

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
Instances details
Storable Bool

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Char

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Double

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Float

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Int

Since: base-2.1

Instance details

Defined in Foreign.Storable

Methods

sizeOf :: Int -> Int Source

alignment :: Int -> Int Source

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

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

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

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

peek :: Ptr Int -> IO Int Source

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

Storable Int8

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Int16

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Int32

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Int64

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Word

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Word8

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Word16

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Word32

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable Word64

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable ()

Since: base-4.9.0.0

Instance details

Defined in Foreign.Storable

Methods

sizeOf :: () -> Int Source

alignment :: () -> Int Source

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

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

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

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

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

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

Storable Fingerprint

Since: base-4.4.0.0

Instance details

Defined in Foreign.Storable

Storable IntPtr
Instance details

Defined in Foreign.Ptr

Storable WordPtr
Instance details

Defined in Foreign.Ptr

Storable CUIntMax
Instance details

Defined in Foreign.C.Types

Storable CIntMax
Instance details

Defined in Foreign.C.Types

Storable CUIntPtr
Instance details

Defined in Foreign.C.Types

Storable CIntPtr
Instance details

Defined in Foreign.C.Types

Storable CSUSeconds
Instance details

Defined in Foreign.C.Types

Storable CUSeconds
Instance details

Defined in Foreign.C.Types

Storable CTime
Instance details

Defined in Foreign.C.Types

Storable CClock
Instance details

Defined in Foreign.C.Types

Storable CSigAtomic
Instance details

Defined in Foreign.C.Types

Storable CWchar
Instance details

Defined in Foreign.C.Types

Storable CSize
Instance details

Defined in Foreign.C.Types

Storable CPtrdiff
Instance details

Defined in Foreign.C.Types

Storable CDouble
Instance details

Defined in Foreign.C.Types

Storable CFloat
Instance details

Defined in Foreign.C.Types

Storable CBool
Instance details

Defined in Foreign.C.Types

Storable CULLong
Instance details

Defined in Foreign.C.Types

Storable CLLong
Instance details

Defined in Foreign.C.Types

Storable CULong
Instance details

Defined in Foreign.C.Types

Storable CLong
Instance details

Defined in Foreign.C.Types

Storable CUInt
Instance details

Defined in Foreign.C.Types

Storable CInt
Instance details

Defined in Foreign.C.Types

Storable CUShort
Instance details

Defined in Foreign.C.Types

Storable CShort
Instance details

Defined in Foreign.C.Types

Storable CUChar
Instance details

Defined in Foreign.C.Types

Storable CSChar
Instance details

Defined in Foreign.C.Types

Storable CChar
Instance details

Defined in Foreign.C.Types

Storable Fd
Instance details

Defined in System.Posix.Types

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 CNfds
Instance details

Defined in System.Posix.Types

Storable CSocklen
Instance details

Defined in System.Posix.Types

Storable CTimer
Instance details

Defined in System.Posix.Types

Storable CKey
Instance details

Defined in System.Posix.Types

Storable CId
Instance details

Defined in System.Posix.Types

Methods

sizeOf :: CId -> Int Source

alignment :: CId -> Int Source

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

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

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

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

peek :: Ptr CId -> IO CId Source

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

Storable CFsFilCnt
Instance details

Defined in System.Posix.Types

Storable CFsBlkCnt
Instance details

Defined in System.Posix.Types

Storable CClockId
Instance details

Defined in System.Posix.Types

Storable CBlkCnt
Instance details

Defined in System.Posix.Types

Storable CBlkSize
Instance details

Defined in System.Posix.Types

Storable CRLim
Instance details

Defined in System.Posix.Types

Storable CTcflag
Instance details

Defined in System.Posix.Types

Storable CSpeed
Instance details

Defined in System.Posix.Types

Storable CCc
Instance details

Defined in System.Posix.Types

Methods

sizeOf :: CCc -> Int Source

alignment :: CCc -> Int Source

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

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

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

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

peek :: Ptr CCc -> IO CCc Source

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

Storable CUid
Instance details

Defined in System.Posix.Types

Storable CNlink
Instance details

Defined in System.Posix.Types

Storable CGid
Instance details

Defined in System.Posix.Types

Storable CSsize
Instance details

Defined in System.Posix.Types

Storable CPid
Instance details

Defined in System.Posix.Types

Storable COff
Instance details

Defined in System.Posix.Types

Storable CMode
Instance details

Defined in System.Posix.Types

Storable CIno
Instance details

Defined in System.Posix.Types

Storable CDev
Instance details

Defined in System.Posix.Types

(Storable a, Integral a) => Storable (Ratio a)

Since: base-4.8.0.0

Instance details

Defined in Foreign.Storable

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)

Since: base-2.1

Instance details

Defined in Foreign.Storable

Storable (Ptr a)

Since: base-2.1

Instance details

Defined in Foreign.Storable

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)

Since: base-2.1

Instance details

Defined in Foreign.Storable

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

Since: base-4.14.0.0

Instance details

Defined in Data.Ord

Methods

sizeOf :: Down a -> Int Source

alignment :: Down a -> Int Source

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

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

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

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

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

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

Storable a => Storable (Identity a)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Identity

Methods

sizeOf :: Identity a -> Int Source

alignment :: Identity a -> Int Source

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

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

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

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

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

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

Storable a => Storable (Complex a)

Since: base-4.8.0.0

Instance details

Defined in Data.Complex

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

Storable a => Storable (Const a b)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Const

Methods

sizeOf :: Const a b -> Int Source

alignment :: Const a b -> Int Source

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

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

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

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

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

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

© The University of Glasgow and others
Licensed under a BSD-style license (see top of the page).
https://downloads.haskell.org/~ghc/8.10.2/docs/html/libraries/base-4.14.1.0/Foreign-Storable.html