User's Guide
Reference Manual
Release Notes
PDF
Top

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

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
  - append_element/2
  - apply/2
  - apply/3
  - atom_to_binary/2
  - atom_to_list/1
  - binary_part/2
  - binary_part/3
  - 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
  - binary_to_term/2
  - bit_size/1
  - bump_reductions/1
  - byte_size/1
  - cancel_timer/1
  - check_old_code/1
  - check_process_code/2
  - crc32/1
  - crc32/2
  - crc32_combine/3
  - date/0
  - decode_packet/3
  - delete_module/1
  - demonitor/1
  - demonitor/2
  - disconnect_node/1
  - display/1
  - element/2
  - erase/0
  - erase/1
  - error/1
  - error/2
  - exit/1
  - exit/2
  - external_size/1
  - external_size/2
  - float/1
  - float_to_list/1
  - fun_info/1
  - fun_info/2
  - fun_to_list/1
  - function_exported/3
  - garbage_collect/0
  - garbage_collect/1
  - get/0
  - get/1
  - get_cookie/0
  - get_keys/1
  - get_stacktrace/0
  - group_leader/0
  - group_leader/2
  - halt/0
  - halt/1
  - halt/2
  - hash/2
  - hd/1
  - hibernate/3
  - integer_to_list/1
  - 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
  - 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
  - list_to_integer/2
  - list_to_pid/1
  - list_to_tuple/1
  - load_module/2
  - load_nif/2
  - loaded/0
  - localtime/0
  - localtime_to_universaltime/1
  - localtime_to_universaltime/2
  - make_ref/0
  - make_tuple/2
  - make_tuple/3
  - max/2
  - md5/1
  - md5_final/1
  - md5_init/0
  - md5_update/2
  - memory/0
  - memory/1
  - min/2
  - module_loaded/1
  - monitor/2
  - monitor_node/2
  - monitor_node/3
  - nif_error/1
  - nif_error/2
  - node/0
  - node/1
  - nodes/0
  - nodes/1
  - now/0
  - open_port/2
  - phash/2
  - phash2/1
  - pid_to_list/1
  - port_close/1
  - port_command/2
  - port_command/3
  - port_connect/2
  - port_control/3
  - port_call/3
  - port_info/1
  - port_info/2
  - port_to_list/1
  - ports/0
  - pre_loaded/0
  - process_display/2
  - process_flag/2
  - process_flag/3
  - process_info/1
  - process_info/2
  - processes/0
  - purge_module/1
  - put/2
  - raise/3
  - read_timer/1
  - ref_to_list/1
  - register/2
  - registered/0
  - resume_process/1
  - round/1
  - self/0
  - send/2
  - send/3
  - send_after/3
  - send_nosuspend/2
  - send_nosuspend/3
  - 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
  - start_timer/3
  - statistics/1
  - suspend_process/2
  - suspend_process/1
  - system_flag/2
  - system_info/1
  - system_monitor/0
  - system_monitor/1
  - system_monitor/2
  - system_profile/0
  - system_profile/2
  - term_to_binary/1
  - term_to_binary/2
  - throw/1
  - time/0
  - tl/1
  - trace/3
  - trace_delivered/1
  - trace_info/2
  - trace_pattern/2
  - trace_pattern/3
  - trunc/1
  - tuple_size/1
  - tuple_to_list/1
  - universaltime/0
  - universaltime_to_localtime/1
  - unlink/1
  - unregister/1
  - whereis/1
  - yield/0
- init
  - Top of manual page
  - boot/1
  - 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_peekqv()
  - 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_alloc()
  - enif_alloc_binary()
  - enif_alloc_env()
  - enif_alloc_resource()
  - enif_clear_env()
  - enif_compare()
  - enif_cond_broadcast()
  - enif_cond_create()
  - enif_cond_destroy()
  - enif_cond_signal()
  - enif_cond_wait()
  - enif_equal_tids()
  - enif_free()
  - enif_free_env()
  - enif_get_atom()
  - enif_get_atom_length()
  - enif_get_double()
  - enif_get_int()
  - enif_get_int64()
  - enif_get_local_pid()
  - enif_get_list_cell()
  - enif_get_list_length()
  - enif_get_long()
  - enif_get_resource()
  - enif_get_string()
  - enif_get_tuple()
  - enif_get_uint()
  - enif_get_uint64()
  - enif_get_ulong()
  - enif_inspect_binary()
  - enif_inspect_iolist_as_binary()
  - enif_is_atom()
  - enif_is_binary()
  - enif_is_empty_list()
  - enif_is_exception()
  - enif_is_number()
  - enif_is_fun()
  - enif_is_identical()
  - enif_is_pid()
  - enif_is_port()
  - enif_is_ref()
  - enif_is_tuple()
  - enif_is_list()
  - enif_keep_resource()
  - enif_make_atom()
  - enif_make_atom_len()
  - enif_make_badarg()
  - enif_make_binary()
  - enif_make_copy()
  - enif_make_double()
  - enif_make_existing_atom()
  - enif_make_existing_atom_len()
  - enif_make_int()
  - enif_make_int64()
  - enif_make_list()
  - enif_make_list1()
  - enif_make_list2()
  - enif_make_list3()
  - enif_make_list4()
  - enif_make_list5()
  - enif_make_list6()
  - enif_make_list7()
  - enif_make_list8()
  - enif_make_list9()
  - enif_make_list_cell()
  - enif_make_list_from_array()
  - enif_make_reverse_list()
  - enif_make_long()
  - enif_make_new_binary()
  - enif_make_pid()
  - enif_make_ref()
  - enif_make_resource()
  - enif_make_resource_binary()
  - enif_make_string()
  - enif_make_string_len()
  - enif_make_sub_binary()
  - enif_make_tuple()
  - enif_make_tuple1()
  - enif_make_tuple2()
  - enif_make_tuple3()
  - enif_make_tuple4()
  - enif_make_tuple5()
  - enif_make_tuple6()
  - enif_make_tuple7()
  - enif_make_tuple8()
  - enif_make_tuple9()
  - enif_make_tuple_from_array()
  - enif_make_uint()
  - enif_make_uint64()
  - enif_make_ulong()
  - enif_mutex_create()
  - enif_mutex_destroy()
  - enif_mutex_lock()
  - enif_mutex_trylock()
  - enif_mutex_unlock()
  - enif_open_resource_type()
  - enif_priv_data()
  - enif_realloc_binary()
  - enif_release_binary()
  - enif_release_resource()
  - enif_rwlock_create()
  - enif_rwlock_destroy()
  - enif_rwlock_rlock()
  - enif_rwlock_runlock()
  - enif_rwlock_rwlock()
  - enif_rwlock_rwunlock()
  - enif_rwlock_tryrlock()
  - enif_rwlock_tryrwlock()
  - enif_self()
  - enif_send()
  - enif_sizeof_resource()
  - enif_system_info()
  - enif_thread_create()
  - enif_thread_exit()
  - enif_thread_join()
  - enif_thread_opts_create()
  - enif_thread_opts_destroy()
  - enif_thread_self()
  - enif_tsd_key_create()
  - enif_tsd_key_destroy()
  - enif_tsd_get()
  - enif_tsd_set()

# erl_nif

### C LIBRARY

erl_nif

### LIBRARY SUMMARY

API functions for an Erlang NIF library

### DESCRIPTION

Note

The NIF concept is officially supported from R14B. NIF source code written for earlier experimental versions might need adaption to run on R14B.

No incompatible changes between R14B and R14A.

Incompatible changes between R14A and R13B04:

- Environment argument removed for enif_alloc, enif_realloc, enif_free, enif_alloc_binary, enif_realloc_binary, enif_release_binary, enif_alloc_resource, enif_release_resource, enif_is_identical and enif_compare.
- Character encoding argument added to enif_get_atom and enif_make_existing_atom.
- Module argument added to enif_open_resource_type while changing name spaces of resource types from global to module local.

Incompatible changes between R13B04 and R13B03:

- The function prototypes of the NIFs have changed to expect argc and argv arguments. The arity of a NIF is by that no longer limited to 3.
- enif_get_data renamed as enif_priv_data.
- enif_make_string got a third argument for character encoding.

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!", ERL_NIF_LATIN1);
    }

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

Note

A NIF does not have to be exported, it can be local to the module. Note however that unused local stub functions will be optimized away by the compiler causing loading of the NIF library to fail.

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 Erlang 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 enif_priv_data.

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.

### FUNCTIONALITY

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:

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 was 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 the struct type ErlNifBinary that 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 should only 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 operates on a binary will 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. But it does not have to happen 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 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 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 totally opaque in nature. It can be stored and passed between processes on the same node, 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 that is guaranteed to still be valid. A resource object will not be deallocated until the last handle term has been garbage collected by the VM and the resource has been 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 that 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.

  Here 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;

  Note 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 solely on the garbage collector to eventually deallocate the resource object when it collects the term.

  Another usage of resource objects is to create binary terms with user defined memory management. enif_make_resource_binary will create a binary term that is connected to a resource object. The destructor of the resource will be 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 takeover an already existing resource type and thereby "inherit" all existing objects of that type. The destructor of the new library will thereafter be 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 will be postponed as long as there exist resource objects with a destructor function in the library.

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. As soon as you write towards 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 will also require synchronization if you treat them as mutable.

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

  Avoid doing lengthy work in NIF calls as that may degrade the responsiveness of the VM. NIFs are called directly by the same scheduler thread that executed the calling Erlang code. The calling scheduler will thus be blocked from doing any other work until the NIF returns.

### 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 order to keep a state between NIF calls. enif_priv_data will return this pointer. *priv_data will be initialized to NULL when load is called.

  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 (*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 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. *priv_data will be initialized to NULL when upgrade is called. 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. Note that unload is not called for a replaced library as a consequence of reload.

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

The reload mechanism is deprecated. It was only intended as a development feature. Do not use it as an upgrade method for live production systems. It might be removed in future releases. Be sure to pass reload as NULL to ERL_NIF_INIT to disable it when not used.

  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.

### DATA TYPES

ERL_NIF_TERM

  Variables of type ERL_NIF_TERM can refer 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. All ERL_NIF_TERM's belong to an environment (ErlNifEnv). A term can not be destructed individually, it is valid until its environment is destructed.

ErlNifEnv

  ErlNifEnv represents an environment that can host Erlang terms. All terms in an environment are valid as long as the environment is valid. ErlNifEnv is an opaque type and pointers to it can only be passed on to API functions. There are two types of environments; process bound and process independent.

  A process bound environment is passed as the first argument to all NIFs. All function arguments passed to a NIF will belong to that environment. The return value from a NIF must also be a term belonging to the same environment. In addition a process bound environment contains transient information about the calling Erlang process. The environment is only valid in the thread where it was supplied as argument until the NIF returns. It is thus useless and dangerous to store pointers to process bound environments between NIF calls.

  A process independent environment is created by calling enif_alloc_env. It 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 invalidates it with enif_free_env or enif_send.

  All elements of a list/tuple must belong to the same environment as the list/tuple itself. 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[]);
      } ErlNifFunc;

  Describes a NIF by its name, arity and implementation. fptr is a pointer to the function that implements the NIF. The argument argv of a NIF will contain the function arguments passed to the NIF and argc is the length of the array, i.e. the function arity. argv[N-1] will thus denote the Nth argument to the NIF. Note that the argc argument allows for the same C function to implement several Erlang functions with different arity (but same name probably).

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.

  Note that ErlNifBinary is a semi-opaque type and you are only allowed to read fields size and data.

ErlNifPid

  ErlNifPid is a process identifier (pid). In contrast to pid terms (instances of ERL_NIF_TERM), ErlNifPid's are self contained and not bound to any environment. ErlNifPid is an opaque type.

ErlNifResourceType

  Each instance of ErlNifResourceType represent 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.

ErlNifResourceDtor

      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.

ErlNifCharEncoding

      typedef enum {
          ERL_NIF_LATIN1
      }ErlNifCharEncoding;

  The character encoding used in strings and atoms. The only supported encoding is currently ERL_NIF_LATIN1 for iso-latin-1 (8-bit ascii).

ErlNifSysInfo

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

ErlNifSInt64

  A native signed 64-bit integer type.

ErlNifUInt64

  A native unsigned 64-bit integer type.

### EXPORTS

void* enif_alloc(size_t size)

Allocate memory of size bytes. Return NULL if allocation failed.

int enif_alloc_binary(size_t size, ErlNifBinary* bin)

Allocate a new binary of size size bytes. Initialize 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.

Return true on success or false if allocation failed.

ErlNifEnv* enif_alloc_env()

Allocate a new process independent environment. The environment can be used to hold terms that is not bound to any process. Such terms can later be copied to a process environment with enif_make_copy or be sent to a process as a message with enif_send.

Return pointer to the new environment.

void* enif_alloc_resource(ErlNifResourceType* type, unsigned size)

Allocate a memory managed resource object of type type and size size bytes.

void enif_clear_env(ErlNifEnv* env)

Free all terms in an environment and clear it for reuse. The environment must have been allocated with enif_alloc_env.

int enif_compare(ERL_NIF_TERM lhs, ERL_NIF_TERM rhs)

Return an integer less than, equal to, or greater than zero if lhs is found, respectively, to be less than, equal, or greater than rhs. Corresponds to the Erlang operators ==, /=, =<, <, >= and > (but not =:= or =/=).

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.

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.

int enif_equal_tids(ErlNifTid tid1, ErlNifTid tid2)

Same as erl_drv_equal_tids.

void enif_free(void* ptr)

Free memory allocated by enif_alloc.

void enif_free_env(ErlNifEnv* env)

Free an environment allocated with enif_alloc_env. All terms created in the environment will be freed as well.

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

Write 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. Return the number of bytes written (including terminating null character) or 0 if term is not an atom with maximum length of size-1.

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

Set *len to the length (number of bytes excluding terminating null character) of the atom term with encoding encode. Return true on success or false if term is not an atom.

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

Set *dp to the floating point value of term. Return true on success or false if term is not a float.

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

Set *ip to the integer value of term. Return true on success or false if term is not an integer or is outside the bounds of type int.

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

Set *ip to the integer value of term. Return true on success or false if term is not an integer or is outside the bounds of a signed 64-bit integer.

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

If term is the pid of a node local process, initialize the pid variable *pid from it and return true. Otherwise return false. No check if the process is alive is done.

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 and return true, or return false if list is not a non-empty list.

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

Set *len to the length of list term and return true, or return false if term is not a list.

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

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

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

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

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

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

Write a null-terminated string, in the buffer pointed to by buf with size size, consisting of the characters in the string list. The characters are written using encoding encode. Return the number of bytes written (including terminating null character), or -size if the string was truncated due to buffer space, or 0 if list is not a string that can be encoded with encode or if size was less than 1. The written string is always null-terminated unless buffer size is less than 1.

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

If term is a tuple, set *array to point to an array containing the elements of the tuple and set *arity to the number of elements. Note that the array is read-only and (*array)[N-1] will be the Nth element of the tuple. *array is undefined if the arity of the tuple is zero.

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

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

Set *ip to the unsigned integer value of term and return true, or return 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)

Set *ip to the unsigned integer value of term and return true, or return false if term is not an unsigned integer or is outside the bounds of an unsigned 64-bit integer.

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

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

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

Initialize the structure pointed to by bin with information about the binary term bin_term. Return 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)

Initialize the structure pointed to by bin with one 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. Return true on success or false if iolist is not an iolist.

int enif_is_atom(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is an atom.

int enif_is_binary(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is a binary

int enif_is_empty_list(ErlNifEnv* env, ERL_NIF_TERM term)

Return 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_number(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is a number.

int enif_is_fun(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is a fun.

int enif_is_identical(ERL_NIF_TERM lhs, ERL_NIF_TERM rhs)

Return true if the two terms are identical. Corresponds to the Erlang operators =:= and =/=.

int enif_is_pid(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is a pid.

int enif_is_port(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is a port.

int enif_is_ref(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is a reference.

int enif_is_tuple(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is a tuple.

int enif_is_list(ErlNifEnv* env, ERL_NIF_TERM term)

Return true if term is a list.

int enif_keep_resource(void* obj)

Add a reference to resource object obj obtained from enif_alloc_resource. Each call to enif_keep_resource for an object must be balanced by a call to enif_release_resource before the object will be destructed.

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

Create an atom term from the null-terminated C-string name with iso-latin-1 encoding.

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

Create an atom term from the string name with length len. Null-characters are treated as any other characters.

ERL_NIF_TERM enif_make_badarg(ErlNifEnv* env)

Make a badarg exception to be returned from a NIF, and set an associated exception reason in env. If enif_make_badarg is called, the term it returns must be returned from the function that called it. No other return value is allowed. Also, the term returned from enif_make_badarg may be passed only to enif_is_exception and not to any other NIF API function.

ERL_NIF_TERM enif_make_binary(ErlNifEnv* env, ErlNifBinary* bin)

Make a binary term from bin. Any ownership of the binary data will be transferred to the created term and bin should be considered read-only for the rest of the NIF call and then as released.

ERL_NIF_TERM enif_make_copy(ErlNifEnv* dst_env, ERL_NIF_TERM src_term)

Make a copy of term src_term. The copy will be created in environment dst_env. The source term may be located in any environment.

ERL_NIF_TERM enif_make_double(ErlNifEnv* env, double d)

Create a floating-point term from a double.

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

Try to create the term of an already existing atom from the null-terminated C-string name with encoding encode. If the atom already exists store the term in *atom and return true, otherwise return false.

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

Try 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 store the term in *atom and return true, otherwise return false.

ERL_NIF_TERM enif_make_int(ErlNifEnv* env, int i)

Create an integer term.

ERL_NIF_TERM enif_make_int64(ErlNifEnv* env, ErlNifSInt64 i)

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

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_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)

Create 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.

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_list_from_array(ErlNifEnv* env, const ERL_NIF_TERM arr[], unsigned cnt)

Create an ordinary list containing the elements of array arr of length cnt. An empty list is returned if cnt is 0.

int enif_make_reverse_list(ErlNifEnv* env, ERL_NIF_TERM term, ERL_NIF_TERM *list)

Set *list to the reverse list of the list term and return true, or return false if term is not a list. This function should only be used on short lists as a copy will be created of the list which will not be released until after the nif returns.

ERL_NIF_TERM enif_make_long(ErlNifEnv* env, long int i)

Create an integer term from a long int.

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

Allocate a binary of size size bytes and create 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. The drawbacks are that the binary can not be kept between NIF calls and it can not be reallocated.

Return a pointer to the raw binary data and set *termp to the binary term.

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

Make a pid term from *pid.

ERL_NIF_TERM enif_make_ref(ErlNifEnv* env)

Create a reference like erlang:make_ref/0.

ERL_NIF_TERM enif_make_resource(ErlNifEnv* env, void* obj)

Create 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 note that the call to enif_release_resource can occur immediately after obtaining the term from enif_make_resource, in which case the resource object will be deallocated when the term is garbage collected. See the example of creating and returning a resource object for more details.

Note that the only defined behaviour of using 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 term_to_binary will have unpredictable (but harmless) results.

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

Create a binary term that is memory managed by a resource object obj obtained by enif_alloc_resource. The returned binary term will consist 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 may be stored external to the resource object in which case it is the responsibility of the destructor to release the data.

Several binary terms may be managed by the same resource object. The destructor will not be called until the last binary is garbage collected. This can be useful as a way to return different parts of a larger binary buffer.

As with enif_make_resource, no ownership transfer is done. The resource still needs to be released with enif_release_resource.

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

Create a list containing the characters of the null-terminated string string with encoding encoding.

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

Create a list containing the characters of the string string with length len and encoding encoding. Null-characters are treated as any other characters.

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

Make 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 and pos+size must be less or equal to the number of whole bytes in bin_term.

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_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)

Create 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.

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

Create a tuple containing the elements of array arr of length cnt.

ERL_NIF_TERM enif_make_uint(ErlNifEnv* env, unsigned int i)

Create an integer term from an unsigned int.

ERL_NIF_TERM enif_make_uint64(ErlNifEnv* env, ErlNifUInt64 i)

Create an integer term from an unsigned 64-bit integer.

ERL_NIF_TERM enif_make_ulong(ErlNifEnv* env, unsigned long i)

Create an integer term from an unsigned long int.

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.

int enif_mutex_trylock(ErlNifMutex *mtx)

Same as erl_drv_mutex_trylock.

void enif_mutex_unlock(ErlNifMutex *mtx)

Same as erl_drv_mutex_unlock.

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

Create or takeover a resource type identified by the string name and give it the destructor function pointed to by dtor. Argument flags can have the following values:

ERL_NIF_RT_CREATE
  Create a new resource type that does not already exist.
ERL_NIF_RT_TAKEOVER
  Open an existing resource type and take over ownership of all its instances. The supplied destructor dtor will be called both for existing instances as well as new instances not yet created by the calling NIF library.

The two flag values can be combined with bitwise-or. The name of the resource type is local to the calling module. Argument module_str is not (yet) used and must be NULL. The dtor may be NULL in case no destructor is needed.

On success, return a pointer to the resource type and *tried will be set to either ERL_NIF_RT_CREATE or ERL_NIF_RT_TAKEOVER to indicate what was actually done. On failure, return NULL and set *tried to flags. It is allowed to set tried to NULL.

Note that enif_open_resource_type is only allowed to be called in the three callbacks load, reload and upgrade.

void* enif_priv_data(ErlNifEnv* env)

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

Was previously named enif_get_data.

int enif_realloc_binary(ErlNifBinary* bin, size_t size)

Change the size of a binary bin. The source binary may be read-only, in which case it will be left untouched and a mutable copy is allocated and assigned to *bin. Return true on success, false if memory allocation failed.

void enif_release_binary(ErlNifBinary* bin)

Release a binary obtained from enif_alloc_binary.

void enif_release_resource(void* obj)

Remove a reference to resource object objobtained from enif_alloc_resource. The resource object will be destructed when the last reference is removed. Each 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.

ErlNifRWLock* enif_rwlock_create(char *name)

Same as erl_drv_rwlock_create.

void enif_rwlock_destroy(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_destroy.

void enif_rwlock_rlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_rlock.

void enif_rwlock_runlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_runlock.

void enif_rwlock_rwlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_rwlock.

void enif_rwlock_rwunlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_rwunlock.

int enif_rwlock_tryrlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_tryrlock.

int enif_rwlock_tryrwlock(ErlNifRWLock *rwlck)

Same as erl_drv_rwlock_tryrwlock.

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

Initialize the pid variable *pid to represent the calling process. Return pid.

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

Send a message to a process.

env
  The environment of the calling process. Must be NULL if and only if calling from a created thread.
*to_pid
  The pid of the receiving process. The pid should 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.
msg
  The message term to send.

Return true on success, or false if *to_pid does not refer to an alive local process.

The message environment msg_env with all its terms (including msg) will be invalidated by a successful call to enif_send. The environment should either be freed with enif_free_env of cleared for reuse with enif_clear_env.

This function is only thread-safe when the emulator with SMP support is used. It can only be used in a non-SMP emulator from a NIF-calling thread.

unsigned enif_sizeof_resource(void* obj)

Get the byte size of a resource object obj obtained by enif_alloc_resource.

void enif_system_info(ErlNifSysInfo *sys_info_ptr, size_t size)

Same as driver_system_info.

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

Same as erl_drv_thread_create.

void enif_thread_exit(void *resp)

Same as erl_drv_thread_exit.

int enif_thread_join(ErlNifTid, void **respp)

Same as erl_drv_thread_join .

ErlNifThreadOpts* enif_thread_opts_create(char *name)

Same as erl_drv_thread_opts_create.

void enif_thread_opts_destroy(ErlNifThreadOpts *opts)

Same as erl_drv_thread_opts_destroy.

ErlNifTid enif_thread_self(void)

Same as erl_drv_thread_self.

int enif_tsd_key_create(char *name, ErlNifTSDKey *key)

Same as erl_drv_tsd_key_create.

void enif_tsd_key_destroy(ErlNifTSDKey key)

Same as erl_drv_tsd_key_destroy.

void* enif_tsd_get(ErlNifTSDKey key)

Same as erl_drv_tsd_get.

void enif_tsd_set(ErlNifTSDKey key, void *data)

Same as erl_drv_tsd_set.

### SEE ALSO

erlang:load_nif/2