| Copyright | (c) 2026 Tushar Adhatrao |
|---|---|
| License | MIT |
| Stability | internal |
| Safe Haskell | None |
| Language | GHC2021 |
WasmEdge.Internal.Ownership
Description
This module implements the two fundamental pointer wrappers:
OwnedCtx— Haskell owns the resource; the GC will call the C delete function via aForeignPtrfinalizer if the Haskell object is collected.BorrowedCtx— Haskell does not own the resource; no finalizer is attached. The lifetime is guaranteed only by the caller.
The phantom type parameter tag prevents mixing up different context types.
Do not import this module in user code.
Synopsis
- data OwnedCtx tag = OwnedCtx {
- unOwnedCtx :: !(ForeignPtr tag)
- disarmAction :: !(IO ())
- mkOwned :: (Ptr tag -> IO ()) -> Ptr tag -> IO (OwnedCtx tag)
- withOwned :: OwnedCtx tag -> (Ptr tag -> IO a) -> IO a
- touchOwned :: OwnedCtx tag -> IO ()
- finalizeOwned :: OwnedCtx tag -> IO ()
- disarmOwned :: OwnedCtx tag -> IO ()
- newtype BorrowedCtx tag = BorrowedCtx {
- unBorrowedCtx :: Ptr tag
- borrowCtx :: OwnedCtx tag -> BorrowedCtx tag
- withBorrowed :: BorrowedCtx tag -> (Ptr tag -> IO a) -> IO a
- unsafeBorrowOwned :: OwnedCtx tag -> BorrowedCtx tag
- unsafeOwnedPtr :: OwnedCtx tag -> Ptr tag
Owned contexts (Haskell owns, GC finalizes)
An holds a OwnedCtx tagForeignPtr with a C delete function
attached as a GC finalizer.
The finalizer is called exactly once, either when:
- The explicit
delete*function is called (viafinalizeOwned), or - The
ForeignPtris garbage-collected.
In practice, prefer explicit cleanup with with* bracket functions.
Constructors
| OwnedCtx | |
Fields
| |
touchOwned :: OwnedCtx tag -> IO () #
Ensure an OwnedCtx is kept alive up to this point in a sequence of
IO actions. Use when passing the underlying pointer to a C function that
does not keep a reference, and you need GHC not to collect the object early.
finalizeOwned :: OwnedCtx tag -> IO () #
Explicitly finalize an OwnedCtx, running its cleanup action immediately
and disabling future GC finalization.
disarmOwned :: OwnedCtx tag -> IO () #
Disarm the finalizer of an OwnedCtx when ownership has been transferred
to a C container (e.g. adding a functiontablememory/global to a ModuleInstance).
Borrowed contexts (no ownership, no finalizer)
newtype BorrowedCtx tag #
A is a non-owning reference to a C context.
It carries no finalizer. The caller must guarantee that the underlying
C object outlives any use of this BorrowedCtx tagBorrowedCtx.
Constructors
| BorrowedCtx | |
Fields
| |
Instances
| Eq (BorrowedCtx tag) # | |
Defined in WasmEdge.Internal.Ownership Methods (==) :: BorrowedCtx tag -> BorrowedCtx tag -> Bool # (/=) :: BorrowedCtx tag -> BorrowedCtx tag -> Bool # | |
borrowCtx :: OwnedCtx tag -> BorrowedCtx tag #
Borrow an OwnedCtx: extract a non-owning BorrowedCtx.
The OwnedCtx must remain alive as long as the BorrowedCtx is used.
withBorrowed :: BorrowedCtx tag -> (Ptr tag -> IO a) -> IO a #
Run an IO action with the underlying Ptr of a BorrowedCtx.
Unsafe coercions (internal use only)
unsafeBorrowOwned :: OwnedCtx tag -> BorrowedCtx tag #
Unsafe: extract a BorrowedCtx from an OwnedCtx without
keeping the owner alive. Use only when the caller guarantees lifetime.
unsafeOwnedPtr :: OwnedCtx tag -> Ptr tag #
Unsafe: extract the raw Ptr from an OwnedCtx.
The ForeignPtr may be GC'd after this call.