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 )
36 import Foreign.C.Types
37 import Foreign.C.TypesISO
41 #ifdef __GLASGOW_HASKELL__
43 #elif defined(__HUGS__)
50 Minimal complete definition: sizeOf, alignment, and one definition
51 in each of the peek/poke families.
55 The member functions of this class facilitate writing values of
56 primitive types to raw memory (which may have been allocated with the
57 above mentioned routines) and reading values from blocks of raw
58 memory. The class, furthermore, includes support for computing the
59 storage requirements and alignment restrictions of storable types.
61 Memory addresses are represented as values of type @'Ptr' a@, for some
62 @a@ which is an instance of class 'Storable'. The type argument to
63 'Ptr' helps provide some valuable type safety in FFI code (you can\'t
64 mix pointers of different types without an explicit cast), while
65 helping the Haskell type system figure out which marshalling method is
66 needed for a given pointer.
68 All marshalling between Haskell and a foreign language ultimately
69 boils down to translating Haskell data structures into the binary
70 representation of a corresponding data structure of the foreign
71 language and vice versa. To code this marshalling in Haskell, it is
72 necessary to manipulate primtive data types stored in unstructured
73 memory blocks. The class 'Storable' facilitates this manipulation on
74 all types for which it is instantiated, which are the standard basic
75 types of Haskell, the fixed size @Int@ types ('Int8', 'Int16',
76 'Int32', 'Int64'), the fixed size @Word@ types ('Word8', 'Word16',
77 'Word32', 'Word64'), 'StablePtr', all types from "CTypes" and
78 "CTypesISO", as well as 'Ptr'.
80 Minimal complete definition: 'sizeOf', 'alignment', one of 'peek',
81 'peekElemOff' and 'peekByteOff', and one of 'poke', 'pokeElemOff' and
85 class Storable a where
88 -- ^ Computes the storage requirements (in bytes) of the argument.
89 -- The value of the argument is not used.
92 -- ^ Computes the alignment constraint of the argument. An
93 -- alignment constraint @x@ is fulfilled by any address divisible
94 -- by @x@. The value of the argument is not used.
96 peekElemOff :: Ptr a -> Int -> IO a
97 -- ^ Read a value from a memory area regarded as an array
98 -- of values of the same kind. The first argument specifies
99 -- the start address of the array and the second the index into
100 -- the array (the first element of the array has index
101 -- @0@). The following equality holds,
103 -- > peekElemOff addr idx = IOExts.fixIO $ \result ->
104 -- > peek (addr \`plusPtr\` (idx * sizeOf result))
106 -- Note that this is only a specification, not
107 -- necessarily the concrete implementation of the
110 pokeElemOff :: Ptr a -> Int -> a -> IO ()
111 -- ^ Write a value to a memory area regarded as an array of
112 -- values of the same kind. The following equality holds:
114 -- > pokeElemOff addr idx x =
115 -- > poke (addr \`plusPtr\` (idx * sizeOf x)) x
117 peekByteOff :: Ptr b -> Int -> IO a
118 -- ^ Read a value from a memory location given by a base
119 -- address and offset. The following equality holds:
121 -- > peekByteOff addr off = peek (addr \`plusPtr\` off)
123 pokeByteOff :: Ptr b -> Int -> a -> IO ()
124 -- ^ Write a value to a memory location given by a base
125 -- address and offset. The following equality holds:
127 -- > pokeByteOff addr off x = poke (addr \`plusPtr\` off) x
129 peek :: Ptr a -> IO a
130 -- ^ Read a value from the given memory location.
132 -- Note that the peek and poke functions might require properly
133 -- aligned addresses to function correctly. This is architecture
134 -- dependent; thus, portable code should ensure that when peeking or
135 -- poking values of some type @a@, the alignment
136 -- constraint for @a@, as given by the function
137 -- 'alignment' is fulfilled.
139 poke :: Ptr a -> a -> IO ()
140 -- ^ Write the given value to the given memory location. Alignment
141 -- restrictions might apply; see 'peek'.
143 -- circular default instances
144 #ifdef __GLASGOW_HASKELL__
145 peekElemOff = peekElemOff_ undefined
146 where peekElemOff_ :: a -> Ptr a -> Int -> IO a
147 peekElemOff_ undef ptr off = peekByteOff ptr (off * sizeOf undef)
149 peekElemOff ptr off = peekByteOff ptr (off * sizeOfPtr ptr undefined)
151 pokeElemOff ptr off val = pokeByteOff ptr (off * sizeOf val) val
153 peekByteOff ptr off = peek (ptr `plusPtr` off)
154 pokeByteOff ptr off = poke (ptr `plusPtr` off)
156 peek ptr = peekElemOff ptr 0
157 poke ptr = pokeElemOff ptr 0
160 #ifndef __GLASGOW_HASKELL__
162 sizeOfPtr :: Storable a => Ptr a -> a -> Int
163 sizeOfPtr px x = sizeOf x
167 System-dependent, but rather obvious instances
170 instance Storable Bool where
171 sizeOf _ = sizeOf (undefined::CInt)
172 alignment _ = alignment (undefined::CInt)
173 peekElemOff p i = liftM (/= (0::CInt)) $ peekElemOff (castPtr p) i
174 pokeElemOff p i x = pokeElemOff (castPtr p) i (if x then 1 else 0::CInt)
176 #define STORABLE(T,size,align,read,write) \
177 instance Storable (T) where { \
179 alignment _ = align; \
180 peekElemOff = read; \
181 pokeElemOff = write }
183 #ifdef __GLASGOW_HASKELL__
184 STORABLE(Char,SIZEOF_INT32,ALIGNMENT_INT32,
185 readWideCharOffPtr,writeWideCharOffPtr)
186 #elif defined(__HUGS__)
187 STORABLE(Char,SIZEOF_CHAR,ALIGNMENT_HSCHAR,
188 readCharOffPtr,writeCharOffPtr)
191 STORABLE(Int,SIZEOF_HSINT,ALIGNMENT_HSINT,
192 readIntOffPtr,writeIntOffPtr)
194 #ifdef __GLASGOW_HASKELL__
195 STORABLE(Word,SIZEOF_HSWORD,ALIGNMENT_HSWORD,
196 readWordOffPtr,writeWordOffPtr)
199 STORABLE((Ptr a),SIZEOF_HSPTR,ALIGNMENT_HSPTR,
200 readPtrOffPtr,writePtrOffPtr)
202 STORABLE((FunPtr a),SIZEOF_HSFUNPTR,ALIGNMENT_HSFUNPTR,
203 readFunPtrOffPtr,writeFunPtrOffPtr)
205 STORABLE((StablePtr a),SIZEOF_HSSTABLEPTR,ALIGNMENT_HSSTABLEPTR,
206 readStablePtrOffPtr,writeStablePtrOffPtr)
208 STORABLE(Float,SIZEOF_HSFLOAT,ALIGNMENT_HSFLOAT,
209 readFloatOffPtr,writeFloatOffPtr)
211 STORABLE(Double,SIZEOF_HSDOUBLE,ALIGNMENT_HSDOUBLE,
212 readDoubleOffPtr,writeDoubleOffPtr)
214 STORABLE(Word8,SIZEOF_WORD8,ALIGNMENT_WORD8,
215 readWord8OffPtr,writeWord8OffPtr)
217 STORABLE(Word16,SIZEOF_WORD16,ALIGNMENT_WORD16,
218 readWord16OffPtr,writeWord16OffPtr)
220 STORABLE(Word32,SIZEOF_WORD32,ALIGNMENT_WORD32,
221 readWord32OffPtr,writeWord32OffPtr)
223 STORABLE(Word64,SIZEOF_WORD64,ALIGNMENT_WORD64,
224 readWord64OffPtr,writeWord64OffPtr)
226 STORABLE(Int8,SIZEOF_INT8,ALIGNMENT_INT8,
227 readInt8OffPtr,writeInt8OffPtr)
229 STORABLE(Int16,SIZEOF_INT16,ALIGNMENT_INT16,
230 readInt16OffPtr,writeInt16OffPtr)
232 STORABLE(Int32,SIZEOF_INT32,ALIGNMENT_INT32,
233 readInt32OffPtr,writeInt32OffPtr)
235 STORABLE(Int64,SIZEOF_INT64,ALIGNMENT_INT64,
236 readInt64OffPtr,writeInt64OffPtr)
238 #define NSTORABLE(T) \
239 instance Storable T where { \
240 sizeOf (T x) = sizeOf x ; \
241 alignment (T x) = alignment x ; \
242 peekElemOff a i = liftM T (peekElemOff (castPtr a) i) ; \
243 pokeElemOff a i (T x) = pokeElemOff (castPtr a) i x }
264 NSTORABLE(CSigAtomic)