<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Foreign.Storable</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[ window.onload = function () {pageLoad();setSynopsis("mini_Foreign-Storable.html");}; //]]> </script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">base-4.3.1.0: Basic libraries</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>portable</td></tr><tr><th>Stability</th><td>provisional</td></tr><tr><th>Maintainer</th><td>ffi@haskell.org</td></tr></table><p class="caption">Foreign.Storable</p></div><div id="description"><p class="caption">Description</p><div class="doc"><p>The module <a href="Foreign-Storable.html">Foreign.Storable</a> 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 <a href="Foreign.html">Foreign</a> module. </p></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">class</span> <a href="#t:Storable">Storable</a> a <span class="keyword">where</span><ul class="subs"><li><a href="#v:sizeOf">sizeOf</a> :: a -> <a href="Data-Int.html#t:Int">Int</a></li><li><a href="#v:alignment">alignment</a> :: a -> <a href="Data-Int.html#t:Int">Int</a></li><li><a href="#v:peekElemOff">peekElemOff</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> a -> <a href="Data-Int.html#t:Int">Int</a> -> <a href="System-IO.html#t:IO">IO</a> a</li><li><a href="#v:pokeElemOff">pokeElemOff</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> a -> <a href="Data-Int.html#t:Int">Int</a> -> a -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></li><li><a href="#v:peekByteOff">peekByteOff</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> b -> <a href="Data-Int.html#t:Int">Int</a> -> <a href="System-IO.html#t:IO">IO</a> a</li><li><a href="#v:pokeByteOff">pokeByteOff</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> b -> <a href="Data-Int.html#t:Int">Int</a> -> a -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></li><li><a href="#v:peek">peek</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> a -> <a href="System-IO.html#t:IO">IO</a> a</li><li><a href="#v:poke">poke</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> a -> a -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></li></ul></li></ul></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src"><span class="keyword">class</span> <a name="t:Storable" class="def">Storable</a> a <span class="keyword">where</span></p><div class="doc"><p>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. </p><p>Memory addresses are represented as values of type <code><code><a href="Foreign-Ptr.html#t:Ptr">Ptr</a></code> a</code>, for some <code>a</code> which is an instance of class <code><a href="Foreign-Storable.html#t:Storable">Storable</a></code>. The type argument to <code><a href="Foreign-Ptr.html#t:Ptr">Ptr</a></code> 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. </p><p>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 <code><a href="Foreign-Storable.html#t:Storable">Storable</a></code> facilitates this manipulation on all types for which it is instantiated, which are the standard basic types of Haskell, the fixed size <code>Int</code> types (<code><a href="Data-Int.html#t:Int8">Int8</a></code>, <code><a href="Data-Int.html#t:Int16">Int16</a></code>, <code><a href="Data-Int.html#t:Int32">Int32</a></code>, <code><a href="Data-Int.html#t:Int64">Int64</a></code>), the fixed size <code>Word</code> types (<code><a href="Data-Word.html#t:Word8">Word8</a></code>, <code><a href="Data-Word.html#t:Word16">Word16</a></code>, <code><a href="Data-Word.html#t:Word32">Word32</a></code>, <code><a href="Data-Word.html#t:Word64">Word64</a></code>), <code><a href="Foreign-StablePtr.html#t:StablePtr">StablePtr</a></code>, all types from <a href="Foreign-C-Types.html">Foreign.C.Types</a>, as well as <code><a href="Foreign-Ptr.html#t:Ptr">Ptr</a></code>. </p><p>Minimal complete definition: <code><a href="Foreign-Storable.html#v:sizeOf">sizeOf</a></code>, <code><a href="Foreign-Storable.html#v:alignment">alignment</a></code>, one of <code><a href="Foreign-Storable.html#v:peek">peek</a></code>, <code><a href="Foreign-Storable.html#v:peekElemOff">peekElemOff</a></code> and <code><a href="Foreign-Storable.html#v:peekByteOff">peekByteOff</a></code>, and one of <code><a href="Foreign-Storable.html#v:poke">poke</a></code>, <code><a href="Foreign-Storable.html#v:pokeElemOff">pokeElemOff</a></code> and <code><a href="Foreign-Storable.html#v:pokeByteOff">pokeByteOff</a></code>. </p></div><div class="subs methods"><p class="caption">Methods</p><p class="src"><a name="v:sizeOf" class="def">sizeOf</a> :: a -> <a href="Data-Int.html#t:Int">Int</a></p><div class="doc"><p>Computes the storage requirements (in bytes) of the argument. The value of the argument is not used. </p></div><p class="src"><a name="v:alignment" class="def">alignment</a> :: a -> <a href="Data-Int.html#t:Int">Int</a></p><div class="doc"><p>Computes the alignment constraint of the argument. An alignment constraint <code>x</code> is fulfilled by any address divisible by <code>x</code>. The value of the argument is not used. </p></div><p class="src"><a name="v:peekElemOff" class="def">peekElemOff</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> a -> <a href="Data-Int.html#t:Int">Int</a> -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>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 <code>0</code>). The following equality holds, </p><pre> peekElemOff addr idx = IOExts.fixIO $ \result -> peek (addr `plusPtr` (idx * sizeOf result)) </pre><p>Note that this is only a specification, not necessarily the concrete implementation of the function. </p></div><p class="src"><a name="v:pokeElemOff" class="def">pokeElemOff</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> a -> <a href="Data-Int.html#t:Int">Int</a> -> a -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></p><div class="doc"><p>Write a value to a memory area regarded as an array of values of the same kind. The following equality holds: </p><pre> pokeElemOff addr idx x = poke (addr `plusPtr` (idx * sizeOf x)) x </pre></div><p class="src"><a name="v:peekByteOff" class="def">peekByteOff</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> b -> <a href="Data-Int.html#t:Int">Int</a> -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>Read a value from a memory location given by a base address and offset. The following equality holds: </p><pre> peekByteOff addr off = peek (addr `plusPtr` off) </pre></div><p class="src"><a name="v:pokeByteOff" class="def">pokeByteOff</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> b -> <a href="Data-Int.html#t:Int">Int</a> -> a -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></p><div class="doc"><p>Write a value to a memory location given by a base address and offset. The following equality holds: </p><pre> pokeByteOff addr off x = poke (addr `plusPtr` off) x </pre></div><p class="src"><a name="v:peek" class="def">peek</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> a -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>Read a value from the given memory location. </p><p>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 <code>a</code>, the alignment constraint for <code>a</code>, as given by the function <code><a href="Foreign-Storable.html#v:alignment">alignment</a></code> is fulfilled. </p></div><p class="src"><a name="v:poke" class="def">poke</a> :: <a href="Foreign-Ptr.html#t:Ptr">Ptr</a> a -> a -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></p><div class="doc"><p>Write the given value to the given memory location. Alignment restrictions might apply; see <code><a href="Foreign-Storable.html#v:peek">peek</a></code>. </p></div></div><div class="subs instances"><p id="control.i:Storable" class="caption collapser" onclick="toggleSection('i:Storable')">Instances</p><div id="section.i:Storable" class="show"><table><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Bool.html#t:Bool">Bool</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Char.html#t:Char">Char</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Prelude.html#t:Double">Double</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Prelude.html#t:Float">Float</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Int.html#t:Int">Int</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Int.html#t:Int8">Int8</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Int.html#t:Int16">Int16</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Int.html#t:Int32">Int32</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Int.html#t:Int64">Int64</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Word.html#t:Word">Word</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Word.html#t:Word8">Word8</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Word.html#t:Word16">Word16</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Word.html#t:Word32">Word32</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Data-Word.html#t:Word64">Word64</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CUIntMax">CUIntMax</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CIntMax">CIntMax</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CUIntPtr">CUIntPtr</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CIntPtr">CIntPtr</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CTime">CTime</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CClock">CClock</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CSigAtomic">CSigAtomic</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CWchar">CWchar</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CSize">CSize</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CPtrdiff">CPtrdiff</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CDouble">CDouble</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CFloat">CFloat</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CULLong">CULLong</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CLLong">CLLong</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CULong">CULong</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CLong">CLong</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CUInt">CUInt</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CInt">CInt</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CUShort">CUShort</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CShort">CShort</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CUChar">CUChar</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CSChar">CSChar</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-C-Types.html#t:CChar">CChar</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-Ptr.html#t:IntPtr">IntPtr</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="Foreign-Ptr.html#t:WordPtr">WordPtr</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:Fd">Fd</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CRLim">CRLim</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CTcflag">CTcflag</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CSpeed">CSpeed</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CCc">CCc</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CUid">CUid</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CNlink">CNlink</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CGid">CGid</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CSsize">CSsize</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CPid">CPid</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:COff">COff</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CMode">CMode</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CIno">CIno</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> <a href="System-Posix-Types.html#t:CDev">CDev</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> CTimeval</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> Event</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> Event</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> PollFd</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> (<a href="Foreign-StablePtr.html#t:StablePtr">StablePtr</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> (<a href="Foreign-Ptr.html#t:Ptr">Ptr</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Foreign-Storable.html#t:Storable">Storable</a> (<a href="Foreign-Ptr.html#t:FunPtr">FunPtr</a> a)</td><td class="doc empty"> </td></tr></table></div></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.9.2</p></div></body></html>