HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulebase-4.20.2.0Haskell2010

Foreign.StablePtr

This module is part of the Foreign Function Interface (FFI) and will usually be imported via the module Foreign.

  • 1 type
  • 5 values
  • Packagebase-4.20.2.0
  • Exports6
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceStablePtr.hs

Stable references to Haskell values

6 declarations
datadata StablePtr a
#

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.

The StablePtr 0 is reserved for representing NULL in foreign code.

A value of type StablePtr a is a stable pointer to a Haskell expression of type a.

Instances2Eq, Storable
  • Eq (StablePtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Stable
  • Storable (StablePtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
valuecastStablePtrToPtr :: StablePtr a -> Ptr ()
#

Coerce a stable pointer to an address. No guarantees are made about the resulting value, except that the original stable pointer can be recovered by castPtrToStablePtr. In particular, the address might not refer to an accessible memory location and any attempt to pass it to the member functions of the class Storable leads to undefined behaviour.

The C-side interface

The following definition is available to C programs inter-operating with Haskell code when including the header HsFFI.h.

typedef void *HsStablePtr;  /* C representation of a StablePtr */

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 function deRefStablePtr will be able to reconstruct the Haskell value referred to by the stable pointer.