Copyright | (c) Mateusz Kłoczko 2016 |
---|---|
License | MIT |
Maintainer | mateusz.p.kloczko@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- class GStorable' f where
- gpeekByteOff' :: [Int] -> Int -> Ptr b -> Int -> IO (f a)
- gpokeByteOff' :: [Int] -> Int -> Ptr b -> Int -> f a -> IO ()
- glistSizeOf' :: f a -> [Size]
- glistAlignment' :: f a -> [Alignment]
- class GStorable a where
- gsizeOf :: a -> Int
- galignment :: a -> Int
- gpeekByteOff :: Ptr b -> Int -> IO a
- gpokeByteOff :: Ptr b -> Int -> a -> IO ()
- class Storable a where
- internalSizeOf :: forall f p. GStorable' f => f p -> Int
- internalAlignment :: forall f p. GStorable' f => f p -> Alignment
- internalPeekByteOff :: forall f p b. (KnownNat (NoFields f), GStorable' f) => Ptr b -> Offset -> IO (f p)
- internalPokeByteOff :: forall f p b. (KnownNat (NoFields f), GStorable' f) => Ptr b -> Offset -> f p -> IO ()
- internalOffsets :: forall f p. GStorable' f => f p -> [Offset]
Documentation
class GStorable' f where Source #
:: [Int] | List of fields' offsets for the type/struct. |
-> Int | The index. Used to obtain the correct offset |
-> Ptr b | The pointer to the type/struct. |
-> Int | Global offset. |
-> IO (f a) | The result, wrapped in GHC.Generic metadata. | Write the element at a given offset. Additional information about the offests of the subfields are needed. |
Read the element at a given offset. Additional information about the offests of the subfields are needed.
:: [Int] | List of fields' offsets for the type/struct. |
-> Int | The index. Used to obtain the correct offset. |
-> Ptr b | The pointer to the type/struct. |
-> Int | Global offset. |
-> f a | The element to write, wrapped in GHC.Generic metadata. |
-> IO () |
:: f a | GHC.Generic information about a given type/struct. |
-> [Size] | List of sizes. |
Calculates the sizes of type's/struct's fields.
:: f a | GHC.Generic information about a given type/struct. |
-> [Alignment] | List of alignments. |
Calculates the alignments of type's/struct's fields.
Instances
GStorable' (U1 :: Type -> Type) Source # | |
Storable a => GStorable' (K1 i a :: Type -> Type) Source # | |
(TypeError SumTypesDisabled :: Constraint) => GStorable' (f :+: g) Source # | |
(KnownNat (NoFields f), KnownNat (NoFields g), GStorable' f, GStorable' g) => GStorable' (f :*: g) Source # | |
GStorable' f => GStorable' (M1 i t f) Source # | |
class GStorable a where Source #
The class uses the default Generic based implementations to
provide Storable instances for types made from primitive types.
Sum types work with sumtypes
cabal flag enabled - or
just with -DGSTORABLE_SUMTYPES cpp flag.
Nothing
:: a | Element of a given type. Can be undefined. |
-> Int | Size. |
Calculate the size of the type.
:: a | Element of a given type. Can be undefined |
-> Int | Alignment. |
Calculate the alignment of the type.
default galignment :: (Generic a, GStorable' (Rep a)) => a -> Int Source #
Read the variable from a given pointer.
default gpeekByteOff :: (KnownNat (NoFields (Rep a)), Generic a, GStorable' (Rep a)) => Ptr b -> Int -> IO a Source #
Write the variable to a pointer.
The member functions of this class facilitate writing values of primitive types to raw memory (which may have been allocated with the above mentioned routines) and reading values from blocks of raw memory. The class, furthermore, includes support for computing the storage requirements and alignment restrictions of storable types.
Memory addresses are represented as values of type
, for some
Ptr
aa
which is an instance of class Storable
. The type argument to
Ptr
helps provide some valuable type safety in FFI code (you can't
mix pointers of different types without an explicit cast), while
helping the Haskell type system figure out which marshalling method is
needed for a given pointer.
All marshalling between Haskell and a foreign language ultimately
boils down to translating Haskell data structures into the binary
representation of a corresponding data structure of the foreign
language and vice versa. To code this marshalling in Haskell, it is
necessary to manipulate primitive data types stored in unstructured
memory blocks. The class Storable
facilitates this manipulation on
all types for which it is instantiated, which are the standard basic
types of Haskell, the fixed size Int
types (Int8
, Int16
,
Int32
, Int64
), the fixed size Word
types (Word8
, Word16
,
Word32
, Word64
), StablePtr
, all types from Foreign.C.Types,
as well as Ptr
.
sizeOf, alignment, (peek | peekElemOff | peekByteOff), (poke | pokeElemOff | pokeByteOff)
Computes the storage requirements (in bytes) of the argument. The value of the argument is not used.
Computes the alignment constraint of the argument. An
alignment constraint x
is fulfilled by any address divisible
by x
. The value of the argument is not used.
peekElemOff :: Ptr a -> Int -> IO a #
Read a value from a memory area regarded as an array
of values of the same kind. The first argument specifies
the start address of the array and the second the index into
the array (the first element of the array has index
0
). The following equality holds,
peekElemOff addr idx = IOExts.fixIO $ \result -> peek (addr `plusPtr` (idx * sizeOf result))
Note that this is only a specification, not necessarily the concrete implementation of the function.
pokeElemOff :: Ptr a -> Int -> a -> IO () #
Write a value to a memory area regarded as an array of values of the same kind. The following equality holds:
pokeElemOff addr idx x = poke (addr `plusPtr` (idx * sizeOf x)) x
peekByteOff :: Ptr b -> Int -> IO a #
Read a value from a memory location given by a base address and offset. The following equality holds:
peekByteOff addr off = peek (addr `plusPtr` off)
pokeByteOff :: Ptr b -> Int -> a -> IO () #
Write a value to a memory location given by a base address and offset. The following equality holds:
pokeByteOff addr off x = poke (addr `plusPtr` off) x
Read a value from the given memory location.
Note that the peek and poke functions might require properly
aligned addresses to function correctly. This is architecture
dependent; thus, portable code should ensure that when peeking or
poking values of some type a
, the alignment
constraint for a
, as given by the function
alignment
is fulfilled.
Write the given value to the given memory location. Alignment
restrictions might apply; see peek
.
Instances
:: forall f p. GStorable' f | |
=> f p | Generic representation |
-> Int | Resulting size |
Calculates the size of generic data-type.
:: forall f p. GStorable' f | |
=> f p | Generic representation |
-> Alignment | Resulting alignment |
Calculates the alignment of generic data-type.
:: forall f p b. (KnownNat (NoFields f), GStorable' f) | |
=> Ptr b | Pointer to peek |
-> Offset | Offset |
-> IO (f p) | Resulting generic representation |
View the variable under a pointer, with offset.
:: forall f p b. (KnownNat (NoFields f), GStorable' f) | |
=> Ptr b | Pointer to write to |
-> Offset | Offset |
-> f p | Written generic representation |
-> IO () |
Write the variable under the pointer, with offset.
internalOffsets :: forall f p. GStorable' f => f p -> [Offset] Source #
Obtain the list of offsets