void*enif_alloc(size_tsize)
Allocates memory of size bytes.
Returns NULL if the allocation fails.
The returned pointer is suitably aligned for any built-in type that fit in the allocated memory.
intenif_alloc_binary(size_tsize,ErlNifBinary*bin)
Allocates a new binary of size size bytes. Initializes 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.
If you do not need to reallocate or keep the data alive across NIF calls, consider using
enif_make_new_binary instead as it will allocate small binaries on the process heap when possible.
Returns true on success, or false if allocation fails.
ErlNifEnv*enif_alloc_env()
Allocates a new process independent environment. The environment can be used to hold terms that
are 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.
Returns pointer to the new environment.
void*enif_alloc_resource(ErlNifResourceType*
type, unsigned size)
Allocates a memory-managed resource object of type type and size size bytes.
size_tenif_binary_to_term(ErlNifEnv*env,
const unsigned char* data, size_t size, ERL_NIF_TERM *term,
ErlNifBinaryToTerm opts)
Creates a term that is the result of decoding the binary data at data, which must be encoded
according to the Erlang external term format. No more than size bytes are read from data. Argument
opts corresponds to the second argument to erlang:binary_to_term/2 and must be either 0 or
ERL_NIF_BIN2TERM_SAFE.
On success, stores the resulting term at *term and returns the number of bytes read. Returns 0 if
decoding fails or if opts is invalid.
See also ErlNifBinaryToTerm, erlang:binary_to_term/2, and enif_term_to_binary.
voidenif_clear_env(ErlNifEnv*env)
Frees all terms in an environment and clears it for reuse. The environment must have been
allocated with enif_alloc_env.
intenif_compare(ERL_NIF_TERMlhs,ERL_NIF_TERMrhs)
Returns an integer < 0 if lhs < rhs, 0 if lhs = rhs, and > 0 if lhs > rhs. Corresponds to the
Erlang operators ==, /=, =<, <, >=, and > (but not=:= or =/=).
intenif_compare_monitors(constErlNifMonitor
*monitor1, const ErlNifMonitor *monitor2)
Compares two ErlNifMonitors. Can also be used to imply some artificial order on monitors, for
whatever reason.
Returns 0 if monitor1 and monitor2 are equal, < 0 if monitor1 < monitor2, and > 0 if monitor1 >
monitor2.
intenif_compare_pids(constErlNifPid*pid1,constErlNifPid*pid2)
Compares two ErlNifPids according to term order.
Returns 0 if pid1 and pid2 are equal, < 0 if pid1 < pid2, and > 0 if pid1 > pid2.
voidenif_cond_broadcast(ErlNifCond*cnd)
Same as erl_drv_cond_broadcast.
ErlNifCond*enif_cond_create(char*name)
Same as erl_drv_cond_create.
voidenif_cond_destroy(ErlNifCond*cnd)
Same as erl_drv_cond_destroy.
char*enif_cond_name(ErlNifCond*cnd)
Same as erl_drv_cond_name.
voidenif_cond_signal(ErlNifCond*cnd)
Same as erl_drv_cond_signal.
voidenif_cond_wait(ErlNifCond*cnd,ErlNifMutex*mtx)
Same as erl_drv_cond_wait.
intenif_consume_timeslice(ErlNifEnv*env,intpercent)
Gives the runtime system a hint about how much CPU time the current NIF call has consumed since
the last hint, or since the start of the NIF if no previous hint has been specified. The time is
specified as a percent of the timeslice that a process is allowed to execute Erlang code until it
can 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.
Notice that it is up to the runtime system to determine if and how to use this information.
Implementations on some platforms can use other means to determine consumed CPU time. Lengthy NIFs
should regardless of this frequently call enif_consume_timeslice to determine if it is allowed to
continue execution.
Argument percent must be an integer between 1 and 100. This function must only be called from a
NIF-calling thread, and argument env must be the environment of the calling process.
Returns 1 if the timeslice is exhausted, otherwise 0. If 1 is returned, the NIF is to return as
soon as possible in order for the process to yield.
This function is provided to better support co-operative scheduling, improve system
responsiveness, and make it easier to prevent misbehaviors of the VM because of a NIF monopolizing
a scheduler thread. It can be used to divide length work into a number of repeated NIF calls
without the need to create threads.
See also the warning text at the beginning of this manual page.
ErlNifTimeenif_convert_time_unit(ErlNifTime
val, ErlNifTimeUnit from, ErlNifTimeUnit to)
Converts the val value of time unit from to the corresponding value of time unit to. The result is
rounded using the floor function.
val:
Value to convert time unit for.
from:
Time unit of val.
to:
Time unit of returned value.
Returns ERL_NIF_TIME_ERROR if called with an invalid time unit argument.
See also ErlNifTime and ErlNifTimeUnit.
ERL_NIF_TERMenif_cpu_time(ErlNifEnv*)
Returns the CPU time in the same format as erlang:timestamp(). The CPU time is the time the
current logical CPU has spent executing since some arbitrary point in the past. If the OS does not
support fetching this value, enif_cpu_time invokes enif_make_badarg.
intenif_demonitor_process(ErlNifEnv*caller_env,
void* obj, const ErlNifMonitor* mon)
Cancels a monitor created earlier with enif_monitor_process. Argument obj is a pointer to the
resource holding the monitor and *mon identifies the monitor.
Argument caller_env is the environment of the calling thread (process bound or callback
environment) or NULL if calling from a custom thread not spawned by ERTS.
Returns 0 if the monitor was successfully identified and removed. Returns a non-zero value if the
monitor could not be identified, which means it was either
* never created for this resource
* already cancelled
* already triggered
* just about to be triggered by a concurrent thread
This function is thread-safe.
intenif_dynamic_resource_call(ErlNifEnv*caller_env, ERL_NIF_MODULE rt_module, ERL_NIF_MODULE
rt_name, ERL_NIF_TERM resource, void* call_data)
Call code of a resource type implemented by another NIF module. The atoms rt_module and rt_name
identifies the resource type to be called. Argument resource identifies a resource object of that
type.
The callback dyncall of the identified resource type will be called with a pointer to the resource
objects obj and the argument call_data passed through. The call_data argument is typically a
pointer to a struct used to passed both arguments to the dyncall function as well as results back
to the caller.
Returns 0 if the dyncall callback function was called. Returns a non-zero value if no call was
made, which happens if rt_module and rt_name did not identify a resource type with a dyncall
callback or if resource was not a resource object of that type.
intenif_equal_tids(ErlNifTidtid1,ErlNifTidtid2)
Same as erl_drv_equal_tids.
intenif_fprintf(FILE*stream,constchar*format,...)
Similar to fprintf but this format string also accepts "%T", which formats Erlang terms of type
ERL_NIF_TERM.
This function is primarily intended for debugging purpose. It is not recommended to print very
large terms with %T. The function may change errno, even if successful.
voidenif_free(void*ptr)
Frees memory allocated by enif_alloc.
voidenif_free_env(ErlNifEnv*env)
Frees an environment allocated with enif_alloc_env. All terms created in the environment are freed
as well.
voidenif_free_iovec(ErlNifIOVec*iov)
Frees an io vector returned from enif_inspect_iovec. This is needed only if a NULL environment is
passed to enif_inspect_iovec.
ErlNifIOVec *iovec = NULL;
size_t max_elements = 128;
ERL_NIF_TERM tail;
if (!enif_inspect_iovec(NULL, max_elements, term, &tail, &iovec))
return 0;
// Do things with the iovec
/* Free the iovector, possibly in another thread or nif function call */
enif_free_iovec(iovec);
intenif_get_atom(ErlNifEnv*env,ERL_NIF_TERM
term, char* buf, unsigned size, ErlNifCharEncoding encode)
Writes 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.
Returns the number of bytes written (including terminating NULL character) or 0 if term is not an
atom with maximum length of size-1.
intenif_get_atom_length(ErlNifEnv*env,
ERL_NIF_TERM term, unsigned* len, ErlNifCharEncoding encode)
Sets *len to the length (number of bytes excluding terminating NULL character) of the atom term
with encoding encode.
Returns true on success, or false if term is not an atom.
intenif_get_double(ErlNifEnv*env,
ERL_NIF_TERM term, double* dp)
Sets *dp to the floating-point value of term.
Returns true on success, or false if term is not a float.
intenif_get_int(ErlNifEnv*env,ERL_NIF_TERM
term, int* ip)
Sets *ip to the integer value of term.
Returns true on success, or false if term is not an integer or is outside the bounds of type int.
intenif_get_int64(ErlNifEnv*env,ERL_NIF_TERM
term, ErlNifSInt64* ip)
Sets *ip to the integer value of term.
Returns true on success, or false if term is not an integer or is outside the bounds of a signed
64-bit integer.
intenif_get_local_pid(ErlNifEnv*env,
ERL_NIF_TERM term, ErlNifPid* pid)
If term is the pid of a node local process, this function initializes the pid variable *pid from
it and returns true. Otherwise returns false. No check is done to see if the process is alive.
Note:enif_get_local_pid will return false if argument term is the atom undefined.
intenif_get_local_port(ErlNifEnv*env,
ERL_NIF_TERM term, ErlNifPort* port_id)
If term identifies a node local port, this function initializes the port variable *port_id from it
and returns true. Otherwise returns false. No check is done to see if the port is alive.
intenif_get_list_cell(ErlNifEnv*env,
ERL_NIF_TERM list, ERL_NIF_TERM* head, ERL_NIF_TERM* tail)
Sets *head and *tail from list list.
Returns true on success, or false if it is not a list or the list is empty.
intenif_get_list_length(ErlNifEnv*env,
ERL_NIF_TERM term, unsigned* len)
Sets *len to the length of list term.
Returns true on success, or false if term is not a proper list.
intenif_get_long(ErlNifEnv*env,ERL_NIF_TERM
term, long int* ip)
Sets *ip to the long integer value of term.
Returns true on success, or false if term is not an integer or is outside the bounds of type longint.
intenif_get_map_size(ErlNifEnv*env,
ERL_NIF_TERM term, size_t *size)
Sets *size to the number of key-value pairs in the map term.
Returns true on success, or false if term is not a map.
intenif_get_map_value(ErlNifEnv*env,
ERL_NIF_TERM map, ERL_NIF_TERM key, ERL_NIF_TERM* value)
Sets *value to the value associated with key in the map map.
Returns true on success, or false if map is not a map or if map does not contain key.
intenif_get_resource(ErlNifEnv*env,
ERL_NIF_TERM term, ErlNifResourceType* type, void** objp)
Sets *objp to point to the resource object referred to by term.
Returns true on success, or false if term is not a handle to a resource object of type type.
enif_get_resource does not add a reference to the resource object. However, the pointer received
in *objp is guaranteed to be valid at least as long as the resource handle term is valid.
intenif_get_string(ErlNifEnv*env,
ERL_NIF_TERM list, char* buf, unsigned size,
ErlNifCharEncoding encode)
Writes 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.
Returns one of the following:
* The number of bytes written (including terminating NULL character)
* -size if the string was truncated because of buffer space
* 0 if list is not a string that can be encoded with encode or if size was < 1.
The written string is always NULL-terminated, unless buffer size is < 1.
intenif_get_tuple(ErlNifEnv*env,ERL_NIF_TERM
term, int* arity, const ERL_NIF_TERM** array)
If term is a tuple, this function sets *array to point to an array containing the elements of the
tuple, and sets *arity to the number of elements. Notice that the array is read-only and
(*array)[N-1] is the Nth element of the tuple. *array is undefined if the arity of the tuple is
zero.
Returns true on success, or false if term is not a tuple.
intenif_get_uint(ErlNifEnv*env,ERL_NIF_TERM
term, unsigned int* ip)
Sets *ip to the unsigned integer value of term.
Returns true on success, or false if term is not an unsigned integer or is outside the bounds of
type unsignedint.
intenif_get_uint64(ErlNifEnv*env,
ERL_NIF_TERM term, ErlNifUInt64* ip)
Sets *ip to the unsigned integer value of term.
Returns true on success, or false if term is not an unsigned integer or is outside the bounds of
an unsigned 64-bit integer.
intenif_get_ulong(ErlNifEnv*env,ERL_NIF_TERM
term, unsigned long* ip)
Sets *ip to the unsigned long integer value of term.
Returns true on success, or false if term is not an unsigned integer or is outside the bounds of
type unsignedlong.
intenif_getenv(constchar*key,char*value,
size_t *value_size)
Same as erl_drv_getenv.
intenif_has_pending_exception(ErlNifEnv*env,
ERL_NIF_TERM* reason)
Returns true if a pending exception is associated with the environment env. If reason is a NULL
pointer, ignore it. Otherwise, if a pending exception associated with env exists, set *reason to
the value of the exception term. For example, if enif_make_badarg is called to set a pending
badarg exception, a later call to enif_has_pending_exception(env,&reason) sets *reason to the
atom badarg, then return true.
See also enif_make_badarg and enif_raise_exception.
ErlNifUInt64enif_hash(ErlNifHashtype,ERL_NIF_TERMterm,ErlNifUInt64salt)
Hashes term according to the specified ErlNifHashtype.
Ranges of taken salt (if any) and returned value depend on the hash type.
intenif_inspect_binary(ErlNifEnv*env,
ERL_NIF_TERM bin_term, ErlNifBinary* bin)
Initializes the structure pointed to by bin with information about binary term bin_term.
Returns true on success, or false if bin_term is not a binary.
intenif_inspect_iolist_as_binary(ErlNifEnv*
env, ERL_NIF_TERM term, ErlNifBinary* bin)
Initializes the structure pointed to by bin with a 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.
Returns true on success, or false if iolist is not an iolist.
intenif_inspect_iovec(ErlNifEnv*
env, size_t max_elements, ERL_NIF_TERM iovec_term, ERL_NIF_TERM* tail,
ErlNifIOVec** iovec)
Fills iovec with the list of binaries provided in iovec_term. The number of elements handled in
the call is limited to max_elements, and tail is set to the remainder of the list. Note that the
output may be longer than max_elements on some platforms.
To create a list of binaries from an arbitrary iolist, use erlang:iolist_to_iovec/1.
When calling this function, iovec should contain a pointer to NULL or a ErlNifIOVec structure that
should be used if possible. e.g.
/* Don't use a pre-allocated structure */
ErlNifIOVec *iovec = NULL;
enif_inspect_iovec(env, max_elements, term, &tail, &iovec);
/* Use a stack-allocated vector as an optimization for vectors with few elements */
ErlNifIOVec vec, *iovec = &vec;
enif_inspect_iovec(env, max_elements, term, &tail, &iovec);
The contents of the iovec is valid until the called nif function returns. If the iovec should be
valid after the nif call returns, it is possible to call this function with a NULL environment. If
no environment is given the iovec owns the data in the vector and it has to be explicitly freed
using enif_free_iovec.
Returns true on success, or false if iovec_term not an iovec.
ErlNifIOQueue*enif_ioq_create(ErlNifIOQueueOptsopts)
Create a new I/O Queue that can be used to store data. opts has to be set to ERL_NIF_IOQ_NORMAL.
voidenif_ioq_destroy(ErlNifIOQueue*q)
Destroy the I/O queue and free all of it's contents
intenif_ioq_deq(ErlNifIOQueue*q,size_tcount,size_t*size)
Dequeue count bytes from the I/O queue. If size is not NULL, the new size of the queue is placed
there.
Returns true on success, or false if the I/O does not contain count bytes. On failure the queue is
left un-altered.
intenif_ioq_enq_binary(ErlNifIOQueue*q,ErlNifBinary*bin,size_tskip)
Enqueue the bin into q skipping the first skip bytes.
Returns true on success, or false if skip is greater than the size of bin. Any ownership of the
binary data is transferred to the queue and bin is to be considered read-only for the rest of the
NIF call and then as released.
intenif_ioq_enqv(ErlNifIOQueue*q,ErlNifIOVec*iovec,size_tskip)
Enqueue the iovec into q skipping the first skip bytes.
Returns true on success, or false if skip is greater than the size of iovec.
SysIOVec*enif_ioq_peek(ErlNifIOQueue*q,int*iovlen)
Get the I/O queue as a pointer to an array of SysIOVecs. It also returns the number of elements in
iovlen.
Nothing is removed from the queue by this function, that must be done with enif_ioq_deq.
The returned array is suitable to use with the Unix system call writev.
intenif_ioq_peek_head(ErlNifEnv*env,ErlNifIOQueue*q,size_t*size,ERL_NIF_TERM*bin_term)
Get the head of the IO Queue as a binary term.
If size is not NULL, the size of the head is placed there.
Nothing is removed from the queue by this function, that must be done with enif_ioq_deq.
Returns true on success, or false if the queue is empty.
size_tenif_ioq_size(ErlNifIOQueue*q)
Get the size of q.
intenif_is_atom(ErlNifEnv*env,ERL_NIF_TERMterm)
Returns true if term is an atom.
intenif_is_binary(ErlNifEnv*env,ERL_NIF_TERMterm)
Returns true if term is a binary.
intenif_is_current_process_alive(ErlNifEnv*env)
Returns true if the currently executing process is currently alive, otherwise false.
This function can only be used from a NIF-calling thread, and with an environment corresponding to
currently executing processes.
intenif_is_empty_list(ErlNifEnv*env,
ERL_NIF_TERM term)
Returns true if term is an empty list.
intenif_is_exception(ErlNifEnv*env,
ERL_NIF_TERM term)
Return true if term is an exception.
intenif_is_fun(ErlNifEnv*env,ERL_NIF_TERM
term)
Returns true if term is a fun.
intenif_is_identical(ERL_NIF_TERMlhs,
ERL_NIF_TERM rhs)
Returns true if the two terms are identical. Corresponds to the Erlang operators =:= and =/=.
intenif_is_list(ErlNifEnv*env,ERL_NIF_TERMterm)
Returns true if term is a list.
intenif_is_map(ErlNifEnv*env,ERL_NIF_TERM
term)
Returns true if term is a map, otherwise false.
intenif_is_number(ErlNifEnv*env,ERL_NIF_TERM
term)
Returns true if term is a number.
intenif_is_pid(ErlNifEnv*env,ERL_NIF_TERMterm)
Returns true if term is a pid.
intenif_is_pid_undefined(constErlNifPid*pid)
Returns true if pid has been set as undefined by enif_set_pid_undefined.
intenif_is_port(ErlNifEnv*env,ERL_NIF_TERMterm)
Returns true if term is a port.
intenif_is_port_alive(ErlNifEnv*env,
ErlNifPort *port_id)
Returns true if port_id is alive.
This function is thread-safe.
intenif_is_process_alive(ErlNifEnv*env,
ErlNifPid *pid)
Returns true if pid is alive.
This function is thread-safe.
intenif_is_ref(ErlNifEnv*env,ERL_NIF_TERMterm)
Returns true if term is a reference.
intenif_is_tuple(ErlNifEnv*env,ERL_NIF_TERMterm)
Returns true if term is a tuple.
intenif_keep_resource(void*obj)
Adds 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 is destructed.
ERL_NIF_TERMenif_make_atom(ErlNifEnv*env,constchar*name)
Creates an atom term from the NULL-terminated C-string name with ISO Latin-1 encoding. If the
length of name exceeds the maximum length allowed for an atom (255 characters), enif_make_atom
invokes enif_make_badarg.
ERL_NIF_TERMenif_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. If len exceeds the maximum length allowed for an atom (255 characters), enif_make_atom
invokes enif_make_badarg.
ERL_NIF_TERMenif_make_badarg(ErlNifEnv*env)
Makes a badarg exception to be returned from a NIF, and associates it with environment env. Once a
NIF or any function it calls invokes enif_make_badarg, the runtime ensures that a badarg exception
is raised when the NIF returns, even if the NIF attempts to return a non-exception term instead.
The return value from enif_make_badarg can be used only as the return value from the NIF that
invoked it (directly or indirectly) or be passed to enif_is_exception, but not to any other NIF
API function.
See also enif_has_pending_exception and enif_raise_exception.
Note:
Before ERTS 7.0 (Erlang/OTP 18), the return value from enif_make_badarg had to be returned from
the NIF. This requirement is now lifted as the return value from the NIF is ignored if
enif_make_badarg has been invoked.
ERL_NIF_TERMenif_make_binary(ErlNifEnv*env,ErlNifBinary*bin)
Makes a binary term from bin. Any ownership of the binary data is transferred to the created term
and bin is to be considered read-only for the rest of the NIF call and then as released.
ERL_NIF_TERMenif_make_copy(ErlNifEnv*dst_env,
ERL_NIF_TERM src_term)
Makes a copy of term src_term. The copy is created in environment dst_env. The source term can be
located in any environment.
ERL_NIF_TERMenif_make_double(ErlNifEnv*env,doubled)
Creates a floating-point term from a double. If argument double is not finite or is NaN,
enif_make_double invokes enif_make_badarg.
intenif_make_existing_atom(ErlNifEnv*env,
const char* name, ERL_NIF_TERM* atom, ErlNifCharEncoding
encode)
Tries to create the term of an already existing atom from the NULL-terminated C-string name with
encoding encode.
If the atom already exists, this function stores the term in *atom and returns true, otherwise
false. Also returns false if the length of name exceeds the maximum length allowed for an atom
(255 characters).
intenif_make_existing_atom_len(ErlNifEnv*env,
const char* name, size_t len, ERL_NIF_TERM* atom, ErlNifCharEncoding
encoding)
Tries 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, this function stores the term in *atom and returns true, otherwise
false. Also returns false if len exceeds the maximum length allowed for an atom (255 characters).
ERL_NIF_TERMenif_make_int(ErlNifEnv*env,inti)
Creates an integer term.
ERL_NIF_TERMenif_make_int64(ErlNifEnv*env,ErlNifSInt64i)
Creates an integer term from a signed 64-bit integer.
ERL_NIF_TERMenif_make_list(ErlNifEnv*env,unsignedcnt,...)
Creates 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.
Returns an empty list if cnt is 0.
ERL_NIF_TERMenif_make_list1(ErlNifEnv*env,ERL_NIF_TERMe1)ERL_NIF_TERMenif_make_list2(ErlNifEnv*env,
ERL_NIF_TERM e1, ERL_NIF_TERM e2)
ERL_NIF_TERMenif_make_list3(ErlNifEnv*env,
ERL_NIF_TERM e1, ERL_NIF_TERM e2, ERL_NIF_TERM e3)
ERL_NIF_TERMenif_make_list4(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e4)
ERL_NIF_TERMenif_make_list5(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e5)
ERL_NIF_TERMenif_make_list6(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e6)
ERL_NIF_TERMenif_make_list7(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e7)
ERL_NIF_TERMenif_make_list8(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e8)
ERL_NIF_TERMenif_make_list9(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9)
Creates 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_TERMenif_make_list_cell(ErlNifEnv*
env, ERL_NIF_TERM head, ERL_NIF_TERM tail)
Creates a list cell [head|tail].
ERL_NIF_TERMenif_make_list_from_array(ErlNifEnv*env,constERL_NIF_TERM
arr[], unsigned cnt)
Creates an ordinary list containing the elements of array arr of length cnt.
Returns an empty list if cnt is 0.
ERL_NIF_TERMenif_make_long(ErlNifEnv*env,longinti)
Creates an integer term from a longint.
intenif_make_map_put(ErlNifEnv*env,
ERL_NIF_TERM map_in, ERL_NIF_TERM key, ERL_NIF_TERM value,
ERL_NIF_TERM* map_out)
Makes a copy of map map_in and inserts key with value. If key already exists in map_in, the old
associated value is replaced by value.
If successful, this function sets *map_out to the new map and returns true. Returns false if
map_in is not a map.
The map_in term must belong to environment env.
intenif_make_map_remove(ErlNifEnv*env,
ERL_NIF_TERM map_in, ERL_NIF_TERM key, ERL_NIF_TERM* map_out)
If map map_in contains key, this function makes a copy of map_in in *map_out, and removes key and
the associated value. If map map_in does not contain key, *map_out is set to map_in.
Returns true on success, or false if map_in is not a map.
The map_in term must belong to environment env.
intenif_make_map_update(ErlNifEnv*env,
ERL_NIF_TERM map_in, ERL_NIF_TERM key, ERL_NIF_TERM new_value,
ERL_NIF_TERM* map_out)
Makes a copy of map map_in and replace the old associated value for key with new_value.
If successful, this function sets *map_out to the new map and returns true. Returns false if
map_in is not a map or if it does not contain key.
The map_in term must belong to environment env.
intenif_make_map_from_arrays(ErlNifEnv*env,ERL_NIF_TERMkeys[],
ERL_NIF_TERM values[], size_t cnt, ERL_NIF_TERM *map_out)
Makes a map term from the given keys and values.
If successful, this function sets *map_out to the new map and returns true. Returns false there
are any duplicate keys.
All keys and values must belong to env.
ERL_NIF_TERMenif_make_monitor_term(ErlNifEnv*env,constErlNifMonitor*mon)
Creates a term identifying the given monitor received from enif_monitor_process.
This function is primarily intended for debugging purpose.
unsignedchar*enif_make_new_binary(ErlNifEnv*
env, size_t size, ERL_NIF_TERM* termp)
Allocates a binary of size size bytes and creates 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 cannot be kept between NIF calls and it cannot be
reallocated.
Returns a pointer to the raw binary data and sets *termp to the binary term.
ERL_NIF_TERMenif_make_new_map(ErlNifEnv*env)
Makes an empty map term.
ERL_NIF_TERMenif_make_pid(ErlNifEnv*env,constErlNifPid*pid)
Makes a pid term or the atom undefined from *pid.
ERL_NIF_TERMenif_make_ref(ErlNifEnv*env)
Creates a reference like erlang:make_ref/0.
ERL_NIF_TERMenif_make_resource(ErlNifEnv*env,void*obj)
Creates 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. However, notice that the call to enif_release_resource can occur
immediately after obtaining the term from enif_make_resource, in which case the resource object is
deallocated when the term is garbage collected. For more details, see the example of creating and
returning a resource object in the User's Guide.
Note:
Since ERTS 9.0 (OTP-20.0), resource terms have a defined behavior when compared and serialized
through term_to_binary or passed between nodes.
* Two resource terms will compare equal if and only if they would yield the same resource object
pointer when passed to enif_get_resource.
* A resource term can be serialized with term_to_binary and later be fully recreated if the
resource object is still alive when binary_to_term is called. A stale resource term will be
returned from binary_to_term if the resource object has been deallocated. enif_get_resource
will return false for stale resource terms.
The same principles of serialization apply when passing resource terms in messages to remote
nodes and back again. A resource term will act stale on all nodes except the node where its
resource object is still alive in memory.
Before ERTS 9.0 (OTP-20.0), all resource terms did compare equal to each other and to empty
binaries (<<>>). If serialized, they would be recreated as plain empty binaries.
ERL_NIF_TERMenif_make_resource_binary(ErlNifEnv*env,void*obj,const
void* data, size_t size)
Creates a binary term that is memory-managed by a resource object obj obtained by
enif_alloc_resource. The returned binary term consists 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 can be stored external to the resource object, in which case the destructor is
responsible for releasing the data.
Several binary terms can be managed by the same resource object. The destructor is not called
until the last binary is garbage collected. This can be useful 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.
intenif_make_reverse_list(ErlNifEnv*env,ERL_NIF_TERMlist_in,
ERL_NIF_TERM *list_out)
Sets *list_out to the reverse list of the list list_in and returns true, or returns false if
list_in is not a list.
This function is only to be used on short lists, as a copy is created of the list, which is not
released until after the NIF returns.
The list_in term must belong to environment env.
ERL_NIF_TERMenif_make_string(ErlNifEnv*env,
const char* string, ErlNifCharEncoding encoding)
Creates a list containing the characters of the NULL-terminated string string with encoding
encoding.
ERL_NIF_TERMenif_make_string_len(ErlNifEnv*
env, const char* string, size_t len, ErlNifCharEncoding
encoding)
Creates 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_TERMenif_make_sub_binary(ErlNifEnv*
env, ERL_NIF_TERM bin_term, size_t pos, size_t size)
Makes 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. pos+size must be less or equal to the number of
whole bytes in bin_term.
ERL_NIF_TERMenif_make_tuple(ErlNifEnv*env,
unsigned cnt, ...)
Creates 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_TERMenif_make_tuple1(ErlNifEnv*env,
ERL_NIF_TERM e1)
ERL_NIF_TERMenif_make_tuple2(ErlNifEnv*env,
ERL_NIF_TERM e1, ERL_NIF_TERM e2)
ERL_NIF_TERMenif_make_tuple3(ErlNifEnv*env,
ERL_NIF_TERM e1, ERL_NIF_TERM e2, ERL_NIF_TERM e3)
ERL_NIF_TERMenif_make_tuple4(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e4)
ERL_NIF_TERMenif_make_tuple5(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e5)
ERL_NIF_TERMenif_make_tuple6(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e6)
ERL_NIF_TERMenif_make_tuple7(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e7)
ERL_NIF_TERMenif_make_tuple8(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e8)
ERL_NIF_TERMenif_make_tuple9(ErlNifEnv*env,
ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9)
Creates 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_TERMenif_make_tuple_from_array(ErlNifEnv*env,constERL_NIF_TERM
arr[], unsigned cnt)
Creates a tuple containing the elements of array arr of length cnt.
ERL_NIF_TERMenif_make_uint(ErlNifEnv*env,unsignedinti)
Creates an integer term from an unsignedint.
ERL_NIF_TERMenif_make_uint64(ErlNifEnv*env,ErlNifUInt64i)
Creates an integer term from an unsigned 64-bit integer.
ERL_NIF_TERMenif_make_ulong(ErlNifEnv*env,unsignedlongi)
Creates an integer term from an unsignedlongint.
ERL_NIF_TERMenif_make_unique_integer(ErlNifEnv
*env, ErlNifUniqueInteger properties)
Returns a unique integer with the same properties as specified by erlang:unique_integer/1.
env is the environment to create the integer in.
ERL_NIF_UNIQUE_POSITIVE and ERL_NIF_UNIQUE_MONOTONIC can be passed as the second argument to
change the properties of the integer returned. They can be combined by OR:ing the two values
together.
See also ErlNifUniqueInteger.
intenif_map_iterator_create(ErlNifEnv*env,
ERL_NIF_TERM map, ErlNifMapIterator *iter, ErlNifMapIteratorEntry
entry)
Creates an iterator for the map map by initializing the structure pointed to by iter. Argument
entry determines the start position of the iterator: ERL_NIF_MAP_ITERATOR_FIRST or
ERL_NIF_MAP_ITERATOR_LAST.
Returns true on success, or false if map is not a map.
A map iterator is only useful during the lifetime of environment env that the map belongs to. The
iterator must be destroyed by calling enif_map_iterator_destroy:
ERL_NIF_TERM key, value;
ErlNifMapIterator iter;
enif_map_iterator_create(env, my_map, &iter, ERL_NIF_MAP_ITERATOR_FIRST);
while (enif_map_iterator_get_pair(env, &iter, &key, &value)) {
do_something(key,value);
enif_map_iterator_next(env, &iter);
}
enif_map_iterator_destroy(env, &iter);
Note:
The key-value pairs of a map have no defined iteration order. The only guarantee is that the
iteration order of a single map instance is preserved during the lifetime of the environment that
the map belongs to.
voidenif_map_iterator_destroy(ErlNifEnv*env,
ErlNifMapIterator *iter)
Destroys a map iterator created by enif_map_iterator_create.
intenif_map_iterator_get_pair(ErlNifEnv*env,
ErlNifMapIterator *iter, ERL_NIF_TERM *key, ERL_NIF_TERM
*value)
Gets key and value terms at the current map iterator position.
On success, sets *key and *value and returns true. Returns false if the iterator is positioned at
head (before first entry) or tail (beyond last entry).
intenif_map_iterator_is_head(ErlNifEnv*env,
ErlNifMapIterator *iter)
Returns true if map iterator iter is positioned before the first entry.
intenif_map_iterator_is_tail(ErlNifEnv*env,
ErlNifMapIterator *iter)
Returns true if map iterator iter is positioned after the last entry.
intenif_map_iterator_next(ErlNifEnv*env,
ErlNifMapIterator *iter)
Increments map iterator to point to the next key-value entry.
Returns true if the iterator is now positioned at a valid key-value entry, or false if the
iterator is positioned at the tail (beyond the last entry).
intenif_map_iterator_prev(ErlNifEnv*env,
ErlNifMapIterator *iter)
Decrements map iterator to point to the previous key-value entry.
Returns true if the iterator is now positioned at a valid key-value entry, or false if the
iterator is positioned at the head (before the first entry).
intenif_monitor_process(ErlNifEnv*caller_env,
void* obj, const ErlNifPid* target_pid, ErlNifMonitor* mon)
Starts monitoring a process from a resource. When a process is monitored, a process exit results
in a call to the provided down callback associated with the resource type.
Argument obj is pointer to the resource to hold the monitor and *target_pid identifies the local
process to be monitored.
If mon is not NULL, a successful call stores the identity of the monitor in the ErlNifMonitor
struct pointed to by mon. This identifier is used to refer to the monitor for later removal with
enif_demonitor_process or compare with enif_compare_monitors. A monitor is automatically removed
when it triggers or when the resource is deallocated.
Argument caller_env is the environment of the calling thread (process bound or callback
environment) or NULL if calling from a custom thread not spawned by ERTS.
Returns 0 on success, < 0 if no down callback is provided, and > 0 if the process is no longer
alive or if target_pid is undefined.
This function is thread-safe.
ErlNifTimeenif_monotonic_time(ErlNifTimeUnittime_unit)
Returns the current Erlang monotonic time. Notice that it is not uncommon with negative values.
time_unit is the time unit of the returned value.
Returns ERL_NIF_TIME_ERROR if called with an invalid time unit argument, or if called from a
thread that is not a scheduler thread.
See also ErlNifTime and ErlNifTimeUnit.
ErlNifMutex*enif_mutex_create(char*name)
Same as erl_drv_mutex_create.
voidenif_mutex_destroy(ErlNifMutex*mtx)
Same as erl_drv_mutex_destroy.
voidenif_mutex_lock(ErlNifMutex*mtx)
Same as erl_drv_mutex_lock.
char*enif_mutex_name(ErlNifMutex*mtx)
Same as erl_drv_mutex_name.
intenif_mutex_trylock(ErlNifMutex*mtx)
Same as erl_drv_mutex_trylock.
voidenif_mutex_unlock(ErlNifMutex*mtx)
Same as erl_drv_mutex_unlock.
ERL_NIF_TERMenif_now_time(ErlNifEnv*env)
Returns an erlang:now() time stamp.
Thisfunctionisdeprecated.ErlNifResourceType*enif_open_resource_type(ErlNifEnv*env,constchar*
module_str, const char* name, ErlNifResourceDtor* dtor,
ErlNifResourceFlags flags, ErlNifResourceFlags* tried)
Creates or takes over a resource type identified by the string name and gives it the destructor
function pointed to by dtor. Argument flags can have the following values:
ERL_NIF_RT_CREATE:
Creates a new resource type that does not already exist.
ERL_NIF_RT_TAKEOVER:
Opens an existing resource type and takes over ownership of all its instances. The supplied
destructor dtor is called both for existing instances and new instances not yet created by the
calling NIF library.
The two flag values can be combined with bitwise OR. The resource type name is local to the
calling module. Argument module_str is not (yet) used and must be NULL. dtor can be NULL if no
destructor is needed.
On success, the function returns a pointer to the resource type and *tried is set to either
ERL_NIF_RT_CREATE or ERL_NIF_RT_TAKEOVER to indicate what was done. On failure, returns NULL and
sets *tried to flags. It is allowed to set tried to NULL.
Notice that enif_open_resource_type is only allowed to be called in the two callbacks load and
upgrade.
See also enif_open_resource_type_x.
ErlNifResourceType*enif_open_resource_type_x(ErlNifEnv*env,constchar*name, const
ErlNifResourceTypeInit* init,
ErlNifResourceFlags flags, ErlNifResourceFlags* tried)
Same as enif_open_resource_type except it accepts additional callback functions for resource types
that are used together with enif_select and enif_monitor_process.
Argument init is a pointer to an ErlNifResourceTypeInit structure that contains the function
pointers for destructor, down and stop callbacks for the resource type.
Note:
Only members dtor, down and stop in ErlNifResourceTypeInit are read by enif_open_resource_type_x.
To implement the new dyncall callback use enif_init_resource_type.
ErlNifResourceType*enif_init_resource_type(ErlNifEnv*env,constchar*name, const
ErlNifResourceTypeInit* init,
ErlNifResourceFlags flags, ErlNifResourceFlags* tried)
Same as enif_open_resource_type_x except it accepts an additional callback function for resource
types that are used together with enif_dynamic_resource_call.
Argument init is a pointer to an ErlNifResourceTypeInit structure that contains the callback
function pointers dtor, down, stop and the new dyncall. The struct also contains the field members
that must be set to the number of initialized callbacks counted from the top of the struct. For
example, to initialize all callbacks including dyncall, members should be set to 4. All callbacks
are optional and may be set to NULL.
intenif_port_command(ErlNifEnv*env,const
ErlNifPort* to_port, ErlNifEnv *msg_env, ERL_NIF_TERM msg)
Works as erlang:port_command/2, except that it is always completely asynchronous.
env:
The environment of the calling process. Must not be NULL.
*to_port:
The port ID of the receiving port. The port ID is to refer to a port on the local node.
msg_env:
The environment of the message term. Can be a process independent environment allocated with
enif_alloc_env or NULL.
msg:
The message term to send. The same limitations apply as on the payload to
erlang:port_command/2.
Using a msg_env of NULL is an optimization, which groups together calls to enif_alloc_env,
enif_make_copy, enif_port_command, and enif_free_env into one call. This optimization is only
useful when a majority of the terms are to be copied from env to msg_env.
Returns true if the command is successfully sent. Returns false if the command fails, for example:
* *to_port does not refer to a local port.
* The currently executing process (that is, the sender) is not alive.
* msg is invalid.
See also enif_get_local_port.
void*enif_priv_data(ErlNifEnv*env)
Returns the pointer to the private data that was set by load or upgrade.
ERL_NIF_TERMenif_raise_exception(ErlNifEnv*
env, ERL_NIF_TERM reason)
Creates an error exception with the term reason to be returned from a NIF, and associates it with
environment env. Once a NIF or any function it calls invokes enif_raise_exception, the runtime
ensures that the exception it creates is raised when the NIF returns, even if the NIF attempts to
return a non-exception term instead.
The return value from enif_raise_exception can only be used as the return value from the NIF that
invoked it (directly or indirectly) or be passed to enif_is_exception, but not to any other NIF
API function.
See also enif_has_pending_exception and enif_make_badarg.
void*enif_realloc(void*ptr,size_tsize)
Reallocates memory allocated by enif_alloc to size bytes.
Returns NULL if the reallocation fails.
The returned pointer is suitably aligned for any built-in type that fit in the allocated memory.
intenif_realloc_binary(ErlNifBinary*bin,size_tsize)
Changes the size of a binary bin. The source binary can be read-only, in which case it is left
untouched and a mutable copy is allocated and assigned to *bin.
Returns true on success, or false if memory allocation failed.
voidenif_release_binary(ErlNifBinary*bin)
Releases a binary obtained from enif_alloc_binary.
voidenif_release_resource(void*obj)
Removes a reference to resource object obj obtained from enif_alloc_resource. The resource object
is 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.
There are no guarantees exactly when the destructor of an unreferenced resource is called. It
could be called directly by enif_release_resource but it could also be scheduled to be called at a
later time possibly by another thread.
ErlNifRWLock*enif_rwlock_create(char*name)
Same as erl_drv_rwlock_create.
voidenif_rwlock_destroy(ErlNifRWLock*rwlck)
Same as erl_drv_rwlock_destroy.
char*enif_rwlock_name(ErlNifRWLock*rwlck)
Same as erl_drv_rwlock_name.
voidenif_rwlock_rlock(ErlNifRWLock*rwlck)
Same as erl_drv_rwlock_rlock.
voidenif_rwlock_runlock(ErlNifRWLock*rwlck)
Same as erl_drv_rwlock_runlock.
voidenif_rwlock_rwlock(ErlNifRWLock*rwlck)
Same as erl_drv_rwlock_rwlock.
voidenif_rwlock_rwunlock(ErlNifRWLock*rwlck)
Same as erl_drv_rwlock_rwunlock.
intenif_rwlock_tryrlock(ErlNifRWLock*rwlck)
Same as erl_drv_rwlock_tryrlock.
intenif_rwlock_tryrwlock(ErlNifRWLock*rwlck)
Same as erl_drv_rwlock_tryrwlock.
ERL_NIF_TERMenif_schedule_nif(
ErlNifEnv* caller_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[])
Schedules NIF fp to execute. This function allows an application to break up long-running work
into multiple regular NIF calls or to schedule a dirty NIF to execute on a dirty scheduler
thread.
caller_env:
Must be process bound environment of the calling NIF.
fun_name:
Provides a name for the NIF that is scheduled for execution. If it cannot be converted to an
atom, enif_schedule_nif returns a badarg exception.
flags:
Must be set to 0 for a regular NIF. If the emulator was built with dirty scheduler support
enabled, flags can be set to either ERL_NIF_DIRTY_JOB_CPU_BOUND if the job is expected to be
CPU-bound, or ERL_NIF_DIRTY_JOB_IO_BOUND for jobs that will be I/O-bound. If dirty scheduler
threads are not available in the emulator, an attempt to schedule such a job results in a
notsup exception.
argc and argv:
Can either be the originals passed into the calling NIF, or can be values created by the
calling NIF.
The calling NIF must use the return value of enif_schedule_nif as its own return value.
Be aware that enif_schedule_nif, 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. This means
that the calling NIF cannot expect to receive the scheduled NIF return value and use it for
further operations.
intenif_select(ErlNifEnv*env,ErlNifEventevent,enumErlNifSelectFlagsmode, void* obj, const
ErlNifPid* pid, ERL_NIF_TERM ref)
This function can be used to receive asynchronous notifications when OS-specific event objects
become ready for either read or write operations.
Argument event identifies the event object. On Unix systems, the functions select/poll are used.
The event object must be a socket, pipe or other file descriptor object that select/poll can use.
Argument mode describes the type of events to wait for. It can be ERL_NIF_SELECT_READ,
ERL_NIF_SELECT_WRITE or a bitwise OR combination to wait for both. It can also be
ERL_NIF_SELECT_STOP or ERL_NIF_SELECT_CANCEL which are described further below. When a read or
write event is triggered, a notification message like this is sent to the process identified by
pid:
{select, Obj, Ref, ready_input | ready_output}
ready_input or ready_output indicates if the event object is ready for reading or writing.
Note:
For complete control over the message format use the newer functions enif_select_read or
enif_select_write introduced in erts-11.0 (OTP-22.0).
Argument pid may be NULL to indicate the calling process. It must not be set as undefined.
Argument obj is a resource object obtained from enif_alloc_resource. The purpose of the resource
objects is as a container of the event object to manage its state and lifetime. A handle to the
resource is received in the notification message as Obj.
Argument ref must be either a reference obtained from erlang:make_ref/0 or the atom undefined. It
will be passed as Ref in the notifications. If a selective receive statement is used to wait for
the notification then a reference created just before the receive will exploit a runtime
optimization that bypasses all earlier received messages in the queue.
The notifications are one-shot only. To receive further notifications of the same type (read or
write), repeated calls to enif_select must be made after receiving each notification.
ERL_NIF_SELECT_CANCEL can be used to cancel previously selected events. It must be used in a
bitwise OR combination with ERL_NIF_SELECT_READ and/or ERL_NIF_SELECT_WRITE to indicate which type
of event to cancel. Arguments pid and ref are ignored when ERL_NIF_SELECT_CANCEL is specified. The
return value will tell if the event was actualy cancelled or if a notification may already have
been sent.
Use ERL_NIF_SELECT_STOP as mode in order to safely close an event object that has been passed to
enif_select. The stop callback of the resource obj will be called when it is safe to close the
event object. This safe way of closing event objects must be used even if all notifications have
been received (or cancelled) and no further calls to enif_select have been made.
ERL_NIF_SELECT_STOP will first cancel any selected events before it calls or schedules the stop
callback. Arguments pid and ref are ignored when ERL_NIF_SELECT_STOP is specified.
The first call to enif_select for a specific OS event will establish a relation between the event
object and the containing resource. All subsequent calls for an event must pass its containing
resource as argument obj. The relation is dissolved when enif_select has been called with mode as
ERL_NIF_SELECT_STOP and the corresponding stop callback has returned. A resource can contain
several event objects but one event object can only be contained within one resource. A resource
will not be destructed until all its contained relations have been dissolved.
Note:
Use enif_monitor_process together with enif_select to detect failing Erlang processes and prevent
them from causing permanent leakage of resources and their contained OS event objects.
Returns a non-negative value on success where the following bits can be set:
ERL_NIF_SELECT_STOP_CALLED:
The stop callback was called directly by enif_select.
ERL_NIF_SELECT_STOP_SCHEDULED:
The stop callback was scheduled to run on some other thread or later by this thread.
ERL_NIF_SELECT_READ_CANCELLED:
A read event was cancelled by ERL_NIF_SELECT_CANCEL or ERL_NIF_SELECT_STOP and is guaranteed
not to generate a ready_input notification message.
ERL_NIF_SELECT_WRITE_CANCELLED:
A write event was cancelled by ERL_NIF_SELECT_CANCEL or ERL_NIF_SELECT_STOP and is guaranteed
not to generate a ready_output notification message.
Returns a negative value if the call failed where the following bits can be set:
ERL_NIF_SELECT_INVALID_EVENT:
Argument event is not a valid OS event object.
ERL_NIF_SELECT_FAILED:
The system call failed to add the event object to the poll set.
Note:
Use bitwise AND to test for specific bits in the return value. New significant bits may be added
in future releases to give more detailed information for both failed and successful calls. Do NOT
use equality tests like ==, as that may cause your application to stop working.
Example:
retval = enif_select(env, fd, ERL_NIF_SELECT_STOP, resource, ref);
if (retval < 0) {
/* handle error */
}
/* Success! */
if (retval & ERL_NIF_SELECT_STOP_CALLED) {
/* ... */
}
Note:
The mode flag ERL_NIF_SELECT_CANCEL and the return flags ERL_NIF_SELECT_READ_CANCELLED and
ERL_NIF_SELECT_WRITE_CANCELLED were introduced in erts-11.0 (OTP-22.0).
intenif_select_read(ErlNifEnv*env,ErlNifEventevent,void*obj,
const ErlNifPid* pid, ERL_NIF_TERM msg, ErlNifEnv* msg_env)
intenif_select_write(ErlNifEnv*env,ErlNifEventevent,void*obj,
const ErlNifPid* pid, ERL_NIF_TERM msg, ErlNifEnv* msg_env)
These are variants of enif_select where you can supply your own message term msg that will be sent
to the process instead of the predefined tuple {select,_,_,_}.
Argument msg_env must either be NULL or the environment of msg allocated with enif_alloc_env. If
argument msg_env is NULL the term msg will be copied, otherwise both msg and msg_env will be
invalidated by a successful call to enif_select_read or enif_select_write. The environment is then
to either be freed with enif_free_env or cleared for reuse with enif_clear_env. An unsuccessful
call will leave msg and msg_env still valid.
Apart from the message format enif_select_read and enif_select_write behaves exactly the same as
enif_select with argument mode as either ERL_NIF_SELECT_READ or ERL_NIF_SELECT_WRITE. To cancel or
close events use enif_select.
ErlNifPid*enif_self(ErlNifEnv*caller_env,ErlNifPid*pid)
Initializes the ErlNifPid variable at *pid to represent the calling process.
Returns pid if successful, or NULL if caller_env is not a process bound environment.
intenif_send(ErlNifEnv*caller_env,
ErlNifPid* to_pid, ErlNifEnv* msg_env, ERL_NIF_TERM msg)
Sends a message to a process.
caller_env:
The environment of the calling thread (process bound or callback environment) or NULL if
calling from a custom thread not spawned by ERTS.
*to_pid:
The pid of the receiving process. The pid is to 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 or NULL.
msg:
The message term to send.
Returns true if the message is successfully sent. Returns false if the send operation fails, that
is:
* *to_pid does not refer to an alive local process.
* The currently executing process (that is, the sender) is not alive.
The message environment msg_env with all its terms (including msg) is invalidated by a successful
call to enif_send. The environment is to either be freed with enif_free_env or cleared for reuse
with enif_clear_env. An unsuccessful call will leave msg and msg_env still valid.
If msg_env is set to NULL, the msg term is copied and the original term and its environment is
still valid after the call.
This function is thread-safe.
Note:
Passing msg_env as NULL is only supported as from ERTS 8.0 (Erlang/OTP 19).
voidenif_set_pid_undefined(ErlNifPid*pid)
Sets an ErlNifPid variable as undefined. See enif_is_pid_undefined.
unsignedenif_sizeof_resource(void*obj)
Gets the byte size of resource object obj obtained by enif_alloc_resource.
intenif_snprintf(char*str,size_tsize,const
char *format, ...)
Similar to snprintf but this format string also accepts "%T", which formats Erlang terms of type
ERL_NIF_TERM.
This function is primarily intended for debugging purpose. It is not recommended to print very
large terms with %T. The function may change errno, even if successful.
voidenif_system_info(ErlNifSysInfo
*sys_info_ptr, size_t size)
Same as driver_system_info.
intenif_term_to_binary(ErlNifEnv*env,
ERL_NIF_TERM term, ErlNifBinary *bin)
Allocates a new binary with enif_alloc_binary and stores the result of encoding term according to
the Erlang external term format.
Returns true on success, or false if the allocation fails.
See also erlang:term_to_binary/1 and enif_binary_to_term.
ErlNifTermTypeenif_term_type(ErlNifEnv*env,ERL_NIF_TERMterm)
Determines the type of the given term. The term must be an ordinary Erlang term and not one of the
special terms returned by enif_raise_exception, enif_schedule_nif, or similar.
The following types are defined at the moment:
ERL_NIF_TERM_TYPE_ATOM:
ERL_NIF_TERM_TYPE_BITSTRING:
A bitstring or binary
ERL_NIF_TERM_TYPE_FLOAT:
ERL_NIF_TERM_TYPE_FUN:
ERL_NIF_TERM_TYPE_INTEGER:
ERL_NIF_TERM_TYPE_LIST:
A list, empty or not
ERL_NIF_TERM_TYPE_MAP:
ERL_NIF_TERM_TYPE_PID:
ERL_NIF_TERM_TYPE_PORT:
ERL_NIF_TERM_TYPE_REFERENCE:
ERL_NIF_TERM_TYPE_TUPLE:
Note that new types may be added in the future, so the caller must be prepared to handle unknown
types.
intenif_thread_create(char*name,ErlNifTid
*tid,void * (*func)(void *),void *args,ErlNifThreadOpts
*opts)
Same as erl_drv_thread_create.
voidenif_thread_exit(void*resp)
Same as erl_drv_thread_exit.
intenif_thread_join(ErlNifTid,void**respp)
Same as erl_drv_thread_join.
char*enif_thread_name(ErlNifTidtid)
Same as erl_drv_thread_name.
ErlNifThreadOpts*enif_thread_opts_create(char*name)
Same as erl_drv_thread_opts_create.
voidenif_thread_opts_destroy(ErlNifThreadOpts*opts)
Same as erl_drv_thread_opts_destroy.
ErlNifTidenif_thread_self(void)
Same as erl_drv_thread_self.
intenif_thread_type(void)
Determine the type of currently executing thread. A positive value indicates a scheduler thread
while a negative value or zero indicates another type of thread. Currently the following specific
types exist (which may be extended in the future):
ERL_NIF_THR_UNDEFINED:
Undefined thread that is not a scheduler thread.
ERL_NIF_THR_NORMAL_SCHEDULER:
A normal scheduler thread.
ERL_NIF_THR_DIRTY_CPU_SCHEDULER:
A dirty CPU scheduler thread.
ERL_NIF_THR_DIRTY_IO_SCHEDULER:
A dirty I/O scheduler thread.
ErlNifTimeenif_time_offset(ErlNifTimeUnittime_unit)
Returns the current time offset between Erlang monotonic time and Erlang system time converted
into the time_unit passed as argument.
time_unit is the time unit of the returned value.
Returns ERL_NIF_TIME_ERROR if called with an invalid time unit argument or if called from a thread
that is not a scheduler thread.
See also ErlNifTime and ErlNifTimeUnit.
void*enif_tsd_get(ErlNifTSDKeykey)
Same as erl_drv_tsd_get.
intenif_tsd_key_create(char*name,ErlNifTSDKey*key)
Same as erl_drv_tsd_key_create.
voidenif_tsd_key_destroy(ErlNifTSDKeykey)
Same as erl_drv_tsd_key_destroy.
voidenif_tsd_set(ErlNifTSDKeykey,void*data)
Same as erl_drv_tsd_set.
intenif_vfprintf(FILE*stream,constchar*format,va_listap)
Equivalent to enif_fprintf except that its called with a va_list instead of a variable number of
arguments.
intenif_vsnprintf(char*str,size_tsize,constchar*format,va_listap)
Equivalent to enif_snprintf except that its called with a va_list instead of a variable number of
arguments.
intenif_whereis_pid(ErlNifEnv*caller_env,
ERL_NIF_TERM name, ErlNifPid *pid)
Looks up a process by its registered name.
caller_env:
The environment of the calling thread (process bound or callback environment) or NULL if
calling from a custom thread not spawned by ERTS.
name:
The name of a registered process, as an atom.
*pid:
The ErlNifPid in which the resolved process id is stored.
On success, sets *pid to the local process registered with name and returns true. If name is not a
registered process, or is not an atom, false is returned and *pid is unchanged.
Works as erlang:whereis/1, but restricted to processes. See enif_whereis_port to resolve
registered ports.
intenif_whereis_port(ErlNifEnv*caller_env,
ERL_NIF_TERM name, ErlNifPort *port)
Looks up a port by its registered name.
caller_env:
The environment of the calling thread (process bound or callback environment) or NULL if
calling from a custom thread not spawned by ERTS.
name:
The name of a registered port, as an atom.
*port:
The ErlNifPort in which the resolved port id is stored.
On success, sets *port to the port registered with name and returns true. If name is not a
registered port, or is not an atom, false is returned and *port is unchanged.
Works as erlang:whereis/1, but restricted to ports. See enif_whereis_pid to resolve registered
processes.