1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.Storable
5 -- Copyright : (c) The FFI task force 2001
6 -- License : see libraries/base/LICENSE
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- The module "Foreign.Storable" provides most elementary support for
13 -- marshalling and is part of the language-independent portion of the
14 -- Foreign Function Interface (FFI), and will normally be imported via
15 -- the "Foreign" module.
17 -----------------------------------------------------------------------------
19 module Foreign.Storable
21 sizeOf, -- :: a -> Int
22 alignment, -- :: a -> Int
23 peekElemOff, -- :: Ptr a -> Int -> IO a
24 pokeElemOff, -- :: Ptr a -> Int -> a -> IO ()
25 peekByteOff, -- :: Ptr b -> Int -> IO a
26 pokeByteOff, -- :: Ptr b -> Int -> a -> IO ()
27 peek, -- :: Ptr a -> IO a
28 poke) -- :: Ptr a -> a -> IO ()
33 import NHC.FFI (Storable(..),Ptr,FunPtr,StablePtr
34 ,Int8,Int16,Int32,Int64,Word8,Word16,Word32,Word64)
37 import Control.Monad ( liftM )
40 #include "HsBaseConfig.h"
42 #ifdef __GLASGOW_HASKELL__
44 import GHC.Stable ( StablePtr )
54 import Foreign.StablePtr
64 The member functions of this class facilitate writing values of
65 primitive types to raw memory (which may have been allocated with the
66 above mentioned routines) and reading values from blocks of raw
67 memory. The class, furthermore, includes support for computing the
68 storage requirements and alignment restrictions of storable types.
70 Memory addresses are represented as values of type @'Ptr' a@, for some
71 @a@ which is an instance of class 'Storable'. The type argument to
72 'Ptr' helps provide some valuable type safety in FFI code (you can\'t
73 mix pointers of different types without an explicit cast), while
74 helping the Haskell type system figure out which marshalling method is
75 needed for a given pointer.
77 All marshalling between Haskell and a foreign language ultimately
78 boils down to translating Haskell data structures into the binary
79 representation of a corresponding data structure of the foreign
80 language and vice versa. To code this marshalling in Haskell, it is
81 necessary to manipulate primitive data types stored in unstructured
82 memory blocks. The class 'Storable' facilitates this manipulation on
83 all types for which it is instantiated, which are the standard basic
84 types of Haskell, the fixed size @Int@ types ('Int8', 'Int16',
85 'Int32', 'Int64'), the fixed size @Word@ types ('Word8', 'Word16',
86 'Word32', 'Word64'), 'StablePtr', all types from "Foreign.C.Types",
89 Minimal complete definition: 'sizeOf', 'alignment', one of 'peek',
90 'peekElemOff' and 'peekByteOff', and one of 'poke', 'pokeElemOff' and
94 class Storable a where
97 -- ^ Computes the storage requirements (in bytes) of the argument.
98 -- The value of the argument is not used.
100 alignment :: a -> Int
101 -- ^ Computes the alignment constraint of the argument. An
102 -- alignment constraint @x@ is fulfilled by any address divisible
103 -- by @x@. The value of the argument is not used.
105 peekElemOff :: Ptr a -> Int -> IO a
106 -- ^ Read a value from a memory area regarded as an array
107 -- of values of the same kind. The first argument specifies
108 -- the start address of the array and the second the index into
109 -- the array (the first element of the array has index
110 -- @0@). The following equality holds,
112 -- > peekElemOff addr idx = IOExts.fixIO $ \result ->
113 -- > peek (addr `plusPtr` (idx * sizeOf result))
115 -- Note that this is only a specification, not
116 -- necessarily the concrete implementation of the
119 pokeElemOff :: Ptr a -> Int -> a -> IO ()
120 -- ^ Write a value to a memory area regarded as an array of
121 -- values of the same kind. The following equality holds:
123 -- > pokeElemOff addr idx x =
124 -- > poke (addr `plusPtr` (idx * sizeOf x)) x
126 peekByteOff :: Ptr b -> Int -> IO a
127 -- ^ Read a value from a memory location given by a base
128 -- address and offset. The following equality holds:
130 -- > peekByteOff addr off = peek (addr `plusPtr` off)
132 pokeByteOff :: Ptr b -> Int -> a -> IO ()
133 -- ^ Write a value to a memory location given by a base
134 -- address and offset. The following equality holds:
136 -- > pokeByteOff addr off x = poke (addr `plusPtr` off) x
138 peek :: Ptr a -> IO a
139 -- ^ Read a value from the given memory location.
141 -- Note that the peek and poke functions might require properly
142 -- aligned addresses to function correctly. This is architecture
143 -- dependent; thus, portable code should ensure that when peeking or
144 -- poking values of some type @a@, the alignment
145 -- constraint for @a@, as given by the function
146 -- 'alignment' is fulfilled.
148 poke :: Ptr a -> a -> IO ()
149 -- ^ Write the given value to the given memory location. Alignment
150 -- restrictions might apply; see 'peek'.
152 -- circular default instances
153 #ifdef __GLASGOW_HASKELL__
154 peekElemOff = peekElemOff_ undefined
155 where peekElemOff_ :: a -> Ptr a -> Int -> IO a
156 peekElemOff_ undef ptr off = peekByteOff ptr (off * sizeOf undef)
158 peekElemOff ptr off = peekByteOff ptr (off * sizeOfPtr ptr undefined)
160 pokeElemOff ptr off val = pokeByteOff ptr (off * sizeOf val) val
162 peekByteOff ptr off = peek (ptr `plusPtr` off)
163 pokeByteOff ptr off = poke (ptr `plusPtr` off)
165 peek ptr = peekElemOff ptr 0
166 poke ptr = pokeElemOff ptr 0
168 #ifndef __GLASGOW_HASKELL__
169 sizeOfPtr :: Storable a => Ptr a -> a -> Int
170 sizeOfPtr px x = sizeOf x
173 -- System-dependent, but rather obvious instances
175 instance Storable Bool where
176 sizeOf _ = sizeOf (undefined::HTYPE_INT)
177 alignment _ = alignment (undefined::HTYPE_INT)
178 peekElemOff p i = liftM (/= (0::HTYPE_INT)) $ peekElemOff (castPtr p) i
179 pokeElemOff p i x = pokeElemOff (castPtr p) i (if x then 1 else 0::HTYPE_INT)
181 #define STORABLE(T,size,align,read,write) \
182 instance Storable (T) where { \
184 alignment _ = align; \
185 peekElemOff = read; \
186 pokeElemOff = write }
188 #ifdef __GLASGOW_HASKELL__
189 STORABLE(Char,SIZEOF_INT32,ALIGNMENT_INT32,
190 readWideCharOffPtr,writeWideCharOffPtr)
191 #elif defined(__HUGS__)
192 STORABLE(Char,SIZEOF_HSCHAR,ALIGNMENT_HSCHAR,
193 readCharOffPtr,writeCharOffPtr)
196 STORABLE(Int,SIZEOF_HSINT,ALIGNMENT_HSINT,
197 readIntOffPtr,writeIntOffPtr)
200 STORABLE(Word,SIZEOF_HSWORD,ALIGNMENT_HSWORD,
201 readWordOffPtr,writeWordOffPtr)
204 STORABLE((Ptr a),SIZEOF_HSPTR,ALIGNMENT_HSPTR,
205 readPtrOffPtr,writePtrOffPtr)
207 STORABLE((FunPtr a),SIZEOF_HSFUNPTR,ALIGNMENT_HSFUNPTR,
208 readFunPtrOffPtr,writeFunPtrOffPtr)
210 STORABLE((StablePtr a),SIZEOF_HSSTABLEPTR,ALIGNMENT_HSSTABLEPTR,
211 readStablePtrOffPtr,writeStablePtrOffPtr)
213 STORABLE(Float,SIZEOF_HSFLOAT,ALIGNMENT_HSFLOAT,
214 readFloatOffPtr,writeFloatOffPtr)
216 STORABLE(Double,SIZEOF_HSDOUBLE,ALIGNMENT_HSDOUBLE,
217 readDoubleOffPtr,writeDoubleOffPtr)
219 STORABLE(Word8,SIZEOF_WORD8,ALIGNMENT_WORD8,
220 readWord8OffPtr,writeWord8OffPtr)
222 STORABLE(Word16,SIZEOF_WORD16,ALIGNMENT_WORD16,
223 readWord16OffPtr,writeWord16OffPtr)
225 STORABLE(Word32,SIZEOF_WORD32,ALIGNMENT_WORD32,
226 readWord32OffPtr,writeWord32OffPtr)
228 STORABLE(Word64,SIZEOF_WORD64,ALIGNMENT_WORD64,
229 readWord64OffPtr,writeWord64OffPtr)
231 STORABLE(Int8,SIZEOF_INT8,ALIGNMENT_INT8,
232 readInt8OffPtr,writeInt8OffPtr)
234 STORABLE(Int16,SIZEOF_INT16,ALIGNMENT_INT16,
235 readInt16OffPtr,writeInt16OffPtr)
237 STORABLE(Int32,SIZEOF_INT32,ALIGNMENT_INT32,
238 readInt32OffPtr,writeInt32OffPtr)
240 STORABLE(Int64,SIZEOF_INT64,ALIGNMENT_INT64,
241 readInt64OffPtr,writeInt64OffPtr)