View Source beam (zigler v0.11.0)
This struct contains adapters designed to facilitate interfacing the BEAM's c-style helpers for NIFs with a more idiomatic Zig-style of programming, for example, the use of slices instead of null-terminated arrays as strings.
This struct derives from zig/beam/beam.zig
, and is provided to the
project as a package. You may import it into any project zig code
using the following code:
const beam = @import("beam")
If there's something you need which is not provided, you can also
import erl_nif
package which provides direct access to the
equivalent calls from erl_nif.h
Summary
Types
a tag identifying the context in which the nif is running
creates a tuple type that corresponds to the call signature of passed function.
A zig enum equivalent to e.ErlNifTermType
Builds a datastructure that encapsulates information needed by a threaded nif
Builds a datastructure that defines callbacks for a threaded nif.
identical to ?*e.ErlNifEnv
identical to e.ErlNifEvent
. This is an event datatype that the BEAM
documentation does not describe.
boilerplate functions for nif module initialization. Contains
identical to e.ErlNifMonitor
. This is a monitor datatype that the BEAM
documentation does not describe.
identical to ?*e.ErlNifPid
identical to ?*e.ErlNifPort
wrapped term.
identical to e.ErlNifTid
. This is a thread id datatype that the BEAM.
documentation does not describe.
Functions (Exceptions)
The equivalent of Kernel.raise/1
from elixir.
The equivalent of error
in erlang.
Raises a special error datatype that contains an term-encoded stacktrace
datastructure. See also make_stacktrace
Functions (Concurrency)
periodic check-in
function for long-running nifs.
Functions (Env)
Synonym for e.enif_alloc_env
copies a term from one env to to another
Synonym for e.enif_free_env
Functions (Term Management)
converts a e.ErlNifBinary
to []const u8
.
converts a []u8
to a term/0
. The binary must be encoded using erlang term format.
generic cleanup function that can be used to cleanup values that have
been created by get
.
compares two terms.
converts BEAM term
dynamic types into static zig types
converts static zig types into BEAM term
dynamic types
returns the empty list term []
.
shortcut for make(env, .@"error", .{})
shortcut for make(env, .{.@"error", value}, options)
turns a []const u8
into a the corresponding atom/0
term.
performs a list cons operation for head
and tail
variables
turns a e.ErlNifPid
into a pid/0
term.
causes the VM to generate a new reference term
equivalent to Kernel.make_ref/0
converts a zig std.builtin.StackTrace
into a special term
that is designed to be translated and concatenated onto a BEAM
stacktrace.
marks a e.ErlNifBinary
as qualified to be garbage
collected.
This is a thin wrapper over e.enif_release_binary
.
returns a pid
value that represents the current or
parent process.
sends data
(as a term) to a target process' mailbox.
converts a term/0
to a e.ErlNifBinary
using erlang term format serialization.
Constants
implements std.mem.Allocator
using the std.mem.GeneralPurposeAllocator
factory, backed by beam.large_allocator
.
provides a BEAM allocator that can perform allocations with greater alignment than the machine word.
wraps e.enif_alloc
and e.enif_free
into the zig standard library allocator interface.
Variables
stores the allocator strategy for the currently running nif.
threadlocal variable that stores the execution context for the nif
Types
@type Compared :: .lt | .gt | .eq
result type for compare
these atoms are used to conform to Elixir's Compare interface see: https://hexdocs.pm/elixir/1.13/Enum.html#sort/2-sorting-structs
@type ExecutionContext :: .yielding | .threaded | .synchronous | .callback | .dirty
a tag identifying the context in which the nif is running
See nif documentation for more detailed information about the concurrency strategies.
.synchronous
: the execution context of a synchronous nif.threaded
: the execution context of a nif that runs in its own os thread.dirty
: the execution context of a nif that runs on a dirty scheduler.yielding
: the execution context of a nif that runs cooperatively with the BEAM scheduler.callback
: the execution context of module setup/teardown callbacks or a resource destruction callback
See context
for the threadlocal variable that stores this.
raw beam functions
nifs called in
raw
mode are not assigned an execution context.
@type Payload(anytype)
creates a tuple type that corresponds to the call signature of passed function.
Using this tuple type, it is possible to call the function using
the @call
builtin.
this is how calling functions can be easily made generic over multiple
concurrency types.
@type Resource(type, type, resource.ResourceOpts)
@type TermType ::
.ref | .bitstring | .tuple | .fun | .map | .float | .port | .pid | .list | .integer | .atom
A zig enum equivalent to e.ErlNifTermType
retrievable from term
using the term.term_type
method.
@type Thread(anytype)
Builds a datastructure that encapsulates information needed by a threaded nif
this datastructure is intended to be wrapped in a resource, so that the death of its parent process can be used to clean up the thread.
@type ThreadedCallbacks(type)
Builds a datastructure that defines callbacks for a threaded nif.
The argument is Thread
type. The datastructure
produces a struct with a dtor
callback that can be used to ensure
proper cleanup of the data in the thread.
these callbacks are called when the thread resource is destroyed.
Most importantly, this callback sets the threadlocal tracker in
that yield
investigates to determine if the parent
process should be terminated.
see Resource
for more details on the callbacks.
@type env :: env
identical to ?*e.ErlNifEnv
env
env
should be considered an opaque type that can be passed around without inspection of its contents.
@type event :: erl_nif_event
identical to e.ErlNifEvent
. This is an event datatype that the BEAM
documentation does not describe.
@type loader :: {}
boilerplate functions for nif module initialization. Contains:
blank_load
blank_upgrade
blank_unload
which are no-op versions of these functions.
@type monitor :: e.ErlNifMonitor
identical to e.ErlNifMonitor
. This is a monitor datatype that the BEAM
documentation does not describe.
@type pid :: pid
identical to ?*e.ErlNifPid
@type port :: e.ErlNifPort
identical to ?*e.ErlNifPort
@type term :: term
wrapped term.
e.ErlNifTerm
is, under the hood, an integer type. This is wrapped in a singleton struct
so that that semantic analysis can identify and distinguish between a
'plain' integer and a term.
The Zig compiler will optimize this away, so there is no runtime cost to
passing this around versus e.ErlNifTerm
, and the following conversion
operations are no-ops:
To convert to a raw
e.ErlNifTerm
, access the.v
field.To convert a raw
e.ErlNifTerm
to this term, use an anonymous struct:.{.v = erl_nif_term_value}
term_type
the struct function term_type
returns the TermType
of the
internal term.
const t = beam.term.make(env, 47, .{});
const term_type = t.term_type(env); // -> .integer
@type tid :: e.ErlNifTid
identical to e.ErlNifTid
. This is a thread id datatype that the BEAM.
documentation does not describe.
Functions (Exceptions)
@spec raise_elixir_exception(env, []const u8, anytype) :: term
The equivalent of Kernel.raise/1
from elixir.
module
should be the name of the module that represents the exceptiondata
should be a struct (possibly anonymous) that represents the Elixir exception payload.
Exception structs are not checked
The validity of the exception struct is not checked when using this function.
@spec raise_exception(env, anytype) :: term
The equivalent of error
in erlang.
raise_with_error_return(env_: env, err: anytype, maybe_return_trace: std.builtin.StackTrace) term
View Source@spec raise_with_error_return(env, anytype, ?stacktrace) :: term
Raises a special error datatype that contains an term-encoded stacktrace
datastructure. See also make_stacktrace
This datastructure is designed to be concatenated onto the existing stacktrace. In order to concatenate this stacktrace onto your BEAM exception, the function that wraps the nif must be able to catch the error and append the zig error return trace to the existing stacktrace.
Functions (Concurrency)
@spec yield(env) :: !void
periodic check-in
function for long-running nifs.
For threaded nifs:
- checks the status of the thread.
- If the thread's parent process has been killed, returns
error.processterminated
For yielding nifs (not implemented yet):
- relinquishes control to the BEAM scheduler.
- If the thread's parent process has been killed, returns
error.processterminated
- when control is returned from the scheduler, resumes with no error.
- creates an async suspend point.
For synchronous or dirty nifs:
- does nothing.
- there may be a slight performance regression as the function identifies the concurrency mode of the nif.
Functions (Env)
@spec alloc_env() :: env
Synonym for e.enif_alloc_env
@spec copy(env, term) :: term
copies a term from one env to to another
@spec free_env(env) :: void
Synonym for e.enif_free_env
Functions (Term Management)
@spec binary_to_slice(erl_nif_binary) :: []u8
converts a e.ErlNifBinary
to []const u8
.
Does not perform allocations or copies
binary data
This points to the data location of the binary, which might either be in the shared binary heap, or it might be in the process heap (for small binaries). This should be considered read-only, attempts to sneakily modify these data will have undefined effects, possibly including broken comparison operations.
@spec binary_to_term(env, []u8) :: !term
converts a []u8
to a term/0
. The binary must be encoded using erlang term format.
This is a thin wrapper over e.enif_binary_to_term
.
@spec cleanup(anytype, anytype) :: void
generic cleanup function that can be used to cleanup values that have
been created by get
.
The second parameter is an options
parameters, which should be passed a
struct (possibly anonymous) with the following fields:
allocator
: which allocator should be used to clean up allocations. optional, defaults to the threadlocalallocator
value
@spec compare(term, term) :: beam.Compared
compares two terms.
get(dest_type: T, beam.env, source_term: beam.term, options: anytype) T
View Source@spec get(T, beam.env, beam.term, anytype) :: T
converts BEAM term
dynamic types into static zig types
The arguments are as follows:
- destination type
- environment
- term to convert
- struct (usually passed as anonymous) of keyword options for additional features. See supported options
See also make
for the reverse operation.
The following type classes (as passed as 1st argument) are supported by get
:
integer
- unsigned and signed integers supported
- all integer sizes from 0..64 bits supported (including non-power-of-2 sizes)
- for sizes bigger than 64, supported, but the passed term must be a native-endian binary.
Example
do_get(47)
pub fn do_get(env: beam.env, term: beam.term) void {
const x: i32 = beam.get(i32, env, term, .{}); // -> x = 47
...
}
enum
- may be passed the integer value of the enum.
- may be passed the atom representation of the enum.
- zero- and one- item enum types are not currently supported
Example
do_get(:foo)
const EnumType = enum {foo, bar};
pub fn do_get(env: beam.env, term: beam.term) void {
const x: EnumType = beam.get(EnumType, env, term, .{}); // -> x = .foo
...
}
float
- supports
f16
,f32
, andf64
types. - may be passed a BEAM
float/0
term - atoms
:infinity
,:neg_infinity
,:NaN
are also supported
Example
do_get(47.0)
pub fn do_get(env: beam.env, term: beam.term) void {
const x: f32 = beam.get(f32, env, term, .{}); // -> x = 47.0
...
}
struct
- may be passed
map/0
withatom/0
keys and values of the appropriate type - may be passed a
keyword/0
list withatom/0
keys and values of the appropriate type. - inner values are recursively converted to the appropriate type.
- NOTE: the struct types must be exported as
pub
in the module's top-level namespace. - if the struct is
packed
orextern
, supports binary data.
Example
do_get(%{foo: 47, bar: %{baz: :quux}})
pub const EnumType = enum {quux, mlem};
pub const InnerType = struct {
baz: EnumType,
};
pub const StructType = struct {
foo: i32,
bar: InnerType
};
pub fn do_get(env: beam.env, term: beam.term) void {
const x = beam.get(StructType, env, term, .{}); // -> x = .{foo: 47, bar: .{baz: .quux}}
...
}
bool
- supports
true
andfalse
boolean/0
terms only.
Example
do_get(true)
pub fn do_get(env: beam.env, term: beam.term) void {
const x: bool = beam.get(bool, env, term, .{}); // -> x = true
...
}
array
- supports lists of terms that can be converted to the array's element type.
- note that arrays have compile-time known length.
- if the array's element is integers, floats, packed or extern structs, or arrays that support binaries, then the array can be passed binary data.
- does not perform allocation
Allocation warning
as allocation is not performed, getting could be a very expensive operation.
Example
do_get([47, 48, 49])
do_get(<<47 :: signed-int-size(32), 48 :: signed-int-size(32), 49 :: signed-int-size(32)>>)
pub fn do_get(env: beam.env, term: beam.term) void {
const x = beam.get([3]i32, env, term, .{}); // -> x = .{47, 48, 49}
...
}
single-item pointer
- allocates memory based on allocator provided in the options, otherwise
defaults to
beam.allocator
- supports any type as above.
- returns an error if the allocation fails.
Example
do_get(%{foo: 47})
const MyStruct = struct { foo: i32 };
pub fn do_get(env: beam.env, term: beam.term) void {
const x = beam.get(*MyStruct, env, term, .{}); // -> x = a pointer to .{.foo = 47}
...
}
slice
- allocates memory based on allocator provided in the options, otherwise
defaults to
beam.allocator
- note that slice carries a runtime length
- supports list of any type
- supports binary of any type that can be represented as a fixed size binary.
Example
do_get([47, 48, 49])
do_get(<<47 :: signed-int-size(32), 48 :: signed-int-size(32), 49 :: signed-int-size(32)>>)
pub fn do_get(env: beam.env, term: beam.term) void {
const x = beam.get([]i32, env, term, .{}); // -> x = a pointer to .{47, 48, 49}
...
}
many-item-pointer
allocates memory based on allocator provided in the options, otherwise defaults to
beam.allocator
supports list of any type
supports binary of any type that can be represented as a fixed size binary.
the runtime length is not a part of this datastructure, you are expected to keep track of it using some other mechanism
Length warning
due to the fact that this datatype drops its length information, this datatype should be handled with extreme care.
Example
do_get([47, 48, 49])
do_get(<<47 :: signed-int-size(32), 48 :: signed-int-size(32), 49 :: signed-int-size(32)>>)
pub fn do_get(env: beam.env, term: beam.term) void {
const x = beam.get([*]i32, env, term, .{}); // -> x = a pointer to .{47, 48, 49}
...
}
cpointer
allocates memory based on allocator provided in the options, otherwise defaults to
beam.allocator
supports list of any type
supports binary of any type that can be represented as a fixed size binary.
the runtime length is not a part of this datastructure, you are expected to keep track of it using some other mechanism
Length warning
due to the fact that this datatype drops its length information, this datatype should only be used where c interop is needed
Example
do_get([47, 48, 49])
do_get(<<47 :: signed-int-size(32), 48 :: signed-int-size(32), 49 :: signed-int-size(32)>>)
pub fn do_get(env: beam.env, term: beam.term) void {
const x = beam.get([*]i32, env, term, .{}); // -> x = a pointer to .{47, 48, 49}
...
}
optional
- accepts
atom/0
nil
as well as whatever the child type is. - note that zig has
null
sonil
will get converted tonull
.
Example
do_get(nil)
pub fn do_get(env: beam.env, term: beam.term) void {
const x = beam.get(?i32, env, term, .{}); // -> x = null
...
}
Supported options
allocator
: the allocator to use for allocations. If not provided, defaults tobeam.allocator
.error_info
: pointer to aterm
that can be populated with error information that gets propagated on failure to convert. If omitted, the code to produce these errors will get optimized out.
@spec make(env, anytype, anytype) :: term
converts static zig types into BEAM term
dynamic types
The arguments are as follows:
- environment
value
to convert to termoptions
struct (usually passed as anonymous) of keyword options for additional features. See supported options. Note this struct must be comptime-known.
See also get
for the reverse operation.
The following zig types are supported:
term
- no conversion is performed
- this type is necessary for recursive make operations
void
- returns atom
:ok
- supporting this type makes metaprogramming easier.
pid
std.builtin.StackTrace
- special interface for returning stacktrace info to BEAM.
integers
- unsigned and signed integers supported
- all integer sizes from 0..64 bits supported (including non-power-of-2 sizes)
- returns a BEAM
integer/0
term - for sizes bigger than 64, supported, but the passed term will be converted into a binary term bearing the integer encoded with native endianness.
- comptime integers supported
Example
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, 47, .{});
}
do_make() # -> 47
floats
- supports
f16
,f32
, andf64
types. - supports comptime float type.
- returns a BEAM
float/0
term - may also return one of the
atom/0
type:infinity
,:neg_infinity
,:NaN
Example
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, 47.0, .{});
}
do_make() # -> 47.0
bool
- supports
bool
types. - returns a BEAM
boolean/0
term
Example
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, true, .{});
}
do_make() # -> true
enum or error enum
- supports
enum
orerror
types. - doesn't support zero or one-item enums.
- returns a BEAM
atom/0
term - also supports enum literals.
Example
with an enum:
const EnumType = enum {foo, bar};
pub fn do_make(env: beam.env) beam.term {
const e = EnumType.foo;
return beam.make(env, e, .{});
}
with an error enum:
const ErrorType = error {foo, bar};
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, error.foo, .{});
}
with an enum literal:
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, .foo, .{});
}
do_make() # -> :foo
Enum literals
Enum literals are especially useful for returning atoms, such as
:ok
or:error
. Note thaterror
is a reserved word in zig, so you will need to use.@"error"
to generate the corresponding atom.
optionals or null
Example
with null literal:
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, null, .{});
}
with an optional type:
pub fn do_make(env: beam.env) beam.term {
const value: ?i32 = null;
return beam.make(env, value, .{});
}
do_make() # -> null
arrays
- supports arrays of any term that can be encoded using
make
- note that arrays have compile-time known length.
- outputs as a list of the encoded terms
- arrays of
u8
default to outputting binary, this is the only exception to the above rule. - if the array's element is integers, floats, packed or extern structs,
or arrays that support binaries, then the array can be output as binary
data, by setting
output_type
option to.binary
- if the array's element is u8 and you would prefer outputting as a list,
setting
output_type
option to.list
will do this.
Examples
array, u8, output as binary/0
:
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, "foo", .{});
}
do_make() # -> "foo"
array, u8, output as list/0
:
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, "foo", .{.output_type = .list});
}
do_make() # -> ~C'foo'
array, u16, output as list/0
:
pub fn do_make(env: beam.env) beam.term {
const list = [_]u16{47, 48, 49}
return beam.make(env, list, .{});
}
do_make() # -> [47, 48, 49]
array, u16, output as binary/0
:
pub fn do_make(env: beam.env) beam.term {
const list = [_]u16{47, 48, 49}
return beam.make(env, list, .{.output_type = .binary});
}
do_make() # -> <<47, 00, 48, 00, 49, 00>>
structs
- supports structs with fields of any term that can be encoded using
make
- outputs as a
map/0
with atom keys and the encoded terms as values - for packed or extern structs, supports binary data by setting
output_type
option to.binary
- encoding options are passed recursively, if something more complex is needed, encoding should be performed manually.
- supports anonymous structs
Examples
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, .{.foo = 123, .bar = "bar", .baz = .baz}, .{});
}
do_make() # -> %{foo: 123, bar: "bar", baz: :baz}
tuples
- supports tuples with any term that can be encoded using
make
- outputs as a
tuple/0
. - encoding options are passed recursively, if something more complex is needed, encoding should be performed manually.
- note that error atom should be encoded as
.@"error"
Examples
pub fn do_make(env: beam.env) beam.term {
return beam.make(env, .{.ok, "foo", 47}, .{});
}
do_make() # -> {:ok, "foo", 47}
single-item-pointer
- these pointers are only supported for arrays and structs
- these are only supported because they are assumed to be pointers to mutable data
- content will be dereferenced and encoded as if it were the child type
output_type
rules (see arrays) apply.
Examples
pub fn do_make(env: beam.env) beam.term {
const array = [_]i32{47, 48, 49}
return beam.make(env, &array, .{});
}
do_make() # -> [47, 48, 49]
slice
- supports arrays of any term that can be encoded using
make
- note that arrays have compile-time known length.
- outputs as a list of the encoded terms
- slices of
u8
default to outputting binary, this is the only exception to the above rule. - if the slice's element is integers, floats, packed or extern structs,
or arrays that support binaries, then the slice can be output as binary
data, by setting
output_type
option to.binary
output_type
rules (see arrays) apply.
Examples
pub fn do_make(env: beam.env) beam.term {
const slice = [_]i32{47, 48, 49}[0..]; // note this is now a slice
return beam.make(env, &slice, .{});
}
do_make() # -> [47, 48, 49]
many-item-pointer
- only supported if the pointer is sentinel-terminated.
- outputs as a list of the encoded terms
- pointers of
u8
default to outputting binary, this is the only exception to the above rule. - if the pointers's element is integers, floats, packed or extern structs,
or arrays that support binaries, then the slice can be output as binary
data, by setting
output_type
option to.binary
output_type
rules (see arrays) apply.
Examples
pub fn do_make(env: beam.env) beam.term {
const slice = [_]i32{47, 48, 49, 0}[0..];
const ptr = @ptrCast([*:0], &slice.ptr);
return beam.make(env, &slice, .{});
}
do_make() # -> [47, 48, 49]
cpointer
- only supported if the pointer has child type
u8
or pointer. - in the case of
u8
interprets it as[*:0]u8
. - in the case of
Pointer
interprets it as[*:null]?Pointer
. - no other types are supported.
- note that the content will be interpreted as the pointer type, so rules on pointers (see single-item-pointers))
output_type
rules (see arrays) apply.
Examples
pub fn do_make(env: beam.env) beam.term {
const slice = [_]i32{47, 48, 49, 0}[0..];
const ptr = @ptrCast([*:0], &slice.ptr);
return beam.make(env, &slice, .{});
}
do_make() # -> [47, 48, 49]
@spec make_empty_list(env) :: term
returns the empty list term []
.
This is a thin wrapper over e.enif_make_empty_list
.
@spec make_error_atom(env) :: term
shortcut for make(env, .@"error", .{})
@spec make_error_pair(env, anytype, anytype) :: term
shortcut for make(env, .{.@"error", value}, options)
@spec make_into_atom(env, []const u8) :: term
turns a []const u8
into a the corresponding atom/0
term.
returns a raised ArgumentError
if the length of the string exceeds the
vm atom size limit (255 bytes)
This is a thin wrapper over e.enif_make_atom_len
.
@spec make_list_cell(env, term, term) :: term
performs a list cons operation for head
and tail
variables
This is a thin wrapper over e.enif_make_list_cell
.
@spec make_pid(env, pid) :: term
turns a e.ErlNifPid
into a pid/0
term.
This is a thin wrapper over e.enif_make_pid
.
@spec make_ref(env) :: term
causes the VM to generate a new reference term
equivalent to Kernel.make_ref/0
This is a thin wrapper over e.enif_make_ref
.
@spec make_stacktrace(env, stacktrace) :: term
converts a zig std.builtin.StackTrace
into a special term
that is designed to be translated and concatenated onto a BEAM
stacktrace.
Example term:
[
%{
line_info: %{file_name: "/path/to/project/lib/my_app/.Elixir.MyApp.MyModule.zig", line: 15},
symbol_name: "my_fun",
compile_unit_name: "Elixir.MyApp.MyModule"
}
...
]
@spec release_binary(erl_nif_binary_pointer) :: void
marks a e.ErlNifBinary
as qualified to be garbage
collected.
This is a thin wrapper over e.enif_release_binary
.
@spec self(env) :: !pid
returns a pid
value that represents the current or
parent process.
equivalent to Kernel.self/0
scope
This function succeeds in all contexts, except for callback contexts. For threaded processes, it will return the process that spawned the thread, whether or not that process is still alive.
@spec send(env, pid, anytype) :: !term
sends data
(as a term) to a target process' mailbox.
equivalent to Kernel.send/2
This function is a context-aware wrapper over
e.enif_send
.
that also serializes the message term using make
send from raw nifs or out of bounds callbacks
This function has undefined behaviour when called from
raw
nifs or callbacks that areout of bounds
(e.g. if a new thread is started from a posix call that is not managed by zigler)in these cases, use
e.enif_send
directly instead. Note you may have to create thebeam.term
from an environment first.
@spec term_to_binary(env, term) :: !erl_nif_binary
converts a term/0
to a e.ErlNifBinary
using erlang term format serialization.
This is a thin wrapper over e.enif_term_to_binary
.
returns error.OutOfMemory
if the allocation fails.
Functions
make_general_purpose_allocator_instance() std.heap.GeneralPurposeAllocator(...)
View Source@spec make_general_purpose_allocator_instance() :: std.heap.GeneralPurposeAllocator(...)
Constants
@spec general_purpose_allocator :: mem.Allocator
implements std.mem.Allocator
using the std.mem.GeneralPurposeAllocator
factory, backed by beam.large_allocator
.
@spec large_allocator :: mem.Allocator
provides a BEAM allocator that can perform allocations with greater alignment than the machine word.
Memory performance
This comes at the cost of some memory to store metadata
currently does not release memory that is resized. For this behaviour
use beam.general_purpose_allocator
.
not threadsafe. for a threadsafe allocator, use beam.general_purpose_allocator
@spec raw_allocator :: mem.Allocator
wraps e.enif_alloc
and e.enif_free
into the zig standard library allocator interface.
Variables
@spec allocator :: mem.Allocator
stores the allocator strategy for the currently running nif.
this variable is threadlocal, so that each called NIF can set it as a global variable and not pass it around.
allocator starts undefined
This threadlocal is set to
undefined
because of architectural differences: we cannot trust loaded dynamic libraries to properly set this on thread creation. Each function is responsible for setting allocator correctly whenever execution control is returned to it.
raw
function calls do not set the allocator and must either set it themselves or always use a specific allocator strategy in its function calls.
@spec context :: beam.ExecutionContext
threadlocal variable that stores the execution context for the nif
See ExecutionContext
for the list of valid enums.
context starts undefined
This threadlocal is set to
undefined
because of architectural differences: we cannot trust loaded dynamic libraries to properly set this on thread creation.
raw
function calls do not setcontext