Copyright | (c) Alexey Kuleshevich 2020 |
---|---|
License | BSD3 |
Maintainer | Alexey Kuleshevich <alexey@kuleshevi.ch> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- module GHC.Ptr
- plusOffPtr :: Prim e => Ptr e -> Off e -> Ptr e
- plusByteOffPtr :: Ptr e -> Off Word8 -> Ptr e
- minusOffPtr :: Prim e => Ptr e -> Ptr e -> Off e
- minusOffRemPtr :: Prim e => Ptr e -> Ptr e -> (Off e, Off Word8)
- minusByteOffPtr :: Ptr e -> Ptr e -> Off Word8
- readPtr :: (MonadPrim s m, Prim e) => Ptr e -> m e
- readOffPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> m e
- readByteOffPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off Word8 -> m e
- writePtr :: (MonadPrim s m, Prim e) => Ptr e -> e -> m ()
- writeOffPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> e -> m ()
- writeByteOffPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off Word8 -> e -> m ()
- setOffPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> Count e -> e -> m ()
- copyPtrToPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> Ptr e -> Off e -> Count e -> m ()
- copyByteOffPtrToPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
- movePtrToPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> Ptr e -> Off e -> Count e -> m ()
- moveByteOffPtrToPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
- comparePtrToPtr :: Prim e => Ptr e -> Off e -> Ptr e -> Off e -> Count e -> Ordering
- compareByteOffPtrToPtr :: Prim e => Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering
- freeHaskellFunPtr :: MonadPrim s m => FunPtr a -> m ()
- data Ptr a
- data FunPtr a
- castPtrToFunPtr :: Ptr a -> FunPtr b
- castFunPtrToPtr :: FunPtr a -> Ptr b
- castFunPtr :: FunPtr a -> FunPtr b
- nullFunPtr :: FunPtr a
- minusPtr :: Ptr a -> Ptr b -> Int
- alignPtr :: Ptr a -> Int -> Ptr a
- plusPtr :: Ptr a -> Int -> Ptr b
- castPtr :: Ptr a -> Ptr b
- nullPtr :: Ptr a
- newtype WordPtr = WordPtr Word
- ptrToWordPtr :: Ptr a -> WordPtr
- wordPtrToPtr :: WordPtr -> Ptr a
- newtype IntPtr = IntPtr Int
- ptrToIntPtr :: Ptr a -> IntPtr
- intPtrToPtr :: IntPtr -> Ptr a
- casOffPtr :: (MonadPrim s m, Atomic e) => Ptr e -> Off e -> e -> e -> m e
- atomicModifyOffPtr :: (MonadPrim s m, Atomic e) => Ptr e -> Off e -> (e -> (e, b)) -> m b
- atomicModifyOffPtr_ :: (MonadPrim s m, Atomic e) => Ptr e -> Off e -> (e -> e) -> m ()
- atomicModifyFetchOldOffPtr :: (MonadPrim s m, Atomic e) => Ptr e -> Off e -> (e -> e) -> m e
- atomicModifyFetchNewOffPtr :: (MonadPrim s m, Atomic e) => Ptr e -> Off e -> (e -> e) -> m e
- atomicAddFetchOldOffPtr :: (MonadPrim s m, AtomicCount e) => Ptr e -> Off e -> e -> m e
- atomicAddFetchNewOffPtr :: (MonadPrim s m, AtomicCount e) => Ptr e -> Off e -> e -> m e
- atomicSubFetchOldOffPtr :: (MonadPrim s m, AtomicCount e) => Ptr e -> Off e -> e -> m e
- atomicSubFetchNewOffPtr :: (MonadPrim s m, AtomicCount e) => Ptr e -> Off e -> e -> m e
- atomicAndFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e
- atomicAndFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e
- atomicNandFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e
- atomicNandFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e
- atomicOrFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e
- atomicOrFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e
- atomicXorFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e
- atomicXorFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e
- atomicNotFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> m e
- atomicNotFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> m e
- prefetchPtr0 :: MonadPrim s m => Ptr e -> m ()
- prefetchPtr1 :: MonadPrim s m => Ptr a -> m ()
- prefetchPtr2 :: MonadPrim s m => Ptr e -> m ()
- prefetchPtr3 :: MonadPrim s m => Ptr e -> m ()
- prefetchOffPtr0 :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> m ()
- prefetchOffPtr1 :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> m ()
- prefetchOffPtr2 :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> m ()
- prefetchOffPtr3 :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> m ()
Documentation
module GHC.Ptr
minusOffPtr :: Prim e => Ptr e -> Ptr e -> Off e Source #
Find the offset in number of elements that is between the two pointers by subtracting one address from another and dividing the result by the size of an element.
Since: 0.1.0
minusOffRemPtr :: Prim e => Ptr e -> Ptr e -> (Off e, Off Word8) Source #
Same as minusOffPtr
, but will also return the remainder in bytes that is left over.
Since: 0.1.0
minusByteOffPtr :: Ptr e -> Ptr e -> Off Word8 Source #
Find the offset in bytes that is between the two pointers by subtracting one address from another.
Since: 0.1.0
copyPtrToPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> Ptr e -> Off e -> Count e -> m () Source #
copyByteOffPtrToPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m () Source #
movePtrToPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off e -> Ptr e -> Off e -> Count e -> m () Source #
moveByteOffPtrToPtr :: (MonadPrim s m, Prim e) => Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m () Source #
comparePtrToPtr :: Prim e => Ptr e -> Off e -> Ptr e -> Off e -> Count e -> Ordering Source #
Compare memory between two pointers. Offsets and count is in number of elements,
instead of byte count. Use compareByteOffPtrToPtr
when offset in bytes is required.
compareByteOffPtrToPtr :: Prim e => Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering Source #
Same as comparePtrToPtr
, except offset is in bytes instead of number of elements.
freeHaskellFunPtr :: MonadPrim s m => FunPtr a -> m () Source #
Same as freeHaskellFunPtr
Since: 0.1.0
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
A value of type
is a pointer to a function callable
from foreign code. The type FunPtr
aa
will normally be a foreign type,
a function type with zero or more arguments where
- the argument types are marshallable foreign types,
i.e.
Char
,Int
,Double
,Float
,Bool
,Int8
,Int16
,Int32
,Int64
,Word8
,Word16
,Word32
,Word64
,
,Ptr
a
,FunPtr
a
or a renaming of any of these usingStablePtr
anewtype
. - the return type is either a marshallable foreign type or has the form
whereIO
tt
is a marshallable foreign type or()
.
A value of type
may be a pointer to a foreign function,
either returned by another foreign function or imported with a
a static address import likeFunPtr
a
foreign import ccall "stdlib.h &free" p_free :: FunPtr (Ptr a -> IO ())
or a pointer to a Haskell function created using a wrapper stub
declared to produce a FunPtr
of the correct type. For example:
type Compare = Int -> Int -> Bool foreign import ccall "wrapper" mkCompare :: Compare -> IO (FunPtr Compare)
Calls to wrapper stubs like mkCompare
allocate storage, which
should be released with freeHaskellFunPtr
when no
longer required.
To convert FunPtr
values to corresponding Haskell functions, one
can define a dynamic stub for the specific foreign type, e.g.
type IntFunction = CInt -> IO () foreign import ccall "dynamic" mkFun :: FunPtr IntFunction -> IntFunction
Instances
castPtrToFunPtr :: Ptr a -> FunPtr b #
castFunPtrToPtr :: FunPtr a -> Ptr b #
nullFunPtr :: FunPtr a #
The constant nullFunPtr
contains a
distinguished value of FunPtr
that is not
associated with a valid memory location.
minusPtr :: Ptr a -> Ptr b -> Int #
Computes the offset required to get from the second to the first argument. We have
p2 == p1 `plusPtr` (p2 `minusPtr` p1)
alignPtr :: Ptr a -> Int -> Ptr a #
Given an arbitrary address and an alignment constraint,
alignPtr
yields the next higher address that fulfills the
alignment constraint. An alignment constraint x
is fulfilled by
any address divisible by x
. This operation is idempotent.
An unsigned integral type that can be losslessly converted to and from
Ptr
. This type is also compatible with the C99 type uintptr_t
, and
can be marshalled to and from that type safely.
Instances
ptrToWordPtr :: Ptr a -> WordPtr #
casts a Ptr
to a WordPtr
wordPtrToPtr :: WordPtr -> Ptr a #
casts a WordPtr
to a Ptr
A signed integral type that can be losslessly converted to and from
Ptr
. This type is also compatible with the C99 type intptr_t
, and
can be marshalled to and from that type safely.
Instances
ptrToIntPtr :: Ptr a -> IntPtr #
casts a Ptr
to an IntPtr
intPtrToPtr :: IntPtr -> Ptr a #
casts an IntPtr
to a Ptr
Atomic
:: (MonadPrim s m, Atomic e) | |
=> Ptr e | Array to be mutated |
-> Off e | Index is in elements of |
-> e | Expected old value |
-> e | New value |
-> m e |
Perform atomic modification of an element in the Ptr
at the supplied
index. Returns the artifact of computation b
. Offset is in number of elements,
rather than bytes. Implies a full memory barrier.
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
:: (MonadPrim s m, Atomic e) | |
=> Ptr e | Array to be mutated |
-> Off e | Index is in elements of |
-> (e -> (e, b)) | Function that is applied to the old value and returns new value
and some artifact of computation |
-> m b |
Perform atomic modification of an element in the Ptr
at the supplied
index. Returns the artifact of computation b
. Offset is in number of elements,
rather than bytes. Implies a full memory barrier.
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
:: (MonadPrim s m, Atomic e) | |
=> Ptr e | Array to be mutated |
-> Off e | Index is in elements of |
-> (e -> e) | Function that is applied to the old value and returns new value. |
-> m () |
Perform atomic modification of an element in the Ptr
at the supplied
index. Offset is in number of elements, rather than bytes. Implies a full memory
barrier.
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
atomicModifyFetchOldOffPtr Source #
:: (MonadPrim s m, Atomic e) | |
=> Ptr e | Array to be mutated |
-> Off e | Index is in elements of |
-> (e -> e) | Function that is applied to the old value and returns the new value |
-> m e |
Perform atomic modification of an element in the Ptr
at the supplied
index. Returns the previous value. Offset is in number of elements, rather than
bytes. Implies a full memory barrier.
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
atomicModifyFetchNewOffPtr Source #
:: (MonadPrim s m, Atomic e) | |
=> Ptr e | Array to be mutated |
-> Off e | Index is in elements of |
-> (e -> e) | Function that is applied to the old value and returns the new value |
-> m e |
Perform atomic modification of an element in the Ptr
at the supplied
index. Offset is in number of elements, rather than bytes. Implies a full memory
barrier.
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
Numeric
atomicAddFetchOldOffPtr :: (MonadPrim s m, AtomicCount e) => Ptr e -> Off e -> e -> m e Source #
atomicAddFetchNewOffPtr :: (MonadPrim s m, AtomicCount e) => Ptr e -> Off e -> e -> m e Source #
atomicSubFetchOldOffPtr :: (MonadPrim s m, AtomicCount e) => Ptr e -> Off e -> e -> m e Source #
atomicSubFetchNewOffPtr :: (MonadPrim s m, AtomicCount e) => Ptr e -> Off e -> e -> m e Source #
Binary
atomicAndFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e Source #
atomicAndFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e Source #
atomicNandFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e Source #
Negation of binary conjunction (NAND) of an element of a Ptr
with the
supplied value, corresponds to \x y ->
done atomically. Returns the previous value. Offset is in number of elements, rather
than bytes. Implies a full memory barrier.complement
(x .&.
y)
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
atomicNandFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e Source #
Negation of binary conjunction (NAND) of an element of a Ptr
with the supplied
value, corresponds to \x y ->
done
atomically. Returns the new value. Offset is in number of elements, rather than
bytes. Implies a full memory barrier.complement
(x .&.
y)
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
atomicOrFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e Source #
atomicOrFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e Source #
atomicXorFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e Source #
Binary exclusive disjunction (XOR) of an element of a Ptr
with the supplied value,
corresponds to
done atomically. Returns the previous value. Offset
is in number of elements, rather than bytes. Implies a full memory barrier.xor
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
atomicXorFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> e -> m e Source #
Binary exclusive disjunction (XOR) of an element of a Ptr
with the supplied value,
corresponds to
done atomically. Returns the new value. Offset is
in number of elements, rather than bytes. Implies a full memory barrier.xor
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
atomicNotFetchOldOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> m e Source #
Binary negation (NOT) of an element of a Ptr
, corresponds to
(
done atomically. Returns the previous value. Offset is in
number of elements, rather than bytes. Implies a full memory barrier.complement
)
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
atomicNotFetchNewOffPtr :: (MonadPrim s m, AtomicBits e) => Ptr e -> Off e -> m e Source #
Binary negation (NOT) of an element of a Ptr
, corresponds to
(
done atomically. Returns the new value. Offset is in number
of elements, rather than bytes. Implies a full memory barrier.complement
)
Note - Bounds are not checked, therefore this function is unsafe.
Since: 0.1.0
Prefetch
prefetchPtr0 :: MonadPrim s m => Ptr e -> m () Source #
prefetchPtr1 :: MonadPrim s m => Ptr a -> m () Source #
prefetchPtr2 :: MonadPrim s m => Ptr e -> m () Source #
prefetchPtr3 :: MonadPrim s m => Ptr e -> m () Source #