erl_nif(3erl)

๐ŸŽ€ erl_nif โ€” C Library Functions (3erl) ๐ŸŽ€

okay so bestie, this is the erl_nif manual page โ€” specifically the C Library Functions one. Itโ€™s giving โ€œpowerful but handle with care,โ€ and we are going to keep every single detail. ๐Ÿซถ

๐ŸŒธ NAME ๐ŸŒธ

erl_nif โ€” API functions for an Erlang NIF library.

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

A NIF library contains native implementation of some functions of an Erlang module. The native implemented functions (NIFs) are called like any other functions without any difference to the caller. A NIF library is built as a dynamically linked library file and loaded in runtime by calling erlang:load_nif/2.

โš ๏ธ Warning: Use this functionality with extreme care.

A native function is executed as a direct extension of the native code of the VM. Execution is not made in a safe environment. The VM cannot provide the same services as provided when executing Erlang code, such as pre-emptive scheduling or memory protection. If the native function does not behave well, the whole VM will misbehave.

  • A native function that crashes will crash the whole VM.
  • An erroneously implemented native function can cause a VM internal state inconsistency, which can cause a crash of the VM, or miscellaneous misbehaviors of the VM at any point after the call to the native function.
  • A native function doing lengthy work before returning degrades responsiveness of the VM, and can cause miscellaneous strange behaviors. Such strange behaviors include, but are not limited to, extreme memory usage, and bad load balancing between schedulers. Strange behaviors that can occur because of lengthy work can also vary between Erlang/OTP releases.

๐Ÿ“ EXAMPLE ๐Ÿ“

A minimal example of a NIF library can look as follows:

/* niftest.c */
#include <erl_nif.h>

static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    return enif_make_string(env, "Hello world!", ERL_NIF_LATIN1);
}

static ErlNifFunc nif_funcs[] =
{
    {"hello", 0, hello}
};

ERL_NIF_INIT(niftest,nif_funcs,NULL,NULL,NULL,NULL)

The Erlang module can look as follows:

-module(niftest).

-export([init/0, hello/0]).

-nifs([hello/0]).

-on_load(init/0).

init() ->
      erlang:load_nif("./niftest", 0).

hello() ->
      erlang:nif_error("NIF library not loaded").

Compile and test can look as follows (on Linux):

$> gcc -fPIC -shared -o niftest.so niftest.c -I $ERL_ROOT/usr/include/
$> erl

1> c(niftest).
{ok,niftest}
2> niftest:hello().
"Hello world!"

In the example above the on_load directive is used get function init called automatically when the module is loaded. Function init in turn calls erlang:load_nif/2 which loads the NIF library and replaces the hello function with its native implementation in C. Once loaded, a NIF library is persistent. It will not be unloaded until the module code version that it belongs to is purged.

The -nifs() attribute specifies which functions in the module that are to be replaced by NIFs.

Each NIF must have an implementation in Erlang to be invoked if the function is called before the NIF library is successfully loaded. A typical such stub implementation is to call erlang:nif_error which will raise an exception. The Erlang function can also be used as a fallback implementation if the NIF library lacks implementation for some OS or hardware architecture for example.

๐Ÿ“ Note: A NIF does not have to be exported, it can be local to the module. However, unused local stub functions will be optimized away by the compiler, causing loading of the NIF library to fail.

โญ FUNCTIONALITY โญ

All interaction between NIF code and the Erlang runtime system is performed by calling NIF API functions. Functions exist for the following functionality:

๐Ÿ’– Read and write Erlang terms ๐Ÿ’–

Any Erlang terms can be passed to a NIF as function arguments and be returned as function return values. The terms are of C-type ERL_NIF_TERM and can only be read or written using API functions. Most functions to read the content of a term are prefixed enif_get_ and usually return true (or false) if the term is of the expected type (or not). The functions to write terms are all prefixed enif_make_ and usually return the created ERL_NIF_TERM. There are also some functions to query terms, like enif_is_atom, enif_is_identical, and enif_compare.

All terms of type ERL_NIF_TERM belong to an environment of type ErlNifEnv. The lifetime of a term is controlled by the lifetime of its environment object. All API functions that read or write terms has the environment that the term belongs to as the first function argument.

๐ŸŽ€ Binaries ๐ŸŽ€

Terms of type binary are accessed with the help of struct type ErlNifBinary, which contains a pointer (data) to the raw binary data and the length (size) of the data in bytes. Both data and size are read-only and are only to be written using calls to API functions. Instances of ErlNifBinary are, however, always allocated by the user (usually as local variables).

The raw data pointed to by data is only mutable after a call to enif_alloc_binary or enif_realloc_binary. All other functions that operate on a binary leave the data as read-only. A mutable binary must in the end either be freed with enif_release_binary or made read-only by transferring it to an Erlang term with enif_make_binary. However, it does not have to occur in the same NIF call. Read-only binaries do not have to be released.

enif_make_new_binary can be used as a shortcut to allocate and return a binary in the same NIF call.

Binaries are sequences of whole bytes. Bitstrings with an arbitrary bit length have no support yet.

๐ŸŒˆ Resource objects ๐ŸŒˆ

The use of resource objects is a safe way to return pointers to native data structures from a NIF. A resource object is only a block of memory allocated with enif_alloc_resource. A handle ("safe pointer") to this memory block can then be returned to Erlang by the use of enif_make_resource. The term returned by enif_make_resource is opaque in nature. It can be stored and passed between processes, but the only real end usage is to pass it back as an argument to a NIF. The NIF can then call enif_get_resource and get back a pointer to the memory block, which is guaranteed to still be valid. A resource object is not deallocated until the last handle term is garbage collected by the VM and the resource is released with enif_release_resource (not necessarily in that order).

All resource objects are created as instances of some resource type. This makes resources from different modules to be distinguishable. A resource type is created by calling enif_open_resource_type when a library is loaded. Objects of that resource type can then later be allocated and enif_get_resource verifies that the resource is of the expected type. A resource type can have a user-supplied destructor function, which is automatically called when resources of that type are released (by either the garbage collector or enif_release_resource). Resource types are uniquely identified by a supplied name string and the name of the implementing module.

The following is a template example of how to create and return a resource object.

ERL_NIF_TERM term;
MyStruct* obj = enif_alloc_resource(my_resource_type, sizeof(MyStruct));

/* initialize struct ... */

term = enif_make_resource(env, obj);

if (keep_a_reference_of_our_own) {
    /* store 'obj' in static variable, private data or other resource object */
}
else {
    enif_release_resource(obj);
    /* resource now only owned by "Erlang" */
}
return term;

Notice that once enif_make_resource creates the term to return to Erlang, the code can choose to either keep its own native pointer to the allocated struct and release it later, or release it immediately and rely only on the garbage collector to deallocate the resource object eventually when it collects the term.

Another use of resource objects is to create binary terms with user-defined memory management. enif_make_resource_binary creates a binary term that is connected to a resource object. The destructor of the resource is called when the binary is garbage collected, at which time the binary data can be released. An example of this can be a binary term consisting of data from a mmap'ed file. The destructor can then do munmap to release the memory region.

Resource types support upgrade in runtime by allowing a loaded NIF library to take over an already existing resource type and by that "inherit" all existing objects of that type. The destructor of the new library is thereafter called for the inherited objects and the library with the old destructor function can be safely unloaded. Existing resource objects, of a module that is upgraded, must either be deleted or taken over by the new NIF library. The unloading of a library is postponed as long as there exist resource objects with a destructor function in the library.

๐Ÿงš Module upgrade and static data ๐Ÿงš

A loaded NIF library is tied to the Erlang module instance that loaded it. If the module is upgraded, the new module instance needs to load its own NIF library (or maybe choose not to). The new module instance can, however, choose to load the exact same NIF library as the old code if it wants to. Sharing the dynamic library means that static data defined by the library is shared as well. To avoid unintentionally shared static data between module instances, each Erlang module version can keep its own private data. This private data can be set when the NIF library is loaded and later retrieved by calling enif_priv_data.

๐Ÿ”ฅ Threads and concurrency ๐Ÿ”ฅ

A NIF is thread-safe without any explicit synchronization as long as it acts as a pure function and only reads the supplied arguments. When you write to a shared state either through static variables or enif_priv_data, you need to supply your own explicit synchronization. This includes terms in process independent environments that are shared between threads. Resource objects also require synchronization if you treat them as mutable.

The library initialization callbacks load and upgrade are thread-safe even for shared state data.

๐Ÿ‘‘ Version Management ๐Ÿ‘‘

When a NIF library is built, information about the NIF API version is compiled into the library. When a NIF library is loaded, the runtime system verifies that the library is of a compatible version. erl_nif.h defines the following:

  • ERL_NIF_MAJOR_VERSION โ€” bestie, this one gets bumped whenever the Erlang runtime ships incompatible changes to the NIF library situation. Usually you just recompile your NIF library and go about your day, but likeโ€ฆ under rare circumstances it can mean your NIF libraries need tiny lil' tweaks. And if that's the case, it will of course be documented. No mystery, periodt. โœจ
  • ERL_NIF_MINOR_VERSION โ€” this one goes up whenever new features drop. The runtime system peeks at the minor version to figure out which features it gets to use. It's giving "what's in season" energy ngl ๐ŸŒธ

The runtime system normally straight-up refuses to load a NIF library if:

  • the major versions differ, or
  • the major versions are equal but the minor version used by the NIF library is greater than the one used by the runtime system.

Old NIF libraries with lower major versions are, however, allowed after a bump of the major version during a transition period of two major releases. ๐Ÿ™ˆ Such old NIF libraries can however fail if deprecated features are used โ€” so like, don't get too comfy.


โฐ Time Measurement

Support for time measurement in NIF libraries, iykyk:

  • ErlNifTime
  • ErlNifTimeUnit
  • enif_monotonic_time()
  • enif_time_offset()
  • enif_convert_time_unit()

๐Ÿซง I/O Queues

The Erlang nif library contains function for easily working with I/O vectors as used by the unix system call writev. ๐Ÿงš

โš ๏ธ Heads up bestie โ€” the I/O Queue is not thread safe, so some other synchronization mechanism has to be used. Don't say I didn't warn you ๐Ÿ’€

The cast of characters:

  • SysIOVec
  • ErlNifIOVec
  • enif_ioq_create()
  • enif_ioq_destroy()
  • enif_ioq_enq_binary()
  • enif_ioq_enqv()
  • enif_ioq_deq()
  • enif_ioq_peek()
  • enif_ioq_peek_head()
  • enif_inspect_iovec()
  • enif_free_iovec()

Typical usage when writing to a file descriptor looks like this โ€” like, this is the whole vibe, look at it:

int writeiovec(ErlNifEnv *env, ERL_NIF_TERM term, ERL_NIF_TERM *tail,
               ErlNifIOQueue *q, int fd) {

    ErlNifIOVec vec, *iovec = &vec;
    SysIOVec *sysiovec;
    int saved_errno;
    int iovcnt, n;

    if (!enif_inspect_iovec(env, 64, term, tail, &iovec))
        return -2;

    if (enif_ioq_size(q) > 0) {
        /* If the I/O queue contains data we enqueue the iovec and
           then peek the data to write out of the queue. */
        if (!enif_ioq_enqv(q, iovec, 0))
            return -3;

        sysiovec = enif_ioq_peek(q, &iovcnt);
    } else {
        /* If the I/O queue is empty we skip the trip through it. */
        iovcnt = iovec->iovcnt;
        sysiovec = iovec->iov;
    }

    /* Attempt to write the data */
    n = writev(fd, sysiovec, iovcnt);
    saved_errno = errno;

    if (enif_ioq_size(q) == 0) {
        /* If the I/O queue was initially empty we enqueue any
           remaining data into the queue for writing later. */
        if (n >= 0 && !enif_ioq_enqv(q, iovec, n))
            return -3;
    } else {
        /* Dequeue any data that was written from the queue. */
        if (n > 0 && !enif_ioq_deq(q, n, NULL))
            return -4;
    }

    /* return n, which is either number of bytes written or -1 if
       some error happened */
    errno = saved_errno;
    return n;
}

๐ŸŒ Long-running NIFs

Okay so โ€” remember that warning text at the beginning of this manual page? Yeah. It is of vital importance that a native function returns relatively fast. ๐Ÿ’…

It's genuinely difficult to give an exact maximum amount of time that a native function is allowed to work, but usually a well-behaving native function is to return to its caller within 1 millisecond. Yeah, one. โฑ๏ธ

This can be achieved using different approaches. If you have full control over the code to execute in the native function, the best approach is to divide the work into multiple chunks of work and call the native function multiple times. This is, however, not always possible โ€” for example when calling third-party libraries. ๐Ÿ˜ญ (third-party libs really said "no thoughts, head empty")

The enif_consume_timeslice() function can be used to inform the runtime system about the length of the NIF call. It is typically always to be used unless the NIF executes very fast.

If the NIF call is too lengthy, this must be handled in one of the following ways to avoid degraded responsiveness, scheduler load balancing problems, and other strange behaviors:

โœจ Yielding NIF

If the functionality of a long-running NIF can be split so that its work can be achieved through a series of shorter NIF calls, the application has two options:

  • Make that series of NIF calls from the Erlang level.
  • Call a NIF that first performs a chunk of the work, then invokes the enif_schedule_nif function to schedule another NIF call to perform the next chunk. The final call scheduled in this manner can then return the overall result.

Breaking up a long-running function in this manner enables the VM to regain control between calls to the NIFs.

This approach is always preferred over the other alternatives described below. This both from a performance perspective and a system characteristics perspective. Like, it's not even close. ๐Ÿ‘‘

๐Ÿงต Threaded NIF

This is accomplished by dispatching the work to another thread managed by the NIF library, return from the NIF, and wait for the result. The thread can send the result back to the Erlang process using enif_send. Information about thread primitives is provided below. ๐Ÿชก

๐Ÿงผ Dirty NIF

A NIF that cannot be split and cannot execute in a millisecond or less is called a "dirty NIF", as it performs work that the ordinary schedulers of the Erlang runtime system cannot handle cleanly. ๐Ÿ˜ค Applications that make use of such functions must indicate to the runtime that the functions are dirty so they can be handled specially. This is handled by executing dirty jobs on a separate set of schedulers called dirty schedulers. A dirty NIF executing on a dirty scheduler does not have the same duration restriction as a normal NIF.

It is important to classify the dirty job correct. An I/O bound job should be classified as such, and a CPU bound job should be classified as such. If you should classify CPU bound jobs as I/O bound jobs, dirty I/O schedulers might starve ordinary schedulers. ๐Ÿ™…โ€โ™€๏ธ I/O bound jobs are expected to either block waiting for I/O, and/or spend a limited amount of time moving data.

To schedule a dirty NIF for execution, the application has two options:

  • Set the appropriate flags value for the dirty NIF in its ErlNifFunc entry.
  • Call enif_schedule_nif, pass to it a pointer to the dirty NIF to be executed, and indicate with argument flags whether it expects the operation to be CPU-bound or I/O-bound.

A job that alternates between I/O bound and CPU bound can be reclassified and rescheduled using enif_schedule_nif so that it executes on the correct type of dirty scheduler at all times. For more information see the documentation of the erl(1) command line arguments +SDcpu, and +SDio. ๐Ÿ“š

๐Ÿ’€ Okay this next bit is the part nobody talks about. While a process executes a dirty NIF, some operations that communicate with it can take a very long time to complete. Suspend or garbage collection of a process executing a dirty NIF cannot be done until the dirty NIF has returned. Thus, other processes waiting for such operations to complete might have to wait for a very long time. Blocking multi-scheduling, that is, calling erlang:system_flag(multi_scheduling, block), can also take a very long time to complete. This is because all ongoing dirty operations on all dirty schedulers must complete before the block operation can complete. ๐Ÿ˜ญ๐Ÿ˜ญ

Many operations communicating with a process executing a dirty NIF can, however, complete while it executes the dirty NIF. For example, retrieving information about it through process_info, setting its group leader, register/unregister its name, and so on. ๐Ÿซถ

Termination of a process executing a dirty NIF can only be completed up to a certain point while it executes the dirty NIF. All Erlang resources, such as its registered name and its ETS tables, are released. All links and monitors are triggered. The execution of the NIF is, however, not stopped. The NIF can safely continue execution, allocate heap memory, and so on, but it is of course better to stop executing as soon as possible. The NIF can check whether a current process is alive using enif_is_current_process_alive. Communication using enif_send and enif_port_command is also dropped when the sending process is not alive. Deallocation of certain internal resources, such as process heap and process control block, is delayed until the dirty NIF has completed. ๐Ÿฆ‹

๐Ÿ’– INITIALIZATION ๐Ÿ’–

okay bestie, this is the part where your library officially wakes up and becomes a Real NIF Library. no cap. ๐Ÿ‘‘

๐ŸŒŸ The magic macro: ERL_NIF_INIT

ERL_NIF_INIT(MODULE, ErlNifFunc funcs[], load, NULL, upgrade, unload)

this is the magic macro that initializes a NIF library. and where does it go? it has to be evaluated in global file scope. like, not inside a function, not tucked into some random block โ€” global file scope, periodt. โœจ

  • MODULE โ€” the name of the Erlang module, written as an identifier without string quotations. the macro stringifies it for you, so no quotes, bestie. ๐ŸŽ€
  • funcs โ€” a static array of function descriptors for all the NIFs implemented in this library. all of them. the whole squad. ๐Ÿ’…
  • load, upgrade, and unload โ€” pointers to functions. one of load or upgrade gets called to initialize the library, and unload gets called to release it. each one has its own little section below. ๐Ÿฆ‹
  • the fourth argument, NULL โ€” literally ignored. it was previously used for the deprecated reload callback, which has not been supported since OTP 20. so yeah, it's just vibing there doing nothing. ๐Ÿ’€
ngl, that NULL is giving "seat reserved for a friend who moved away." don't try to use it for anything.

๐ŸŒˆ static NIFs, if that's your thing

if you're compiling a NIF lib for static inclusion through --enable-static-nifs, then the macro STATIC_ERLANG_NIF_LIBNAME must be defined as the name of the archive file โ€” excluding the file extension .a โ€” and again with no string quotations. ๐Ÿ™ˆ

it must only contain characters allowed in a C identifier. oh, and the macro has to be defined before erl_nif.h is included. order matters, bestie! โญ

if you're using the older macro STATIC_ERLANG_NIF instead, then the name of the archive file must match the name of the module. that's the rule. iykyk. ๐Ÿงš


๐ŸŒธ int (*load)(ErlNifEnv* caller_env, void** priv_data, ERL_NIF_TERM load_info)

int (*load)(ErlNifEnv* caller_env, void** priv_data, ERL_NIF_TERM load_info)

load gets called when the NIF library is loaded and there's no previously loaded library for this module. main character moment. ๐Ÿ‘‘

  • *priv_data can be set to point to some private data if your library needs to keep state between NIF calls. enif_priv_data returns this pointer. and when load is called, *priv_data is initialized to NULL. ๐Ÿซถ
  • load_info is the second argument to erlang:load_nif/2. โœจ
heads up: the library fails to load if load returns anything other than 0. also, load can be NULL if initialization isn't needed. lowkey convenient.

๐Ÿฆ‹ int (*upgrade)(ErlNifEnv* caller_env, void** priv_data, void** old_priv_data, ERL_NIF_TERM load_info)

int (*upgrade)(ErlNifEnv* caller_env, void** priv_data, void** old_priv_data, ERL_NIF_TERM load_info)

upgrade gets called when the NIF library is loaded and there is old code of this module with a loaded NIF library. glow-up time. ๐Ÿ”ฅ

it works just like load, except that *old_priv_data already contains the value set by the last call to load or upgrade for the old module code. and *priv_data is initialized to NULL when upgrade is called. ๐Ÿ’œ

you're allowed to write to both *priv_data and *old_priv_data. both. no cap. ๐ŸŽ€

warning bestie: the library fails to load if upgrade returns anything other than 0, or if upgrade is NULL. so don't leave it null and expect a cute little upgrade to happen. ๐Ÿ’€

๐ŸŽ€ void (*unload)(ErlNifEnv* caller_env, void* priv_data)

void (*unload)(ErlNifEnv* caller_env, void* priv_data)

unload gets called when the module code that the NIF library belongs to is purged as old. new code of the same module may or may not exist โ€” it's giving "we'll see" energy. ๐ŸŒธ


that's the init squad, bestie. every pointer, every NULL, every return-value rule, all accounted for. next up, more manual page to slay. ๐Ÿ’…โœจ

๐Ÿ’– DATA TYPES ๐Ÿ’–

๐Ÿ”ฎ ERL_NIF_TERM

Okay so variables of type ERL_NIF_TERM can refer to literally any Erlang term, no cap. It's an opaque type, which means the only things you're allowed to do with a value of it are: pass it as an argument to API functions, or return it from NIFs. That's it. That's the whole vibe.

All ERL_NIF_TERMs belong to an environment (ErlNifEnv). And here's the tea โ€” a term cannot be destructed individually. It stays valid until its environment gets destructed. Periodt. ๐Ÿ’…


๐ŸŒธ ErlNifEnv

ErlNifEnv represents an environment that can host Erlang terms. Every term inside an environment is valid for as long as the environment itself is valid. ErlNifEnv is an opaque type too โ€” pointers to it can only be handed off to API functions, nothing else.

There are three types of environments, bestie, and you need to know which is which:

๐Ÿซถ Process bound environment

Passed as the first argument to all NIFs. All function arguments passed to a NIF belong to that environment, and the return value from a NIF must also be a term belonging to that same environment.

A process bound environment contains transient information about the calling Erlang process. It's only valid in the thread where it was supplied as argument, until the NIF returns. So storing pointers to process bound environments between NIF calls is useless and dangerous. Don't do it. ๐Ÿ˜ญ

๐ŸŽ€ Callback environment

Passed as the first argument to all the non-NIF callback functions (load, upgrade, unload, dtor, down, stop and dyncall). Works like a process bound environment but with a temporary pseudo process that "terminates" when the callback has returned. You can create terms in this environment, but they'll only be accessible during the callback. Like a party that ends when the callback leaves. ๐ŸŽ‰

๐Ÿฆ‹ Process independent environment

Created by calling enif_alloc_env. This environment can be used to store terms between NIF calls and to send terms with enif_send. A process independent environment with all its terms is valid until you explicitly invalidate it with enif_free_env or enif_send. This is the one that actually sticks around, icon behaviour. ๐Ÿ‘‘

All contained terms of a list/tuple/map must belong to the same environment as the list/tuple/map itself. Mixing environments is giving chaos. Terms can be copied between environments with enif_make_copy.

๐Ÿงš ErlNifFunc

typedef struct {
    const char* name;
    unsigned arity;
    ERL_NIF_TERM (*fptr)(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
    unsigned flags;
} ErlNifFunc;

Describes a NIF by its name, arity, and implementation. Slay.

  • fptr โ€” a pointer to the function that implements the NIF.
  • argv โ€” contains the function arguments passed to the NIF.
  • argc โ€” the array length, that is, the function arity. argv[N-1] thus denotes the Nth argument to the NIF. Notice that the argument argc allows for the same C function to implement several Erlang functions with different arity (but probably with the same name). Reusable king. ๐Ÿ”ฅ
  • flags โ€” is 0 for a regular NIF (and so its value can be omitted for statically initialized ErlNifFunc instances).

flags can be used to indicate that the NIF is a dirty NIF that is to be executed on a dirty scheduler thread.

If the dirty NIF is expected to be CPU-bound, its flags field is to be set to ERL_NIF_DIRTY_JOB_CPU_BOUND or ERL_NIF_DIRTY_JOB_IO_BOUND.

Note: if one of the ERL_NIF_DIRTY_JOB_*_BOUND flags is set, and the runtime system has no support for dirty schedulers, the runtime system refuses to load the NIF library. So like... check first, bestie. ๐Ÿ’€

๐Ÿ“ ErlNifBinary

typedef struct {
    size_t size;
    unsigned char* data;
} ErlNifBinary;

ErlNifBinary contains transient information about an inspected binary term. data is a pointer to a buffer of size bytes with the raw content of the binary.

Notice that ErlNifBinary is a semi-opaque type and you are only allowed to read fields size and data. Hands off the rest, bestie. ๐Ÿ™ˆ

โญ ErlNifBinaryToTerm

An enumeration of the options that can be specified to enif_binary_to_term. For default behavior, use value 0.

When receiving data from untrusted sources, use option ERL_NIF_BIN2TERM_SAFE. Please. For me. ๐Ÿฅบ


๐Ÿ’œ ErlNifMonitor

This is an opaque data type that identifies a monitor.

The nif writer is to provide the memory for storing the monitor when calling enif_monitor_process. The address of the data is not stored by the runtime system, so ErlNifMonitor can be used as any other data โ€” it can be copied, moved in memory, forgotten, and so on. To compare two monitors, enif_compare_monitors must be used. No == shenanigans, iykyk.


๐ŸŒˆ ErlNifPid

A process identifier (pid). In contrast to pid terms (instances of ERL_NIF_TERM), ErlNifPids are self-contained and not bound to any environment. ErlNifPid is an opaque type. It can be copied, moved in memory, forgotten, and so on. Free spirit energy. ๐Ÿฆ‹


๐Ÿ’ซ ErlNifPort

A port identifier. In contrast to port ID terms (instances of ERL_NIF_TERM), ErlNifPorts are self-contained and not bound to any environment. ErlNifPort is an opaque type. It can be copied, moved in memory, forgotten, and so on.


๐Ÿ‘‘ ErlNifResourceType

Each instance of ErlNifResourceType represents a class of memory-managed resource objects that can be garbage collected. Each resource type has a unique name and a destructor function that is called when objects of its type are released. Very main character of them.


๐ŸŽ€ ErlNifResourceTypeInit

typedef struct {
    ErlNifResourceDtor* dtor;       // #1 Destructor
    ErlNifResourceStop* stop;       // #2 Select stop
    ErlNifResourceDown* down;       // #3 Monitor down
    int members;
    ErlNifResourceDynCall* dyncall; // #4 Dynamic call
} ErlNifResourceTypeInit;

Initialization structure read by enif_open_resource_type_x enif_init_resource_type.


๐ŸŒธ ErlNifResourceDtor

typedef void ErlNifResourceDtor(ErlNifEnv* caller_env, void* obj);

The function prototype of a resource destructor function.

The obj argument is a pointer to the resource. The only allowed use for the resource in the destructor is to access its user data one final time. The destructor is guaranteed to be the last callback before the resource is deallocated. Last words only, no funny business. ๐Ÿ˜ค


๐Ÿซถ ErlNifResourceDown

typedef void ErlNifResourceDown(ErlNifEnv* caller_env, void* obj, ErlNifPid* pid, ErlNifMonitor* mon);

The function prototype of a resource down function, called on the behalf of enif_monitor_process. obj is the resource, pid is the identity of the monitored process that is exiting, and mon is the identity of the monitor.


๐Ÿฆ‹ ErlNifResourceStop

typedef void ErlNifResourceStop(ErlNifEnv* caller_env, void* obj, ErlNifEvent event, int is_direct_call);

The function prototype of a resource stop function, called on the behalf of enif_select. obj is the resource, event is OS event, is_direct_call is true if the call is made directly from enif_select or false if it is a scheduled call (potentially from another thread).


โœจ ErlNifResourceDynCall

typedef void ErlNifResourceDynCall(ErlNifEnv* caller_env, void* obj, void* call_data);

The function prototype of a dynamic resource call function, called by enif_dynamic_resource_call. Argument obj is the resource object and call_data is the last argument to enif_dynamic_resource_call passed through.


๐ŸŽ€ ErlNifCharEncoding

typedef enum {
    ERL_NIF_LATIN1
}ErlNifCharEncoding;

The character encoding used in strings and atoms. The only supported encoding is ERL_NIF_LATIN1 for ISO Latin-1 (8-bit ASCII). That's the whole menu, bestie. ๐Ÿฝ๏ธ


๐Ÿ’– ErlNifSysInfo

Used by enif_system_info to return information about the runtime system. Contains the same content as ErlDrvSysInfo.


๐Ÿ”ฅ ErlNifSInt64

A native signed 64-bit integer type.

๐Ÿ”ฅ ErlNifUInt64

A native unsigned 64-bit integer type.

โฐ ErlNifTime

A signed 64-bit integer type for representation of time.


๐Ÿ•ฐ๏ธ ErlNifTimeUnit

An enumeration of time units supported by the NIF API:

  • ERL_NIF_SEC โ€” seconds
  • ERL_NIF_MSEC โ€” milliseconds
  • ERL_NIF_USEC โ€” microseconds
  • ERL_NIF_NSEC โ€” nanoseconds

๐ŸŒŸ ErlNifUniqueInteger

An enumeration of the properties that can be requested from enif_make_unique_integer. For default properties, use value 0.

  • ERL_NIF_UNIQUE_POSITIVE โ€” return only positive integers.
  • ERL_NIF_UNIQUE_MONOTONIC โ€” return only strictly monotonically increasing integer corresponding to creation time.

๐ŸŒˆ ErlNifHash

An enumeration of the supported hash types that can be generated using enif_hash.

  • ERL_NIF_INTERNAL_HASH โ€” non-portable hash function that only guarantees the same hash for the same term within one Erlang VM instance.

It takes 32-bit salt values and generates hashes within 0..2^32-1.

  • ERL_NIF_PHASH2 โ€” portable hash function that gives the same hash for the same Erlang term regardless of machine architecture and ERTS version.

It ignores salt values and generates hashes within 0..2^27-1.

Slower than ERL_NIF_INTERNAL_HASH. It corresponds to erlang:phash2/1.


๐Ÿงต SysIOVec

A system I/O vector, as used by writev on Unix and WSASend on Win32. It is used in ErlNifIOVec and by enif_ioq_peek.


๐ŸŽ ErlNifIOVec

typedef struct {
  int iovcnt;
  size_t size;
  SysIOVec* iov;
} ErlNifIOVec;

An I/O vector containing iovcnt SysIOVecs pointing to the data. It is used by enif_inspect_iovec and enif_ioq_enqv.


๐Ÿ›๏ธ ErlNifIOQueueOpts

Options to configure a ErlNifIOQueue.

  • ERL_NIF_IOQ_NORMAL โ€” create a normal I/O Queue.

๐Ÿš€ EXPORTS ๐Ÿš€

Alright bestie, buckle up, this is the part everyone actually came for ๐Ÿ’…

void *enif_alloc(size_t size)

Allocates memory of size bytes.

Returns NULL if the allocation fails. ๐Ÿ˜ญ

The returned pointer is suitably aligned for any built-in type that fit in the allocated memory.


int enif_alloc_binary(size_t size, ErlNifBinary* bin)

Allocates a new binary of size size bytes. Initializes the structure pointed to by bin to refer to the allocated binary. The binary must either be released by enif_release_binary or ownership transferred to an Erlang term with enif_make_binary. An allocated (and owned) ErlNifBinary can be kept between NIF calls. Long distance relationship with your data, we love to see it. ๐Ÿ’ž

If you do not need to reallocate or keep the data alive across NIF calls, consider using enif_make_new_binary instead as it will allocate small binaries on the process heap when possible.

Returns true on success, or false if allocation fails.


ErlNifEnv *enif_alloc_env()

(okay this one's just getting started โ€” keep reading bestie, we're literally not done ๐Ÿซถ)

so like, your shiny new env from enif_alloc_env is fully process independent โœจ it's its own little realm where you can stash terms that aren't bound to any process at all. Later you can copy those terms into a process env with enif_make_copy, or yeet them to a process as a message with enif_send ๐Ÿ“จ Returns a pointer to the new environment, bestie. That's your handle. Guard it with your life ๐Ÿ’–

enif_alloc_resource ๐ŸŽ€

void *enif_alloc_resource(ErlNifResourceType*
         type, unsigned size)

Allocates a memory-managed resource object โ€” type is the resource type, size is the size in bytes. Memory-managed means the VM's got your back on lifetime, no cap ๐Ÿซถ

enif_binary_to_term ๐Ÿฆ‹

size_t enif_binary_to_term(ErlNifEnv *env,
         const unsigned char* data, size_t size, ERL_NIF_TERM *term,
         ErlNifBinaryToTerm opts)

Makes a term that's the result of decoding the binary data sitting at data, which must be encoded according to the Erlang external term format ๐Ÿ’… No more than size bytes get read from data โ€” it won't wander past the fence.

The opts argument corresponds to the second argument to erlang:binary_to_term/2, and it has to be either 0 or ERL_NIF_BIN2TERM_SAFE. Those are your only two options, periodt.

On success it stores the resulting term at *term and returns the number of bytes read. It returns 0 if decoding fails or if opts is invalid โ€” so 0 is the "something went sideways" number, don't @ me.

See also ErlNifBinaryToTerm, erlang:binary_to_term/2, and enif_term_to_binary.

enif_clear_env ๐Ÿงน

void enif_clear_env(ErlNifEnv* env)

Frees all the terms in an environment and clears it for reuse โ™ป๏ธ The environment must have been allocated with enif_alloc_env โ€” don't try this on some random env you found on the street, it's not gonna slay.

enif_compare โš–๏ธ

int enif_compare(ERL_NIF_TERM lhs, ERL_NIF_TERM rhs)

Returns an integer < 0 if lhs < rhs, 0 if lhs = rhs, and > 0 if lhs > rhs. It corresponds to the Erlang operators ==, /=, =<, <, >=, and > โ€” but not =:= or =/= ๐Ÿšจ so like, it's the loose comparison vibe check, not the strict one. Know the difference, iykyk.

enif_compare_monitors ๐ŸŽฏ

int enif_compare_monitors(const ErlNifMonitor
         *monitor1, const ErlNifMonitor *monitor2)

Compares two ErlNifMonitors. You can also use it to imply some artificial order on monitors, for whatever reason โ€” the docs literally say "for whatever reason", iconic ๐Ÿ’€

Returns 0 if monitor1 and monitor2 are equal, < 0 if monitor1 < monitor2, and > 0 if monitor1 > monitor2.

enif_compare_pids ๐Ÿ‘ฏ

int enif_compare_pids(const ErlNifPid *pid1, const ErlNifPid *pid2)

Compares two ErlNifPids according to term order โ€” yes, pids have an order, that's the lore ๐ŸŒˆ

Returns 0 if pid1 and pid2 are equal, < 0 if pid1 < pid2, and > 0 if pid1 > pid2.


condition variable besties, all in a row ๐Ÿงš

These five are literally just the driver versions wearing a NIF outfit:

  • void enif_cond_broadcast(ErlNifCond *cnd) โ€” Same as erl_drv_cond_broadcast.
  • ErlNifCond *enif_cond_create(char *name) โ€” Same as erl_drv_cond_create.
  • void enif_cond_destroy(ErlNifCond *cnd) โ€” Same as erl_drv_cond_destroy.
  • char*enif_cond_name(ErlNifCond* cnd) โ€” Same as erl_drv_cond_name.
  • void enif_cond_signal(ErlNifCond *cnd) โ€” Same as erl_drv_cond_signal.
  • void enif_cond_wait(ErlNifCond *cnd, ErlNifMutex *mtx) โ€” Same as erl_drv_cond_wait.

Go read the erl_drv_* pages for the actual tea, we're just pointing you there ๐Ÿซก


enif_consume_timeslice โฑ๏ธ

int enif_consume_timeslice(ErlNifEnv *env, int percent)

This is the "hey bestie, I've been hogging the CPU" hint ๐Ÿ’… It gives the runtime system a hint about how much CPU time the current NIF call has consumed since the last hint, or since the start of the NIF if no previous hint was specified.

The time is specified as a percent of the timeslice that a process is allowed to execute Erlang code until it can be suspended, so other runnable processes get a turn. The scheduling timeslice is not an exact entity, but can usually be approximated to about 1 millisecond โ€” an approximation, not a promise, ngl.

Notice that it's up to the runtime system to determine if and how to use this information. Implementations on some platforms can use other means to determine consumed CPU time. Lengthy NIFs should, regardless of this, frequently call enif_consume_timeslice to determine if it's allowed to continue execution.

percent must be an integer between 1 and 100. This function must only be called from a NIF-calling thread, and env must be the environment of the calling process.

Returns 1 if the timeslice is exhausted, otherwise 0. If 1 comes back, the NIF is to return as soon as possible so the process can yield ๐Ÿƒโ€โ™€๏ธ

This function is provided to better support co-operative scheduling, improve system responsiveness, and make it easier to prevent misbehaviors of the VM because of a NIF monopolizing a scheduler thread. You can use it to divide long work into a number of repeated NIF calls without needing to create threads โ€” literally the whole point, no cap ๐Ÿงต

See also the warning text at the beginning of this manual page. Yeah, that warning. Go reread it, I'll wait ๐Ÿ’€

enif_convert_time_unit ๐Ÿ”„

ErlNifTime enif_convert_time_unit(ErlNifTime
         val, ErlNifTimeUnit from, ErlNifTimeUnit to)

Converts the val value of time unit from to the corresponding value of time unit to. The result is rounded using the floor function โ€” always down, never dramatic ๐ŸŒธ

  • val: Value to convert time unit for.
  • from: Time unit of val.
  • to: Time unit of returned value.

Returns ERL_NIF_TIME_ERROR if called with an invalid time unit argument ๐Ÿšฉ

See also ErlNifTime and ErlNifTimeUnit.

enif_cpu_time โฐ

ERL_NIF_TERM enif_cpu_time(ErlNifEnv *)

Returns the CPU time in the same format as erlang:timestamp(). This is the time the current logical CPU has spent executing since some arbitrary point in the past โ€” arbitrary, we said what we said ๐Ÿซถ

If the OS does not support fetching this value, enif_cpu_time invokes enif_make_badarg ๐Ÿ˜ญ so like, be ready for that.

enif_demonitor_process ๐Ÿ•ต๏ธโ€โ™€๏ธ

int enif_demonitor_process(ErlNifEnv* caller_env,
       void* obj, const ErlNifMonitor* mon)

Cancels a monitor created earlier with enif_monitor_process โœ‚๏ธ obj is a pointer to the resource holding the monitor, and *mon identifies the monitor.

caller_env is the environment of the calling thread (process bound or callback environment) or NULL if calling from a custom thread not spawned by ERTS.

Returns 0 if the monitor was successfully identified and removed. Returns a non-zero value if the monitor could not be identified, which means it was either:

  • never created for this resource
  • already cancelled
  • already triggered
  • just about to be triggered by a concurrent thread

This function is thread-safe ๐Ÿ’–

enif_dynamic_resource_call ๐ŸŒ

int enif_dynamic_resource_call(ErlNifEnv* caller_env,
      ERL_NIF_MODULE rt_module, ERL_NIF_MODULE rt_name, ERL_NIF_TERM
      resource,      void* call_data)

Calls code of a resource type implemented by another NIF module โ€” cross-module collab, we love to see it ๐Ÿค The atoms rt_module and rt_name identify the resource type to be called. resource identifies a resource object of that type.

The callback dyncall of the identified resource type will be called with a pointer to the resource object's obj and the call_data argument passed through. The call_data argument is typically a pointer to a struct used to pass both arguments to the dyncall function and results back to the caller โ€” one struct doing double duty, main character energy ๐Ÿ‘‘

Returns 0 if the dyncall callback function was called. Returns a non-zero value if no call was made, which happens if rt_module and rt_name did not identify a resource type with a dyncall callback, or if resource was not a resource object of that type ๐Ÿ™ˆ

enif_equal_tids ๐Ÿชข

int enif_equal_tids(ErlNifTid tid1, ErlNifTid tid2)

Same as erl_drv_equal_tids. That's it, that's the whole entry โœจ

enif_fprintf ๐Ÿ–จ๏ธ

int enif_fprintf(FILE *stream, const char *format, ...)

Similar to fprintf, but this format string also accepts "%T", which formats Erlang terms of type ERL_NIF_TERM ๐ŸŽ€

This function is primarily intended for debugging purposes. It is not recommended to print very large terms with %T โ€” like bestie, please don't dump a whole gigaterm into your terminal, your scrollback will never recover ๐Ÿ˜ญ

The function may change errno, even if successful. Yes, even when it works. Sneaky.

enif_free ๐Ÿ—‘๏ธ

void enif_free(void* ptr)

Frees memory allocated by enif_alloc. That's the whole deal โ€” pair them up, every enif_alloc needs its enif_free, or you're gonna have a leak era ๐Ÿ’€

enif_free_env ๐Ÿƒ

void enif_free_env(ErlNifEnv* env)

Frees an environment allocated with enif_alloc_env. All terms created in the environment are freed as well โ€” package deal, everything goes ๐Ÿซถ

enif_free_iovec ๐Ÿ“ฆ

void enif_free_iovec(ErlNifIOVec* iov)

Frees an io vector returned from enif_inspect_iovec. This is needed only if a NULL environment is passed to enif_inspect_iovec. Not every iovec needs this โ€” read the room.

ErlNifIOVec *iovec = NULL;
size_t max_elements = 128;
ERL_NIF_TERM tail;
if (!enif_inspect_iovec(NULL, max_elements, term, &tail, &iovec))
  return 0;

// Do things with the iovec

/* Free the iovector, possibly in another thread or nif function call */
enif_free_iovec(iovec);

See that comment? You can free it possibly in another thread or nif function call. Flexible โœจ

enif_get_atom ๐Ÿ”ค

int enif_get_atom(ErlNifEnv* env, ERL_NIF_TERM
         term, char* buf, unsigned size, ErlNifCharEncoding encode)

Writes a NULL-terminated string in the buffer pointed to by buf of size size, consisting of the string representation of the atom term with encoding encode ๐Ÿ’…

Returns the number of bytes written (including the terminating NULL character) or 0 if term is not an atom with maximum length of size-1 ๐Ÿšฉ Watch that size-1, it's the gotcha.

enif_get_atom_length ๐Ÿ“

int enif_get_atom_length(ErlNifEnv* env,
         ERL_NIF_TERM term, unsigned* len, ErlNifCharEncoding encode)

Sets *len to the length (number of bytes excluding the terminating NULL character) of the atom term with encoding encode.

Returns true on success, or false if term is not an atom. That's it, little guy does exactly one thing ๐ŸŒธ

enif_get_double ๐Ÿ“

int enif_get_double(ErlNifEnv* env,
         ERL_NIF_TERM term, double* dp)

Sets *dp to the floating-point value of term.

Returns true on success, or false if term is not a float.

enif_get_int ๐Ÿ”ข

int enif_get_int(ErlNifEnv* env, ERL_NIF_TERM
         term, int* ip)

Sets *ip to the integer value of term.

Returns true on success, or false if term is not an integer or is outside the bounds of type int โ€” so like, too big is still a no ๐Ÿšซ

enif_get_int64 ๐Ÿ”ขโœจ

int enif_get_int64(ErlNifEnv* env, ERL_NIF_TERM
         term, ErlNifSInt64* ip)

Sets *ip to the integer value of term.

Returns true on success, or false if term is not an integer or is outside the bounds of a signed 64-bit integer. Bigger box than enif_get_int, same rules ๐Ÿ’œ

enif_get_local_pid ๐Ÿ‘ค

int enif_get_local_pid(ErlNifEnv* env,
         ERL_NIF_TERM term, ErlNifPid* pid)

If term is the pid of a node local process, this function initializes the pid variable *pid from it and returns true. Otherwise returns false. No check is done to see if the process is alive โ€” it could be literal ghost data and we'd still say true ๐Ÿ‘ป

Note: enif_get_local_pid will return false if argument term is the atom undefined. Very important, very specific, remember it ๐Ÿ’€

enif_get_local_port ๐Ÿ”Œ

int enif_get_local_port(ErlNifEnv* env,
         ERL_NIF_TERM term, ErlNifPort* port_id)

If term identifies a node local port, this function initializes the port variable *port_id from it and returns true. Otherwise returns false. Again, no check is done to see if the port is alive โ€” we're not running a wellness check here ๐Ÿซถ

enif_get_list_cell ๐Ÿงฉ

int enif_get_list_cell(ErlNifEnv* env,
         ERL_NIF_TERM list, ERL_NIF_TERM* head, ERL_NIF_TERM* tail)

okay and the deets on this one are literally right below, keep reading bestie โ€”

okay continuing right where we left off in the function buffet โ€” enif_get_list_head was mid-sentence, so let's finish her off ๐Ÿ’…


Sets *head and *tail from list list.

Returns true on success, or false if it is not a list or the list is empty.

int enif_get_list_length(ErlNifEnv* env, ERL_NIF_TERM term, unsigned* len)

Sets *len to the length of list term.

Returns true on success, or false if term is not a proper list. (Like, a proper list bestie โ€” improper lists get an โœจerrorโœจ vibe check.)

int enif_get_long(ErlNifEnv* env, ERL_NIF_TERM term, long int* ip)

Sets *ip to the long integer value of term.

Returns true on success, or false if term is not an integer or is outside the bounds of type long int. No overflow sneaking in, nope ๐Ÿšซ

int enif_get_map_size(ErlNifEnv* env, ERL_NIF_TERM term, size_t *size)

Sets *size to the number of key-value pairs in the map term.

Returns true on success, or false if term is not a map.

int enif_get_map_value(ErlNifEnv* env, ERL_NIF_TERM map, ERL_NIF_TERM key, ERL_NIF_TERM* value)

Sets *value to the value associated with key in the map map.

Returns true on success, or false if map is not a map or if map does not contain key. (so if the key ghosted the map, you get false ๐Ÿ’€)

int enif_get_resource(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifResourceType* type, void** objp)

Sets *objp to point to the resource object referred to by term.

Returns true on success, or false if term is not a handle to a resource object of type type.

Heads up, because this part is lowkey important: enif_get_resource does not add a reference to the resource object. BUT โ€” and this is the slay part โ€” the pointer you receive in *objp is guaranteed to be valid at least as long as the resource handle term is valid. ๐Ÿซถ

int enif_get_string(ErlNifEnv* env, ERL_NIF_TERM list, char* buf, unsigned size, ErlNifCharEncoding encode)

Writes a NULL-terminated string into the buffer pointed to by buf with size size, made of the characters in the string list. The characters are written using encoding encode.

Returns one of the following, bestie โ€” pay attention, there's three outcomes:

  • The number of bytes written (including the terminating NULL character) ๐ŸŽ€
  • -size if the string was truncated because of buffer space (she got cut off mid-sentence ๐Ÿ˜ญ)
  • 0 if list is not a string that can be encoded with encode or if size was < 1

The written string is always NULL-terminated, unless buffer size is < 1.

int enif_get_tuple(ErlNifEnv* env, ERL_NIF_TERM term, int* arity, const ERL_NIF_TERM** array)

If term is a tuple, this function sets *array to point to an array containing the elements of the tuple, and sets *arity to the number of elements. Note that the array is read-only, and (*array)[N-1] is the *N*th element of the tuple. Also *array is undefined if the arity of the tuple is zero (so like, don't touch it then ๐Ÿซฃ).

Returns true on success, or false if term is not a tuple.

int enif_get_uint(ErlNifEnv* env, ERL_NIF_TERM term, unsigned int* ip)

Sets *ip to the unsigned integer value of term.

Returns true on success, or false if term is not an unsigned integer or is outside the bounds of type unsigned int.

int enif_get_uint64(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifUInt64* ip)

Sets *ip to the unsigned integer value of term.

Returns true on success, or false if term is not an unsigned integer or is outside the bounds of an unsigned 64-bit integer. Sixty-four bits of pure โœจrangeโœจ, no more.

int enif_get_ulong(ErlNifEnv* env, ERL_NIF_TERM term, unsigned long* ip)

Sets *ip to the unsigned long integer value of term.

Returns true on success, or false if term is not an unsigned integer or is outside the bounds of type unsigned long.

int enif_getenv(const char* key, char* value, size_t *value_size)

Same as erl_drv_getenv. That's it, that's the whole entry โ€” she's short and she knows it ๐Ÿ’…

int enif_has_pending_exception(ErlNifEnv* env, ERL_NIF_TERM* reason)

Returns true if a pending exception is associated with the environment env. If reason is a NULL pointer, just ignore it (she said what she said). Otherwise, if a pending exception associated with env exists, set *reason to the value of the exception term.

For example: if enif_make_badarg is called to set a pending badarg exception, a later call to enif_has_pending_exception(env, &reason) sets *reason to the atom badarg, then returns true. Look at her go ๐Ÿƒโ€โ™€๏ธ

See also enif_make_badarg and enif_raise_exception.

ErlNifUInt64 enif_hash(ErlNifHash type, ERL_NIF_TERM term, ErlNifUInt64 salt)

Hashes term according to the specified ErlNifHash type.

Ranges of taken salt (if any) and returned value depend on the hash type. iykyk ๐Ÿง‚

int enif_inspect_binary(ErlNifEnv* env, ERL_NIF_TERM bin_term, ErlNifBinary* bin)

Initializes the structure pointed to by bin with information about binary term bin_term.

Returns true on success, or false if bin_term is not a binary.

int enif_inspect_iolist_as_binary(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifBinary* bin)

Initializes the structure pointed to by bin with a continuous buffer with the same byte content as iolist. As with inspect_binary, the data pointed to by bin is transient and does not need to be released. Free real estate, no cleanup required ๐ŸŒธ

Returns true on success, or false if iolist is not an iolist.

int enif_inspect_iovec(ErlNifEnv* env, size_t max_elements, ERL_NIF_TERM iovec_term, ERL_NIF_TERM* tail, ErlNifIOVec** iovec)

Fills iovec with the list of binaries provided in iovec_term. The number of elements handled in the call is limited to max_elements, and tail is set to the remainder of the list. Note that the output may be longer than max_elements on some platforms (she's a little extra like that ๐Ÿ™ˆ).

To create a list of binaries from an arbitrary iolist, use erlang:iolist_to_iovec/1.

When calling this function, iovec should contain a pointer to NULL or an ErlNifIOVec structure that should be used if possible. e.g.

/* Don't use a pre-allocated structure */
ErlNifIOVec *iovec = NULL;
enif_inspect_iovec(env, max_elements, term, &tail, &iovec);

/* Use a stack-allocated vector as an optimization for vectors with few elements */
ErlNifIOVec vec, *iovec = &vec;
enif_inspect_iovec(env, max_elements, term, &tail, &iovec);

The contents of the iovec is valid until the called nif function returns. If the iovec should be valid after the nif call returns, it is possible to call this function with a NULL environment. If no environment is given, the iovec owns the data in the vector and it has to be explicitly freed using enif_free_iovec. โš ๏ธ so like, don't forget her, okay?

Returns true on success, or false if iovec_term not an iovec.

ErlNifIOQueue *enif_ioq_create(ErlNifIOQueueOpts opts)

Create a new I/O Queue that can be used to store data. opts has to be set to ERL_NIF_IOQ_NORMAL. No other options, bestie, that's the whole menu.

void enif_ioq_destroy(ErlNifIOQueue *q)

Destroy the I/O queue and free all of its contents. ๐Ÿงน bye girl, you had a good run

int enif_ioq_deq(ErlNifIOQueue *q, size_t count, size_t *size)

Dequeue count bytes from the I/O queue. If size is not NULL, the new size of the queue is placed there.

Returns true on success, or false if the I/O does not contain count bytes. On failure the queue is left un-altered โ€” nothing removed, no damage done, we love a clean rollback ๐Ÿ’–

int enif_ioq_enq_binary(ErlNifIOQueue *q, ErlNifBinary *bin, size_t skip)

Enqueue the bin into q skipping the first skip bytes.

Returns true on success, or false if skip is greater than the size of bin. Any ownership of the binary data is transferred to the queue and bin is to be considered read-only for the rest of the NIF call and then as released. ๐Ÿ‘‘ ownership handover, no take-backs

int enif_ioq_enqv(ErlNifIOQueue *q, ErlNifIOVec *iovec, size_t skip)

Enqueue the iovec into q skipping the first skip bytes.

Returns true on success, or false if skip is greater than the size of iovec.

SysIOVec *enif_ioq_peek(ErlNifIOQueue *q, int *iovlen)

Get the I/O queue as a pointer to an array of SysIOVecs. It also returns the number of elements in iovlen.

Nothing is removed from the queue by this function โ€” that must be done with enif_ioq_deq. Peek, don't snatch ๐Ÿ‘€

The returned array is suitable to use with the Unix system call writev. iconic behavior fr

int enif_ioq_peek_head(ErlNifEnv *env, ErlNifIOQueue *q, size_t *size, ERL_NIF_TERM *bin_term)

Get the head of the IO Queue as a binary term.

If size is not NULL, the size of the head is placed there.

Nothing is removed from the queue by this function โ€” again, that must be done with enif_ioq_deq.

Returns true on success, or false if the queue is empty. (empty queue = no head to peek at, tragic ๐Ÿ˜”)

size_t enif_ioq_size(ErlNifIOQueue *q)

Get the size of q. Short queen, short entry.

int enif_is_atom(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is an atom.

int enif_is_binary(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is a binary.

int enif_is_current_process_alive(ErlNifEnv* env)

Returns true if the currently executing process is currently alive, otherwise false.

Heads up though: this function can only be used from a NIF-calling thread, and with an environment corresponding to currently executing processes. So like, read the room before you call her ๐Ÿซถ

int enif_is_empty_list(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is an empty list.

int enif_is_exception(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is an exception.

int enif_is_fun(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is a fun. fun detected ๐ŸŽ‰

int enif_is_identical(ERL_NIF_TERM lhs, ERL_NIF_TERM rhs)

Returns true if the two terms are identical. Corresponds to the Erlang operators =:= and =/=. Note there's no env here, she doesn't need one ๐Ÿ’…

int enif_is_list(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is a list.

int enif_is_map(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is a map, otherwise false.

int enif_is_number(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is a number.

int enif_is_pid(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is a pid.

int enif_is_pid_undefined(const ErlNifPid* pid)

Returns true if pid has been set as undefined by enif_set_pid_undefined.

int enif_is_port(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is a port.

int enif_is_port_alive(ErlNifEnv* env, ErlNifPort *port_id)

Returns true if port_id is alive.

This function is thread-safe. ๐Ÿ™Œ go off, concurrency queen

int enif_is_process_alive(ErlNifEnv* env, ErlNifPid *pid)

Returns true if pid is alive.

int enif_is_ref(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is a reference. That's it, that's the whole vibe check ๐Ÿ’…

This function is thread-safe.

int enif_is_tuple(ErlNifEnv* env, ERL_NIF_TERM term)

Returns true if term is a tuple. Simple as ๐ŸŽ€

int enif_keep_resource(void* obj)

Adds a reference to resource object obj obtained from enif_alloc_resource.

Bestie, listen: every single call to enif_keep_resource for an object must be balanced by a call to enif_release_resource before the object is destructed. Balance your references like you balance your iced coffee intake, okay? โ˜•โœจ

ERL_NIF_TERM enif_make_atom(ErlNifEnv* env, const char* name)

Creates an atom term from the NULL-terminated C-string name with ISO Latin-1 encoding.

Heads up though: if the length of name exceeds the maximum length allowed for an atom (255 characters), enif_make_atom invokes enif_make_badarg. So likeโ€ฆ don't be extra, keep it under 255 ๐Ÿ’€

ERL_NIF_TERM enif_make_atom_len(ErlNifEnv* env, const char* name, size_t len)

Creates an atom term from the string name with length len. NULL characters are treated as any other characters โ€” no special treatment, no main character energy for them.

If len exceeds the maximum length allowed for an atom (255 characters), enif_make_atom invokes enif_make_badarg.

ERL_NIF_TERM enif_make_badarg(ErlNifEnv* env)

Makes a badarg exception to be returned from a NIF, and associates it with environment env.

Okay this one's kind of the icon ๐Ÿ‘‘ โ€” once a NIF or any function it calls invokes enif_make_badarg, the runtime ensures that a badarg exception is raised when the NIF returns, even if the NIF attempts to return a non-exception term instead. Like, the runtime said "no ma'am, we're raising" and it will raise. Periodt.

The return value from enif_make_badarg can be used only as the return value from the NIF that invoked it (directly or indirectly) or be passed to enif_is_exception, but not to any other NIF API function. That's its only lane, don't try to make it do other stuff ๐Ÿšซ

See also enif_has_pending_exception and enif_raise_exception.

Note: Before ERTS 7.0 (Erlang/OTP 18), the return value from enif_make_badarg had to be returned from the NIF. This requirement is now lifted, as the return value from the NIF is ignored if enif_make_badarg has been invoked. Glow-up moment fr fr โœจ

ERL_NIF_TERM enif_make_binary(ErlNifEnv* env, ErlNifBinary* bin)

Makes a binary term from bin.

Any ownership of the binary data is transferred to the created term, and bin is to be considered read-only for the rest of the NIF call and then as released. So likeโ€ฆ after this, bin is not yours anymore, hands off ๐Ÿ™ˆ

ERL_NIF_TERM enif_make_copy(ErlNifEnv* dst_env, ERL_NIF_TERM src_term)

Makes a copy of term src_term. The copy is created in environment dst_env. The source term can be located in any environment โ€” she travels ๐Ÿฆ‹

ERL_NIF_TERM enif_make_double(ErlNifEnv* env, double d)

Creates a floating-point term from a double.

If the argument double is not finite or is NaN, enif_make_double invokes enif_make_badarg. No infinity, no NaN, we don't do that here ๐Ÿ’…

int enif_make_existing_atom(ErlNifEnv* env, const char* name, ERL_NIF_TERM* atom, ErlNifCharEncoding encode)

Tries to create the term of an already existing atom from the NULL-terminated C-string name with encoding encode.

If the atom already exists, this function stores the term in *atom and returns true, otherwise false. Also returns false if the length of name exceeds the maximum length allowed for an atom (255 characters). So the vibes are: existing atom or nothing ๐ŸŽ€

int enif_make_existing_atom_len(ErlNifEnv* env, const char* name, size_t len, ERL_NIF_TERM* atom, ErlNifCharEncoding encoding)

Tries to create the term of an already existing atom from the string name with length len and encoding encode.

NULL characters are treated as any other characters.

If the atom already exists, this function stores the term in *atom and returns true, otherwise false. Also returns false if len exceeds the maximum length allowed for an atom (255 characters).

ERL_NIF_TERM enif_make_int(ErlNifEnv* env, int i)

Creates an integer term. Tiny little guy, does exactly what it says ๐Ÿ“

ERL_NIF_TERM enif_make_int64(ErlNifEnv* env, ErlNifSInt64 i)

Creates an integer term from a signed 64-bit integer.

ERL_NIF_TERM enif_make_list(ErlNifEnv* env, unsigned cnt, ...)

Creates an ordinary list term of length cnt. Expects cnt number of arguments (after cnt) of type ERL_NIF_TERM as the elements of the list.

Returns an empty list if cnt is 0.

ERL_NIF_TERM enif_make_list1 โ€ฆ enif_make_list9

ERL_NIF_TERM enif_make_list1(ErlNifEnv* env, ERL_NIF_TERM e1)
ERL_NIF_TERM enif_make_list2(ErlNifEnv* env,
        ERL_NIF_TERM e1, ERL_NIF_TERM e2)
ERL_NIF_TERM enif_make_list3(ErlNifEnv* env,
        ERL_NIF_TERM e1, ERL_NIF_TERM e2, ERL_NIF_TERM e3)
ERL_NIF_TERM enif_make_list4(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e4)
ERL_NIF_TERM enif_make_list5(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e5)
ERL_NIF_TERM enif_make_list6(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e6)
ERL_NIF_TERM enif_make_list7(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e7)
ERL_NIF_TERM enif_make_list8(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e8)
ERL_NIF_TERM enif_make_list9(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9)

Creates an ordinary list term with length indicated by the function name.

Prefer these functions (macros) over the variadic enif_make_list to get a compile-time error if the number of arguments does not match. Like, the compiler catching your mistakes for you? We love a protective king ๐Ÿซถ

ERL_NIF_TERM enif_make_list_cell(ErlNifEnv* env, ERL_NIF_TERM head, ERL_NIF_TERM tail)

Creates a list cell [head | tail]. Cons cell realness ๐Ÿ’…

ERL_NIF_TERM enif_make_list_from_array(ErlNifEnv* env, const ERL_NIF_TERM arr[], unsigned cnt)

Creates an ordinary list containing the elements of array arr of length cnt.

Returns an empty list if cnt is 0.

ERL_NIF_TERM enif_make_long(ErlNifEnv* env, long int i)

Creates an integer term from a long int.

int enif_make_map_put(ErlNifEnv* env, ERL_NIF_TERM map_in, ERL_NIF_TERM key, ERL_NIF_TERM value, ERL_NIF_TERM* map_out)

Makes a copy of map map_in and inserts key with value. If key already exists in map_in, the old associated value is replaced by value. Out with the old, in with the new โœจ

If successful, this function sets *map_out to the new map and returns true. Returns false if map_in is not a map.

The map_in term must belong to environment env.

int enif_make_map_remove(ErlNifEnv* env, ERL_NIF_TERM map_in, ERL_NIF_TERM key, ERL_NIF_TERM* map_out)

If map map_in contains key, this function makes a copy of map_in in *map_out, and removes key and the associated value. If map map_in does not contain key, *map_out is set to map_in. She's flexible like that ๐Ÿ’โ€โ™€๏ธ

Returns true on success, or false if map_in is not a map.

The map_in term must belong to environment env.

int enif_make_map_update(ErlNifEnv* env, ERL_NIF_TERM map_in, ERL_NIF_TERM key, ERL_NIF_TERM new_value, ERL_NIF_TERM* map_out)

Makes a copy of map map_in and replaces the old associated value for key with new_value.

If successful, this function sets *map_out to the new map and returns true. Returns false if map_in is not a map or if it does not contain key. So no key, no slay ๐Ÿ˜ญ

The map_in term must belong to environment env.

int enif_make_map_from_arrays(ErlNifEnv* env, ERL_NIF_TERM keys[], ERL_NIF_TERM values[], size_t cnt, ERL_NIF_TERM *map_out)

Makes a map term from the given keys and values.

If successful, this function sets *map_out to the new map and returns true. Returns false if there are any duplicate keys. Duplicates? Not on her watch ๐Ÿšซ

All keys and values must belong to env.

ERL_NIF_TERM enif_make_monitor_term(ErlNifEnv* env, const ErlNifMonitor* mon)

Creates a term identifying the given monitor received from enif_monitor_process.

This function is primarily intended for debugging purpose. So like, it's giving debug tool, not main character energy ๐Ÿ”

unsigned char *enif_make_new_binary(ErlNifEnv* env, size_t size, ERL_NIF_TERM* termp)

Allocates a binary of size size bytes and creates an owning term. The binary data is mutable until the calling NIF returns. This is a quick way to create a new binary without having to use ErlNifBinary.

Lowkey the trade-off is real though: the binary cannot be kept between NIF calls and it cannot be reallocated. So use it for a quick little moment, not a long-term commitment ๐Ÿ’€

Returns a pointer to the raw binary data and sets *termp to the binary term.

ERL_NIF_TERM enif_make_new_map(ErlNifEnv* env)

Makes an empty map term. Blank canvas, full potential ๐ŸŽจ

ERL_NIF_TERM enif_make_pid(ErlNifEnv* env, const ErlNifPid* pid)

Makes a pid term or the atom undefined from *pid.

ERL_NIF_TERM enif_make_ref(ErlNifEnv* env)

Creates a reference like erlang:make_ref/0. Fresh and unique every time, iconic behaviour โœจ

ERL_NIF_TERM enif_make_resource(ErlNifEnv* env, void* obj)

Creates an opaque handle to a memory-managed resource object obtained by enif_alloc_resource. No ownership transfer is done, as the resource object still needs to be released by enif_release_resource.

But bestie, notice this part: the call to enif_release_resource can occur immediately after obtaining the term from enif_make_resource, in which case the resource object is deallocated when the term is garbage collected. For more details, see the example of creating and returning a resource object in the User's Guide. ๐Ÿซถ

Note: Since ERTS 9.0 (OTP-20.0), resource terms have a defined behavior when compared and serialized through term_to_binary or passed between nodes. - Two resource terms will compare equal if and only if they would yield the same resource object pointer when passed to enif_get_resource. - A resource term can be serialized with term_to_binary and later be fully recreated if the resource object is still alive when binary_to_term is called. A stale resource term will be returned from binary_to_term if the resource object has been deallocated. enif_get_resource will return false for stale resource terms. The same principles of serialization apply when passing resource terms in messages to remote nodes and back again. A resource term will act stale on all nodes except the node where its resource object is still alive in memory. Before ERTS 9.0 (OTP-20.0), all resource terms did compare equal to each other and to empty binaries (<<>>). If serialized, they would be recreated as plain empty binaries. Different era entirely ๐Ÿ˜ญ

ERL_NIF_TERM enif_make_resource_binary(ErlNifEnv* env, void* obj, const void* data, size_t size)

โ€ฆokay this one's about to get juicy, hold that thought ๐Ÿงš

๐ŸŽ€ enif_make_resource_binary โ€” the "binary hanging off a resource" one

Creates a binary term that is memory-managed by a resource object obj obtained by enif_alloc_resource. The returned binary term consists of size bytes pointed to by data. This raw binary data must be kept readable and unchanged until the destructor of the resource is called. The binary data can be stored external to the resource object, in which case the destructor is responsible for releasing the data. ๐Ÿซถ

Several binary terms can be managed by the same resource object. The destructor is not called until the last binary is garbage collected. This can be useful to return different parts of a larger binary buffer. Literally so efficient of it. โœจ

As with enif_make_resource, no ownership transfer is done. The resource still needs to be released with enif_release_resource. Don't forget, bestie โ€” no cap, that leak is on you. ๐Ÿ˜ญ

๐Ÿฆ‹ enif_make_reverse_list

int enif_make_reverse_list(ErlNifEnv* env, ERL_NIF_TERM list_in,
        ERL_NIF_TERM *list_out)

Sets *list_out to the reverse list of the list list_in and returns true, or returns false if list_in is not a list. ๐Ÿ’…

This function is only to be used on short lists, as a copy is created of the list, which is not released until after the NIF returns. So likeโ€ฆ short lists only, gorgeous. Long lists = pain. ๐Ÿ’€

The list_in term must belong to environment env.

๐ŸŒธ enif_make_string

ERL_NIF_TERM enif_make_string(ErlNifEnv* env,
        const char* string, ErlNifCharEncoding encoding)

Creates a list containing the characters of the NULL-terminated string string with encoding encoding. Iconic, classic, does exactly what it says. ๐Ÿ’–

๐ŸŽ€ enif_make_string_len

ERL_NIF_TERM enif_make_string_len(ErlNifEnv*
        env, const char* string, size_t len, ErlNifCharEncoding
        encoding)

Creates a list containing the characters of the string string with length len and encoding encoding. NULL characters are treated as any other characters. Yes, even the sneaky NULs get to be main characters here. โญ

๐ŸŒˆ enif_make_sub_binary

ERL_NIF_TERM enif_make_sub_binary(ErlNifEnv*
        env, ERL_NIF_TERM bin_term, size_t pos, size_t size)

Makes a subbinary of binary bin_term, starting at zero-based position pos with a length of size bytes. bin_term must be a binary or bitstring. pos+size must be less or equal to the number of whole bytes in bin_term. Do the math, bestie โ€” if it overflows you're in trouble. ๐Ÿงš

๐Ÿ‘‘ enif_make_tuple

ERL_NIF_TERM enif_make_tuple(ErlNifEnv* env,
        unsigned cnt, ...)

Creates a tuple term of arity cnt. Expects cnt number of arguments (after cnt) of type ERL_NIF_TERM as the elements of the tuple. Variadic serve, honestly. ๐Ÿซถ

๐Ÿ’œ enif_make_tuple1 โ€ฆ enif_make_tuple9

ERL_NIF_TERM enif_make_tuple1(ErlNifEnv* env,
        ERL_NIF_TERM e1)
ERL_NIF_TERM enif_make_tuple2(ErlNifEnv* env,
        ERL_NIF_TERM e1, ERL_NIF_TERM e2)
ERL_NIF_TERM enif_make_tuple3(ErlNifEnv* env,
        ERL_NIF_TERM e1, ERL_NIF_TERM e2, ERL_NIF_TERM e3)
ERL_NIF_TERM enif_make_tuple4(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e4)
ERL_NIF_TERM enif_make_tuple5(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e5)
ERL_NIF_TERM enif_make_tuple6(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e6)
ERL_NIF_TERM enif_make_tuple7(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e7)
ERL_NIF_TERM enif_make_tuple8(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e8)
ERL_NIF_TERM enif_make_tuple9(ErlNifEnv* env,
        ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9)

Creates a tuple term with length indicated by the function name. Prefer these functions (macros) over the variadic enif_make_tuple to get a compile-time error if the number of arguments does not match. Compile-time errors >>> runtime mystery crashes, fr fr. ๐Ÿ”ฅ

๐Ÿ“ enif_make_tuple_from_array

ERL_NIF_TERM enif_make_tuple_from_array(ErlNifEnv* env, const
ERL_NIF_TERM
        arr[], unsigned cnt)

Creates a tuple containing the elements of array arr of length cnt. Array girlies, this one's for you. ๐Ÿ’…

โœจ enif_make_uint

ERL_NIF_TERM enif_make_uint(ErlNifEnv* env, unsigned int i)

Creates an integer term from an unsigned int. Smol. ๐ŸŽ€

โœจ enif_make_uint64

ERL_NIF_TERM enif_make_uint64(ErlNifEnv* env, ErlNifUInt64 i)

Creates an integer term from an unsigned 64-bit integer. Big number energy. ๐Ÿ‘‘

โœจ enif_make_ulong

ERL_NIF_TERM enif_make_ulong(ErlNifEnv* env, unsigned long i)

Creates an integer term from an unsigned long int. Also slaying, just a different flavour. ๐ŸŒธ

๐Ÿ’ซ enif_make_unique_integer

ERL_NIF_TERM enif_make_unique_integer(ErlNifEnv
        *env, ErlNifUniqueInteger properties)

Returns a unique integer with the same properties as specified by erlang:unique_integer/1. Literally the NIF version of that vibe. ๐ŸŒˆ

env is the environment to create the integer in.

ERL_NIF_UNIQUE_POSITIVE and ERL_NIF_UNIQUE_MONOTONIC can be passed as the second argument to change the properties of the integer returned. They can be combined by OR:ing the two values together. Mix and match, bestie. ๐Ÿ’…

See also ErlNifUniqueInteger.


๐Ÿ—บ๏ธ enif_map_iterator_create โ€” the map-walking queen

int enif_map_iterator_create(ErlNifEnv *env,
        ERL_NIF_TERM map, ErlNifMapIterator *iter,
ErlNifMapIteratorEntry
        entry)

Creates an iterator for the map map by initializing the structure pointed to by iter. Argument entry determines the start position of the iterator: ERL_NIF_MAP_ITERATOR_FIRST or ERL_NIF_MAP_ITERATOR_LAST.

Returns true on success, or false if map is not a map. Vibe check: failed. ๐Ÿ’€

A map iterator is only useful during the lifetime of environment env that the map belongs to. The iterator must be destroyed by calling enif_map_iterator_destroy:

ERL_NIF_TERM key, value;
ErlNifMapIterator iter;
enif_map_iterator_create(env, my_map, &iter, ERL_NIF_MAP_ITERATOR_FIRST);

while (enif_map_iterator_get_pair(env, &iter, &key, &value)) {
    do_something(key,value);
    enif_map_iterator_next(env, &iter);
}
enif_map_iterator_destroy(env, &iter);
Note: The key-value pairs of a map have no defined iteration order. The only guarantee is that the iteration order of a single map instance is preserved during the lifetime of the environment that the map belongs to. So like โ€” don't assume order, but at least it's consistently chaotic for one map. ๐Ÿ™ˆ

๐Ÿงน enif_map_iterator_destroy

void enif_map_iterator_destroy(ErlNifEnv *env,
        ErlNifMapIterator *iter)

Destroys a map iterator created by enif_map_iterator_create. Clean up after yourself, icon. ๐Ÿซถ

๐ŸŽฏ enif_map_iterator_get_pair

int enif_map_iterator_get_pair(ErlNifEnv *env,
        ErlNifMapIterator *iter, ERL_NIF_TERM *key, ERL_NIF_TERM
        *value)

Gets key and value terms at the current map iterator position.

On success, sets *key and *value and returns true. Returns false if the iterator is positioned at head (before first entry) or tail (beyond last entry). No entries there, babe. ๐Ÿ’…

๐Ÿ”ฎ enif_map_iterator_is_head

int enif_map_iterator_is_head(ErlNifEnv *env,
        ErlNifMapIterator *iter)

Returns true if map iterator iter is positioned before the first entry. Head empty, no thoughts. ๐Ÿง โœจ

๐Ÿ”ฎ enif_map_iterator_is_tail

int enif_map_iterator_is_tail(ErlNifEnv *env,
        ErlNifMapIterator *iter)

Returns true if map iterator iter is positioned after the last entry.

โžก๏ธ enif_map_iterator_next

int enif_map_iterator_next(ErlNifEnv *env,
        ErlNifMapIterator *iter)

Increments map iterator to point to the next key-value entry.

Returns true if the iterator is now positioned at a valid key-value entry, or false if the iterator is positioned at the tail (beyond the last entry). Moving forward, always. ๐ŸŒฑ

โฌ…๏ธ enif_map_iterator_prev

int enif_map_iterator_prev(ErlNifEnv *env,
        ErlNifMapIterator *iter)

Decrements map iterator to point to the previous key-value entry.

Returns true if the iterator is now positioned at a valid key-value entry, or false if the iterator is positioned at the head (before the first entry). The nostalgic direction. ๐ŸŽ€


๐Ÿ‘€ enif_monitor_process โ€” keeping tabs, but the respectful kind

int enif_monitor_process(ErlNifEnv* caller_env,
      void* obj, const ErlNifPid* target_pid, ErlNifMonitor* mon)

Starts monitoring a process from a resource. When a process is monitored, a process exit results in a call to the provided down callback associated with the resource type. So like, you will find out. ๐Ÿ’…

Argument obj is pointer to the resource to hold the monitor and *target_pid identifies the local process to be monitored.

If mon is not NULL, a successful call stores the identity of the monitor in the ErlNifMonitor struct pointed to by mon. This identifier is used to refer to the monitor for later removal with enif_demonitor_process or compare with enif_compare_monitors. A monitor is automatically removed when it triggers or when the resource is deallocated. Self-cleaning, obsessed. โœจ

Argument caller_env is the environment of the calling thread (process bound or callback environment) or NULL if calling from a custom thread not spawned by ERTS.

Returns 0 on success, < 0 if no down callback is provided, and > 0 if the process is no longer alive or if target_pid is undefined.

This function is thread-safe. ๐Ÿซถ

โฑ๏ธ enif_monotonic_time

ErlNifTime enif_monotonic_time(ErlNifTimeUnit time_unit)

Returns the current Erlang monotonic time. Notice that it is not uncommon with negative values. Yeah, negative times are just a thing here, don't @ me. ๐Ÿ˜ญ

time_unit is the time unit of the returned value.

Returns ERL_NIF_TIME_ERROR if called with an invalid time unit argument, or if called from a thread that is not a scheduler thread.

See also ErlNifTime and ErlNifTimeUnit.

๐Ÿ”’ Mutex crew โ€” the erl_drv_mutex_* renames

ErlNifMutex *enif_mutex_create(char *name)

Same as erl_drv_mutex_create.

void enif_mutex_destroy(ErlNifMutex *mtx)

Same as erl_drv_mutex_destroy.

void enif_mutex_lock(ErlNifMutex *mtx)

Same as erl_drv_mutex_lock.

char*enif_mutex_name(ErlNifMutex* mtx)

Same as erl_drv_mutex_name.

int enif_mutex_trylock(ErlNifMutex *mtx)

Same as erl_drv_mutex_trylock.

void enif_mutex_unlock(ErlNifMutex *mtx)

Same as erl_drv_mutex_unlock.

Six little aliases living their best life. Lock it, unlock it, know its name. ๐Ÿ’…

โฐ enif_now_time

ERL_NIF_TERM enif_now_time(ErlNifEnv *env)

Returns an erlang:now() time stamp.

This function is deprecated. Ma'am, she's retired. Please stop calling her. ๐Ÿ’€

๐Ÿ—„๏ธ enif_open_resource_type โ€” the resource type origin story

ErlNifResourceType *enif_open_resource_type(ErlNifEnv* env, const char*
        module_str, const char* name, ErlNifResourceDtor* dtor,
        ErlNifResourceFlags flags, ErlNifResourceFlags* tried)

Creates or takes over a resource type identified by the string name and gives it the destructor function pointed to by dtor. Argument flags can have the following values:

  • ERL_NIF_RT_CREATE โ€” Creates a new resource type that does not already exist.
  • ERL_NIF_RT_TAKEOVER โ€” Opens an existing resource type and takes over ownership of all its instances. The supplied destructor dtor is called both for existing instances and new instances not yet created by the calling NIF library. Adopting the whole family, iconic. ๐Ÿ‘‘

The two flag values can be combined with bitwise OR. The resource type name is local to the calling module. Argument module_str is not (yet) used and must be NULL. dtor can be NULL if no destructor is needed. Yes, "not yet" โ€” she said what she said. ๐ŸŽ€

...and that's the whole deal, bestie:

  • On success, you get back a pointer to the resource type, and *tried is set to either ERL_NIF_RT_CREATE or ERL_NIF_RT_TAKEOVER so you know exactly what went down โœจ
  • On failure, it returns NULL and sets *tried to flags.
  • It's totally allowed to set tried to NULL if you don't care. ๐Ÿ™ˆ
Heads up: enif_open_resource_type is only allowed to be called inside the two callbacks load and upgrade. That's the vibe check. No calling it from anywhere else, periodt.

See also enif_open_resource_type_x. ๐Ÿ’…


enif_open_resource_type_x

ErlNifResourceType *enif_open_resource_type_x(ErlNifEnv* env, const
char* name,      const ErlNifResourceTypeInit* init,
        ErlNifResourceFlags flags, ErlNifResourceFlags* tried)

Same as enif_open_resource_type, except it also takes extra callback functions for resource types that get used together with enif_select and enif_monitor_process. ๐Ÿฆ‹

The init argument is a pointer to an ErlNifResourceTypeInit structure stuffed with the function pointers for the destructor, down and stop callbacks for the resource type.

Note: only the members dtor, down and stop in ErlNifResourceTypeInit are actually read by enif_open_resource_type_x. If you wanna implement the new dyncall callback, use enif_init_resource_type instead. iykyk ๐Ÿ’ซ

enif_init_resource_type

ErlNifResourceType *enif_init_resource_type(ErlNifEnv* env, const char*
name,      const ErlNifResourceTypeInit* init,
        ErlNifResourceFlags flags, ErlNifResourceFlags* tried)

Same as enif_open_resource_type_x, except it also accepts an extra callback function for resource types that are used together with enif_dynamic_resource_call. ๐ŸŽ€

The init argument is a pointer to an ErlNifResourceTypeInit structure holding the callback function pointers dtor, down, stop and the new dyncall. The struct also has field members that must be set to the number of initialized callbacks, counted from the top of the struct. So like, to initialize all callbacks including dyncall, set those members to 4. All callbacks are optional and may be set to NULL. No pressure. ๐Ÿ’…


enif_port_command

int enif_port_command(ErlNifEnv* env, const
       ErlNifPort* to_port, ErlNifEnv *msg_env, ERL_NIF_TERM msg)

Works exactly like erlang:port_command/2, except it is always completely asynchronous. slay.

  • env: โ€” the environment of the calling process. Must not be NULL. ๐Ÿšซ
  • *to_port: โ€” the port ID of the receiving port. This port ID has to refer to a port on the local node.
  • msg_env: โ€” the environment of the message term. Can be a process-independent environment allocated with enif_alloc_env, or NULL.
  • msg: โ€” the message term to send. The same limitations apply here as on the payload to erlang:port_command/2.

Using a msg_env of NULL is a lil optimization that groups together calls to enif_alloc_env, enif_make_copy, enif_port_command, and enif_free_env into one single call. This optimization is only actually useful when a majority of the terms are gonna be copied from env to msg_env. ๐Ÿ’ซ

Returns true if the command is successfully sent. Returns false if the command fails, for example when:

  • *to_port does not refer to a local port.
  • The currently executing process (that is, the sender) is not alive. ๐Ÿ˜ญ
  • msg is invalid.

See also enif_get_local_port.


enif_priv_data

void *enif_priv_data(ErlNifEnv* env)

Returns the pointer to the private data that was set by load or upgrade. Your lil secret stash. ๐Ÿ“


enif_raise_exception

ERL_NIF_TERM enif_raise_exception(ErlNifEnv*
        env, ERL_NIF_TERM reason)

Creates an error exception with the term reason to be returned from a NIF, and associates it with environment env. Once a NIF โ€” or any function it calls โ€” invokes enif_raise_exception, the runtime ensures that the exception it creates is raised when the NIF returns, even if the NIF tries to return a non-exception term instead. Like, it literally will not let you fumble it. ๐Ÿ’€

The return value from enif_raise_exception can only be used as the return value from the NIF that invoked it (directly or indirectly), or be passed to enif_is_exception. It can not be passed to any other NIF API function. Periodt.

See also enif_has_pending_exception and enif_make_badarg.


enif_realloc

void *enif_realloc(void* ptr, size_t size)

Reallocates memory that was allocated by enif_alloc to size bytes.

Returns NULL if the reallocation fails. ๐Ÿ˜ฌ

The returned pointer is suitably aligned for any built-in type that fits in the allocated memory. โœจ


enif_realloc_binary

int enif_realloc_binary(ErlNifBinary* bin, size_t size)

Changes the size of a binary bin. The source binary can be read-only, and in that case it's left untouched and a mutable copy is allocated and assigned to *bin. ๐Ÿฆ‹

Returns true on success, or false if memory allocation failed.


enif_release_binary

void enif_release_binary(ErlNifBinary* bin)

Releases a binary obtained from enif_alloc_binary. Bye bestie ๐Ÿ‘‹


enif_release_resource

void enif_release_resource(void* obj)

Removes a reference to resource object obj obtained from enif_alloc_resource. The resource object is destructed when the last reference is removed. Every single call to enif_release_resource must correspond to a previous call to enif_alloc_resource or enif_keep_resource. References made by enif_make_resource can only be removed by the garbage collector โ€” that's just how it is, no cap.

There are no guarantees exactly when the destructor of an unreferenced resource is called. It could be called directly by enif_release_resource, but it could also be scheduled to be called at a later time, possibly by another thread. So don't be out here assuming timing, bestie. ๐Ÿ™ˆ

enif_rwlock_create

ErlNifRWLock *enif_rwlock_create(char *name)

Same as erl_drv_rwlock_create. ๐Ÿ’…


enif_rwlock_destroy

void enif_rwlock_destroy(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_destroy.


enif_rwlock_name

char*enif_rwlock_name(ErlNifRWLock* rwlck)

Same as erl_drv_rwlock_name.


enif_rwlock_rlock

void enif_rwlock_rlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_rlock.


enif_rwlock_runlock

void enif_rwlock_runlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_runlock.


enif_rwlock_rwlock

void enif_rwlock_rwlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_rwlock.


enif_rwlock_rwunlock

void enif_rwlock_rwunlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_rwunlock.


enif_rwlock_tryrlock

int enif_rwlock_tryrlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_tryrlock.


enif_rwlock_tryrwlock

int enif_rwlock_tryrwlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_tryrwlock.


enif_schedule_nif

ERL_NIF_TERM enif_schedule_nif(
        ErlNifEnv* caller_env, const char* fun_name, int flags,
     ERL_NIF_TERM (*fp)(ErlNifEnv* env, int argc, const ERL_NIF_TERM
argv[]),      int argc, const ERL_NIF_TERM argv[])

Schedules NIF fp to execute. This function lets an application break up long-running work into multiple regular NIF calls, or schedule a dirty NIF to execute on a dirty scheduler thread. Multi-tasking icon behavior. ๐Ÿ‘‘

  • caller_env: โ€” must be a process-bound environment of the calling NIF.
  • fun_name: โ€” provides a name for the NIF that is scheduled for execution. If it can't be converted to an atom, enif_schedule_nif returns a badarg exception. ๐Ÿ˜ฌ
  • flags: โ€” must be set to 0 for a regular NIF. If the emulator was built with dirty scheduler support enabled, flags can be set to either ERL_NIF_DIRTY_JOB_CPU_BOUND if the job is expected to be CPU-bound, or ERL_NIF_DIRTY_JOB_IO_BOUND for jobs that are gonna be I/O-bound. If dirty scheduler threads are not available in the emulator, an attempt to schedule such a job results in a notsup exception. ๐Ÿ’€
  • argc and argv: โ€” can either be the originals passed into the calling NIF, or values created by the calling NIF.

The calling NIF must use the return value of enif_schedule_nif as its own return value. No substitutions.

Be aware that enif_schedule_nif, as the name literally implies, only schedules the NIF for future execution. The calling NIF does not block waiting for the scheduled NIF to execute and return. Which means the calling NIF can't expect to receive the scheduled NIF return value and use it for further operations. Soz. ๐Ÿ˜ญ

enif_select

int enif_select(ErlNifEnv* env, ErlNifEvent event, enum
ErlNifSelectFlags mode,      void* obj, const ErlNifPid* pid,
ERL_NIF_TERM ref)

This function is how you receive asynchronous notifications when OS-specific event objects become ready for either read or write operations. It's giving event loop energy. ๐ŸŒˆ

Argument event identifies the event object. On Unix systems, the functions select/poll are used. The event object must be a socket, pipe or other file descriptor object that select/poll can use.

Argument mode describes the type of events to wait for. It can be ERL_NIF_SELECT_READ, ERL_NIF_SELECT_WRITE, or a bitwise OR combination to wait for both. It can also be ERL_NIF_SELECT_STOP or ERL_NIF_SELECT_CANCEL, which are described further below. When a read or write event is triggered, a notification message like this is sent to the process identified by pid:

{select, Obj, Ref, ready_input | ready_output}

ready_input or ready_output indicates whether the event object is ready for reading or writing.

Note: for complete control over the message format, use the newer functions enif_select_read or enif_select_write, introduced in erts-11.0 (OTP-22.0). โœจ

Argument pid may be NULL to indicate the calling process. It must not be set as undefined. ๐Ÿšซ

Argument obj is a resource object obtained from enif_alloc_resource. The whole purpose of the resource objects is to act as a container of the event object, managing its state and lifetime. A handle to the resource is received in the notification message as Obj. ๐ŸŽ€

Argument ref must be either a reference obtained from erlang:make_ref/0 or the atom undefined. It's passed as Ref in the notifications. If a selective receive statement is used to wait for the notification, then a reference created just before the receive will exploit a runtime optimization that bypasses all earlier received messages in the queue. iykyk ๐Ÿ’ซ

The notifications are one-shot only. To receive further notifications of the same type (read or write), repeated calls to enif_select must be made after receiving each notification. No infinite freebies, bestie. ๐Ÿ’…

So the way ERL_NIF_SELECT_CANCEL works is: it cancels events you already selected, bestie ๐Ÿ’…. You gotta use it in a bitwise OR combo with ERL_NIF_SELECT_READ and/or ERL_NIF_SELECT_WRITE so it knows which type of event to cancel. The pid and ref arguments just get ignored when ERL_NIF_SELECT_CANCEL is specified, no cap. The return value will tell you if the event was actually cancelled or if a notification may already have been sent ๐Ÿ‘€.

Then there's ERL_NIF_SELECT_STOP โ€” use it as your mode when you need to safely close an event object that's been passed to enif_select. The stop callback of the resource obj gets called when it's safe to close the event object. This safe way of closing event objects must be used even if all notifications have been received (or cancelled) and no further calls to enif_select have been made. ERL_NIF_SELECT_STOP will first cancel any selected events before it calls or schedules the stop callback. And yeah, pid and ref are ignored when ERL_NIF_SELECT_STOP is specified too ๐Ÿ™ˆ.

The first call to enif_select for a specific OS event establishes a relation between the event object and the containing resource. All subsequent calls for an event must pass its containing resource as argument obj. That relation is dissolved when enif_select has been called with mode as ERL_NIF_SELECT_STOP and the corresponding stop callback has returned. A resource can contain several event objects, but one event object can only be contained within one resource. And a resource will not be destructed until all its contained relations have been dissolved. periodt โœจ

Note: use enif_monitor_process together with enif_select to detect failing Erlang processes and prevent them from causing permanent leakage of resources and their contained OS event objects. like, this is the leak-prevention combo, iykyk ๐Ÿซถ

It returns a non-negative value on success, where the following bits can be set:

  • ERL_NIF_SELECT_STOP_CALLED โ€” the stop callback was called directly by enif_select.
  • ERL_NIF_SELECT_STOP_SCHEDULED โ€” the stop callback was scheduled to run on some other thread or later by this thread.
  • ERL_NIF_SELECT_READ_CANCELLED โ€” a read event was cancelled by ERL_NIF_SELECT_CANCEL or ERL_NIF_SELECT_STOP and is guaranteed not to generate a ready_input notification message.
  • ERL_NIF_SELECT_WRITE_CANCELLED โ€” a write event was cancelled by ERL_NIF_SELECT_CANCEL or ERL_NIF_SELECT_STOP and is guaranteed not to generate a ready_output notification message.

It returns a negative value if the call failed, where the following bits can be set:

  • ERL_NIF_SELECT_INVALID_EVENT โ€” argument event is not a valid OS event object.
  • ERL_NIF_SELECT_FAILED โ€” the system call failed to add the event object to the poll set.
Note: use bitwise AND to test for specific bits in the return value. New significant bits may be added in future releases to give more detailed information for both failed and successful calls. Do NOT use equality tests like ==, because that may cause your application to stop working ๐Ÿ˜ญ fr fr this is the classic trap.

Example:

retval = enif_select(env, fd, ERL_NIF_SELECT_STOP, resource, ref);
if (retval < 0) {
    /* handle error */
}
/* Success! */
if (retval & ERL_NIF_SELECT_STOP_CALLED) {
    /* ... */
}
Note: the mode flag ERL_NIF_SELECT_CANCEL and the return flags ERL_NIF_SELECT_READ_CANCELLED and ERL_NIF_SELECT_WRITE_CANCELLED were introduced in erts-11.0 (OTP-22.0). ๐ŸŽ€

๐Ÿ’Œ enif_select_read / enif_select_write

int enif_select_read(ErlNifEnv* env, ErlNifEvent event, void* obj,
      const ErlNifPid* pid, ERL_NIF_TERM msg, ErlNifEnv* msg_env)
int enif_select_write(ErlNifEnv* env, ErlNifEvent event, void* obj,
      const ErlNifPid* pid, ERL_NIF_TERM msg, ErlNifEnv* msg_env)

These are variants of enif_select where you can supply your own message term msg that gets sent to the process instead of the predefined tuple {select,_,_,_}. obsessed, honestly ๐Ÿ’–

Argument msg_env must either be NULL or the environment of msg allocated with enif_alloc_env. If argument msg_env is NULL the term msg will be copied, otherwise both msg and msg_env will be invalidated by a successful call to enif_select_read or enif_select_write. The environment is then to either be freed with enif_free_env or cleared for reuse with enif_clear_env. An unsuccessful call will leave msg and msg_env still valid. ๐Ÿ’€ so like, mind the lifetime, bestie.

Apart from the message format, enif_select_read and enif_select_write behave exactly the same as enif_select with argument mode as either ERL_NIF_SELECT_READ or ERL_NIF_SELECT_WRITE. To cancel or close events, use enif_select.

๐Ÿชž enif_self

ErlNifPid *enif_self(ErlNifEnv* caller_env, ErlNifPid* pid)

Initializes the ErlNifPid variable at *pid to represent the calling process.

Returns pid if successful, or NULL if caller_env is not a process bound environment. like, vibe check failed on that env, no ๐ŸŽ€ for it.

๐Ÿ“จ enif_send

int enif_send(ErlNifEnv* caller_env,
      ErlNifPid* to_pid, ErlNifEnv* msg_env, ERL_NIF_TERM msg)

Sends a message to a process. Iconic behavior, truly ๐Ÿ‘‘

  • caller_env โ€” the environment of the calling thread (process bound or callback environment) or NULL if calling from a custom thread not spawned by ERTS.
  • *to_pid โ€” the pid of the receiving process. The pid is to refer to a process on the local node.
  • msg_env โ€” the environment of the message term. Must be a process independent environment allocated with enif_alloc_env or NULL.
  • msg โ€” the message term to send.

Returns true if the message is successfully sent. Returns false if the send operation fails, that is:

  • *to_pid does not refer to an alive local process.
  • The currently executing process (that is, the sender) is not alive.

The message environment msg_env with all its terms (including msg) is invalidated by a successful call to enif_send. The environment is to either be freed with enif_free_env or cleared for reuse with enif_clear_env. An unsuccessful call will leave msg and msg_env still valid.

If msg_env is set to NULL, the msg term is copied and the original term and its environment is still valid after the call. ๐Ÿซถ

This function is thread-safe. slay.

Note: passing msg_env as NULL is only supported as from ERTS 8.0 (Erlang/OTP 19). older than that and it's giving โœจunsupportedโœจ.

๐Ÿšซ enif_set_pid_undefined

void enif_set_pid_undefined(ErlNifPid* pid)

Sets an ErlNifPid variable as undefined. See enif_is_pid_undefined.

๐Ÿ“ enif_sizeof_resource

unsigned enif_sizeof_resource(void* obj)

Gets the byte size of resource object obj obtained by enif_alloc_resource. it's giving "how big is my resource" energy, and honestly same ๐Ÿ’…

๐Ÿ–จ๏ธ enif_snprintf

int enif_snprintf(char *str, size_t size, const
        char *format, ...)

Similar to snprintf but this format string also accepts "%T", which formats Erlang terms of type ERL_NIF_TERM. cute little bonus โœจ

This function is primarily intended for debugging purpose. It is not recommended to print very large terms with %T. The function may change errno, even if successful. ๐Ÿ™ˆ yeah she's a little chaotic like that.

๐Ÿ–ฅ๏ธ enif_system_info

void enif_system_info(ErlNifSysInfo
        *sys_info_ptr, size_t size)

Same as driver_system_info.

๐ŸงŠ enif_term_to_binary

int enif_term_to_binary(ErlNifEnv *env,
        ERL_NIF_TERM term, ErlNifBinary *bin)

Allocates a new binary with enif_alloc_binary and stores the result of encoding term according to the Erlang external term format.

Returns true on success, or false if the allocation fails. ๐Ÿ˜ญ

See also erlang:term_to_binary/1 and enif_binary_to_term.

๐Ÿ” enif_term_type

ErlNifTermType enif_term_type(ErlNifEnv *env, ERL_NIF_TERM term)

Determines the type of the given term. The term must be an ordinary Erlang term and not one of the special terms returned by enif_raise_exception, enif_schedule_nif, or similar. (those are the "special guest" terms, they don't play by these rules ๐Ÿ’€)

The following types are defined at the moment:

  • ERL_NIF_TERM_TYPE_ATOM
  • ERL_NIF_TERM_TYPE_BITSTRING โ€” a bitstring or binary
  • ERL_NIF_TERM_TYPE_FLOAT
  • ERL_NIF_TERM_TYPE_FUN
  • ERL_NIF_TERM_TYPE_INTEGER
  • ERL_NIF_TERM_TYPE_LIST โ€” a list, empty or not
  • ERL_NIF_TERM_TYPE_MAP
  • ERL_NIF_TERM_TYPE_PID
  • ERL_NIF_TERM_TYPE_PORT
  • ERL_NIF_TERM_TYPE_REFERENCE
  • ERL_NIF_TERM_TYPE_TUPLE

Note that new types may be added in the future, so the caller must be prepared to handle unknown types. like, keep a default: branch or the future will humble you ๐ŸŒ€

๐Ÿงต enif_thread_create

int enif_thread_create(char *name,ErlNifTid
        *tid,void * (*func)(void *),void *args,ErlNifThreadOpts
        *opts)

Same as erl_drv_thread_create.

๐Ÿšช enif_thread_exit

void enif_thread_exit(void *resp)

Same as erl_drv_thread_exit.

๐Ÿค enif_thread_join

int enif_thread_join(ErlNifTid, void **respp)

Same as erl_drv_thread_join.

๐Ÿท๏ธ enif_thread_name

char*enif_thread_name(ErlNifTid tid)

Same as erl_drv_thread_name.

๐ŸŽ€ enif_thread_opts_create

ErlNifThreadOpts *enif_thread_opts_create(char *name)

Same as erl_drv_thread_opts_create.

๐Ÿ—‘๏ธ enif_thread_opts_destroy

void enif_thread_opts_destroy(ErlNifThreadOpts *opts)

Same as erl_drv_thread_opts_destroy.

๐Ÿชž enif_thread_self

ErlNifTid enif_thread_self(void)

Same as erl_drv_thread_self.

๐Ÿงš enif_thread_type

int enif_thread_type(void)

Determine the type of currently executing thread. A positive value indicates a scheduler thread while a negative value or zero indicates another type of thread. Currently the following specific types exist (which may be extended in the future):

  • ERL_NIF_THR_UNDEFINED โ€” undefined thread that is not a scheduler thread.
  • ERL_NIF_THR_NORMAL_SCHEDULER โ€” a normal scheduler thread.
  • ERL_NIF_THR_DIRTY_CPU_SCHEDULER โ€” a dirty CPU scheduler thread.
  • ERL_NIF_THR_DIRTY_IO_SCHEDULER โ€” a dirty I/O scheduler thread.

the way this lets you know exactly what kind of thread you're onโ€ฆ main character self-awareness ๐Ÿ˜Œ

โฐ enif_time_offset

ErlNifTime enif_time_offset(ErlNifTimeUnit time_unit)

Returns the current time offset between Erlang monotonic time and Erlang system time converted into the time_unit passed as argument.

time_unit is the time unit of the returned value.

Returns ERL_NIF_TIME_ERROR if called with an invalid time unit argument or if called from a thread that is not a scheduler thread. ๐Ÿซ 

See also ErlNifTime and ErlNifTimeUnit.

๐Ÿงต void *enif_tsd_get(ErlNifTSDKey key)

Okay bestie, this one is literally the same as erl_drv_tsd_get. Same behavior, zero plot twist. ๐Ÿ’…

๐ŸŽ€ int enif_tsd_key_create(char *name, ErlNifTSDKey *key)

Same as erl_drv_tsd_key_create. Thatโ€™s the whole vibe. ๐Ÿซถ

๐Ÿฆ‹ void enif_tsd_key_destroy(ErlNifTSDKey key)

Same as erl_drv_tsd_key_destroy. Cleanup era. โœจ

๐ŸŒธ void enif_tsd_set(ErlNifTSDKey key, void *data)

Same as erl_drv_tsd_set. No extra lore. ๐Ÿ’–

โœจ int enif_vfprintf(FILE *stream, const char *format, va_list ap)

Equivalent to enif_fprintf except that itโ€™s called with a va_list instead of a variable number of arguments. Same energy, different arg delivery. ๐ŸŽ€

โญ int enif_vsnprintf(char *str, size_t size, const char *format, va_list ap)

Equivalent to enif_snprintf except that itโ€™s called with a va_list instead of a variable number of arguments. Iconic little wrapper behavior. ๐Ÿ’ซ

๐Ÿ”Ž enif_whereis_pid

int enif_whereis_pid(ErlNifEnv *caller_env,
          ERL_NIF_TERM name, ErlNifPid *pid)

This one looks up a process by its registered name. Very main character. ๐Ÿ‘‘

  • caller_env: The environment of the calling thread (process bound or callback environment) or NULL if calling from a custom thread not spawned by ERTS.
  • name: The name of a registered process, as an atom.
  • *pid: The ErlNifPid in which the resolved process id is stored.

On success, sets *pid to the local process registered with name and returns true. If name is not a registered process, or is not an atom, false is returned and *pid is unchanged.

Works as erlang:whereis/1, but restricted to processes. See enif_whereis_port to resolve registered ports. ๐Ÿ’…

๐Ÿ” enif_whereis_port

int enif_whereis_port(ErlNifEnv *caller_env,
          ERL_NIF_TERM name, ErlNifPort *port)

This one looks up a port by its registered name. Port girlies, this is for you. ๐ŸŽ€

  • caller_env: The environment of the calling thread (process bound or callback environment) or NULL if calling from a custom thread not spawned by ERTS.
  • name: The name of a registered port, as an atom.
  • *port: The ErlNifPort in which the resolved port id is stored.

On success, sets *port to the port registered with name and returns true. If name is not a registered port, or is not an atom, false is returned and *port is unchanged.

Works as erlang:whereis/1, but restricted to ports. See enif_whereis_pid to resolve registered processes. โœจ


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

Go peek at these, iykyk:

  • erlang:load_nif/2
  • NIFs (tutorial)
  • Debugging NIFs and Port Drivers

Thatโ€™s a wrap on erl_nif(3erl)! ๐ŸŽฌ๐Ÿ’– Cute little footer sign-off: Ericsson AB, source/version/date vibes erts 13.2.2.5 and erl_nif(3erl). Mwah. ๐Ÿ’‹โœจ

๐Ÿ“ฆ where this page comes from