.TH erl_nif 3erl "erts 6.2" "Ericsson AB" "C Library Functions" .SH NAME erl_nif \- API functions for an Erlang NIF library .SH DESCRIPTION .LP 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\&. .LP .RS -4 .B Warning: .RE \fIUse this functionality with extreme care!\fR\& .LP 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 can \fInot\fR\& provide the same services as provided when executing Erlang code, such as preemptive scheduling or memory protection\&. If the native function doesn\&'t behave well, the whole VM will misbehave\&. .RS 2 .TP 2 * A native function that crash will crash the whole VM\&. .LP .TP 2 * An erroneously implemented native function might cause a VM internal state inconsistency which may cause a crash of the VM, or miscellaneous misbehaviors of the VM at any point after the call to the native function\&. .LP .TP 2 * A native function that do \fBlengthy work\fR\& before returning will degrade responsiveness of the VM, and may 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 might occur due to lengthy work may also vary between OTP releases\&. .LP .RE .LP The NIF concept is officially supported from R14B\&. NIF source code written for earlier experimental versions might need adaption to run on R14B or later versions: .RS 2 .TP 2 * No incompatible changes between \fIR14B\fR\& and R14A\&. .LP .TP 2 * Incompatible changes between \fIR14A\fR\& and R13B04: .RS 2 .TP 2 * Environment argument removed for \fIenif_alloc\fR\&, \fIenif_realloc\fR\&, \fIenif_free\fR\&, \fIenif_alloc_binary\fR\&, \fIenif_realloc_binary\fR\&, \fIenif_release_binary\fR\&, \fIenif_alloc_resource\fR\&, \fIenif_release_resource\fR\&, \fIenif_is_identical\fR\& and \fIenif_compare\fR\&\&. .LP .TP 2 * Character encoding argument added to \fIenif_get_atom\fR\& and \fIenif_make_existing_atom\fR\&\&. .LP .TP 2 * Module argument added to \fIenif_open_resource_type\fR\& while changing name spaces of resource types from global to module local\&. .LP .RE .LP .TP 2 * Incompatible changes between \fIR13B04\fR\& and R13B03: .RS 2 .TP 2 * The function prototypes of the NIFs have changed to expect \fIargc\fR\& and \fIargv\fR\& arguments\&. The arity of a NIF is by that no longer limited to 3\&. .LP .TP 2 * \fIenif_get_data\fR\& renamed as \fIenif_priv_data\fR\&\&. .LP .TP 2 * \fIenif_make_string\fR\& got a third argument for character encoding\&. .LP .RE .LP .RE .LP A minimal example of a NIF library can look like this: .LP .LP .nf /* 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) .fi .LP and the Erlang module would have to look something like this: .LP .LP .nf -module(niftest). -export([init/0, hello/0]). init() -> erlang:load_nif("./niftest", 0). hello() -> "NIF library not loaded". .fi .LP and compile and test something like this (on Linux): .LP .LP .nf $> 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!" .fi .LP A better solution for a real module is to take advantage of the new directive \fBon_load\fR\& to automatically load the NIF library when the module is loaded\&. .LP .RS -4 .B Note: .RE 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\&. .LP 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 \fBenif_priv_data\fR\&\&. .LP 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\&. .LP As mentioned in the \fBwarning\fR\& text at the beginning of this document it is of vital importance that a native function return relatively quickly\&. It is hard to give an exact maximum amount of time that a native function is allowed to work, but as a rule of thumb a well-behaving native function should return to its caller before a millisecond has passed\&. 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, either directly from Erlang code or by having a native function schedule a future NIF call via the \fB enif_schedule_nif\fR\& function\&. Function \fBenif_consume_timeslice\fR\& can be used to help with such work division\&. In some cases, however, this might not be possible, e\&.g\&. when calling third-party libraries\&. Then you typically want to dispatch the work to another thread, return from the native function, and wait for the result\&. The thread can send the result back to the calling thread using message passing\&. Information about thread primitives can be found below\&. If you have built your system with \fIthe currently experimental\fR\& support for dirty schedulers, you may want to try out this functionality by dispatching the work to a \fBdirty NIF\fR\&, which does not have the same duration restriction as a normal NIF\&. .SH "FUNCTIONALITY" .LP 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: .RS 2 .TP 2 .B 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 \fBERL_NIF_TERM\fR\& and can only be read or written using API functions\&. Most functions to read the content of a term are prefixed \fIenif_get_\fR\& and usually return true (or false) if the term was of the expected type (or not)\&. The functions to write terms are all prefixed \fIenif_make_\fR\& and usually return the created \fIERL_NIF_TERM\fR\&\&. There are also some functions to query terms, like \fIenif_is_atom\fR\&, \fIenif_is_identical\fR\& and \fIenif_compare\fR\&\&. .RS 2 .LP All terms of type \fIERL_NIF_TERM\fR\& belong to an environment of type \fBErlNifEnv\fR\&\&. 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\&. .RE .TP 2 .B Binaries: Terms of type binary are accessed with the help of the struct type \fBErlNifBinary\fR\& that contains a pointer (\fIdata\fR\&) to the raw binary data and the length (\fIsize\fR\&) of the data in bytes\&. Both \fIdata\fR\& and \fIsize\fR\& are read-only and should only be written using calls to API functions\&. Instances of \fIErlNifBinary\fR\& are however always allocated by the user (usually as local variables)\&. .RS 2 .LP The raw data pointed to by \fIdata\fR\& is only mutable after a call to \fBenif_alloc_binary\fR\& or \fBenif_realloc_binary\fR\&\&. 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 \fBenif_release_binary\fR\& or made read-only by transferring it to an Erlang term with \fBenif_make_binary\fR\&\&. But it does not have to happen in the same NIF call\&. Read-only binaries do not have to be released\&. .RE .RS 2 .LP \fBenif_make_new_binary\fR\& can be used as a shortcut to allocate and return a binary in the same NIF call\&. .RE .RS 2 .LP Binaries are sequences of whole bytes\&. Bitstrings with an arbitrary bit length have no support yet\&. .RE .TP 2 .B 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 just a block of memory allocated with \fBenif_alloc_resource\fR\&\&. A handle ("safe pointer") to this memory block can then be returned to Erlang by the use of \fBenif_make_resource\fR\&\&. The term returned by \fIenif_make_resource\fR\& 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 \fBenif_get_resource\fR\& 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 \fBenif_release_resource\fR\& (not necessarily in that order)\&. .RS 2 .LP All resource objects are created as instances of some \fIresource type\fR\&\&. This makes resources from different modules to be distinguishable\&. A resource type is created by calling \fBenif_open_resource_type\fR\& when a library is loaded\&. Objects of that resource type can then later be allocated and \fIenif_get_resource\fR\& 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 \fIenif_release_resource\fR\&)\&. Resource types are uniquely identified by a supplied name string and the name of the implementing module\&. .RE .RS 2 .LP Here is a template example of how to create and return a resource object\&. .RE .RS 2 .LP .RE .LP .nf 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; .fi .RS 2 .LP Note that once \fIenif_make_resource\fR\& 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\&. .RE .RS 2 .LP Another usage of resource objects is to create binary terms with user defined memory management\&. \fBenif_make_resource_binary\fR\& 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 \fImmap\fR\&\&'ed file\&. The destructor can then do \fImunmap\fR\& to release the memory region\&. .RE .RS 2 .LP 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\&. .RE .TP 2 .B 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 \fBenif_priv_data\fR\& 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\&. .RS 2 .LP The library initialization callbacks \fIload\fR\&, \fIreload\fR\& and \fIupgrade\fR\& are all thread-safe even for shared state data\&. .RE .TP 2 .B Version Management: When a NIF library is built, information about 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\&. \fIerl_nif\&.h\fR\& defines \fIERL_NIF_MAJOR_VERSION\fR\&, and \fIERL_NIF_MINOR_VERSION\fR\&\&. \fIERL_NIF_MAJOR_VERSION\fR\& will be incremented when NIF library incompatible changes are made to the Erlang runtime system\&. Normally it will suffice to recompile the NIF library when the \fIERL_NIF_MAJOR_VERSION\fR\& has changed, but it could, under rare circumstances, mean that NIF libraries have to be slightly modified\&. If so, this will of course be documented\&. \fIERL_NIF_MINOR_VERSION\fR\& will be incremented when new features are added\&. The runtime system uses the minor version to determine what features to use\&. .RS 2 .LP The runtime system will normally refuse to load a NIF library if the major versions differ, or if the major versions are equal and 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 will however be allowed after a bump of the major version during a transition period of two major releases\&. Such old NIF libraries might however fail if deprecated features are used\&. .RE .TP 2 .B Long-running NIFs: Native functions \fB must normally run quickly\fR\&, as explained earlier in this document\&. They generally should execute for no more than a millisecond\&. But not all native functions can execute so quickly; for example, functions that encrypt large blocks of data or perform lengthy file system operations can often run for tens of seconds or more\&. .RS 2 .LP 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 can either make that series of NIF calls from the Erlang level, or it can call a NIF that first performs a chunk of the work, then invokes the \fBenif_schedule_nif\fR\& 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, thereby avoiding degraded responsiveness, scheduler load balancing problems, and other strange behaviours\&. .RE .RS 2 .LP A NIF that cannot be split and cannot execute in a millisecond or less is called a "dirty NIF" because it performs work that the Erlang runtime cannot handle cleanly\&. \fINote that the dirty NIF functionality described here is experimental\fR\& and that you have to enable support for dirty schedulers when building OTP in order to try the functionality out\&. Applications that make use of such functions must indicate to the runtime that the functions are dirty so they can be handled specially\&. To schedule a dirty NIF for execution, the appropriate flags value can be set for the NIF in its \fBErlNifFunc\fR\& entry, or the application can call \fBenif_schedule_nif\fR\&, passing to it a pointer to the dirty NIF to be executed and indicating with the \fIflags\fR\& argument whether it expects the operation to be CPU-bound or I/O-bound\&. .RE .LP .RS -4 .B Note: .RE Dirty NIF support is available only when the emulator is configured with dirty schedulers enabled\&. This feature is currently disabled by default\&. To determine whether the dirty NIF API is available, native code can check to see if the C preprocessor macro \fIERL_NIF_DIRTY_SCHEDULER_SUPPORT\fR\& is defined\&. Also, if the Erlang runtime was built without threading support, dirty schedulers are disabled\&. To check at runtime for the presence of dirty scheduler threads, code can use the \fB\fI enif_system_info()\fR\&\fR\& API function\&. .RE .SH "INITIALIZATION" .RS 2 .TP 2 .B 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\&. .RS 2 .LP \fIMODULE\fR\& is the name of the Erlang module as an identifier without string quotations\&. It will be stringified by the macro\&. .RE .RS 2 .LP \fIfuncs\fR\& is a static array of function descriptors for all the implemented NIFs in this library\&. .RE .RS 2 .LP \fIload\fR\&, \fIreload\fR\&, \fIupgrade\fR\& and \fIunload\fR\& are pointers to functions\&. One of \fIload\fR\&, \fIreload\fR\& or \fIupgrade\fR\& will be called to initialize the library\&. \fIunload\fR\& is called to release the library\&. They are all described individually below\&. .RE .RS 2 .LP If compiling a nif for static inclusion via --enable-static-nifs you have to define STATIC_ERLANG_NIF before the ERL_NIF_INIT declaration\&. .RE .TP 2 .B int (*load)(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info): \fIload\fR\& is called when the NIF library is loaded and there is no previously loaded library for this module\&. .RS 2 .LP \fI*priv_data\fR\& can be set to point to some private data that the library needs in order to keep a state between NIF calls\&. \fIenif_priv_data\fR\& will return this pointer\&. \fI*priv_data\fR\& will be initialized to NULL when \fIload\fR\& is called\&. .RE .RS 2 .LP \fIload_info\fR\& is the second argument to \fBerlang:load_nif/2\fR\&\&. .RE .RS 2 .LP The library will fail to load if \fIload\fR\& returns anything other than 0\&. \fIload\fR\& can be NULL in case no initialization is needed\&. .RE .TP 2 .B int (*upgrade)(ErlNifEnv* env, void** priv_data, void** old_priv_data, ERL_NIF_TERM load_info): \fIupgrade\fR\& is called when the NIF library is loaded and there is old code of this module with a loaded NIF library\&. .RS 2 .LP Works the same as \fIload\fR\&\&. The only difference is that \fI*old_priv_data\fR\& already contains the value set by the last call to \fIload\fR\& or \fIreload\fR\& for the old module code\&. \fI*priv_data\fR\& will be initialized to NULL when \fIupgrade\fR\& is called\&. It is allowed to write to both *priv_data and *old_priv_data\&. .RE .RS 2 .LP The library will fail to load if \fIupgrade\fR\& returns anything other than 0 or if \fIupgrade\fR\& is NULL\&. .RE .TP 2 .B void (*unload)(ErlNifEnv* env, void* priv_data): \fIunload\fR\& 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 \fIunload\fR\& is not called for a replaced library as a consequence of \fIreload\fR\&\&. .TP 2 .B int (*reload)(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info): \fIreload\fR\& is called when the NIF library is loaded and there is already a previously loaded library for this module code\&. .RS 2 .LP Works the same as \fIload\fR\&\&. The only difference is that \fI*priv_data\fR\& already contains the value set by the previous call to \fIload\fR\& or \fIreload\fR\&\&. .RE .RS 2 .LP The library will fail to load if \fIreload\fR\& returns anything other than 0 or if \fIreload\fR\& is NULL\&. .RE .RE .SH "DATA TYPES" .RS 2 .TP 2 .B ERL_NIF_TERM: Variables of type \fIERL_NIF_TERM\fR\& 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 \fIERL_NIF_TERM\fR\&\&'s belong to an environment (\fBErlNifEnv\fR\&)\&. A term can not be destructed individually, it is valid until its environment is destructed\&. .TP 2 .B ErlNifEnv: \fIErlNifEnv\fR\& represents an environment that can host Erlang terms\&. All terms in an environment are valid as long as the environment is valid\&. \fIErlNifEnv\fR\& 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\&. .RS 2 .LP A \fIprocess bound environment\fR\& 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\&. .RE .RS 2 .LP A \fIprocess independent environment\fR\& is created by calling \fBenif_alloc_env\fR\&\&. It can be used to store terms between NIF calls and to send terms with \fBenif_send\fR\&\&. A process independent environment with all its terms is valid until you explicitly invalidates it with \fBenif_free_env\fR\& or \fIenif_send\fR\&\&. .RE .RS 2 .LP All elements of a list/tuple must belong to the same environment as the list/tuple itself\&. Terms can be copied between environments with \fBenif_make_copy\fR\&\&. .RE .TP 2 .B ErlNifFunc: .LP .nf typedef struct { const char* \fIname\fR\&; unsigned \fIarity\fR\&; ERL_NIF_TERM (*\fIfptr\fR\&)(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); unsigned flags; } ErlNifFunc; .fi .RS 2 .LP Describes a NIF by its name, arity and implementation\&. \fIfptr\fR\& is a pointer to the function that implements the NIF\&. The argument \fIargv\fR\& of a NIF will contain the function arguments passed to the NIF and \fIargc\fR\& is the length of the array, i\&.e\&. the function arity\&. \fIargv[N-1]\fR\& will thus denote the Nth argument to the NIF\&. Note that the \fIargc\fR\& argument allows for the same C function to implement several Erlang functions with different arity (but same name probably)\&. For a regular NIF, \fIflags\fR\& is 0 (and so its value can be omitted for statically initialized \fIErlNifFunc\fR\& instances), or it can be used to indicate that the NIF is a \fBdirty NIF\fR\& that should be executed on a dirty scheduler thread (\fInote that the dirty NIF functionality described here is experimental\fR\& and that you have to enable support for dirty schedulers when building OTP in order to try the functionality out)\&. If the dirty NIF is expected to be CPU-bound, its \fIflags\fR\& field should be set to \fIERL_NIF_DIRTY_JOB_CPU_BOUND\fR\&, or for I/O-bound jobs, \fIERL_NIF_DIRTY_JOB_IO_BOUND\fR\&\&. .RE .TP 2 .B ErlNifBinary: .LP .nf typedef struct { unsigned \fIsize\fR\&; unsigned char* \fIdata\fR\&; } ErlNifBinary; .fi .RS 2 .LP \fIErlNifBinary\fR\& contains transient information about an inspected binary term\&. \fIdata\fR\& is a pointer to a buffer of \fIsize\fR\& bytes with the raw content of the binary\&. .RE .RS 2 .LP Note that \fIErlNifBinary\fR\& is a semi-opaque type and you are only allowed to read fields \fIsize\fR\& and \fIdata\fR\&\&. .RE .TP 2 .B ErlNifPid: \fIErlNifPid\fR\& is a process identifier (pid)\&. In contrast to pid terms (instances of \fIERL_NIF_TERM\fR\&), \fIErlNifPid\fR\&\&'s are self contained and not bound to any \fBenvironment\fR\&\&. \fIErlNifPid\fR\& is an opaque type\&. .TP 2 .B ErlNifResourceType: Each instance of \fIErlNifResourceType\fR\& 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\&. .TP 2 .B ErlNifResourceDtor: .LP .nf typedef void ErlNifResourceDtor(ErlNifEnv* env, void* obj); .fi .RS 2 .LP The function prototype of a resource destructor function\&. A destructor function is not allowed to call any term-making functions\&. .RE .TP 2 .B ErlNifCharEncoding: .LP .nf typedef enum { ERL_NIF_LATIN1 }ErlNifCharEncoding; .fi .RS 2 .LP The character encoding used in strings and atoms\&. The only supported encoding is currently \fIERL_NIF_LATIN1\fR\& for iso-latin-1 (8-bit ascii)\&. .RE .TP 2 .B ErlNifSysInfo: Used by \fBenif_system_info\fR\& to return information about the runtime system\&. Contains currently the exact same content as \fBErlDrvSysInfo\fR\&\&. .TP 2 .B ErlNifSInt64: A native signed 64-bit integer type\&. .TP 2 .B ErlNifUInt64: A native unsigned 64-bit integer type\&. .RE .SH EXPORTS .LP .B void *enif_alloc(size_t size) .br .RS .LP Allocate memory of \fIsize\fR\& bytes\&. Return NULL if allocation failed\&. .RE .LP .B int enif_alloc_binary(size_t size, ErlNifBinary* bin) .br .RS .LP Allocate a new binary of size \fIsize\fR\& bytes\&. Initialize the structure pointed to by \fIbin\fR\& to refer to the allocated binary\&. The binary must either be released by \fBenif_release_binary\fR\& or ownership transferred to an Erlang term with \fBenif_make_binary\fR\&\&. An allocated (and owned) \fIErlNifBinary\fR\& can be kept between NIF calls\&. .LP Return true on success or false if allocation failed\&. .RE .LP .B ErlNifEnv *enif_alloc_env() .br .RS .LP 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 \fBenif_make_copy\fR\& or be sent to a process as a message with \fBenif_send\fR\&\&. .LP Return pointer to the new environment\&. .RE .LP .B void *enif_alloc_resource(ErlNifResourceType* type, unsigned size) .br .RS .LP Allocate a memory managed resource object of type \fItype\fR\& and size \fIsize\fR\& bytes\&. .RE .LP .B void enif_clear_env(ErlNifEnv* env) .br .RS .LP Free all terms in an environment and clear it for reuse\&. The environment must have been allocated with \fBenif_alloc_env\fR\&\&. .RE .LP .B int enif_compare(ERL_NIF_TERM lhs, ERL_NIF_TERM rhs) .br .RS .LP Return an integer less than, equal to, or greater than zero if \fIlhs\fR\& is found, respectively, to be less than, equal, or greater than \fIrhs\fR\&\&. Corresponds to the Erlang operators \fI==\fR\&, \fI/=\fR\&, \fI=<\fR\&, \fI<\fR\&, \fI>=\fR\& and \fI>\fR\& (but \fInot\fR\& \fI=:=\fR\& or \fI=/=\fR\&)\&. .RE .LP .B void enif_cond_broadcast(ErlNifCond *cnd) .br .RS .LP Same as \fBerl_drv_cond_broadcast\fR\&\&. .RE .LP .B ErlNifCond *enif_cond_create(char *name) .br .RS .LP Same as \fBerl_drv_cond_create\fR\&\&. .RE .LP .B void enif_cond_destroy(ErlNifCond *cnd) .br .RS .LP Same as \fBerl_drv_cond_destroy\fR\&\&. .RE .LP .B void enif_cond_signal(ErlNifCond *cnd) .br .RS .LP Same as \fBerl_drv_cond_signal\fR\&\&. .RE .LP .B void enif_cond_wait(ErlNifCond *cnd, ErlNifMutex *mtx) .br .RS .LP Same as \fBerl_drv_cond_wait\fR\&\&. .RE .LP .B int enif_consume_timeslice(ErlNifEnv *env, int percent) .br .RS .LP Give the runtime system a hint about how much CPU time the current NIF call has consumed since last hint, or since the start of the NIF if no previous hint has been given\&. The time is given as a \fIpercent\fR\& of the timeslice that a process is allowed to execute Erlang code until it may be suspended to give time for other runnable processes\&. The scheduling timeslice is not an exact entity, but can usually be approximated to about 1 millisecond\&. .LP Note that it is up to the runtime system to determine if and how to use this information\&. Implementations on some platforms may use other means in order to determine consumed CPU time\&. Lengthy NIFs should regardless of this frequently call \fIenif_consume_timeslice\fR\& in order to determine if it is allowed to continue execution or not\&. .LP Returns 1 if the timeslice is exhausted, or 0 otherwise\&. If 1 is returned the NIF should return as soon as possible in order for the process to yield\&. .LP Argument \fIpercent\fR\& must be an integer between 1 and 100\&. This function must only be called from a NIF-calling thread and argument \fIenv\fR\& must be the environment of the calling process\&. .LP This function is provided to better support co-operative scheduling, improve system responsiveness, and make it easier to prevent misbehaviors of the VM due to a NIF monopolizing a scheduler thread\&. It can be used to divide \fBlength work\fR\& into a number of repeated NIF-calls without the need to create threads\&. See also the \fBwarning\fR\& text at the beginning of this document\&. .RE .LP .B int enif_equal_tids(ErlNifTid tid1, ErlNifTid tid2) .br .RS .LP Same as \fBerl_drv_equal_tids\fR\&\&. .RE .LP .B void enif_free(void* ptr) .br .RS .LP Free memory allocated by \fIenif_alloc\fR\&\&. .RE .LP .B void enif_free_env(ErlNifEnv* env) .br .RS .LP Free an environment allocated with \fBenif_alloc_env\fR\&\&. All terms created in the environment will be freed as well\&. .RE .LP .B int enif_get_atom(ErlNifEnv* env, ERL_NIF_TERM term, char* buf, unsigned size, ErlNifCharEncoding encode) .br .RS .LP Write a null-terminated string, in the buffer pointed to by \fIbuf\fR\& of size \fIsize\fR\&, consisting of the string representation of the atom \fIterm\fR\& with encoding \fBencode\fR\&\&. Return the number of bytes written (including terminating null character) or 0 if \fIterm\fR\& is not an atom with maximum length of \fIsize-1\fR\&\&. .RE .LP .B int enif_get_atom_length(ErlNifEnv* env, ERL_NIF_TERM term, unsigned* len, ErlNifCharEncoding encode) .br .RS .LP Set \fI*len\fR\& to the length (number of bytes excluding terminating null character) of the atom \fIterm\fR\& with encoding \fIencode\fR\&\&. Return true on success or false if \fIterm\fR\& is not an atom\&. .RE .LP .B int enif_get_double(ErlNifEnv* env, ERL_NIF_TERM term, double* dp) .br .RS .LP Set \fI*dp\fR\& to the floating point value of \fIterm\fR\&\&. Return true on success or false if \fIterm\fR\& is not a float\&. .RE .LP .B int enif_get_int(ErlNifEnv* env, ERL_NIF_TERM term, int* ip) .br .RS .LP Set \fI*ip\fR\& to the integer value of \fIterm\fR\&\&. Return true on success or false if \fIterm\fR\& is not an integer or is outside the bounds of type \fIint\fR\&\&. .RE .LP .B int enif_get_int64(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifSInt64* ip) .br .RS .LP Set \fI*ip\fR\& to the integer value of \fIterm\fR\&\&. Return true on success or false if \fIterm\fR\& is not an integer or is outside the bounds of a signed 64-bit integer\&. .RE .LP .B int enif_get_local_pid(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifPid* pid) .br .RS .LP If \fIterm\fR\& is the pid of a node local process, initialize the pid variable \fI*pid\fR\& from it and return true\&. Otherwise return false\&. No check if the process is alive is done\&. .RE .LP .B int enif_get_list_cell(ErlNifEnv* env, ERL_NIF_TERM list, ERL_NIF_TERM* head, ERL_NIF_TERM* tail) .br .RS .LP Set \fI*head\fR\& and \fI*tail\fR\& from \fIlist\fR\& and return true, or return false if \fIlist\fR\& is not a non-empty list\&. .RE .LP .B int enif_get_list_length(ErlNifEnv* env, ERL_NIF_TERM term, unsigned* len) .br .RS .LP Set \fI*len\fR\& to the length of list \fIterm\fR\& and return true, or return false if \fIterm\fR\& is not a list\&. .RE .LP .B int enif_get_long(ErlNifEnv* env, ERL_NIF_TERM term, long int* ip) .br .RS .LP Set \fI*ip\fR\& to the long integer value of \fIterm\fR\& and return true, or return false if \fIterm\fR\& is not an integer or is outside the bounds of type \fIlong int\fR\&\&. .RE .LP .B int enif_get_resource(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifResourceType* type, void** objp) .br .RS .LP Set \fI*objp\fR\& to point to the resource object referred to by \fIterm\fR\&\&. .LP Return true on success or false if \fIterm\fR\& is not a handle to a resource object of type \fItype\fR\&\&. .RE .LP .B int enif_get_string(ErlNifEnv* env, ERL_NIF_TERM list, char* buf, unsigned size, ErlNifCharEncoding encode) .br .RS .LP Write a null-terminated string, in the buffer pointed to by \fIbuf\fR\& with size \fIsize\fR\&, consisting of the characters in the string \fIlist\fR\&\&. The characters are written using encoding \fBencode\fR\&\&. Return the number of bytes written (including terminating null character), or \fI-size\fR\& if the string was truncated due to buffer space, or 0 if \fIlist\fR\& is not a string that can be encoded with \fIencode\fR\& or if \fIsize\fR\& was less than 1\&. The written string is always null-terminated unless buffer \fIsize\fR\& is less than 1\&. .RE .LP .B int enif_get_tuple(ErlNifEnv* env, ERL_NIF_TERM term, int* arity, const ERL_NIF_TERM** array) .br .RS .LP If \fIterm\fR\& is a tuple, set \fI*array\fR\& to point to an array containing the elements of the tuple and set \fI*arity\fR\& to the number of elements\&. Note that the array is read-only and \fI(*array)[N-1]\fR\& will be the Nth element of the tuple\&. \fI*array\fR\& is undefined if the arity of the tuple is zero\&. .LP Return true on success or false if \fIterm\fR\& is not a tuple\&. .RE .LP .B int enif_get_uint(ErlNifEnv* env, ERL_NIF_TERM term, unsigned int* ip) .br .RS .LP Set \fI*ip\fR\& to the unsigned integer value of \fIterm\fR\& and return true, or return false if \fIterm\fR\& is not an unsigned integer or is outside the bounds of type \fIunsigned int\fR\&\&. .RE .LP .B int enif_get_uint64(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifUInt64* ip) .br .RS .LP Set \fI*ip\fR\& to the unsigned integer value of \fIterm\fR\& and return true, or return false if \fIterm\fR\& is not an unsigned integer or is outside the bounds of an unsigned 64-bit integer\&. .RE .LP .B int enif_get_ulong(ErlNifEnv* env, ERL_NIF_TERM term, unsigned long* ip) .br .RS .LP Set \fI*ip\fR\& to the unsigned long integer value of \fIterm\fR\& and return true, or return false if \fIterm\fR\& is not an unsigned integer or is outside the bounds of type \fIunsigned long\fR\&\&. .RE .LP .B int enif_inspect_binary(ErlNifEnv* env, ERL_NIF_TERM bin_term, ErlNifBinary* bin) .br .RS .LP Initialize the structure pointed to by \fIbin\fR\& with information about the binary term \fIbin_term\fR\&\&. Return true on success or false if \fIbin_term\fR\& is not a binary\&. .RE .LP .B int enif_inspect_iolist_as_binary(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifBinary* bin) .br .RS .LP Initialize the structure pointed to by \fIbin\fR\& with one continuous buffer with the same byte content as \fIiolist\fR\&\&. As with inspect_binary, the data pointed to by \fIbin\fR\& is transient and does not need to be released\&. Return true on success or false if \fIiolist\fR\& is not an iolist\&. .RE .LP .B int enif_is_atom(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is an atom\&. .RE .LP .B int enif_is_binary(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is a binary .RE .LP .B int enif_is_empty_list(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is an empty list\&. .RE .LP .B int enif_is_exception(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is an exception\&. .RE .LP .B int enif_is_number(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is a number\&. .RE .LP .B int enif_is_fun(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is a fun\&. .RE .LP .B int enif_is_identical(ERL_NIF_TERM lhs, ERL_NIF_TERM rhs) .br .RS .LP Return true if the two terms are identical\&. Corresponds to the Erlang operators \fI=:=\fR\& and \fI=/=\fR\&\&. .RE .LP .B int enif_is_on_dirty_scheduler(ErlNifEnv* env) .br .RS .LP Check to see if the current NIF is executing on a dirty scheduler thread\&. If the emulator is built with threading support, calling \fIenif_is_on_dirty_scheduler\fR\& from within a dirty NIF returns true\&. It returns false when the calling NIF is a regular NIF running on a normal scheduler thread, or when the emulator is built without threading support\&. .LP .RS -4 .B Note: .RE This function is available only when the emulator is configured with dirty schedulers enabled\&. This feature is currently disabled by default\&. To determine whether the dirty NIF API is available, native code can check to see if the C preprocessor macro \fIERL_NIF_DIRTY_SCHEDULER_SUPPORT\fR\& is defined\&. .RE .LP .B int enif_is_pid(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is a pid\&. .RE .LP .B int enif_is_port(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is a port\&. .RE .LP .B int enif_is_ref(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is a reference\&. .RE .LP .B int enif_is_tuple(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is a tuple\&. .RE .LP .B int enif_is_list(ErlNifEnv* env, ERL_NIF_TERM term) .br .RS .LP Return true if \fIterm\fR\& is a list\&. .RE .LP .B int enif_keep_resource(void* obj) .br .RS .LP Add a reference to resource object \fIobj\fR\& obtained from \fBenif_alloc_resource\fR\&\&. Each call to \fIenif_keep_resource\fR\& for an object must be balanced by a call to \fBenif_release_resource\fR\& before the object will be destructed\&. .RE .LP .B ERL_NIF_TERM enif_make_atom(ErlNifEnv* env, const char* name) .br .RS .LP Create an atom term from the null-terminated C-string \fIname\fR\& with iso-latin-1 encoding\&. .RE .LP .B ERL_NIF_TERM enif_make_atom_len(ErlNifEnv* env, const char* name, size_t len) .br .RS .LP Create an atom term from the string \fIname\fR\& with length \fIlen\fR\&\&. Null-characters are treated as any other characters\&. .RE .LP .B ERL_NIF_TERM enif_make_badarg(ErlNifEnv* env) .br .RS .LP Make a badarg exception to be returned from a NIF, and set an associated exception reason in \fIenv\fR\&\&. If \fIenif_make_badarg\fR\& is called, the term it returns \fImust\fR\& be returned from the function that called it\&. No other return value is allowed\&. Also, the term returned from \fIenif_make_badarg\fR\& may be passed only to \fBenif_is_exception\fR\& and not to any other NIF API function\&. .RE .LP .B ERL_NIF_TERM enif_make_binary(ErlNifEnv* env, ErlNifBinary* bin) .br .RS .LP Make a binary term from \fIbin\fR\&\&. Any ownership of the binary data will be transferred to the created term and \fIbin\fR\& should be considered read-only for the rest of the NIF call and then as released\&. .RE .LP .B ERL_NIF_TERM enif_make_copy(ErlNifEnv* dst_env, ERL_NIF_TERM src_term) .br .RS .LP Make a copy of term \fIsrc_term\fR\&\&. The copy will be created in environment \fIdst_env\fR\&\&. The source term may be located in any environment\&. .RE .LP .B ERL_NIF_TERM enif_make_double(ErlNifEnv* env, double d) .br .RS .LP Create a floating-point term from a \fIdouble\fR\&\&. .RE .LP .B int enif_make_existing_atom(ErlNifEnv* env, const char* name, ERL_NIF_TERM* atom, ErlNifCharEncoding encode) .br .RS .LP Try to create the term of an already existing atom from the null-terminated C-string \fIname\fR\& with encoding \fBencode\fR\&\&. If the atom already exists store the term in \fI*atom\fR\& and return true, otherwise return false\&. .RE .LP .B int enif_make_existing_atom_len(ErlNifEnv* env, const char* name, size_t len, ERL_NIF_TERM* atom, ErlNifCharEncoding encoding) .br .RS .LP Try to create the term of an already existing atom from the string \fIname\fR\& with length \fIlen\fR\& and encoding \fBencode\fR\&\&. Null-characters are treated as any other characters\&. If the atom already exists store the term in \fI*atom\fR\& and return true, otherwise return false\&. .RE .LP .B ERL_NIF_TERM enif_make_int(ErlNifEnv* env, int i) .br .RS .LP Create an integer term\&. .RE .LP .B ERL_NIF_TERM enif_make_int64(ErlNifEnv* env, ErlNifSInt64 i) .br .RS .LP Create an integer term from a signed 64-bit integer\&. .RE .LP .B ERL_NIF_TERM enif_make_list(ErlNifEnv* env, unsigned cnt, ...) .br .RS .LP Create an ordinary list term of length \fIcnt\fR\&\&. Expects \fIcnt\fR\& number of arguments (after \fIcnt\fR\&) of type ERL_NIF_TERM as the elements of the list\&. An empty list is returned if \fIcnt\fR\& is 0\&. .RE .LP .B ERL_NIF_TERM enif_make_list1(ErlNifEnv* env, ERL_NIF_TERM e1) .br .B ERL_NIF_TERM enif_make_list2(ErlNifEnv* env, ERL_NIF_TERM e1, ERL_NIF_TERM e2) .br .B ERL_NIF_TERM enif_make_list3(ErlNifEnv* env, ERL_NIF_TERM e1, ERL_NIF_TERM e2, ERL_NIF_TERM e3) .br .B ERL_NIF_TERM enif_make_list4(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e4) .br .B ERL_NIF_TERM enif_make_list5(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e5) .br .B ERL_NIF_TERM enif_make_list6(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e6) .br .B ERL_NIF_TERM enif_make_list7(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e7) .br .B ERL_NIF_TERM enif_make_list8(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e8) .br .B ERL_NIF_TERM enif_make_list9(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9) .br .RS .LP Create an ordinary list term with length indicated by the function name\&. Prefer these functions (macros) over the variadic \fIenif_make_list\fR\& to get a compile time error if the number of arguments does not match\&. .RE .LP .B ERL_NIF_TERM enif_make_list_cell(ErlNifEnv* env, ERL_NIF_TERM head, ERL_NIF_TERM tail) .br .RS .LP Create a list cell \fI[head | tail]\fR\&\&. .RE .LP .B ERL_NIF_TERM enif_make_list_from_array(ErlNifEnv* env, const ERL_NIF_TERM arr[], unsigned cnt) .br .RS .LP Create an ordinary list containing the elements of array \fIarr\fR\& of length \fIcnt\fR\&\&. An empty list is returned if \fIcnt\fR\& is 0\&. .RE .LP .B int enif_make_reverse_list(ErlNifEnv* env, ERL_NIF_TERM term, ERL_NIF_TERM *list) .br .RS .LP Set \fI*list\fR\& to the reverse list of the list \fIterm\fR\& and return true, or return false if \fIterm\fR\& 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\&. .RE .LP .B ERL_NIF_TERM enif_make_long(ErlNifEnv* env, long int i) .br .RS .LP Create an integer term from a \fIlong int\fR\&\&. .RE .LP .B unsigned char *enif_make_new_binary(ErlNifEnv* env, size_t size, ERL_NIF_TERM* termp) .br .RS .LP Allocate a binary of size \fIsize\fR\& 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 \fBErlNifBinary\fR\&\&. The drawbacks are that the binary can not be kept between NIF calls and it can not be reallocated\&. .LP Return a pointer to the raw binary data and set \fI*termp\fR\& to the binary term\&. .RE .LP .B ERL_NIF_TERM enif_make_pid(ErlNifEnv* env, const ErlNifPid* pid) .br .RS .LP Make a pid term from \fI*pid\fR\&\&. .RE .LP .B ERL_NIF_TERM enif_make_ref(ErlNifEnv* env) .br .RS .LP Create a reference like \fBerlang:make_ref/0\fR\&\&. .RE .LP .B ERL_NIF_TERM enif_make_resource(ErlNifEnv* env, void* obj) .br .RS .LP Create an opaque handle to a memory managed resource object obtained by \fBenif_alloc_resource\fR\&\&. No ownership transfer is done, as the resource object still needs to be released by \fBenif_release_resource\fR\&, but note that the call to \fIenif_release_resource\fR\& can occur immediately after obtaining the term from \fIenif_make_resource\fR\&, in which case the resource object will be deallocated when the term is garbage collected\&. See the \fBexample of creating and returning a resource object\fR\& for more details\&. .LP 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 \fIterm_to_binary\fR\& will have unpredictable (but harmless) results\&. .RE .LP .B ERL_NIF_TERM enif_make_resource_binary(ErlNifEnv* env, void* obj, const void* data, size_t size) .br .RS .LP Create a binary term that is memory managed by a resource object \fIobj\fR\& obtained by \fBenif_alloc_resource\fR\&\&. The returned binary term will consist of \fIsize\fR\& bytes pointed to by \fIdata\fR\&\&. 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\&. .LP 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\&. .LP As with \fBenif_make_resource\fR\&, no ownership transfer is done\&. The resource still needs to be released with \fBenif_release_resource\fR\&\&. .RE .LP .B ERL_NIF_TERM enif_make_string(ErlNifEnv* env, const char* string, ErlNifCharEncoding encoding) .br .RS .LP Create a list containing the characters of the null-terminated string \fIstring\fR\& with encoding \fBencoding\fR\&\&. .RE .LP .B ERL_NIF_TERM enif_make_string_len(ErlNifEnv* env, const char* string, size_t len, ErlNifCharEncoding encoding) .br .RS .LP Create a list containing the characters of the string \fIstring\fR\& with length \fIlen\fR\& and encoding \fBencoding\fR\&\&. Null-characters are treated as any other characters\&. .RE .LP .B ERL_NIF_TERM enif_make_sub_binary(ErlNifEnv* env, ERL_NIF_TERM bin_term, size_t pos, size_t size) .br .RS .LP Make a subbinary of binary \fIbin_term\fR\&, starting at zero-based position \fIpos\fR\& with a length of \fIsize\fR\& bytes\&. \fIbin_term\fR\& must be a binary or bitstring and \fIpos+size\fR\& must be less or equal to the number of whole bytes in \fIbin_term\fR\&\&. .RE .LP .B ERL_NIF_TERM enif_make_tuple(ErlNifEnv* env, unsigned cnt, ...) .br .RS .LP Create a tuple term of arity \fIcnt\fR\&\&. Expects \fIcnt\fR\& number of arguments (after \fIcnt\fR\&) of type ERL_NIF_TERM as the elements of the tuple\&. .RE .LP .B ERL_NIF_TERM enif_make_tuple1(ErlNifEnv* env, ERL_NIF_TERM e1) .br .B ERL_NIF_TERM enif_make_tuple2(ErlNifEnv* env, ERL_NIF_TERM e1, ERL_NIF_TERM e2) .br .B ERL_NIF_TERM enif_make_tuple3(ErlNifEnv* env, ERL_NIF_TERM e1, ERL_NIF_TERM e2, ERL_NIF_TERM e3) .br .B ERL_NIF_TERM enif_make_tuple4(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e4) .br .B ERL_NIF_TERM enif_make_tuple5(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e5) .br .B ERL_NIF_TERM enif_make_tuple6(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e6) .br .B ERL_NIF_TERM enif_make_tuple7(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e7) .br .B ERL_NIF_TERM enif_make_tuple8(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e8) .br .B ERL_NIF_TERM enif_make_tuple9(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9) .br .RS .LP Create a tuple term with length indicated by the function name\&. Prefer these functions (macros) over the variadic \fIenif_make_tuple\fR\& to get a compile time error if the number of arguments does not match\&. .RE .LP .B ERL_NIF_TERM enif_make_tuple_from_array(ErlNifEnv* env, const ERL_NIF_TERM arr[], unsigned cnt) .br .RS .LP Create a tuple containing the elements of array \fIarr\fR\& of length \fIcnt\fR\&\&. .RE .LP .B ERL_NIF_TERM enif_make_uint(ErlNifEnv* env, unsigned int i) .br .RS .LP Create an integer term from an \fIunsigned int\fR\&\&. .RE .LP .B ERL_NIF_TERM enif_make_uint64(ErlNifEnv* env, ErlNifUInt64 i) .br .RS .LP Create an integer term from an unsigned 64-bit integer\&. .RE .LP .B ERL_NIF_TERM enif_make_ulong(ErlNifEnv* env, unsigned long i) .br .RS .LP Create an integer term from an \fIunsigned long int\fR\&\&. .RE .LP .B ErlNifMutex *enif_mutex_create(char *name) .br .RS .LP Same as \fBerl_drv_mutex_create\fR\&\&. .RE .LP .B void enif_mutex_destroy(ErlNifMutex *mtx) .br .RS .LP Same as \fBerl_drv_mutex_destroy\fR\&\&. .RE .LP .B void enif_mutex_lock(ErlNifMutex *mtx) .br .RS .LP Same as \fBerl_drv_mutex_lock\fR\&\&. .RE .LP .B int enif_mutex_trylock(ErlNifMutex *mtx) .br .RS .LP Same as \fBerl_drv_mutex_trylock\fR\&\&. .RE .LP .B void enif_mutex_unlock(ErlNifMutex *mtx) .br .RS .LP Same as \fBerl_drv_mutex_unlock\fR\&\&. .RE .LP .B ErlNifResourceType *enif_open_resource_type(ErlNifEnv* env, const char* module_str, const char* name, ErlNifResourceDtor* dtor, ErlNifResourceFlags flags, ErlNifResourceFlags* tried) .br .RS .LP Create or takeover a resource type identified by the string \fIname\fR\& and give it the destructor function pointed to by \fBdtor\fR\&\&. Argument \fIflags\fR\& can have the following values: .RS 2 .TP 2 .B \fIERL_NIF_RT_CREATE\fR\&: Create a new resource type that does not already exist\&. .TP 2 .B \fIERL_NIF_RT_TAKEOVER\fR\&: Open an existing resource type and take over ownership of all its instances\&. The supplied destructor \fIdtor\fR\& will be called both for existing instances as well as new instances not yet created by the calling NIF library\&. .RE .LP The two flag values can be combined with bitwise-or\&. The name of the resource type is local to the calling module\&. Argument \fImodule_str\fR\& is not (yet) used and must be NULL\&. The \fIdtor\fR\& may be \fINULL\fR\& in case no destructor is needed\&. .LP On success, return a pointer to the resource type and \fI*tried\fR\& will be set to either \fIERL_NIF_RT_CREATE\fR\& or \fIERL_NIF_RT_TAKEOVER\fR\& to indicate what was actually done\&. On failure, return \fINULL\fR\& and set \fI*tried\fR\& to \fIflags\fR\&\&. It is allowed to set \fItried\fR\& to \fINULL\fR\&\&. .LP Note that \fIenif_open_resource_type\fR\& is only allowed to be called in the three callbacks \fBload\fR\&, \fBreload\fR\& and \fBupgrade\fR\&\&. .RE .LP .B void *enif_priv_data(ErlNifEnv* env) .br .RS .LP Return the pointer to the private data that was set by \fIload\fR\&, \fIreload\fR\& or \fIupgrade\fR\&\&. .LP Was previously named \fIenif_get_data\fR\&\&. .RE .LP .B int enif_realloc_binary(ErlNifBinary* bin, size_t size) .br .RS .LP Change the size of a binary \fIbin\fR\&\&. The source binary may be read-only, in which case it will be left untouched and a mutable copy is allocated and assigned to \fI*bin\fR\&\&. Return true on success, false if memory allocation failed\&. .RE .LP .B void enif_release_binary(ErlNifBinary* bin) .br .RS .LP Release a binary obtained from \fIenif_alloc_binary\fR\&\&. .RE .LP .B void enif_release_resource(void* obj) .br .RS .LP Remove a reference to resource object \fIobj\fR\&obtained from \fBenif_alloc_resource\fR\&\&. The resource object will be destructed when the last reference is removed\&. Each call to \fIenif_release_resource\fR\& must correspond to a previous call to \fIenif_alloc_resource\fR\& or \fBenif_keep_resource\fR\&\&. References made by \fBenif_make_resource\fR\& can only be removed by the garbage collector\&. .RE .LP .B ErlNifRWLock *enif_rwlock_create(char *name) .br .RS .LP Same as \fBerl_drv_rwlock_create\fR\&\&. .RE .LP .B void enif_rwlock_destroy(ErlNifRWLock *rwlck) .br .RS .LP Same as \fBerl_drv_rwlock_destroy\fR\&\&. .RE .LP .B void enif_rwlock_rlock(ErlNifRWLock *rwlck) .br .RS .LP Same as \fBerl_drv_rwlock_rlock\fR\&\&. .RE .LP .B void enif_rwlock_runlock(ErlNifRWLock *rwlck) .br .RS .LP Same as \fBerl_drv_rwlock_runlock\fR\&\&. .RE .LP .B void enif_rwlock_rwlock(ErlNifRWLock *rwlck) .br .RS .LP Same as \fBerl_drv_rwlock_rwlock\fR\&\&. .RE .LP .B void enif_rwlock_rwunlock(ErlNifRWLock *rwlck) .br .RS .LP Same as \fBerl_drv_rwlock_rwunlock\fR\&\&. .RE .LP .B int enif_rwlock_tryrlock(ErlNifRWLock *rwlck) .br .RS .LP Same as \fBerl_drv_rwlock_tryrlock\fR\&\&. .RE .LP .B int enif_rwlock_tryrwlock(ErlNifRWLock *rwlck) .br .RS .LP Same as \fBerl_drv_rwlock_tryrwlock\fR\&\&. .RE .LP .B ERL_NIF_TERM enif_schedule_nif(ErlNifEnv* 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[]) .br .RS .LP Schedule NIF \fIfp\fR\& to execute\&. This function allows an application to break up long-running work into multiple regular NIF calls or to schedule a \fBdirty NIF\fR\& to execute on a dirty scheduler thread (\fInote that the dirty NIF functionality described here is experimental\fR\& and that you have to enable support for dirty schedulers when building OTP in order to try the functionality out)\&. .LP The \fIfun_name\fR\& argument provides a name for the NIF being scheduled for execution\&. If it cannot be converted to an atom, \fIenif_schedule_nif\fR\& returns a \fIbadarg\fR\& exception\&. .LP The \fIflags\fR\& argument must be set to 0 for a regular NIF, or if the emulator was built the experimental dirty scheduler support enabled, \fIflags\fR\& can be set to either \fIERL_NIF_DIRTY_JOB_CPU_BOUND\fR\& if the job is expected to be primarily CPU-bound, or \fIERL_NIF_DIRTY_JOB_IO_BOUND\fR\& for jobs that will be I/O-bound\&. If dirty scheduler threads are not available in the emulator, a try to schedule such a job will result in a \fIbadarg\fR\& exception\&. .LP The \fIargc\fR\& and \fIargv\fR\& arguments can either be the originals passed into the calling NIF, or they can be values created by the calling NIF\&. .LP The calling NIF must use the return value of \fIenif_schedule_nif\fR\& as its own return value\&. .LP Be aware that \fIenif_schedule_nif\fR\&, as its name 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 that the calling NIF can\&'t expect to receive the scheduled NIF return value and use it for further operations\&. .RE .LP .B ErlNifPid *enif_self(ErlNifEnv* caller_env, ErlNifPid* pid) .br .RS .LP Initialize the pid variable \fI*pid\fR\& to represent the calling process\&. Return \fIpid\fR\&\&. .RE .LP .B int enif_send(ErlNifEnv* env, ErlNifPid* to_pid, ErlNifEnv* msg_env, ERL_NIF_TERM msg) .br .RS .LP Send a message to a process\&. .RS 2 .TP 2 .B \fIenv\fR\&: The environment of the calling process\&. Must be NULL if and only if calling from a created thread\&. .TP 2 .B \fI*to_pid\fR\&: The pid of the receiving process\&. The pid should refer to a process on the local node\&. .TP 2 .B \fImsg_env\fR\&: The environment of the message term\&. Must be a process independent environment allocated with \fBenif_alloc_env\fR\&\&. .TP 2 .B \fImsg\fR\&: The message term to send\&. .RE .LP Return true on success, or false if \fI*to_pid\fR\& does not refer to an alive local process\&. .LP The message environment \fImsg_env\fR\& with all its terms (including \fImsg\fR\&) will be invalidated by a successful call to \fIenif_send\fR\&\&. The environment should either be freed with \fBenif_free_env\fR\& of cleared for reuse with \fBenif_clear_env\fR\&\&. .LP 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\&. .RE .LP .B unsigned enif_sizeof_resource(void* obj) .br .RS .LP Get the byte size of a resource object \fIobj\fR\& obtained by \fBenif_alloc_resource\fR\&\&. .RE .LP .B void enif_system_info(ErlNifSysInfo *sys_info_ptr, size_t size) .br .RS .LP Same as \fBdriver_system_info\fR\&\&. .RE .LP .B int enif_thread_create(char *name,ErlNifTid *tid,void * (*func)(void *),void *args,ErlNifThreadOpts *opts) .br .RS .LP Same as \fBerl_drv_thread_create\fR\&\&. .RE .LP .B void enif_thread_exit(void *resp) .br .RS .LP Same as \fBerl_drv_thread_exit\fR\&\&. .RE .LP .B int enif_thread_join(ErlNifTid, void **respp) .br .RS .LP Same as \fBerl_drv_thread_join \fR\&\&. .RE .LP .B ErlNifThreadOpts *enif_thread_opts_create(char *name) .br .RS .LP Same as \fBerl_drv_thread_opts_create\fR\&\&. .RE .LP .B void enif_thread_opts_destroy(ErlNifThreadOpts *opts) .br .RS .LP Same as \fBerl_drv_thread_opts_destroy\fR\&\&. .RE .LP .B ErlNifTid enif_thread_self(void) .br .RS .LP Same as \fBerl_drv_thread_self\fR\&\&. .RE .LP .B int enif_tsd_key_create(char *name, ErlNifTSDKey *key) .br .RS .LP Same as \fBerl_drv_tsd_key_create\fR\&\&. .RE .LP .B void enif_tsd_key_destroy(ErlNifTSDKey key) .br .RS .LP Same as \fBerl_drv_tsd_key_destroy\fR\&\&. .RE .LP .B void *enif_tsd_get(ErlNifTSDKey key) .br .RS .LP Same as \fBerl_drv_tsd_get\fR\&\&. .RE .LP .B void enif_tsd_set(ErlNifTSDKey key, void *data) .br .RS .LP Same as \fBerl_drv_tsd_set\fR\&\&. .RE .SH "SEE ALSO" .LP \fBerlang:load_nif/2\fR\&