The NIF concept is introduced in R13B03 as an EXPERIMENTAL feature. The interfaces may be changed in any way in coming releases. The API introduced in this release is very sparse and contains only the most basic functions to read and write Erlang terms.
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)
{
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 attribute
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
global private data can be set when the NIF library is loaded and
then retrieved by calling
There is currently 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 can also be unloaded by replacing it with another
version of the library by a second call to
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
typedef struct {
const char* name;
unsigned arity;
ERL_NIF_TERM (*fptr)(ErlNifEnv* env, ...);
} ErlNifFunc;
Describes a NIF by its name, arity and implementation.
ERL_NIF_TERM my_nif(ErlNifEnv* env, ERL_NIF_TERM arg1, ERL_NIF_TERM arg2)
{
/* ... */
}
The maximum allowed arity for a NIF is 3 in current implementation.
typedef struct {
unsigned size;
unsigned char* data;
} ErlNifBinary;
Variables of type
Returns the pointer to the private data that was set by
Allocate memory of
Free memory allocated by
Return true if
Initialize the structure pointed to by
Allocate a new binary of size of
Release a binary obtained from
Set
Set
Set
Make a binary term from
Make a badarg exception to be returned from a NIF.
Create an integer term.
Create an integer term from an
Create an atom term from the C-string
Create a tuple term of arity
Create an ordinary list term of length
Create a list cell
Creates a list containing the characters of the
C-string