Stable references to Haskell values
A stable pointer is a reference to a Haskell expression that is guaranteed not to be affected by garbage collection, i.e., it will neither be deallocated nor will the value of the stable pointer itself change during garbage collection (ordinary references may be relocated during garbage collection). Consequently, stable pointers can be passed to foreign code, which can treat it as an opaque reference to a Haskell value.
A value of type
StablePtr a is a stable pointer to a Haskell
expression of type
Create a stable pointer referring to the given Haskell value.
Dissolve the association between the stable pointer and the Haskell
value. Afterwards, if the stable pointer is passed to
freeStablePtr, the behaviour is
undefined. However, the stable pointer may still be passed to
castStablePtrToPtr, but the
castStablePtrToPtr, in this case, is undefined (in particular,
it may be
nullPtr). Nevertheless, the call
castStablePtrToPtr is guaranteed not to diverge.
Coerce a stable pointer to an address. No guarantees are made about
the resulting value, except that the original stable pointer can be
castPtrToStablePtr. In particular, the address may not
refer to an accessible memory location and any attempt to pass it to
the member functions of the class
Storable leads to
The C-side interface
The following definition is available to C programs inter-operating with
Haskell code when including the header
typedef void *HsStablePtr;
Note that no assumptions may be made about the values representing stable
pointers. In fact, they need not even be valid memory addresses. The only
guarantee provided is that if they are passed back to Haskell land, the
deRefStablePtr will be able to reconstruct the
Haskell value referred to by the stable pointer.