Copyright | (C) 2018 Johann Lee <me@qinka.pro> |
---|---|
License | LGPL3 |
Maintainer | me@qinka.pro |
Stability | experimental |
Portability | unknown |
Safe Haskell | Safe |
Language | Haskell2010 |
The types and the class of FAI.
Synopsis
- type family Pf p t :: *
- data Buffer p a = Buffer {
- bufPtr :: !(ForeignPtr (Pf p a))
- bufSize :: !Int
- newtype Context p = Context {
- unContextPtr :: ForeignPtr (Context p)
- newtype Accelerate p a = Accelerate {
- doAccelerate :: Context p -> IO (a, Context p)
- class FAI p where
- class (FAI p1, FAI p2) => FAICopy p1 p2 where
- type FinalizerContextPtr p a = Either (FinalizerEnvPtr (Context p) a) (FinalizerPtr a)
- class Storable a where
- data Ptr a
- data ForeignPtr a
- liftIO :: MonadIO m => IO a -> m a
Documentation
type family Pf p t :: * Source #
Platform types
Context of platform
The Haskell GC can not guarantee that Context p
will be released after
all the Buffer p a
is released.
So the C implement at lower level need to make sure it.
Context | |
|
newtype Accelerate p a Source #
Accelearate type.
Accelerate | |
|
Instances
Monad (Accelerate p) Source # | |
Defined in Foreign.FAI.Types (>>=) :: Accelerate p a -> (a -> Accelerate p b) -> Accelerate p b # (>>) :: Accelerate p a -> Accelerate p b -> Accelerate p b # return :: a -> Accelerate p a # fail :: String -> Accelerate p a # | |
Functor (Accelerate p) Source # | |
Defined in Foreign.FAI.Types fmap :: (a -> b) -> Accelerate p a -> Accelerate p b # (<$) :: a -> Accelerate p b -> Accelerate p a # | |
Applicative (Accelerate p) Source # | |
Defined in Foreign.FAI.Types pure :: a -> Accelerate p a # (<*>) :: Accelerate p (a -> b) -> Accelerate p a -> Accelerate p b # liftA2 :: (a -> b -> c) -> Accelerate p a -> Accelerate p b -> Accelerate p c # (*>) :: Accelerate p a -> Accelerate p b -> Accelerate p b # (<*) :: Accelerate p a -> Accelerate p b -> Accelerate p a # | |
MonadIO (Accelerate p) Source # | |
Defined in Foreign.FAI.Types liftIO :: IO a -> Accelerate p a # |
FAI interface
:: Context p | Context |
-> IO (FinalizerContextPtr p a) | pointer of the function of release the pointer |
type FinalizerContextPtr p a = Either (FinalizerEnvPtr (Context p) a) (FinalizerPtr a) Source #
Context concened finalizer
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
A value of type
represents a pointer to an object, or an
array of objects, which may be marshalled to or from Haskell values
of type Ptr
aa
.
The type a
will often be an instance of class
Storable
which provides the marshalling operations.
However this is not essential, and you can provide your own operations
to access the pointer. For example you might write small foreign
functions to get or set the fields of a C struct
.
Instances
Eq (Ptr a) | |
Ord (Ptr a) | |
Show (Ptr a) | Since: base-2.1 |
Storable (Ptr a) | Since: base-2.1 |
Foldable (URec (Ptr ()) :: * -> *) | |
Defined in Data.Foldable fold :: Monoid m => URec (Ptr ()) m -> m # foldMap :: Monoid m => (a -> m) -> URec (Ptr ()) a -> m # foldr :: (a -> b -> b) -> b -> URec (Ptr ()) a -> b # foldr' :: (a -> b -> b) -> b -> URec (Ptr ()) a -> b # foldl :: (b -> a -> b) -> b -> URec (Ptr ()) a -> b # foldl' :: (b -> a -> b) -> b -> URec (Ptr ()) a -> b # foldr1 :: (a -> a -> a) -> URec (Ptr ()) a -> a # foldl1 :: (a -> a -> a) -> URec (Ptr ()) a -> a # toList :: URec (Ptr ()) a -> [a] # null :: URec (Ptr ()) a -> Bool # length :: URec (Ptr ()) a -> Int # elem :: Eq a => a -> URec (Ptr ()) a -> Bool # maximum :: Ord a => URec (Ptr ()) a -> a # minimum :: Ord a => URec (Ptr ()) a -> a # | |
Traversable (URec (Ptr ()) :: * -> *) | |
Defined in Data.Traversable |
data ForeignPtr a #
The type ForeignPtr
represents references to objects that are
maintained in a foreign language, i.e., that are not part of the
data structures usually managed by the Haskell storage manager.
The essential difference between ForeignPtr
s and vanilla memory
references of type Ptr a
is that the former may be associated
with finalizers. A finalizer is a routine that is invoked when
the Haskell storage manager detects that - within the Haskell heap
and stack - there are no more references left that are pointing to
the ForeignPtr
. Typically, the finalizer will, then, invoke
routines in the foreign language that free the resources bound by
the foreign object.
The ForeignPtr
is parameterised in the same way as Ptr
. The
type argument of ForeignPtr
should normally be an instance of
class Storable
.
Instances
Eq (ForeignPtr a) | Since: base-2.1 |
Defined in GHC.ForeignPtr (==) :: ForeignPtr a -> ForeignPtr a -> Bool # (/=) :: ForeignPtr a -> ForeignPtr a -> Bool # | |
Ord (ForeignPtr a) | Since: base-2.1 |
Defined in GHC.ForeignPtr compare :: ForeignPtr a -> ForeignPtr a -> Ordering # (<) :: ForeignPtr a -> ForeignPtr a -> Bool # (<=) :: ForeignPtr a -> ForeignPtr a -> Bool # (>) :: ForeignPtr a -> ForeignPtr a -> Bool # (>=) :: ForeignPtr a -> ForeignPtr a -> Bool # max :: ForeignPtr a -> ForeignPtr a -> ForeignPtr a # min :: ForeignPtr a -> ForeignPtr a -> ForeignPtr a # | |
Show (ForeignPtr a) | Since: base-2.1 |
Defined in GHC.ForeignPtr showsPrec :: Int -> ForeignPtr a -> ShowS # show :: ForeignPtr a -> String # showList :: [ForeignPtr a] -> ShowS # |