2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
5 -- Module : Foreign.Storable
6 -- Copyright : (c) The FFI task force 2001
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : ffi@haskell.org
10 -- Stability : provisional
11 -- Portability : portable
13 -- The module "Storable" provides most elementary support for
14 -- marshalling and is part of the language-independent portion of the
15 -- Foreign Function Interface (FFI), and will normally be imported via
16 -- the "Foreign" module.
18 -----------------------------------------------------------------------------
20 module Foreign.Storable
22 sizeOf, -- :: a -> Int
23 alignment, -- :: a -> Int
24 peekElemOff, -- :: Ptr a -> Int -> IO a
25 pokeElemOff, -- :: Ptr a -> Int -> a -> IO ()
26 peekByteOff, -- :: Ptr b -> Int -> IO a
27 pokeByteOff, -- :: Ptr b -> Int -> a -> IO ()
28 peek, -- :: Ptr a -> IO a
29 poke) -- :: Ptr a -> a -> IO ()
34 import Control.Monad ( liftM )
35 import Foreign.C.Types
36 import Foreign.C.TypesISO
38 #ifdef __GLASGOW_HASKELL__
40 #elif defined(__HUGS__)
47 Minimal complete definition: sizeOf, alignment, and one definition
48 in each of the peek/poke families.
52 The member functions of this class facilitate writing values of
53 primitive types to raw memory (which may have been allocated with the
54 above mentioned routines) and reading values from blocks of raw
55 memory. The class, furthermore, includes support for computing the
56 storage requirements and alignment restrictions of storable types.
58 Memory addresses are represented as values of type @'Ptr' a@, for some
59 @a@ which is an instance of class 'Storable'. The type argument to
60 'Ptr' helps provide some valuable type safety in FFI code (you can\'t
61 mix pointers of different types without an explicit cast), while
62 helping the Haskell type system figure out which marshalling method is
63 needed for a given pointer.
65 All marshalling between Haskell and a foreign language ultimately
66 boils down to translating Haskell data structures into the binary
67 representation of a corresponding data structure of the foreign
68 language and vice versa. To code this marshalling in Haskell, it is
69 necessary to manipulate primtive data types stored in unstructured
70 memory blocks. The class 'Storable' facilitates this manipulation on
71 all types for which it is instantiated, which are the standard basic
72 types of Haskell, the fixed size @Int@ types ('Int8', 'Int16',
73 'Int32', 'Int64'), the fixed size @Word@ types ('Word8', 'Word16',
74 'Word32', 'Word64'), 'StablePtr', all types from "CTypes" and
75 "CTypesISO", as well as 'Ptr'.
77 Minimal complete definition: 'sizeOf', 'alignment', one of 'peek',
78 'peekElemOff' and 'peekByteOff', and one of 'poke', 'pokeElemOff' and
82 class Storable a where
85 -- ^ Computes the storage requirements (in bytes) of the argument.
86 -- The value of the argument is not used.
89 -- ^ Computes the alignment constraint of the argument. An
90 -- alignment constraint @x@ is fulfilled by any address divisible
91 -- by @x@. The value of the argument is not used.
93 peekElemOff :: Ptr a -> Int -> IO a
94 -- ^ Read a value from a memory area regarded as an array
95 -- of values of the same kind. The first argument specifies
96 -- the start address of the array and the second the index into
97 -- the array (the first element of the array has index
98 -- @0@). The following equality holds,
100 -- > peekElemOff addr idx = IOExts.fixIO $ \result ->
101 -- > peek (addr \`plusPtr\` (idx * sizeOf result))
103 -- Note that this is only a specification, not
104 -- necessarily the concrete implementation of the
107 pokeElemOff :: Ptr a -> Int -> a -> IO ()
108 -- ^ Write a value to a memory area regarded as an array of
109 -- values of the same kind. The following equality holds:
111 -- > pokeElemOff addr idx x =
112 -- > poke (addr \`plusPtr\` (idx * sizeOf x)) x
114 peekByteOff :: Ptr b -> Int -> IO a
115 -- ^ Read a value from a memory location given by a base
116 -- address and offset. The following equality holds:
118 -- > peekByteOff addr off = peek (addr \`plusPtr\` off)
120 pokeByteOff :: Ptr b -> Int -> a -> IO ()
121 -- ^ Write a value to a memory location given by a base
122 -- address and offset. The following equality holds:
124 -- > pokeByteOff addr off x = poke (addr \`plusPtr\` off) x
126 peek :: Ptr a -> IO a
127 -- ^ Read a value from the given memory location.
129 -- Note that the peek and poke functions might require properly
130 -- aligned addresses to function correctly. This is architecture
131 -- dependent; thus, portable code should ensure that when peeking or
132 -- poking values of some type @a@, the alignment
133 -- constraint for @a@, as given by the function
134 -- 'alignment' is fulfilled.
136 poke :: Ptr a -> a -> IO ()
137 -- ^ Write the given value to the given memory location. Alignment
138 -- restrictions might apply; see 'peek'.
140 -- circular default instances
141 peekElemOff = peekElemOff_ undefined
142 where peekElemOff_ :: a -> Ptr a -> Int -> IO a
143 peekElemOff_ undef ptr off = peekByteOff ptr (off * sizeOf undef)
144 pokeElemOff ptr off val = pokeByteOff ptr (off * sizeOf val) val
146 peekByteOff ptr off = peek (ptr `plusPtr` off)
147 pokeByteOff ptr off = poke (ptr `plusPtr` off)
149 peek ptr = peekElemOff ptr 0
150 poke ptr = pokeElemOff ptr 0
153 System-dependent, but rather obvious instances
156 instance Storable Bool where
157 sizeOf _ = sizeOf (undefined::CInt)
158 alignment _ = alignment (undefined::CInt)
159 peekElemOff p i = liftM (/= (0::CInt)) $ peekElemOff (castPtr p) i
160 pokeElemOff p i x = pokeElemOff (castPtr p) i (if x then 1 else 0::CInt)
162 #define STORABLE(T,size,align,read,write) \
163 instance Storable (T) where { \
165 alignment _ = align; \
166 peekElemOff = read; \
167 pokeElemOff = write }
169 STORABLE(Char,SIZEOF_INT32,ALIGNMENT_INT32,
170 readWideCharOffPtr,writeWideCharOffPtr)
172 STORABLE(Int,SIZEOF_HSINT,ALIGNMENT_HSINT,
173 readIntOffPtr,writeIntOffPtr)
175 #ifdef __GLASGOW_HASKELL__
176 STORABLE(Word,SIZEOF_HSWORD,ALIGNMENT_HSWORD,
177 readWordOffPtr,writeWordOffPtr)
180 STORABLE((Ptr a),SIZEOF_HSPTR,ALIGNMENT_HSPTR,
181 readPtrOffPtr,writePtrOffPtr)
183 STORABLE((FunPtr a),SIZEOF_HSFUNPTR,ALIGNMENT_HSFUNPTR,
184 readFunPtrOffPtr,writeFunPtrOffPtr)
186 STORABLE((StablePtr a),SIZEOF_HSSTABLEPTR,ALIGNMENT_HSSTABLEPTR,
187 readStablePtrOffPtr,writeStablePtrOffPtr)
189 STORABLE(Float,SIZEOF_HSFLOAT,ALIGNMENT_HSFLOAT,
190 readFloatOffPtr,writeFloatOffPtr)
192 STORABLE(Double,SIZEOF_HSDOUBLE,ALIGNMENT_HSDOUBLE,
193 readDoubleOffPtr,writeDoubleOffPtr)
195 STORABLE(Word8,SIZEOF_WORD8,ALIGNMENT_WORD8,
196 readWord8OffPtr,writeWord8OffPtr)
198 STORABLE(Word16,SIZEOF_WORD16,ALIGNMENT_WORD16,
199 readWord16OffPtr,writeWord16OffPtr)
201 STORABLE(Word32,SIZEOF_WORD32,ALIGNMENT_WORD32,
202 readWord32OffPtr,writeWord32OffPtr)
204 STORABLE(Word64,SIZEOF_WORD64,ALIGNMENT_WORD64,
205 readWord64OffPtr,writeWord64OffPtr)
207 STORABLE(Int8,SIZEOF_INT8,ALIGNMENT_INT8,
208 readInt8OffPtr,writeInt8OffPtr)
210 STORABLE(Int16,SIZEOF_INT16,ALIGNMENT_INT16,
211 readInt16OffPtr,writeInt16OffPtr)
213 STORABLE(Int32,SIZEOF_INT32,ALIGNMENT_INT32,
214 readInt32OffPtr,writeInt32OffPtr)
216 STORABLE(Int64,SIZEOF_INT64,ALIGNMENT_INT64,
217 readInt64OffPtr,writeInt64OffPtr)
219 #define NSTORABLE(T) \
220 instance Storable T where { \
221 sizeOf (T x) = sizeOf x ; \
222 alignment (T x) = alignment x ; \
223 peekElemOff a i = liftM T (peekElemOff (castPtr a) i) ; \
224 pokeElemOff a i (T x) = pokeElemOff (castPtr a) i x }
243 NSTORABLE(CSigAtomic)