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