regional-pointers- Regional memory pointers

MaintainerBas van Dijk <>







Note that this module re-exports the Control.Monad.Trans.Region module from the regions package which allows you to run regions using runRegionT and duplicate a RegionalPtr to a parent region using dup.

Regional pointers

data RegionalPtr α r Source

A regional pointer to memory.

This should provide a safer replacement for Foreign.Ptr.Ptr

Null pointers

nullPtr :: NullPtr α RootRegionSource

The constant nullPtr is a pointer which is not associated with a valid memory location.

Note that nullPtr is a pure value. This means it does not perform the side-effect of registering a finalizer like "free nullPtr" in the RegionT monad.

Finally note that the region parameter of the NullPtr is set to RootRegion which is the ancestor of any region. This allows nullPtr to be used in any region.

Class of pointers

class Pointer pointer whereSource


mapPointer :: (Ptr α -> Ptr β) -> pointer α r -> pointer β rSource

Apply a pure function to the inner pointer of a regional pointer.

class PrivateAllocatedPointer pointer => AllocatedPointer pointer Source

Class of pointers which point to allocated memory. NullPtr is the only pointer which is not an instance of this class.

The super class PrivateAllocatedPointer is not exported by this module which ensures you can't accidentally make NullPtr an instance of this class.

Pure functions on pointers

castPtr :: Pointer pointer => pointer α r -> pointer β rSource

The castPtr function casts a pointer from one type to another.

Wraps: Foreign.Ptr.castPtr

alignPtr :: AllocatedPointer pointer => pointer α r -> Int -> pointer α rSource

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.

Wraps: Foreign.Ptr.alignPtr

plusPtr :: AllocatedPointer pointer => pointer α r -> Int -> pointer β rSource

Advances the given address by the given offset in bytes.

Wraps: Foreign.Ptr.plusPtr

minusPtr :: AllocatedPointer pointer => pointer α r1 -> pointer β r2 -> IntSource

Computes the offset required to get from the second to the first argument. We have

 p2 == p1 `plusPtr` (p2 `minusPtr` p1)

Wraps: Foreign.Ptr.minusPtr