User's Guide
Reference Manual
Release Notes
PDF
Top

Erlang Run-Time System Application (ERTS)
Reference Manual
Version 5.7.4

Expand All
Contract All

Table of Contents

- erl_prim_loader
  - Top of manual page
  - start/3
  - get_file/1
  - get_path/0
  - list_dir/1
  - read_file_info/1
  - set_path/1
- erlang
  - Top of manual page
  - abs/1
  - adler32/1
  - adler32/2
  - adler32_combine/3
  - erlang:append_element/2
  - apply/2
  - apply/3
  - atom_to_binary/2
  - atom_to_list/1
  - binary_to_atom/2
  - binary_to_existing_atom/2
  - binary_to_list/1
  - binary_to_list/3
  - bitstring_to_list/1
  - binary_to_term/1
  - bit_size/1
  - erlang:bump_reductions/1
  - byte_size/1
  - erlang:cancel_timer/1
  - check_process_code/2
  - concat_binary/1
  - crc32/1
  - crc32/2
  - crc32_combine/3
  - date/0
  - decode_packet/3
  - delete_module/1
  - erlang:demonitor/1
  - erlang:demonitor/2
  - disconnect_node/1
  - erlang:display/1
  - element/2
  - erase/0
  - erase/1
  - erlang:error/1
  - erlang:error/2
  - exit/1
  - exit/2
  - float/1
  - float_to_list/1
  - erlang:fun_info/1
  - erlang:fun_info/2
  - erlang:fun_to_list/1
  - erlang:function_exported/3
  - garbage_collect/0
  - garbage_collect/1
  - get/0
  - get/1
  - erlang:get_cookie/0
  - get_keys/1
  - erlang:get_stacktrace/0
  - group_leader/0
  - group_leader/2
  - halt/0
  - halt/1
  - erlang:hash/2
  - hd/1
  - erlang:hibernate/3
  - integer_to_list/1
  - erlang:integer_to_list/2
  - iolist_to_binary/1
  - iolist_size/1
  - is_alive/0
  - is_atom/1
  - is_binary/1
  - is_bitstring/1
  - is_boolean/1
  - erlang:is_builtin/3
  - is_float/1
  - is_function/1
  - is_function/2
  - is_integer/1
  - is_list/1
  - is_number/1
  - is_pid/1
  - is_port/1
  - is_process_alive/1
  - is_record/2
  - is_record/3
  - is_reference/1
  - is_tuple/1
  - length/1
  - link/1
  - list_to_atom/1
  - list_to_binary/1
  - list_to_bitstring/1
  - list_to_existing_atom/1
  - list_to_float/1
  - list_to_integer/1
  - erlang:list_to_integer/2
  - list_to_pid/1
  - list_to_tuple/1
  - load_module/2
  - erlang:load_nif/2
  - erlang:loaded/0
  - erlang:localtime/0
  - erlang:localtime_to_universaltime/2
  - erlang:localtime_to_universaltime/3
  - make_ref/0
  - erlang:make_tuple/2
  - erlang:make_tuple/3
  - erlang:max/2
  - erlang:md5/1
  - erlang:md5_final/1
  - erlang:md5_init/0
  - erlang:md5_update/2
  - erlang:memory/0
  - erlang:memory/1
  - erlang:min/2
  - module_loaded/1
  - erlang:monitor/2
  - monitor_node/2
  - erlang:monitor_node/3
  - node/0
  - node/1
  - nodes/0
  - nodes/1
  - now/0
  - open_port/2
  - erlang:phash/2
  - erlang:phash2/2
  - pid_to_list/1
  - port_close/1
  - port_command/2
  - erlang:port_command/3
  - port_connect/2
  - port_control/3
  - erlang:port_call/3
  - erlang:port_info/1
  - erlang:port_info/2
  - erlang:port_to_list/1
  - erlang:ports/0
  - pre_loaded/0
  - erlang:process_display/2
  - process_flag/2
  - process_flag/3
  - process_info/1
  - process_info/2
  - processes/0
  - purge_module/1
  - put/2
  - erlang:raise/3
  - erlang:read_timer/1
  - erlang:ref_to_list/1
  - register/2
  - registered/0
  - erlang:resume_process/1
  - round/1
  - self/0
  - erlang:send/2
  - erlang:send/3
  - erlang:send_after/3
  - erlang:send_nosuspend/2
  - erlang:send_nosuspend/3
  - erlang:set_cookie/2
  - setelement/3
  - size/1
  - spawn/1
  - spawn/2
  - spawn/3
  - spawn/4
  - spawn_link/1
  - spawn_link/2
  - spawn_link/3
  - spawn_link/4
  - spawn_monitor/1
  - spawn_monitor/3
  - spawn_opt/2
  - spawn_opt/3
  - spawn_opt/4
  - spawn_opt/5
  - split_binary/2
  - erlang:start_timer/3
  - statistics/1
  - erlang:suspend_process/2
  - erlang:suspend_process/1
  - erlang:system_flag/2
  - erlang:system_info/1
  - erlang:system_monitor/0
  - erlang:system_monitor/2
  - erlang:system_monitor/2
  - erlang:system_profile/0
  - erlang:system_profile/2
  - term_to_binary/1
  - term_to_binary/2
  - throw/1
  - time/0
  - tl/1
  - erlang:trace/3
  - erlang:trace_delivered/1
  - erlang:trace_info/2
  - erlang:trace_pattern/2
  - erlang:trace_pattern/3
  - trunc/1
  - tuple_size/1
  - tuple_to_list/1
  - erlang:universaltime/0
  - erlang:universaltime_to_localtime/2
  - unlink/1
  - unregister/1
  - whereis/1
  - erlang:yield/0
- init
  - Top of manual page
  - boot/1
  - get_args/0
  - get_argument/1
  - get_arguments/0
  - get_plain_arguments/0
  - get_status/0
  - reboot/0
  - restart/0
  - script_id/0
  - stop/0
  - stop/1
- zlib
  - Top of manual page
  - open/0
  - close/1
  - deflateInit/1
  - deflateInit/2
  - deflateInit/6
  - deflate/2
  - deflate/3
  - deflateSetDictionary/2
  - deflateReset/1
  - deflateParams/3
  - deflateEnd/1
  - inflateInit/1
  - inflateInit/2
  - inflate/2
  - inflateSetDictionary/2
  - inflateReset/1
  - inflateEnd/1
  - setBufSize/2
  - getBufSize/1
  - crc32/1
  - crc32/2
  - crc32/3
  - crc32_combine/4
  - adler32/2
  - adler32/3
  - adler32_combine/4
  - compress/1
  - uncompress/1
  - zip/1
  - unzip/1
  - gzip/1
  - gunzip/1
- epmd
- erl
- erlc
- werl
- escript
- erlsrv
- start_erl
- erl_set_memory_block
  - Top of manual page
  - erl_set_memory_block()
  - erl_memory_show()
  - erl_mem_info_get()
- run_erl
- start
- erl_driver
  - Top of manual page
  - driver_system_info()
  - driver_output()
  - driver_output2()
  - driver_output_binary()
  - driver_outputv()
  - driver_vec_to_buf()
  - driver_set_timer()
  - driver_cancel_timer()
  - driver_read_timer()
  - driver_get_now()
  - driver_select()
  - driver_alloc()
  - driver_realloc()
  - driver_free()
  - driver_alloc_binary()
  - driver_realloc_binary()
  - driver_free_binary()
  - driver_binary_get_refc()
  - driver_binary_inc_refc()
  - driver_binary_dec_refc()
  - driver_enq()
  - driver_pushq()
  - driver_deq()
  - driver_sizeq()
  - driver_enq_bin()
  - driver_pushq_bin()
  - driver_peekq()
  - driver_enqv()
  - driver_pushqv()
  - driver_pdl_create()
  - driver_pdl_lock()
  - driver_pdl_unlock()
  - driver_pdl_get_refc()
  - driver_pdl_inc_refc()
  - driver_pdl_dec_refc()
  - driver_monitor_process()
  - driver_demonitor_process()
  - driver_get_monitored_process()
  - driver_compare_monitors()
  - add_driver_entry()
  - remove_driver_entry()
  - erl_errno_id()
  - set_busy_port()
  - set_port_control_flags()
  - driver_failure_eof()
  - driver_failure_atom()
  - driver_failure_posix()
  - driver_failure()
  - driver_connected()
  - driver_caller()
  - driver_output_term()
  - driver_mk_atom()
  - driver_mk_port()
  - driver_send_term()
  - driver_async ()
  - driver_async_cancel()
  - driver_lock_driver()
  - driver_create_port()
  - erl_drv_thread_create()
  - erl_drv_thread_opts_create()
  - erl_drv_thread_opts_destroy()
  - erl_drv_thread_exit()
  - erl_drv_thread_join()
  - erl_drv_thread_self()
  - erl_drv_equal_tids()
  - erl_drv_mutex_create()
  - erl_drv_mutex_destroy()
  - erl_drv_mutex_lock()
  - erl_drv_mutex_trylock()
  - erl_drv_mutex_unlock()
  - erl_drv_cond_create()
  - erl_drv_cond_destroy()
  - erl_drv_cond_signal()
  - erl_drv_cond_broadcast()
  - erl_drv_cond_wait()
  - erl_drv_rwlock_create()
  - erl_drv_rwlock_destroy()
  - erl_drv_rwlock_rlock()
  - erl_drv_rwlock_tryrlock()
  - erl_drv_rwlock_runlock()
  - erl_drv_rwlock_rwlock()
  - erl_drv_rwlock_tryrwlock()
  - erl_drv_rwlock_rwunlock()
  - erl_drv_tsd_key_create()
  - erl_drv_tsd_key_destroy()
  - erl_drv_tsd_set()
  - erl_drv_tsd_get()
  - erl_drv_putenv()
  - erl_drv_getenv()
- driver_entry
- erts_alloc
- erl_nif
  - Top of manual page
  - enif_get_data()
  - enif_alloc()
  - enif_free()
  - enif_is_binary()
  - enif_inspect_binary()
  - enif_alloc_binary()
  - enif_release_binary()
  - enif_get_int()
  - enif_get_ulong()
  - enif_get_list_cell()
  - enif_make_binary()
  - enif_make_badarg()
  - enif_make_int()
  - enif_make_ulong()
  - enif_make_atom()
  - enif_make_tuple()
  - enif_make_list()
  - enif_make_list_cell()
  - enif_make_string()

# erl_nif

### C LIBRARY

erl_nif

### LIBRARY SUMMARY

API functions for an Erlang NIF library

### DESCRIPTION

Warning

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 on_load to automatically load the NIF library when the module is loaded.

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 enif_get_data().

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 erlang:load_nif/2 from the same module code.

### INITIALIZATION

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

  This is the magic macro to initialize a NIF library. It should be evaluated in global file scope.

  MODULE is the name of the Erlang module as an identifier without string quotations. It will be stringified by the macro.

  funcs is a static array of function descriptors for all the implemented NIFs in this library.

  load, reload, upgrade and unload are pointers to functions. One of load, reload or upgrade will be called to initialize the library. unload is called to release the library. They are all described individually below.

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

  load is called when the NIF library is loaded and there is no previously loaded library for this module.

  *priv_data can be set to point to some private data that the library needs in able to keep a state between NIF calls. enif_get_data() will return this pointer.

  load_info is the second argument to erlang:load_nif/2.

  The library will fail to load if load returns anything other than 0. load can be NULL in case no initialization is needed.

int (*reload)(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)

  reload is called when the NIF library is loaded and there is already a previously loaded library for this module code.

  Works the same as load. The only difference is that *priv_data already contains the value set by the previous call to load or reload.

  The library will fail to load if reload returns anything other than 0 or if reload is NULL.

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

  upgrade is called when the NIF library is loaded and there is no previously loaded library for this module code, BUT there is old code of this module with a loaded NIF library.

  Works the same as load. The only difference is that *old_priv_data already contains the value set by the last call to load or reload for the old module code. It is allowed to write to both *priv_data and *old_priv_data.

  The library will fail to load if upgrade returns anything other than 0 or if upgrade is NULL.

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

  unload is 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.

### DATA TYPES

ErlDrvEnv

  ErlNifEnv contains information about the context in which a NIF call is made. This pointer should not be dereferenced in any way, but only passed on to API functions. An ErlNifEnv pointer is only valid until the function, where is what supplied as argument, returns. There is thus useless and dangerous to store ErlNifEnv pointers in between NIF calls.

ErlNifFunc

      typedef struct {
          const char* name;
          unsigned arity;
          ERL_NIF_TERM (*fptr)(ErlNifEnv* env, ...);
      } ErlNifFunc;

  Describes a NIF by its name, arity and implementation. fptr is a pointer to the function that implements the NIF. The number of arguments must match the arity. A NIF of arity 2 will thus look like:

      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.

ErlNifBinary

      typedef struct {
          unsigned 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.

ERL_NIF_TERM

  Variables of type ERL_NIF_TERM can refere to any Erlang term. This is an opaque type and values of it can only by used either as arguments to API functions or as return values from NIFs. A variable of type ERL_NIF_TERM is only valid until the NIF call, where it was obtained, returns.

### EXPORTS

void* enif_get_data(ErlNifEnv* env)

Returns the pointer to the private data that was set by load, reload or upgrade.

void* enif_alloc(ErlNifEnv* env, size_t size)

Allocate memory of size bytes.

void enif_free(ErlNifEnv* env, void* ptr)

Free memory allocated by enif_alloc.

int enif_is_binary(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is a binary

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

Initialize the structure pointed to by bin with transient information about the binary term bin_term. Return false if bin_term is not a binary.

int enif_alloc_binary(ErlNifEnv* env, unsigned size, ErlNifBinary* bin)

Allocate a new binary of size of size bytes. Initialize the structure pointed to by bin to refer to the allocated binary.

void enif_release_binary(ErlNifEnv* env, ErlNifBinary* bin)

Release a binary obtained from enif_alloc_binary or enif_inspect_binary.

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

Set *ip to the integer value of term or return false if term is not an integer or is outside the bounds of type int

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

Set *ip to the unsigned long integer value of term or return false if term is not an unsigned integer or is outside the bounds of type unsigned long

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

Set *head and *tail from list or return false if list is not a non-empty list.

ERL_NIF_TERM enif_make_binary(ErlNifEnv* env, ErlNifBinary* bin)

Make a binary term from bin. Will also release the binary.

ERL_NIF_TERM enif_make_badarg(ErlNifEnv* env)

Make a badarg exception to be returned from a NIF.

ERL_NIF_TERM enif_make_int(ErlNifEnv* env, int i)

Create an integer term.

ERL_NIF_TERM enif_make_ulong(ErlNifEnv* env, unsigned long i)

Create an integer term from an unsigned long int.

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

Create an atom term from the C-string name. Atom terms may be saved and used between NIF calls.

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

Create a tuple term of arity cnt. Expects cnt number of arguments (after cnt) of type ERL_NIF_TERM as the elements of the tuple.

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

Create 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. An empty list is returned if cnt is 0.

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

Create a list cell [head | tail].

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

Creates a list containing the characters of the C-string string.

### SEE ALSO

load_nif(3)