Nix 2.17.0
Nix, the purely functional package manager; stable external interfaces
Loading...
Searching...
No Matches

Reference counting and garbage collector operations. More...

Functions

nix_err nix_gc_incref (nix_c_context *context, const void *object)
 Increase the GC refcount. More...
 
nix_err nix_gc_decref (nix_c_context *context, const void *object)
 Decrease the GC refcount. More...
 
void nix_gc_now ()
 Trigger the garbage collector manually. More...
 
void nix_gc_register_finalizer (void *obj, void *cd, void(*finalizer)(void *obj, void *cd))
 Register a callback that gets called when the object is garbage collected. More...
 

Detailed Description

Reference counting and garbage collector operations.

Nix's evaluator uses a garbage collector. To ease C interop, we implement a reference counting scheme, where objects will be deallocated when there are no references from the Nix side, and the reference count kept by the C API reaches 0.

Functions returning a garbage-collected object will automatically increase the refcount for you. You should make sure to call nix_gc_decref when you're done.

Function Documentation

◆ nix_gc_decref()

nix_err nix_gc_decref ( nix_c_context context,
const void *  object 
)

Decrease the GC refcount.

Parameters
[out]contextOptional, stores error information
[in]objectThe object to stop referencing

◆ nix_gc_incref()

nix_err nix_gc_incref ( nix_c_context context,
const void *  object 
)

Increase the GC refcount.

The nix C api keeps alive objects by refcounting. When you're done with a refcounted pointer, call nix_gc_decref.

Parameters
[out]contextOptional, stores error information
[in]objectThe object to keep alive

◆ nix_gc_now()

void nix_gc_now ( )

Trigger the garbage collector manually.

You should not need to do this, but it can be useful for debugging.

◆ nix_gc_register_finalizer()

void nix_gc_register_finalizer ( void *  obj,
void *  cd,
void(*)(void *obj, void *cd)  finalizer 
)

Register a callback that gets called when the object is garbage collected.

Note
objects can only have a single finalizer. This function overwrites silently.
Parameters
[in]objthe object to watch
[in]cdthe data to pass to the finalizer
[in]finalizerthe callback function, called with obj and cd