1 {-# OPTIONS -fno-implicit-prelude #-}
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 ()
32 import Control.Monad ( liftM )
37 #ifdef __GLASGOW_HASKELL__
39 import GHC.Stable ( StablePtr )
59 The member functions of this class facilitate writing values of
60 primitive types to raw memory (which may have been allocated with the
61 above mentioned routines) and reading values from blocks of raw
62 memory. The class, furthermore, includes support for computing the
63 storage requirements and alignment restrictions of storable types.
65 Memory addresses are represented as values of type @'Ptr' a@, for some
66 @a@ which is an instance of class 'Storable'. The type argument to
67 'Ptr' helps provide some valuable type safety in FFI code (you can\'t
68 mix pointers of different types without an explicit cast), while
69 helping the Haskell type system figure out which marshalling method is
70 needed for a given pointer.
72 All marshalling between Haskell and a foreign language ultimately
73 boils down to translating Haskell data structures into the binary
74 representation of a corresponding data structure of the foreign
75 language and vice versa. To code this marshalling in Haskell, it is
76 necessary to manipulate primtive data types stored in unstructured
77 memory blocks. The class 'Storable' facilitates this manipulation on
78 all types for which it is instantiated, which are the standard basic
79 types of Haskell, the fixed size @Int@ types ('Int8', 'Int16',
80 'Int32', 'Int64'), the fixed size @Word@ types ('Word8', 'Word16',
81 'Word32', 'Word64'), 'StablePtr', all types from "CTypes" and
82 "CTypesISO", as well as 'Ptr'.
84 Minimal complete definition: 'sizeOf', 'alignment', one of 'peek',
85 'peekElemOff' and 'peekByteOff', and one of 'poke', 'pokeElemOff' and
89 class Storable a where
92 -- ^ Computes the storage requirements (in bytes) of the argument.
93 -- The value of the argument is not used.
96 -- ^ Computes the alignment constraint of the argument. An
97 -- alignment constraint @x@ is fulfilled by any address divisible
98 -- by @x@. The value of the argument is not used.
100 peekElemOff :: Ptr a -> Int -> IO a
101 -- ^ Read a value from a memory area regarded as an array
102 -- of values of the same kind. The first argument specifies
103 -- the start address of the array and the second the index into
104 -- the array (the first element of the array has index
105 -- @0@). The following equality holds,
107 -- > peekElemOff addr idx = IOExts.fixIO $ \result ->
108 -- > peek (addr \`plusPtr\` (idx * sizeOf result))
110 -- Note that this is only a specification, not
111 -- necessarily the concrete implementation of the
114 pokeElemOff :: Ptr a -> Int -> a -> IO ()
115 -- ^ Write a value to a memory area regarded as an array of
116 -- values of the same kind. The following equality holds:
118 -- > pokeElemOff addr idx x =
119 -- > poke (addr \`plusPtr\` (idx * sizeOf x)) x
121 peekByteOff :: Ptr b -> Int -> IO a
122 -- ^ Read a value from a memory location given by a base
123 -- address and offset. The following equality holds:
125 -- > peekByteOff addr off = peek (addr \`plusPtr\` off)
127 pokeByteOff :: Ptr b -> Int -> a -> IO ()
128 -- ^ Write a value to a memory location given by a base
129 -- address and offset. The following equality holds:
131 -- > pokeByteOff addr off x = poke (addr \`plusPtr\` off) x
133 peek :: Ptr a -> IO a
134 -- ^ Read a value from the given memory location.
136 -- Note that the peek and poke functions might require properly
137 -- aligned addresses to function correctly. This is architecture
138 -- dependent; thus, portable code should ensure that when peeking or
139 -- poking values of some type @a@, the alignment
140 -- constraint for @a@, as given by the function
141 -- 'alignment' is fulfilled.
143 poke :: Ptr a -> a -> IO ()
144 -- ^ Write the given value to the given memory location. Alignment
145 -- restrictions might apply; see 'peek'.
147 -- circular default instances
148 #ifdef __GLASGOW_HASKELL__
149 peekElemOff = peekElemOff_ undefined
150 where peekElemOff_ :: a -> Ptr a -> Int -> IO a
151 peekElemOff_ undef ptr off = peekByteOff ptr (off * sizeOf undef)
153 peekElemOff ptr off = peekByteOff ptr (off * sizeOfPtr ptr undefined)
155 pokeElemOff ptr off val = pokeByteOff ptr (off * sizeOf val) val
157 peekByteOff ptr off = peek (ptr `plusPtr` off)
158 pokeByteOff ptr off = poke (ptr `plusPtr` off)
160 peek ptr = peekElemOff ptr 0
161 poke ptr = pokeElemOff ptr 0
163 #ifndef __GLASGOW_HASKELL__
164 sizeOfPtr :: Storable a => Ptr a -> a -> Int
165 sizeOfPtr px x = sizeOf x
168 -- System-dependent, but rather obvious instances
170 instance Storable Bool where
171 sizeOf _ = sizeOf (undefined::HTYPE_INT)
172 alignment _ = alignment (undefined::HTYPE_INT)
173 peekElemOff p i = liftM (/= (0::HTYPE_INT)) $ peekElemOff (castPtr p) i
174 pokeElemOff p i x = pokeElemOff (castPtr p) i (if x then 1 else 0::HTYPE_INT)
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)