The module Foreign.Ptr provides typed pointers to foreign entities. We distinguish two kinds of pointers: pointers to data and pointers to functions. It is understood that these two kinds of pointers may be represented differently as they may be references to data and text segments, respectively.
data Ptr a |
The type a will often be an instance of class Foreign.Storable.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.
instance Eq (Ptr a) |
instance Ord (Ptr a) |
instance Show (Ptr a) |
instance Storable (Ptr a) |
nullPtr :: Ptr a |
castPtr :: Ptr a -> Ptr b |
plusPtr :: Ptr a -> Int -> Ptr b |
alignPtr :: Ptr a -> Int -> Ptr a |
minusPtr :: Ptr a -> Ptr b -> Int |
data FunPtr a |
A value of type FunPtr a may be a pointer to a foreign function, either returned by another foreign function or imported with a a static address import like
or a pointer to a Haskell function created using a wrapper stub declared to produce a FunPtr of the correct type. For example:
Calls to wrapper stubs like mkCompare allocate storage, which should be released with Foreign.Ptr.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.
instance Eq (FunPtr a) |
instance Ord (FunPtr a) |
instance Show (FunPtr a) |
instance Storable (FunPtr a) |
nullFunPtr :: FunPtr a |
castFunPtr :: FunPtr a -> FunPtr b |
castFunPtrToPtr :: FunPtr a -> Ptr b |
Note: this is valid only on architectures where data and function pointers range over the same set of addresses, and should only be used for bindings to external libraries whose interface already relies on this assumption.
castPtrToFunPtr :: Ptr a -> FunPtr b |
Note: this is valid only on architectures where data and function pointers range over the same set of addresses, and should only be used for bindings to external libraries whose interface already relies on this assumption.
freeHaskellFunPtr :: FunPtr a -> IO () |
data IntPtr |
instance Bounded IntPtr |
instance Enum IntPtr |
instance Eq IntPtr |
instance Integral IntPtr |
instance Num IntPtr |
instance Ord IntPtr |
instance Read IntPtr |
instance Real IntPtr |
instance Show IntPtr |
instance Storable IntPtr |
instance Bits IntPtr |
ptrToIntPtr :: Ptr a -> IntPtr |
intPtrToPtr :: IntPtr -> Ptr a |
data WordPtr |
instance Bounded WordPtr |
instance Enum WordPtr |
instance Eq WordPtr |
instance Integral WordPtr |
instance Num WordPtr |
instance Ord WordPtr |
instance Read WordPtr |
instance Real WordPtr |
instance Show WordPtr |
instance Storable WordPtr |
instance Bits WordPtr |
ptrToWordPtr :: Ptr a -> WordPtr |
wordPtrToPtr :: WordPtr -> Ptr a |