The NIF concept was introduced in R13B03 as an EXPERIMENTAL feature. The interfaces may be changed in any way in coming releases. The API is still sparse and contains only the most basic functions to read and write Erlang terms.
R13B04: The function prototypes of the NIFs
have changed to expect
enif_get_data renamed as enif_priv_data.
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. Each NIF must also have an implementation in Erlang that will be invoked if the function is called before the NIF library has been successfully loaded. A typical such stub implementation is to throw an exception. But it can also be used as a fallback implementation if the NIF library is not implemented for some architecture.
A minimal example of a NIF library can look like this:
/* 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!");
}
static ErlNifFunc nif_funcs[] =
{
{"hello", 0, hello}
};
ERL_NIF_INIT(niftest,nif_funcs,NULL,NULL,NULL,NULL)
and the erlang module would have to look something like this:
-module(niftest).
-export([init/0, hello/0]).
init() ->
erlang:load_nif("./niftest", 0).
hello() ->
"NIF library not loaded".
and compile and test something like this (on Linux):
$> gcc -fPIC -shared -o niftest.so niftest.c -I $ERL_ROOT/usr/include/
$> erl
1> c(niftest).
{ok,niftest}
2> niftest:hello().
"NIF library not loaded"
3> niftest:init().
ok
4> niftest:hello().
"Hello world!"
A better solution for a real module is to take advantage of
the new directive
A loaded NIF library is tied to the Erlang module code version
that loaded it. If the module is upgraded with a new version, the
new code will have to load its own NIF library (or maybe choose not
to). The new code version can however choose to load the exact
same NIF library as the old code if it wants to. Sharing the same
dynamic library will mean that static data defined by the library
will be shared as well. To avoid unintentionally shared static
data, each Erlang module code can keep its own private data. This
private data can be set when the NIF library is loaded and
then retrieved by calling
There is no way to explicitly unload a NIF library. A library will be
automatically unloaded when the module code that it belongs to is purged
by the code server. A NIF library will also be unloaded if it is replaced
by another version of the library by a second call to
All functions that a NIF library needs to do with Erlang are performed through the NIF API functions. There are functions for the following functionality:
Any Erlang terms can be passed to a NIF as function arguments and
be returned as function return value. The terms are of C-type
Resource objects are a way to return pointers to native data
structures from a NIF in a safe way. A resource object is just a block
of memory allocated with
All resource objects are created as instances of some resource type.
This makes resources from different modules or applications to be distinguishable.
A resource type is created by calling
A NIF is thread-safe without any explicit synchronization as
long as it acts as a pure function and only operates on the supplied
arguments. As soon as you access a shared state either through static
variables or
The library initialization callbacks
This is the magic macro to initialize a NIF library. It should be evaluated in global file scope.
The library will fail to load if
Works the same as
The library will fail to load if
Works the same as
The library will fail to load if
Variables of type
typedef struct {
const char* name;
unsigned arity;
ERL_NIF_TERM (*fptr)(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
} ErlNifFunc;
Describes a NIF by its name, arity and implementation.
typedef struct {
unsigned size;
unsigned char* data;
} ErlNifBinary;
Each instance of
typedef void ErlNifResourceDtor(ErlNifEnv* env, void* obj);
The function prototype of a resource destructor function. A destructor function is not allowed to call any term-making functions.
typedef enum {
ERL_NIF_LATIN1
}ErlNifCharEncoding;
The character encoding used in strings. The only supported
encoding is currently
Used by
Allocate memory of
Allocate a new binary of size of
Return false if allocation failed.
Allocate a memory managed resource object of type
Return an integer less than, equal to, or greater than
zero if
Same as
Same as
Same as
Same as
Same as
Same as
Free memory allocated by
Write a null-terminated string, in the buffer pointed to by
Set
Set
Set
Set
Set
Write a null-terminated string, in the buffer pointed to by
If
Return false if
Set
Set
Initialize the structure pointed to by
Initialize the structure pointed to by
Return true if
Return true if
Return true if
Return true if
Return true if the two terms are identical. Corresponds to the
Erlang operators
Return true if
Return true if
Return true if
Create an atom term from the C-string
Make a badarg exception to be returned from a NIF.
Make a binary term from
Create an floating-point term from a
Try to create the term of an already existing atom from
the C-string
Create an integer term.
Create an ordinary list term of length
Create an ordinary list term with length indicated by the
function name. Prefere these functions (macros) over the variadic
Create a list cell
Create an ordinary list containing the elements of array
Create an integer term from a
Create a reference like
Create an opaque handle to a memory managed resource object
obtained by
Note that the only defined behaviour when using of a resource term in
an Erlang program is to store it and send it between processes on the
same node. Other operations such as matching or
Create a list containing the characters of the
null-terminated string
Make a subbinary of binary
Create a tuple term of arity
Create a tuple term with length indicated by the
function name. Prefere these functions (macros) over the variadic
Create a tuple containing the elements of array
Create an integer term from an
Create an integer term from an
Same as
Same as
Same as
Same as
Same as
Create or takeover a resource type identified by the string
The two flag values can be combined with bitwise-or. To avoid unintentionally
name clashes a good practice is to include the module name as part of the
type
On success, return a pointer to the resource type and
Note that
Return the pointer to the private data that was set by
Was previously named
Release a binary obtained
from
Release a resource objects obtained from
Same as
Same as
Same as
Same as
Same as
Same as
Same as
Same as
Get the byte size of a resource object
Same as
Same as
Same as
Same as
Same as
Same as
Same as
Same as
Same as
Same as
Same as