1erl_nif(3)                    C Library Functions                   erl_nif(3)
2
3
4

NAME

6       erl_nif - API functions for an Erlang NIF library.
7

DESCRIPTION

9       A  NIF  library  contains native implementation of some functions of an
10       Erlang module. The native implemented functions (NIFs) are called  like
11       any other functions without any difference to the caller. A NIF library
12       is built as a dynamically linked library file and loaded in runtime  by
13       calling erlang:load_nif/2.
14
15   Warning:
16
17       Use this functionality with extreme care.
18
19       A  native function is executed as a direct extension of the native code
20       of the VM. Execution is not made in a safe environment. The  VM  cannot
21       provide  the same services as provided when executing Erlang code, such
22       as pre-emptive scheduling or memory protection. If the native  function
23       does not behave well, the whole VM will misbehave.
24
25         * A native function that crashes will crash the whole VM.
26
27         * An  erroneously implemented native function can cause a VM internal
28           state inconsistency, which can cause a crash of the VM, or  miscel‐
29           laneous  misbehaviors  of the VM at any point after the call to the
30           native function.
31
32         * A native function doing lengthy work before returning degrades  re‐
33           sponsiveness  of the VM, and can cause miscellaneous strange behav‐
34           iors. Such strange behaviors include, but are not limited  to,  ex‐
35           treme  memory  usage,  and  bad  load balancing between schedulers.
36           Strange behaviors that can occur because of lengthy work  can  also
37           vary between Erlang/OTP releases.
38

EXAMPLE

40       A minimal example of a NIF library can look as follows:
41
42       /* niftest.c */
43       #include <erl_nif.h>
44
45       static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
46       {
47           return enif_make_string(env, "Hello world!", ERL_NIF_LATIN1);
48       }
49
50       static ErlNifFunc nif_funcs[] =
51       {
52           {"hello", 0, hello}
53       };
54
55       ERL_NIF_INIT(niftest,nif_funcs,NULL,NULL,NULL,NULL)
56
57       The Erlang module can look as follows:
58
59       -module(niftest).
60
61       -export([init/0, hello/0]).
62
63       -on_load(init/0).
64
65       init() ->
66             erlang:load_nif("./niftest", 0).
67
68       hello() ->
69             erlang:nif_error("NIF library not loaded").
70
71       Compile and test can look as follows (on Linux):
72
73       $> gcc -fPIC -shared -o niftest.so niftest.c -I $ERL_ROOT/usr/include/
74       $> erl
75
76       1> c(niftest).
77       {ok,niftest}
78       2> niftest:hello().
79       "Hello world!"
80
81       In  the  example  above the on_load directive is used get function init
82       called automatically when the module is loaded. Function init  in  turn
83       calls  erlang:load_nif/2  which  loads the NIF library and replaces the
84       hello function with its native implementation in C. Once loaded, a  NIF
85       library  is  persistent.  It will not be unloaded until the module code
86       version that it belongs to is purged.
87
88       Each NIF must have an implementation in Erlang to  be  invoked  if  the
89       function  is  called  before  the NIF library is successfully loaded. A
90       typical such stub implementation is to call erlang:nif_error which will
91       raise  an exception. The Erlang function can also be used as a fallback
92       implementation if the NIF library lacks implementation for some  OS  or
93       hardware architecture for example.
94
95   Note:
96       A NIF does not have to be exported, it can be local to the module. How‐
97       ever, unused local stub functions will be optimized away  by  the  com‐
98       piler, causing loading of the NIF library to fail.
99
100
101   Warning:
102       There  is  a  known  limitation  for Erlang fallback functions of NIFs.
103       Avoid functions involved in traversal of binaries by matching  and  re‐
104       cursion. If a NIF is loaded over such function, binary arguments to the
105       NIF may get corrupted and cause VM crash or other misbehavior.
106
107       Example of such bad fallback function:
108
109       skip_until(Byte, <<Byte, Rest/binary>>) ->
110           Rest;
111       skip_until(Byte, <<_, Rest/binary>>) ->
112           skip_until(Byte, Rest).
113
114
115

FUNCTIONALITY

117       All interaction between NIF code and the Erlang runtime system is  per‐
118       formed  by calling NIF API functions. Functions exist for the following
119       functionality:
120
121         Read and write Erlang terms:
122           Any Erlang terms can be passed to a NIF as function  arguments  and
123           be  returned  as  function  return  values. The terms are of C-type
124           ERL_NIF_TERM and can only be read or written using  API  functions.
125           Most functions to read the content of a term are prefixed enif_get_
126           and usually return true (or false) if the term is of  the  expected
127           type  (or  not).  The  functions  to  write  terms are all prefixed
128           enif_make_ and usually return the created ERL_NIF_TERM.  There  are
129           also   some   functions   to   query   terms,   like  enif_is_atom,
130           enif_is_identical, and enif_compare.
131
132           All terms of type ERL_NIF_TERM belong to  an  environment  of  type
133           ErlNifEnv.  The lifetime of a term is controlled by the lifetime of
134           its environment object. All API functions that read or write  terms
135           has  the environment that the term belongs to as the first function
136           argument.
137
138         Binaries:
139           Terms of type binary are accessed with the help of struct type Erl‐
140           NifBinary,  which  contains a pointer (data) to the raw binary data
141           and the length (size) of the data in bytes. Both data and size  are
142           read-only  and are only to be written using calls to API functions.
143           Instances of ErlNifBinary are, however,  always  allocated  by  the
144           user (usually as local variables).
145
146           The  raw  data  pointed  to by data is only mutable after a call to
147           enif_alloc_binary or enif_realloc_binary. All other functions  that
148           operate  on  a binary leave the data as read-only. A mutable binary
149           must in the end either be freed with  enif_release_binary  or  made
150           read-only  by  transferring it to an Erlang term with enif_make_bi‐
151           nary. However, it does not have to occur  in  the  same  NIF  call.
152           Read-only binaries do not have to be released.
153
154           enif_make_new_binary  can be used as a shortcut to allocate and re‐
155           turn a binary in the same NIF call.
156
157           Binaries are sequences of whole bytes. Bitstrings with an arbitrary
158           bit length have no support yet.
159
160         Resource objects:
161           The use of resource objects is a safe way to return pointers to na‐
162           tive data structures from a NIF. A resource object is only a  block
163           of  memory  allocated  with  enif_alloc_resource.  A  handle ("safe
164           pointer") to this memory block can then be returned  to  Erlang  by
165           the  use  of enif_make_resource. The term returned by enif_make_re‐
166           source is opaque in nature. It can be  stored  and  passed  between
167           processes, but the only real end usage is to pass it back as an ar‐
168           gument to a NIF. The NIF can then call  enif_get_resource  and  get
169           back a pointer to the memory block, which is guaranteed to still be
170           valid. A resource object is not deallocated until the  last  handle
171           term  is  garbage  collected by the VM and the resource is released
172           with enif_release_resource (not necessarily in that order).
173
174           All resource objects are created  as  instances  of  some  resource
175           type.  This  makes  resources  from different modules to be distin‐
176           guishable. A resource type  is  created  by  calling  enif_open_re‐
177           source_type when a library is loaded. Objects of that resource type
178           can then later be allocated and enif_get_resource verifies that the
179           resource  is of the expected type. A resource type can have a user-
180           supplied destructor function, which is  automatically  called  when
181           resources  of that type are released (by either the garbage collec‐
182           tor or enif_release_resource). Resource types are uniquely  identi‐
183           fied  by  a  supplied  name string and the name of the implementing
184           module.
185
186           The following is a template example of how to create and  return  a
187           resource object.
188
189         ERL_NIF_TERM term;
190         MyStruct* obj = enif_alloc_resource(my_resource_type, sizeof(MyStruct));
191
192         /* initialize struct ... */
193
194         term = enif_make_resource(env, obj);
195
196         if (keep_a_reference_of_our_own) {
197             /* store 'obj' in static variable, private data or other resource object */
198         }
199         else {
200             enif_release_resource(obj);
201             /* resource now only owned by "Erlang" */
202         }
203         return term;
204
205           Notice  that  once enif_make_resource creates the term to return to
206           Erlang, the code can choose to either keep its own  native  pointer
207           to the allocated struct and release it later, or release it immedi‐
208           ately and rely only on the garbage collector to deallocate the  re‐
209           source object eventually when it collects the term.
210
211           Another  use  of  resource  objects  is to create binary terms with
212           user-defined memory management. enif_make_resource_binary creates a
213           binary  term that is connected to a resource object. The destructor
214           of the resource is called when the binary is garbage collected,  at
215           which  time the binary data can be released. An example of this can
216           be a binary term consisting of data from a mmap'ed  file.  The  de‐
217           structor can then do munmap to release the memory region.
218
219           Resource  types support upgrade in runtime by allowing a loaded NIF
220           library to take over an already existing resource type and by  that
221           "inherit"  all existing objects of that type. The destructor of the
222           new library is thereafter called for the inherited objects and  the
223           library  with  the  old destructor function can be safely unloaded.
224           Existing resource objects, of a module that is upgraded,  must  ei‐
225           ther be deleted or taken over by the new NIF library. The unloading
226           of a library is postponed as long as there exist  resource  objects
227           with a destructor function in the library.
228
229         Module upgrade and static data:
230           A  loaded  NIF  library  is tied to the Erlang module instance that
231           loaded it. If the module is upgraded, the new module instance needs
232           to  load its own NIF library (or maybe choose not to). The new mod‐
233           ule instance can, however, choose to load the exact  same  NIF  li‐
234           brary  as  the old code if it wants to. Sharing the dynamic library
235           means that static data defined by the library is shared as well. To
236           avoid  unintentionally shared static data between module instances,
237           each Erlang module version can keep its own private data. This pri‐
238           vate  data  can be set when the NIF library is loaded and later re‐
239           trieved by calling enif_priv_data.
240
241         Threads and concurrency:
242           A NIF is thread-safe without any explicit synchronization  as  long
243           as  it  acts  as  a pure function and only reads the supplied argu‐
244           ments. When you write to a shared state either through static vari‐
245           ables  or enif_priv_data, you need to supply your own explicit syn‐
246           chronization. This includes terms in process  independent  environ‐
247           ments  that  are  shared between threads. Resource objects also re‐
248           quire synchronization if you treat them as mutable.
249
250           The library initialization callbacks load and upgrade  are  thread-
251           safe even for shared state data.
252
253         Version Management:
254           When  a NIF library is built, information about the NIF API version
255           is compiled into the library. When a NIF  library  is  loaded,  the
256           runtime  system  verifies  that the library is of a compatible ver‐
257           sion. erl_nif.h defines the following:
258
259           ERL_NIF_MAJOR_VERSION:
260             Incremented when NIF library incompatible changes are made to the
261             Erlang  runtime system. Normally it suffices to recompile the NIF
262             library when the ERL_NIF_MAJOR_VERSION has changed, but  it  can,
263             under  rare  circumstances,  mean  that  NIF  libraries  must  be
264             slightly modified. If so, this will of course be documented.
265
266           ERL_NIF_MINOR_VERSION:
267             Incremented when new features are added. The runtime system  uses
268             the minor version to determine what features to use.
269
270           The  runtime  system  normally refuses to load a NIF library if the
271           major versions differ, or if the major versions are equal  and  the
272           minor  version used by the NIF library is greater than the one used
273           by the runtime system. Old NIF libraries with lower major  versions
274           are,  however,  allowed  after a bump of the major version during a
275           transition period of two major releases. Such old NIF libraries can
276           however fail if deprecated features are used.
277
278         Time Measurement:
279           Support for time measurement in NIF libraries:
280
281           * ErlNifTime
282
283           * ErlNifTimeUnit
284
285           * enif_monotonic_time()
286
287           * enif_time_offset()
288
289           * enif_convert_time_unit()
290
291         I/O Queues:
292           The  Erlang  nif  library contains function for easily working with
293           I/O vectors as used by the unix system call writev. The  I/O  Queue
294           is  not thread safe, so some other synchronization mechanism has to
295           be used.
296
297           * SysIOVec
298
299           * ErlNifIOVec
300
301           * enif_ioq_create()
302
303           * enif_ioq_destroy()
304
305           * enif_ioq_enq_binary()
306
307           * enif_ioq_enqv()
308
309           * enif_ioq_deq()
310
311           * enif_ioq_peek()
312
313           * enif_ioq_peek_head()
314
315           * enif_inspect_iovec()
316
317           * enif_free_iovec()
318
319           Typical usage when writing to a file descriptor looks like this:
320
321         int writeiovec(ErlNifEnv *env, ERL_NIF_TERM term, ERL_NIF_TERM *tail,
322                        ErlNifIOQueue *q, int fd) {
323
324             ErlNifIOVec vec, *iovec = &vec;
325             SysIOVec *sysiovec;
326             int saved_errno;
327             int iovcnt, n;
328
329             if (!enif_inspect_iovec(env, 64, term, tail, &iovec))
330                 return -2;
331
332             if (enif_ioq_size(q) > 0) {
333                 /* If the I/O queue contains data we enqueue the iovec and
334                    then peek the data to write out of the queue. */
335                 if (!enif_ioq_enqv(q, iovec, 0))
336                     return -3;
337
338                 sysiovec = enif_ioq_peek(q, &iovcnt);
339             } else {
340                 /* If the I/O queue is empty we skip the trip through it. */
341                 iovcnt = iovec->iovcnt;
342                 sysiovec = iovec->iov;
343             }
344
345             /* Attempt to write the data */
346             n = writev(fd, sysiovec, iovcnt);
347             saved_errno = errno;
348
349             if (enif_ioq_size(q) == 0) {
350                 /* If the I/O queue was initially empty we enqueue any
351                    remaining data into the queue for writing later. */
352                 if (n >= 0 && !enif_ioq_enqv(q, iovec, n))
353                     return -3;
354             } else {
355                 /* Dequeue any data that was written from the queue. */
356                 if (n > 0 && !enif_ioq_deq(q, n, NULL))
357                     return -4;
358             }
359
360             /* return n, which is either number of bytes written or -1 if
361                some error happened */
362             errno = saved_errno;
363             return n;
364         }
365
366         Long-running NIFs:
367           As mentioned in the warning text at the beginning  of  this  manual
368           page, it is of vital importance that a native function returns rel‐
369           atively fast. It is difficult to give an exact  maximum  amount  of
370           time that a native function is allowed to work, but usually a well-
371           behaving native function is to return to its caller within  1  mil‐
372           lisecond.  This  can be achieved using different approaches. If you
373           have full control over the code to execute in the native  function,
374           the  best  approach  is  to divide the work into multiple chunks of
375           work and call the native function multiple times. This is, however,
376           not  always  possible,  for  example  when  calling third-party li‐
377           braries.
378
379           The enif_consume_timeslice() function can be  used  to  inform  the
380           runtime  system  about  the length of the NIF call. It is typically
381           always to be used unless the NIF executes very fast.
382
383           If the NIF call is too lengthy, this must be handled in one of  the
384           following  ways  to  avoid  degraded responsiveness, scheduler load
385           balancing problems, and other strange behaviors:
386
387           Yielding NIF:
388             If the functionality of a long-running NIF can be split  so  that
389             its  work  can be achieved through a series of shorter NIF calls,
390             the application has two options:
391
392             * Make that series of NIF calls from the Erlang level.
393
394             * Call a NIF that first performs a chunk of the  work,  then  in‐
395               vokes  the  enif_schedule_nif  function to schedule another NIF
396               call to perform the next chunk. The  final  call  scheduled  in
397               this manner can then return the overall result.
398
399             Breaking up a long-running function in this manner enables the VM
400             to regain control between calls to the NIFs.
401
402             This approach is always preferred over the other alternatives de‐
403             scribed  below.  This  both  from a performance perspective and a
404             system characteristics perspective.
405
406           Threaded NIF:
407             This is accomplished by dispatching the work  to  another  thread
408             managed by the NIF library, return from the NIF, and wait for the
409             result. The thread can send the result back to the Erlang process
410             using  enif_send. Information about thread primitives is provided
411             below.
412
413           Dirty NIF:
414
415
416       Note:
417           Dirty NIF support is available only when the emulator is configured
418           with  dirty scheduler support. As of ERTS version 9.0, dirty sched‐
419           uler support is enabled by default on the runtime system  with  SMP
420           support.  The  Erlang  runtime without SMP support does not support
421           dirty schedulers even when the dirty scheduler support  is  explic‐
422           itly  enabled. To check at runtime for the presence of dirty sched‐
423           uler threads, code can use the enif_system_info() API function.
424
425
426             A NIF that cannot be split and cannot execute in a millisecond or
427             less  is called a "dirty NIF", as it performs work that the ordi‐
428             nary schedulers  of  the  Erlang  runtime  system  cannot  handle
429             cleanly.  Applications that make use of such functions must indi‐
430             cate to the runtime that the functions are dirty so they  can  be
431             handled  specially.  This is handled by executing dirty jobs on a
432             separate set of schedulers called dirty schedulers. A  dirty  NIF
433             executing  on  a  dirty scheduler does not have the same duration
434             restriction as a normal NIF.
435
436             It is important to classify the dirty job correct. An  I/O  bound
437             job  should  be classified as such, and a CPU bound job should be
438             classified as such. If you should classify CPU bound jobs as  I/O
439             bound  jobs,  dirty  I/O  schedulers might starve ordinary sched‐
440             ulers. I/O bound jobs are expected to either  block  waiting  for
441             I/O, and/or spend a limited amount of time moving data.
442
443             To  schedule  a  dirty NIF for execution, the application has two
444             options:
445
446             * Set the appropriate flags value for the dirty NIF in  its  Erl‐
447               NifFunc entry.
448
449             * Call  enif_schedule_nif,  pass to it a pointer to the dirty NIF
450               to be executed, and indicate with argument flags whether it ex‐
451               pects the operation to be CPU-bound or I/O-bound.
452
453             A  job that alternates between I/O bound and CPU bound can be re‐
454             classified and rescheduled using enif_schedule_nif so that it ex‐
455             ecutes  on  the correct type of dirty scheduler at all times. For
456             more information see the documentation of the erl(1) command line
457             arguments +SDcpu, and +SDio.
458
459             While a process executes a dirty NIF, some operations that commu‐
460             nicate with it can take a very long time to complete. Suspend  or
461             garbage  collection  of a process executing a dirty NIF cannot be
462             done until the dirty NIF  has  returned.  Thus,  other  processes
463             waiting  for such operations to complete might have to wait for a
464             very long time. Blocking multi-scheduling, that is,  calling  er‐
465             lang:system_flag(multi_scheduling,  block),  can also take a very
466             long time to complete. This is because all ongoing  dirty  opera‐
467             tions  on all dirty schedulers must complete before the block op‐
468             eration can complete.
469
470             Many operations communicating with a process  executing  a  dirty
471             NIF  can,  however, complete while it executes the dirty NIF. For
472             example, retrieving information about  it  through  process_info,
473             setting  its  group  leader, register/unregister its name, and so
474             on.
475
476             Termination of a process executing a dirty NIF can only  be  com‐
477             pleted up to a certain point while it executes the dirty NIF. All
478             Erlang resources, such as its registered name and its ETS tables,
479             are released. All links and monitors are triggered. The execution
480             of the NIF is, however, not stopped. The NIF can safely  continue
481             execution,  allocate  heap memory, and so on, but it is of course
482             better to stop executing as soon as possible. The NIF  can  check
483             whether   a   current   process   is   alive  using  enif_is_cur‐
484             rent_process_alive.    Communication    using    enif_send    and
485             enif_port_command is also dropped when the sending process is not
486             alive.  Deallocation  of  certain  internal  resources,  such  as
487             process  heap  and  process  control  block, is delayed until the
488             dirty NIF has completed.
489

INITIALIZATION

491         ERL_NIF_INIT(MODULE, ErlNifFunc funcs[],  load,  NULL,  upgrade,  un‐
492         load):
493           This  is  the  magic macro to initialize a NIF library. It is to be
494           evaluated in global file scope.
495
496           MODULE is the name of the Erlang module as  an  identifier  without
497           string quotations. It is stringified by the macro.
498
499           funcs  is a static array of function descriptors for all the imple‐
500           mented NIFs in this library.
501
502           load, upgrade and unload are pointers to functions. One of load  or
503           upgrade  is  called  to initialize the library. unload is called to
504           release the library. All are described individually below.
505
506           The fourth argument NULL is ignored. It was earlier  used  for  the
507           deprecated  reload  callback which is no longer supported since OTP
508           20.
509
510           If compiling a NIF for static  inclusion  through  --enable-static-
511           nifs,  you  must  define  STATIC_ERLANG_NIF before the ERL_NIF_INIT
512           declaration.
513
514         int (*load)(ErlNifEnv*  caller_env,  void**  priv_data,  ERL_NIF_TERM
515         load_info):
516           load  is  called  when  the NIF library is loaded and no previously
517           loaded library exists for this module.
518
519           *priv_data can be set to point to some private data if the  library
520           needs  to  keep  a  state between NIF calls. enif_priv_data returns
521           this pointer. *priv_data  is  initialized  to  NULL  when  load  is
522           called.
523
524           load_info is the second argument to erlang:load_nif/2.
525
526           The  library  fails  to load if load returns anything other than 0.
527           load can be NULL if initialization is not needed.
528
529         int  (*upgrade)(ErlNifEnv*  caller_env,  void**   priv_data,   void**
530         old_priv_data, ERL_NIF_TERM load_info):
531           upgrade  is  called when the NIF library is loaded and there is old
532           code of this module with a loaded NIF library.
533
534           Works as load, except  that  *old_priv_data  already  contains  the
535           value  set  by  the last call to load or upgrade for the old module
536           code. *priv_data is initialized to NULL when upgrade is called.  It
537           is allowed to write to both *priv_data and *old_priv_data.
538
539           The  library fails to load if upgrade returns anything other than 0
540           or if upgrade is NULL.
541
542         void (*unload)(ErlNifEnv* caller_env, void* priv_data):
543           unload is called when the module code that the NIF library  belongs
544           to is purged as old. New code of the same module may or may not ex‐
545           ist.
546

DATA TYPES

548         ERL_NIF_TERM:
549           Variables of type ERL_NIF_TERM can refer to any Erlang  term.  This
550           is an opaque type and values of it can only by used either as argu‐
551           ments  to  API  functions  or  as  return  values  from  NIFs.  All
552           ERL_NIF_TERMs  belong  to an environment (ErlNifEnv). A term cannot
553           be destructed individually, it is valid until  its  environment  is
554           destructed.
555
556         ErlNifEnv:
557           ErlNifEnv represents an environment that can host Erlang terms. All
558           terms in an environment are valid as long  as  the  environment  is
559           valid.  ErlNifEnv  is  an  opaque  type; pointers to it can only be
560           passed on to API functions. Three types of environments exist:
561
562           Process bound environment:
563             Passed as the first argument to all NIFs. All function  arguments
564             passed to a NIF belong to that environment. The return value from
565             a NIF must also be a term belonging to the same environment.
566
567             A process bound environment contains transient information  about
568             the  calling Erlang process. The environment is only valid in the
569             thread where it was supplied as argument until the  NIF  returns.
570             It  is  thus  useless  and dangerous to store pointers to process
571             bound environments between NIF calls.
572
573           Callback environment:
574             Passed as the first argument to all the  non-NIF  callback  func‐
575             tions  (load, upgrade, unload, dtor, down and stop). Works like a
576             process bound environment but with  a  temporary  pseudo  process
577             that  "terminates"  when  the callback has returned. Terms may be
578             created in this environment but they will only be accessible dur‐
579             ing the callback.
580
581           Process independent environment:
582             Created  by  calling enif_alloc_env. This environment can be used
583             to  store  terms  between  NIF  calls  and  to  send  terms  with
584             enif_send.  A  process independent environment with all its terms
585             is valid until you explicitly invalidate it with enif_free_env or
586             enif_send.
587
588           All contained terms of a list/tuple/map must belong to the same en‐
589           vironment as the list/tuple/map itself. Terms can be copied between
590           environments with enif_make_copy.
591
592         ErlNifFunc:
593
594
595         typedef struct {
596             const char* name;
597             unsigned arity;
598             ERL_NIF_TERM (*fptr)(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
599             unsigned flags;
600         } ErlNifFunc;
601
602           Describes a NIF by its name, arity, and implementation.
603
604           fptr:
605             A pointer to the function that implements the NIF.
606
607           argv:
608             Contains the function arguments passed to the NIF.
609
610           argc:
611             The array length, that is, the function arity. argv[N-1] thus de‐
612             notes the Nth argument to the NIF. Notice that the argument  argc
613             allows  for the same C function to implement several Erlang func‐
614             tions with different arity (but probably with the same name).
615
616           flags:
617             Is 0 for a regular NIF (and so its value can be omitted for stat‐
618             ically initialized ErlNifFunc instances).
619
620             flags can be used to indicate that the NIF is a dirty NIF that is
621             to be executed on a dirty scheduler thread.
622
623             If the dirty NIF is expected to be CPU-bound, its flags field  is
624             to      be      set     to     ERL_NIF_DIRTY_JOB_CPU_BOUND     or
625             ERL_NIF_DIRTY_JOB_IO_BOUND.
626
627       Note:
628           If one of the ERL_NIF_DIRTY_JOB_*_BOUND flags is set, and the  run‐
629           time system has no support for dirty schedulers, the runtime system
630           refuses to load the NIF library.
631
632
633         ErlNifBinary:
634
635
636         typedef struct {
637             size_t size;
638             unsigned char* data;
639         } ErlNifBinary;
640
641           ErlNifBinary contains transient information about an inspected  bi‐
642           nary term. data is a pointer to a buffer of size bytes with the raw
643           content of the binary.
644
645           Notice that ErlNifBinary is a semi-opaque type and you are only al‐
646           lowed to read fields size and data.
647
648         ErlNifBinaryToTerm:
649           An  enumeration  of  the  options that can be specified to enif_bi‐
650           nary_to_term. For default behavior, use value 0.
651
652           When  receiving   data   from   untrusted   sources,   use   option
653           ERL_NIF_BIN2TERM_SAFE.
654
655         ErlNifMonitor:
656           This is an opaque data type that identifies a monitor.
657
658           The  nif  writer  is  to provide the memory for storing the monitor
659           when calling enif_monitor_process. The address of the data  is  not
660           stored  by  the runtime system, so ErlNifMonitor can be used as any
661           other data, it can be copied, moved in memory,  forgotten,  and  so
662           on. To compare two monitors, enif_compare_monitors must be used.
663
664         ErlNifPid:
665           A  process identifier (pid). In contrast to pid terms (instances of
666           ERL_NIF_TERM), ErlNifPids are self-contained and not bound  to  any
667           environment.  ErlNifPid  is an opaque type. It can be copied, moved
668           in memory, forgotten, and so on.
669
670         ErlNifPort:
671           A port identifier. In contrast  to  port  ID  terms  (instances  of
672           ERL_NIF_TERM),  ErlNifPorts are self-contained and not bound to any
673           environment. ErlNifPort is an opaque type. It can be copied,  moved
674           in memory, forgotten, and so on.
675
676         ErlNifResourceType:
677           Each  instance  of ErlNifResourceType represents a class of memory-
678           managed resource objects that can be garbage  collected.  Each  re‐
679           source  type  has  a  unique name and a destructor function that is
680           called when objects of its type are released.
681
682         ErlNifResourceTypeInit:
683
684
685         typedef struct {
686             ErlNifResourceDtor* dtor;
687             ErlNifResourceStop* stop;
688             ErlNifResourceDown* down;
689         } ErlNifResourceTypeInit;
690
691           Initialization structure read by  enif_open_resource_type_x.
692
693         ErlNifResourceDtor:
694
695
696         typedef void ErlNifResourceDtor(ErlNifEnv* caller_env, void* obj);
697
698           The function prototype of a resource destructor function.
699
700           The obj argument is a pointer to the resource. The only allowed use
701           for  the  resource in the destructor is to access its user data one
702           final time. The destructor is guaranteed to be  the  last  callback
703           before the resource is deallocated.
704
705         ErlNifResourceDown:
706
707
708         typedef void ErlNifResourceDown(ErlNifEnv* caller_env, void* obj, ErlNifPid* pid, ErlNifMonitor* mon);
709
710           The  function  prototype of a resource down function, called on the
711           behalf of  enif_monitor_process. obj is the resource,  pid  is  the
712           identity  of  the monitored process that is exiting, and mon is the
713           identity of the monitor.
714
715         ErlNifResourceStop:
716
717
718         typedef void ErlNifResourceStop(ErlNifEnv* caller_env, void* obj, ErlNifEvent event, int is_direct_call);
719
720           The function prototype of a resource stop function, called  on  the
721           behalf  of   enif_select.  obj  is the resource, event is OS event,
722           is_direct_call is true if the call is made directly  from  enif_se‐
723           lect  or  false if it is a scheduled call (potentially from another
724           thread).
725
726         ErlNifCharEncoding:
727
728
729         typedef enum {
730             ERL_NIF_LATIN1
731         }ErlNifCharEncoding;
732
733           The character encoding used in strings and  atoms.  The  only  sup‐
734           ported encoding is ERL_NIF_LATIN1 for ISO Latin-1 (8-bit ASCII).
735
736         ErlNifSysInfo:
737           Used  by  enif_system_info  to return information about the runtime
738           system. Contains the same content as ErlDrvSysInfo.
739
740         ErlNifSInt64:
741           A native signed 64-bit integer type.
742
743         ErlNifUInt64:
744           A native unsigned 64-bit integer type.
745
746         ErlNifTime:
747           A signed 64-bit integer type for representation of time.
748
749         ErlNifTimeUnit:
750           An enumeration of time units supported by the NIF API:
751
752           ERL_NIF_SEC:
753             Seconds
754
755           ERL_NIF_MSEC:
756             Milliseconds
757
758           ERL_NIF_USEC:
759             Microseconds
760
761           ERL_NIF_NSEC:
762             Nanoseconds
763
764         ErlNifUniqueInteger:
765           An enumeration  of  the  properties  that  can  be  requested  from
766           enif_make_unique_integer. For default properties, use value 0.
767
768           ERL_NIF_UNIQUE_POSITIVE:
769             Return only positive integers.
770
771           ERL_NIF_UNIQUE_MONOTONIC:
772             Return  only   strictly  monotonically  increasing integer corre‐
773             sponding to creation time.
774
775         ErlNifHash:
776           An enumeration of the supported hash types that  can  be  generated
777           using enif_hash.
778
779           ERL_NIF_INTERNAL_HASH:
780             Non-portable hash function that only guarantees the same hash for
781             the same term within one Erlang VM instance.
782
783             It  takes  32-bit  salt  values  and  generates   hashes   within
784             0..2^32-1.
785
786           ERL_NIF_PHASH2:
787             Portable  hash function that gives the same hash for the same Er‐
788             lang term regardless of machine architecture and ERTS version.
789
790             It ignores salt values and generates hashes within 0..2^27-1.
791
792             Slower  than  ERL_NIF_INTERNAL_HASH.  It   corresponds   to   er‐
793             lang:phash2/1.
794
795         SysIOVec:
796           A  system  I/O  vector,  as  used  by writev on Unix and WSASend on
797           Win32. It is used in ErlNifIOVec and by enif_ioq_peek.
798
799         ErlNifIOVec:
800
801
802         typedef struct {
803           int iovcnt;
804           size_t size;
805           SysIOVec* iov;
806         } ErlNifIOVec;
807
808           An I/O vector containing iovcnt SysIOVecs pointing to the data.  It
809           is used by enif_inspect_iovec and enif_ioq_enqv.
810
811         ErlNifIOQueueOpts:
812            Options to configure a ErlNifIOQueue.
813
814           ERL_NIF_IOQ_NORMAL:
815             Create a normal I/O Queue
816

EXPORTS

818       void *enif_alloc(size_t size)
819
820              Allocates memory of size bytes.
821
822              Returns NULL if the allocation fails.
823
824              The  returned  pointer is suitably aligned for any built-in type
825              that fit in the allocated memory.
826
827       int enif_alloc_binary(size_t size, ErlNifBinary* bin)
828
829              Allocates a new binary  of  size  size  bytes.  Initializes  the
830              structure  pointed  to  by bin to refer to the allocated binary.
831              The binary must either be  released  by  enif_release_binary  or
832              ownership  transferred  to an Erlang term with enif_make_binary.
833              An allocated (and owned) ErlNifBinary can be  kept  between  NIF
834              calls.
835
836              If  you  do not need to reallocate or keep the data alive across
837              NIF calls, consider using  enif_make_new_binary  instead  as  it
838              will allocate small binaries on the process heap when possible.
839
840              Returns true on success, or false if allocation fails.
841
842       ErlNifEnv *enif_alloc_env()
843
844              Allocates a new process independent environment. The environment
845              can be used to hold terms that are not  bound  to  any  process.
846              Such  terms  can  later  be copied to a process environment with
847              enif_make_copy or be  sent  to  a  process  as  a  message  with
848              enif_send.
849
850              Returns pointer to the new environment.
851
852       void *enif_alloc_resource(ErlNifResourceType*
853               type, unsigned size)
854
855              Allocates a memory-managed resource object of type type and size
856              size bytes.
857
858       size_t enif_binary_to_term(ErlNifEnv *env,
859               const unsigned char* data, size_t size, ERL_NIF_TERM *term,
860               ErlNifBinaryToTerm opts)
861
862              Creates a term that is the result of decoding the binary data at
863              data,  which  must  be  encoded according to the Erlang external
864              term format. No more than size bytes are read from  data.  Argu‐
865              ment  opts  corresponds  to  the  second  argument to erlang:bi‐
866              nary_to_term/2 and must be either 0 or ERL_NIF_BIN2TERM_SAFE.
867
868              On success, stores the resulting term at *term and  returns  the
869              number  of bytes read. Returns 0 if decoding fails or if opts is
870              invalid.
871
872              See  also   ErlNifBinaryToTerm,   erlang:binary_to_term/2,   and
873              enif_term_to_binary.
874
875       void enif_clear_env(ErlNifEnv* env)
876
877              Frees  all  terms in an environment and clears it for reuse. The
878              environment must have been allocated with enif_alloc_env.
879
880       int enif_compare(ERL_NIF_TERM lhs, ERL_NIF_TERM rhs)
881
882              Returns an integer < 0 if lhs < rhs, 0 if lhs = rhs, and > 0  if
883              lhs  >  rhs.  Corresponds to the Erlang operators ==, /=, =<, <,
884              >=, and > (but not =:= or =/=).
885
886       int enif_compare_monitors(const ErlNifMonitor
887               *monitor1, const ErlNifMonitor *monitor2)
888
889              Compares two ErlNifMonitors. Can also be used to imply some  ar‐
890              tificial order on monitors, for whatever reason.
891
892              Returns  0 if monitor1 and monitor2 are equal, < 0 if monitor1 <
893              monitor2, and > 0 if monitor1 > monitor2.
894
895       int enif_compare_pids(const ErlNifPid *pid1, const ErlNifPid *pid2)
896
897
898              Compares two ErlNifPids according to term order.
899
900              Returns 0 if pid1 and pid2 are equal, < 0 if pid1 < pid2, and  >
901              0 if pid1 > pid2.
902
903       void enif_cond_broadcast(ErlNifCond *cnd)
904
905              Same as erl_drv_cond_broadcast.
906
907       ErlNifCond *enif_cond_create(char *name)
908
909              Same as erl_drv_cond_create.
910
911       void enif_cond_destroy(ErlNifCond *cnd)
912
913              Same as erl_drv_cond_destroy.
914
915       char*enif_cond_name(ErlNifCond* cnd)
916
917              Same as erl_drv_cond_name.
918
919       void enif_cond_signal(ErlNifCond *cnd)
920
921              Same as erl_drv_cond_signal.
922
923       void enif_cond_wait(ErlNifCond *cnd, ErlNifMutex *mtx)
924
925              Same as erl_drv_cond_wait.
926
927       int enif_consume_timeslice(ErlNifEnv *env, int percent)
928
929              Gives the runtime system a hint about how much CPU time the cur‐
930              rent NIF call has consumed since the last  hint,  or  since  the
931              start  of  the  NIF  if no previous hint has been specified. The
932              time is specified as a percent of the timeslice that  a  process
933              is  allowed  to execute Erlang code until it can be suspended to
934              give time for other runnable processes. The scheduling timeslice
935              is not an exact entity, but can usually be approximated to about
936              1 millisecond.
937
938              Notice that it is up to the runtime system to determine  if  and
939              how  to  use this information. Implementations on some platforms
940              can use other means to determine consumed CPU time. Lengthy NIFs
941              should regardless of this frequently call enif_consume_timeslice
942              to determine if it is allowed to continue execution.
943
944              Argument percent must be an integer  between  1  and  100.  This
945              function  must only be called from a NIF-calling thread, and ar‐
946              gument env must be the environment of the calling process.
947
948              Returns 1 if the timeslice is exhausted, otherwise 0.  If  1  is
949              returned,  the NIF is to return as soon as possible in order for
950              the process to yield.
951
952              This function is provided to better support co-operative  sched‐
953              uling, improve system responsiveness, and make it easier to pre‐
954              vent misbehaviors of the VM because  of  a  NIF  monopolizing  a
955              scheduler  thread.  It can be used to divide  length work into a
956              number of repeated NIF calls without the need to create threads.
957
958              See also the warning text at the beginning of this manual page.
959
960       ErlNifTime enif_convert_time_unit(ErlNifTime
961               val, ErlNifTimeUnit from, ErlNifTimeUnit to)
962
963              Converts the val value of time unit from  to  the  corresponding
964              value  of  time  unit  to. The result is rounded using the floor
965              function.
966
967                val:
968                  Value to convert time unit for.
969
970                from:
971                  Time unit of val.
972
973                to:
974                  Time unit of returned value.
975
976              Returns ERL_NIF_TIME_ERROR if called with an invalid  time  unit
977              argument.
978
979              See also ErlNifTime and ErlNifTimeUnit.
980
981       ERL_NIF_TERM enif_cpu_time(ErlNifEnv *)
982
983              Returns  the  CPU time in the same format as erlang:timestamp().
984              The CPU time is the time the current logical CPU has spent  exe‐
985              cuting  since  some  arbitrary point in the past. If the OS does
986              not  support  fetching   this   value,   enif_cpu_time   invokes
987              enif_make_badarg.
988
989       int enif_demonitor_process(ErlNifEnv* caller_env,
990             void* obj, const ErlNifMonitor* mon)
991
992              Cancels a monitor created earlier with enif_monitor_process. Ar‐
993              gument obj is a pointer to the resource holding the monitor  and
994              *mon identifies the monitor.
995
996              Argument  caller_env  is  the  environment of the calling thread
997              (process bound or callback environment) or NULL if calling  from
998              a custom thread not spawned by ERTS.
999
1000              Returns  0  if  the  monitor was successfully identified and re‐
1001              moved. Returns a non-zero value if  the  monitor  could  not  be
1002              identified, which means it was either
1003
1004                * never created for this resource
1005
1006                * already cancelled
1007
1008                * already triggered
1009
1010                * just about to be triggered by a concurrent thread
1011
1012              This  function  is  only  thread-safe when the emulator with SMP
1013              support is used. It can only be used in a non-SMP emulator  from
1014              a NIF-calling thread.
1015
1016       int enif_equal_tids(ErlNifTid tid1, ErlNifTid tid2)
1017
1018              Same as erl_drv_equal_tids.
1019
1020       int enif_fprintf(FILE *stream, const char *format, ...)
1021
1022              Similar  to  fprintf  but  this format string also accepts "%T",
1023              which formats Erlang terms of type ERL_NIF_TERM.
1024
1025              This function is primarily intended for debugging purpose. It is
1026              not  recommended to print very large terms with %T. The function
1027              may change errno, even if successful.
1028
1029       void enif_free(void* ptr)
1030
1031              Frees memory allocated by enif_alloc.
1032
1033       void enif_free_env(ErlNifEnv* env)
1034
1035              Frees an environment allocated with  enif_alloc_env.  All  terms
1036              created in the environment are freed as well.
1037
1038       void enif_free_iovec(ErlNifIOVec* iov)
1039
1040              Frees  an  io  vector  returned from enif_inspect_iovec. This is
1041              needed  only  if  a  NULL  environment  is  passed  to  enif_in‐
1042              spect_iovec.
1043
1044              ErlNifIOVec *iovec = NULL;
1045              size_t max_elements = 128;
1046              ERL_NIF_TERM tail;
1047              if (!enif_inspect_iovec(NULL, max_elements, term, &tail, &iovec))
1048                return 0;
1049
1050              // Do things with the iovec
1051
1052              /* Free the iovector, possibly in another thread or nif function call */
1053              enif_free_iovec(iovec);
1054
1055       int enif_get_atom(ErlNifEnv* env, ERL_NIF_TERM
1056               term, char* buf, unsigned size, ErlNifCharEncoding encode)
1057
1058              Writes  a NULL-terminated string in the buffer pointed to by buf
1059              of size size, consisting of the  string  representation  of  the
1060              atom term with encoding encode.
1061
1062              Returns  the number of bytes written (including terminating NULL
1063              character) or 0 if term is not an atom with  maximum  length  of
1064              size-1.
1065
1066       int enif_get_atom_length(ErlNifEnv* env,
1067               ERL_NIF_TERM term, unsigned* len, ErlNifCharEncoding encode)
1068
1069              Sets  *len  to the length (number of bytes excluding terminating
1070              NULL character) of the atom term with encoding encode.
1071
1072              Returns true on success, or false if term is not an atom.
1073
1074       int enif_get_double(ErlNifEnv* env,
1075               ERL_NIF_TERM term, double* dp)
1076
1077              Sets *dp to the floating-point value of term.
1078
1079              Returns true on success, or false if term is not a float.
1080
1081       int enif_get_int(ErlNifEnv* env, ERL_NIF_TERM
1082               term, int* ip)
1083
1084              Sets *ip to the integer value of term.
1085
1086              Returns true on success, or false if term is not an  integer  or
1087              is outside the bounds of type int.
1088
1089       int enif_get_int64(ErlNifEnv* env, ERL_NIF_TERM
1090               term, ErlNifSInt64* ip)
1091
1092              Sets *ip to the integer value of term.
1093
1094              Returns  true  on success, or false if term is not an integer or
1095              is outside the bounds of a signed 64-bit integer.
1096
1097       int enif_get_local_pid(ErlNifEnv* env,
1098               ERL_NIF_TERM term, ErlNifPid* pid)
1099
1100              If term is the pid of a node local process, this  function  ini‐
1101              tializes  the pid variable *pid from it and returns true. Other‐
1102              wise returns false. No check is done to see if  the  process  is
1103              alive.
1104
1105          Note:
1106              enif_get_local_pid  will  return  false  if argument term is the
1107              atom undefined.
1108
1109
1110       int enif_get_local_port(ErlNifEnv* env,
1111               ERL_NIF_TERM term, ErlNifPort* port_id)
1112
1113              If term identifies a node local port, this function  initializes
1114              the  port  variable *port_id from it and returns true. Otherwise
1115              returns false. No check is done to see if the port is alive.
1116
1117       int enif_get_list_cell(ErlNifEnv* env,
1118               ERL_NIF_TERM list, ERL_NIF_TERM* head, ERL_NIF_TERM* tail)
1119
1120              Sets *head and *tail from list list.
1121
1122              Returns true on success, or false if it is not  a  list  or  the
1123              list is empty.
1124
1125       int enif_get_list_length(ErlNifEnv* env,
1126               ERL_NIF_TERM term, unsigned* len)
1127
1128              Sets *len to the length of list term.
1129
1130              Returns true on success, or false if term is not a proper list.
1131
1132       int enif_get_long(ErlNifEnv* env, ERL_NIF_TERM
1133               term, long int* ip)
1134
1135              Sets *ip to the long integer value of term.
1136
1137              Returns  true  on success, or false if term is not an integer or
1138              is outside the bounds of type long int.
1139
1140       int enif_get_map_size(ErlNifEnv* env,
1141               ERL_NIF_TERM term, size_t *size)
1142
1143              Sets *size to the number of key-value pairs in the map term.
1144
1145              Returns true on success, or false if term is not a map.
1146
1147       int enif_get_map_value(ErlNifEnv* env,
1148               ERL_NIF_TERM map, ERL_NIF_TERM key, ERL_NIF_TERM* value)
1149
1150              Sets *value to the value associated with key in the map map.
1151
1152              Returns true on success, or false if map is not a map or if  map
1153              does not contain key.
1154
1155       int enif_get_resource(ErlNifEnv* env,
1156               ERL_NIF_TERM term, ErlNifResourceType* type, void** objp)
1157
1158              Sets *objp to point to the resource object referred to by term.
1159
1160              Returns  true  on success, or false if term is not a handle to a
1161              resource object of type type.
1162
1163              enif_get_resource does not add a reference to the  resource  ob‐
1164              ject. However, the pointer received in *objp is guaranteed to be
1165              valid at least as long as the resource handle term is valid.
1166
1167       int enif_get_string(ErlNifEnv* env,
1168               ERL_NIF_TERM list, char* buf, unsigned size,
1169               ErlNifCharEncoding encode)
1170
1171              Writes a NULL-terminated string in the buffer pointed to by  buf
1172              with size size, consisting of the characters in the string list.
1173              The characters are written using encoding encode.
1174
1175              Returns one of the following:
1176
1177                * The number of  bytes  written  (including  terminating  NULL
1178                  character)
1179
1180                * -size if the string was truncated because of buffer space
1181
1182                * 0 if list is not a string that can be encoded with encode or
1183                  if size was < 1.
1184
1185              The written string is always NULL-terminated, unless buffer size
1186              is < 1.
1187
1188       int enif_get_tuple(ErlNifEnv* env, ERL_NIF_TERM
1189               term, int* arity, const ERL_NIF_TERM** array)
1190
1191              If term is a tuple, this function sets *array to point to an ar‐
1192              ray containing the elements of the tuple, and sets *arity to the
1193              number of elements. Notice that the array is read-only and (*ar‐
1194              ray)[N-1] is the Nth element of the tuple. *array  is  undefined
1195              if the arity of the tuple is zero.
1196
1197              Returns true on success, or false if term is not a tuple.
1198
1199       int enif_get_uint(ErlNifEnv* env, ERL_NIF_TERM
1200               term, unsigned int* ip)
1201
1202              Sets *ip to the unsigned integer value of term.
1203
1204              Returns true on success, or false if term is not an unsigned in‐
1205              teger or is outside the bounds of type unsigned int.
1206
1207       int enif_get_uint64(ErlNifEnv* env,
1208               ERL_NIF_TERM term, ErlNifUInt64* ip)
1209
1210              Sets *ip to the unsigned integer value of term.
1211
1212              Returns true on success, or false if term is not an unsigned in‐
1213              teger or is outside the bounds of an unsigned 64-bit integer.
1214
1215       int enif_get_ulong(ErlNifEnv* env, ERL_NIF_TERM
1216               term, unsigned long* ip)
1217
1218              Sets *ip to the unsigned long integer value of term.
1219
1220              Returns true on success, or false if term is not an unsigned in‐
1221              teger or is outside the bounds of type unsigned long.
1222
1223       int enif_getenv(const char* key, char* value,
1224               size_t *value_size)
1225
1226              Same as erl_drv_getenv.
1227
1228       int enif_has_pending_exception(ErlNifEnv* env,
1229               ERL_NIF_TERM* reason)
1230
1231              Returns true if a pending exception is associated with the envi‐
1232              ronment  env. If reason is a NULL pointer, ignore it. Otherwise,
1233              if a pending exception associated with env exists,  set  *reason
1234              to   the   value   of   the  exception  term.  For  example,  if
1235              enif_make_badarg is called to set a pending badarg exception,  a
1236              later  call  to  enif_has_pending_exception(env,  &reason)  sets
1237              *reason to the atom badarg, then return true.
1238
1239              See also enif_make_badarg and enif_raise_exception.
1240
1241       ErlNifUInt64 enif_hash(ErlNifHash type, ERL_NIF_TERM term, ErlNifUInt64
1242       salt)
1243
1244              Hashes term according to the specified ErlNifHash type.
1245
1246              Ranges  of  taken salt (if any) and returned value depend on the
1247              hash type.
1248
1249       int enif_inspect_binary(ErlNifEnv* env,
1250               ERL_NIF_TERM bin_term, ErlNifBinary* bin)
1251
1252              Initializes the structure pointed to  by  bin  with  information
1253              about binary term bin_term.
1254
1255              Returns true on success, or false if bin_term is not a binary.
1256
1257       int enif_inspect_iolist_as_binary(ErlNifEnv*
1258               env, ERL_NIF_TERM term, ErlNifBinary* bin)
1259
1260              Initializes  the  structure  pointed to by bin with a continuous
1261              buffer with the same byte content as iolist. As with inspect_bi‐
1262              nary,  the data pointed to by bin is transient and does not need
1263              to be released.
1264
1265              Returns true on success, or false if iolist is not an iolist.
1266
1267       int enif_inspect_iovec(ErlNifEnv*
1268               env,    size_t    max_elements,    ERL_NIF_TERM     iovec_term,
1269       ERL_NIF_TERM* tail,
1270               ErlNifIOVec** iovec)
1271
1272              Fills  iovec  with  the list of binaries provided in iovec_term.
1273              The number of elements handled in the call is limited to max_el‐
1274              ements,  and tail is set to the remainder of the list. Note that
1275              the output may be longer than max_elements on some platforms.
1276
1277              To create a list of binaries from an arbitrary iolist,  use  er‐
1278              lang:iolist_to_iovec/1.
1279
1280              When  calling  this  function, iovec should contain a pointer to
1281              NULL or a ErlNifIOVec structure that should be used if possible.
1282              e.g.
1283
1284              /* Don't use a pre-allocated structure */
1285              ErlNifIOVec *iovec = NULL;
1286              enif_inspect_iovec(env, max_elements, term, &tail, &iovec);
1287
1288              /* Use a stack-allocated vector as an optimization for vectors with few elements */
1289              ErlNifIOVec vec, *iovec = &vec;
1290              enif_inspect_iovec(env, max_elements, term, &tail, &iovec);
1291
1292
1293              The contents of the iovec is valid until the called nif function
1294              returns. If the iovec should be valid after  the  nif  call  re‐
1295              turns, it is possible to call this function with a NULL environ‐
1296              ment. If no environment is given the iovec owns the data in  the
1297              vector and it has to be explicitly freed using enif_free_iovec.
1298
1299              Returns true on success, or false if iovec_term not an iovec.
1300
1301       ErlNifIOQueue *enif_ioq_create(ErlNifIOQueueOpts opts)
1302
1303              Create  a new I/O Queue that can be used to store data. opts has
1304              to be set to ERL_NIF_IOQ_NORMAL.
1305
1306       void enif_ioq_destroy(ErlNifIOQueue *q)
1307
1308              Destroy the I/O queue and free all of it's contents
1309
1310       int enif_ioq_deq(ErlNifIOQueue *q, size_t count, size_t *size)
1311
1312              Dequeue count bytes from the I/O queue. If size is not NULL, the
1313              new size of the queue is placed there.
1314
1315              Returns  true  on  success, or false if the I/O does not contain
1316              count bytes. On failure the queue is left un-altered.
1317
1318       int enif_ioq_enq_binary(ErlNifIOQueue  *q,  ErlNifBinary  *bin,  size_t
1319       skip)
1320
1321              Enqueue the bin into q skipping the first skip bytes.
1322
1323              Returns  true  on  success, or false if skip is greater than the
1324              size of bin. Any ownership of the binary data is transferred  to
1325              the  queue and bin is to be considered read-only for the rest of
1326              the NIF call and then as released.
1327
1328       int enif_ioq_enqv(ErlNifIOQueue *q, ErlNifIOVec *iovec, size_t skip)
1329
1330              Enqueue the iovec into q skipping the first skip bytes.
1331
1332              Returns true on success, or false if skip is  greater  than  the
1333              size of iovec.
1334
1335       SysIOVec *enif_ioq_peek(ErlNifIOQueue *q, int *iovlen)
1336
1337              Get the I/O queue as a pointer to an array of SysIOVecs. It also
1338              returns the number of elements in iovlen.
1339
1340              Nothing is removed from the queue by this function, that must be
1341              done with enif_ioq_deq.
1342
1343              The  returned array is suitable to use with the Unix system call
1344              writev.
1345
1346       int enif_ioq_peek_head(ErlNifEnv *env, ErlNifIOQueue *q, size_t  *size,
1347       ERL_NIF_TERM *bin_term)
1348
1349              Get the head of the IO Queue as a binary term.
1350
1351              If size is not NULL, the size of the head is placed there.
1352
1353              Nothing is removed from the queue by this function, that must be
1354              done with enif_ioq_deq.
1355
1356              Returns true on success, or false if the queue is empty.
1357
1358       size_t enif_ioq_size(ErlNifIOQueue *q)
1359
1360              Get the size of q.
1361
1362       int enif_is_atom(ErlNifEnv* env, ERL_NIF_TERM term)
1363
1364              Returns true if term is an atom.
1365
1366       int enif_is_binary(ErlNifEnv* env, ERL_NIF_TERM term)
1367
1368              Returns true if term is a binary.
1369
1370       int enif_is_current_process_alive(ErlNifEnv* env)
1371
1372              Returns true if the currently  executing  process  is  currently
1373              alive, otherwise false.
1374
1375              This  function  can  only be used from a NIF-calling thread, and
1376              with an environment corresponding to  currently  executing  pro‐
1377              cesses.
1378
1379       int enif_is_empty_list(ErlNifEnv* env,
1380               ERL_NIF_TERM term)
1381
1382              Returns true if term is an empty list.
1383
1384       int enif_is_exception(ErlNifEnv* env,
1385               ERL_NIF_TERM term)
1386
1387              Return true if term is an exception.
1388
1389       int enif_is_fun(ErlNifEnv* env, ERL_NIF_TERM
1390               term)
1391
1392              Returns true if term is a fun.
1393
1394       int enif_is_identical(ERL_NIF_TERM lhs,
1395               ERL_NIF_TERM rhs)
1396
1397              Returns  true if the two terms are identical. Corresponds to the
1398              Erlang operators =:= and =/=.
1399
1400       int enif_is_list(ErlNifEnv* env, ERL_NIF_TERM term)
1401
1402              Returns true if term is a list.
1403
1404       int enif_is_map(ErlNifEnv* env, ERL_NIF_TERM
1405               term)
1406
1407              Returns true if term is a map, otherwise false.
1408
1409       int enif_is_number(ErlNifEnv* env, ERL_NIF_TERM
1410               term)
1411
1412              Returns true if term is a number.
1413
1414       int enif_is_pid(ErlNifEnv* env, ERL_NIF_TERM term)
1415
1416              Returns true if term is a pid.
1417
1418       int enif_is_pid_undefined(const ErlNifPid* pid)
1419
1420              Returns  true  if   pid   has   been   set   as   undefined   by
1421              enif_set_pid_undefined.
1422
1423       int enif_is_port(ErlNifEnv* env, ERL_NIF_TERM term)
1424
1425              Returns true if term is a port.
1426
1427       int enif_is_port_alive(ErlNifEnv* env,
1428               ErlNifPort *port_id)
1429
1430              Returns true if port_id is alive.
1431
1432              This  function  is  only  thread-safe when the emulator with SMP
1433              support is used. It can only be used in a non-SMP emulator  from
1434              a NIF-calling thread.
1435
1436       int enif_is_process_alive(ErlNifEnv* env,
1437               ErlNifPid *pid)
1438
1439              Returns true if pid is alive.
1440
1441              This  function  is  only  thread-safe when the emulator with SMP
1442              support is used. It can only be used in a non-SMP emulator  from
1443              a NIF-calling thread.
1444
1445       int enif_is_ref(ErlNifEnv* env, ERL_NIF_TERM term)
1446
1447              Returns true if term is a reference.
1448
1449       int enif_is_tuple(ErlNifEnv* env, ERL_NIF_TERM term)
1450
1451              Returns true if term is a tuple.
1452
1453       int enif_keep_resource(void* obj)
1454
1455              Adds  a  reference to resource object obj obtained from enif_al‐
1456              loc_resource. Each call to enif_keep_resource for an object must
1457              be balanced by a call to enif_release_resource before the object
1458              is destructed.
1459
1460       ERL_NIF_TERM enif_make_atom(ErlNifEnv* env, const char* name)
1461
1462              Creates an atom term from the NULL-terminated C-string name with
1463              ISO  Latin-1 encoding. If the length of name exceeds the maximum
1464              length allowed for an atom (255 characters), enif_make_atom  in‐
1465              vokes enif_make_badarg.
1466
1467       ERL_NIF_TERM enif_make_atom_len(ErlNifEnv* env,
1468               const char* name, size_t len)
1469
1470              Create  an  atom term from the string name with length len. NULL
1471              characters are treated as any other characters. If  len  exceeds
1472              the  maximum  length  allowed  for  an  atom  (255  characters),
1473              enif_make_atom invokes enif_make_badarg.
1474
1475       ERL_NIF_TERM enif_make_badarg(ErlNifEnv* env)
1476
1477              Makes a badarg exception to be returned from a  NIF,  and  asso‐
1478              ciates  it  with  environment env. Once a NIF or any function it
1479              calls invokes  enif_make_badarg,  the  runtime  ensures  that  a
1480              badarg exception is raised when the NIF returns, even if the NIF
1481              attempts to return a non-exception term instead.
1482
1483              The return value from enif_make_badarg can be used only  as  the
1484              return  value  from  the  NIF that invoked it (directly or indi‐
1485              rectly) or be passed to enif_is_exception, but not to any  other
1486              NIF API function.
1487
1488              See also enif_has_pending_exception and enif_raise_exception.
1489
1490          Note:
1491              Before   ERTS   7.0  (Erlang/OTP  18),  the  return  value  from
1492              enif_make_badarg had to be returned from the NIF. This  require‐
1493              ment  is  now lifted as the return value from the NIF is ignored
1494              if enif_make_badarg has been invoked.
1495
1496
1497       ERL_NIF_TERM enif_make_binary(ErlNifEnv* env, ErlNifBinary* bin)
1498
1499              Makes a binary term from bin. Any ownership of the  binary  data
1500              is  transferred  to the created term and bin is to be considered
1501              read-only for the rest of the NIF call and then as released.
1502
1503       ERL_NIF_TERM enif_make_copy(ErlNifEnv* dst_env,
1504               ERL_NIF_TERM src_term)
1505
1506              Makes a copy of term src_term. The copy is created  in  environ‐
1507              ment dst_env. The source term can be located in any environment.
1508
1509       ERL_NIF_TERM enif_make_double(ErlNifEnv* env, double d)
1510
1511              Creates  a floating-point term from a double. If argument double
1512              is   not   finite   or   is   NaN,   enif_make_double    invokes
1513              enif_make_badarg.
1514
1515       int enif_make_existing_atom(ErlNifEnv* env,
1516               const char* name, ERL_NIF_TERM* atom, ErlNifCharEncoding
1517               encode)
1518
1519              Tries  to  create  the term of an already existing atom from the
1520              NULL-terminated C-string name with encoding encode.
1521
1522              If the atom already exists, this function  stores  the  term  in
1523              *atom  and  returns true, otherwise false. Also returns false if
1524              the length of name exceeds the maximum  length  allowed  for  an
1525              atom (255 characters).
1526
1527       int enif_make_existing_atom_len(ErlNifEnv* env,
1528               const char* name, size_t len, ERL_NIF_TERM* atom, ErlNifCharEn‐
1529       coding
1530               encoding)
1531
1532              Tries to create the term of an already existing  atom  from  the
1533              string name with length len and encoding encode. NULL characters
1534              are treated as any other characters.
1535
1536              If the atom already exists, this function  stores  the  term  in
1537              *atom  and  returns true, otherwise false. Also returns false if
1538              len exceeds the maximum length allowed for an atom (255  charac‐
1539              ters).
1540
1541       ERL_NIF_TERM enif_make_int(ErlNifEnv* env, int i)
1542
1543              Creates an integer term.
1544
1545       ERL_NIF_TERM enif_make_int64(ErlNifEnv* env, ErlNifSInt64 i)
1546
1547              Creates an integer term from a signed 64-bit integer.
1548
1549       ERL_NIF_TERM enif_make_list(ErlNifEnv* env, unsigned cnt, ...)
1550
1551              Creates  an ordinary list term of length cnt. Expects cnt number
1552              of arguments (after cnt) of type ERL_NIF_TERM as the elements of
1553              the list.
1554
1555              Returns an empty list if cnt is 0.
1556
1557       ERL_NIF_TERM enif_make_list1(ErlNifEnv* env, ERL_NIF_TERM e1)
1558       ERL_NIF_TERM enif_make_list2(ErlNifEnv* env,
1559               ERL_NIF_TERM e1, ERL_NIF_TERM e2)
1560       ERL_NIF_TERM enif_make_list3(ErlNifEnv* env,
1561               ERL_NIF_TERM e1, ERL_NIF_TERM e2, ERL_NIF_TERM e3)
1562       ERL_NIF_TERM enif_make_list4(ErlNifEnv* env,
1563               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e4)
1564       ERL_NIF_TERM enif_make_list5(ErlNifEnv* env,
1565               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e5)
1566       ERL_NIF_TERM enif_make_list6(ErlNifEnv* env,
1567               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e6)
1568       ERL_NIF_TERM enif_make_list7(ErlNifEnv* env,
1569               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e7)
1570       ERL_NIF_TERM enif_make_list8(ErlNifEnv* env,
1571               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e8)
1572       ERL_NIF_TERM enif_make_list9(ErlNifEnv* env,
1573               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9)
1574
1575              Creates an ordinary list term with length indicated by the func‐
1576              tion name. Prefer these functions  (macros)  over  the  variadic
1577              enif_make_list  to get a compile-time error if the number of ar‐
1578              guments does not match.
1579
1580       ERL_NIF_TERM enif_make_list_cell(ErlNifEnv*
1581               env, ERL_NIF_TERM head, ERL_NIF_TERM tail)
1582
1583              Creates a list cell [head | tail].
1584
1585       ERL_NIF_TERM    enif_make_list_from_array(ErlNifEnv*     env,     const
1586       ERL_NIF_TERM
1587                 arr[], unsigned cnt)
1588
1589              Creates an ordinary list containing the elements of array arr of
1590              length cnt.
1591
1592              Returns an empty list if cnt is 0.
1593
1594       ERL_NIF_TERM enif_make_long(ErlNifEnv* env, long int i)
1595
1596              Creates an integer term from a long int.
1597
1598       int enif_make_map_put(ErlNifEnv* env,
1599               ERL_NIF_TERM map_in, ERL_NIF_TERM key, ERL_NIF_TERM value,
1600               ERL_NIF_TERM* map_out)
1601
1602              Makes a copy of map map_in and inserts key with  value.  If  key
1603              already  exists  in map_in, the old associated value is replaced
1604              by value.
1605
1606              If successful, this function sets *map_out to the  new  map  and
1607              returns true. Returns false if map_in is not a map.
1608
1609              The map_in term must belong to environment env.
1610
1611       int enif_make_map_remove(ErlNifEnv* env,
1612               ERL_NIF_TERM map_in, ERL_NIF_TERM key, ERL_NIF_TERM* map_out)
1613
1614              If map map_in contains key, this function makes a copy of map_in
1615              in *map_out, and removes key and the associated  value.  If  map
1616              map_in does not contain key, *map_out is set to map_in.
1617
1618              Returns true on success, or false if map_in is not a map.
1619
1620              The map_in term must belong to environment env.
1621
1622       int enif_make_map_update(ErlNifEnv* env,
1623               ERL_NIF_TERM map_in, ERL_NIF_TERM key, ERL_NIF_TERM new_value,
1624               ERL_NIF_TERM* map_out)
1625
1626              Makes  a copy of map map_in and replace the old associated value
1627              for key with new_value.
1628
1629              If successful, this function sets *map_out to the  new  map  and
1630              returns true. Returns false if map_in is not a map or if it does
1631              not contain key.
1632
1633              The map_in term must belong to environment env.
1634
1635       int enif_make_map_from_arrays(ErlNifEnv* env, ERL_NIF_TERM keys[],
1636               ERL_NIF_TERM values[], size_t cnt, ERL_NIF_TERM *map_out)
1637
1638              Makes a map term from the given keys and values.
1639
1640              If successful, this function sets *map_out to the  new  map  and
1641              returns true. Returns false there are any duplicate keys.
1642
1643              All keys and values must belong to env.
1644
1645       ERL_NIF_TERM  enif_make_monitor_term(ErlNifEnv*  env, const ErlNifMoni‐
1646       tor* mon)
1647
1648              Creates a term  identifying  the  given  monitor  received  from
1649              enif_monitor_process.
1650
1651              This function is primarily intended for debugging purpose.
1652
1653       unsigned char *enif_make_new_binary(ErlNifEnv*
1654               env, size_t size, ERL_NIF_TERM* termp)
1655
1656              Allocates  a  binary  of  size  size bytes and creates an owning
1657              term. The binary data is mutable until the calling NIF  returns.
1658              This is a quick way to create a new binary without having to use
1659              ErlNifBinary. The drawbacks are that the binary cannot  be  kept
1660              between NIF calls and it cannot be reallocated.
1661
1662              Returns  a pointer to the raw binary data and sets *termp to the
1663              binary term.
1664
1665       ERL_NIF_TERM enif_make_new_map(ErlNifEnv* env)
1666
1667              Makes an empty map term.
1668
1669       ERL_NIF_TERM enif_make_pid(ErlNifEnv* env, const ErlNifPid* pid)
1670
1671              Makes a pid term or the atom undefined from *pid.
1672
1673       ERL_NIF_TERM enif_make_ref(ErlNifEnv* env)
1674
1675              Creates a reference like erlang:make_ref/0.
1676
1677       ERL_NIF_TERM enif_make_resource(ErlNifEnv* env, void* obj)
1678
1679              Creates an opaque handle to a memory-managed resource object ob‐
1680              tained by enif_alloc_resource. No ownership transfer is done, as
1681              the resource object still  needs  to  be  released  by  enif_re‐
1682              lease_resource.  However,  notice  that  the  call  to  enif_re‐
1683              lease_resource can occur immediately after  obtaining  the  term
1684              from  enif_make_resource,  in  which case the resource object is
1685              deallocated when the term is garbage  collected.  For  more  de‐
1686              tails,  see the example of creating and returning a resource ob‐
1687              ject in the User's Guide.
1688
1689          Note:
1690              Since ERTS 9.0 (OTP-20.0), resource terms have a defined  behav‐
1691              ior  when  compared  and  serialized  through  term_to_binary or
1692              passed between nodes.
1693
1694                * Two resource terms will compare equal if and  only  if  they
1695                  would  yield the same resource object pointer when passed to
1696                  enif_get_resource.
1697
1698                * A resource term can be serialized  with  term_to_binary  and
1699                  later  be  fully  recreated  if the resource object is still
1700                  alive when binary_to_term is called. A stale  resource  term
1701                  will  be returned from binary_to_term if the resource object
1702                  has been deallocated. enif_get_resource  will  return  false
1703                  for stale resource terms.
1704
1705                  The  same principles of serialization apply when passing re‐
1706                  source terms in messages to remote nodes and back  again.  A
1707                  resource  term  will  act stale on all nodes except the node
1708                  where its resource object is still alive in memory.
1709
1710              Before ERTS 9.0 (OTP-20.0), all resource terms did compare equal
1711              to  each other and to empty binaries (<<>>). If serialized, they
1712              would be recreated as plain empty binaries.
1713
1714
1715       ERL_NIF_TERM enif_make_resource_binary(ErlNifEnv* env, void* obj, const
1716               void* data, size_t size)
1717
1718              Creates a binary term that is memory-managed by a  resource  ob‐
1719              ject  obj  obtained  by enif_alloc_resource. The returned binary
1720              term consists of size bytes pointed to by data. This raw  binary
1721              data must be kept readable and unchanged until the destructor of
1722              the resource is called. The binary data can be  stored  external
1723              to  the resource object, in which case the destructor is respon‐
1724              sible for releasing the data.
1725
1726              Several binary terms can be managed by the same resource object.
1727              The  destructor  is  not called until the last binary is garbage
1728              collected. This can be useful to return  different  parts  of  a
1729              larger binary buffer.
1730
1731              As  with  enif_make_resource, no ownership transfer is done. The
1732              resource still needs to be released with enif_release_resource.
1733
1734       int enif_make_reverse_list(ErlNifEnv* env, ERL_NIF_TERM list_in,
1735               ERL_NIF_TERM *list_out)
1736
1737              Sets *list_out to the reverse list of the list list_in  and  re‐
1738              turns true, or returns false if list_in is not a list.
1739
1740              This  function  is  only to be used on short lists, as a copy is
1741              created of the list, which is not released until after  the  NIF
1742              returns.
1743
1744              The list_in term must belong to environment env.
1745
1746       ERL_NIF_TERM enif_make_string(ErlNifEnv* env,
1747               const char* string, ErlNifCharEncoding encoding)
1748
1749              Creates  a list containing the characters of the NULL-terminated
1750              string string with encoding encoding.
1751
1752       ERL_NIF_TERM enif_make_string_len(ErlNifEnv*
1753               env, const char* string, size_t len, ErlNifCharEncoding
1754               encoding)
1755
1756              Creates a list containing the characters of  the  string  string
1757              with  length  len  and  encoding  encoding.  NULL characters are
1758              treated as any other characters.
1759
1760       ERL_NIF_TERM enif_make_sub_binary(ErlNifEnv*
1761               env, ERL_NIF_TERM bin_term, size_t pos, size_t size)
1762
1763              Makes a subbinary of binary bin_term, starting at zero-based po‐
1764              sition  pos  with a length of size bytes. bin_term must be a bi‐
1765              nary or bitstring. pos+size must be less or equal to the  number
1766              of whole bytes in bin_term.
1767
1768       ERL_NIF_TERM enif_make_tuple(ErlNifEnv* env,
1769               unsigned cnt, ...)
1770
1771              Creates  a  tuple term of arity cnt. Expects cnt number of argu‐
1772              ments (after cnt) of type ERL_NIF_TERM as the  elements  of  the
1773              tuple.
1774
1775       ERL_NIF_TERM enif_make_tuple1(ErlNifEnv* env,
1776               ERL_NIF_TERM e1)
1777       ERL_NIF_TERM enif_make_tuple2(ErlNifEnv* env,
1778               ERL_NIF_TERM e1, ERL_NIF_TERM e2)
1779       ERL_NIF_TERM enif_make_tuple3(ErlNifEnv* env,
1780               ERL_NIF_TERM e1, ERL_NIF_TERM e2, ERL_NIF_TERM e3)
1781       ERL_NIF_TERM enif_make_tuple4(ErlNifEnv* env,
1782               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e4)
1783       ERL_NIF_TERM enif_make_tuple5(ErlNifEnv* env,
1784               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e5)
1785       ERL_NIF_TERM enif_make_tuple6(ErlNifEnv* env,
1786               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e6)
1787       ERL_NIF_TERM enif_make_tuple7(ErlNifEnv* env,
1788               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e7)
1789       ERL_NIF_TERM enif_make_tuple8(ErlNifEnv* env,
1790               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e8)
1791       ERL_NIF_TERM enif_make_tuple9(ErlNifEnv* env,
1792               ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9)
1793
1794              Creates a tuple term with length indicated by the function name.
1795              Prefer these functions (macros) over the variadic  enif_make_tu‐
1796              ple  to get a compile-time error if the number of arguments does
1797              not match.
1798
1799       ERL_NIF_TERM    enif_make_tuple_from_array(ErlNifEnv*    env,     const
1800       ERL_NIF_TERM
1801               arr[], unsigned cnt)
1802
1803              Creates  a  tuple containing the elements of array arr of length
1804              cnt.
1805
1806       ERL_NIF_TERM enif_make_uint(ErlNifEnv* env, unsigned int i)
1807
1808              Creates an integer term from an unsigned int.
1809
1810       ERL_NIF_TERM enif_make_uint64(ErlNifEnv* env, ErlNifUInt64 i)
1811
1812              Creates an integer term from an unsigned 64-bit integer.
1813
1814       ERL_NIF_TERM enif_make_ulong(ErlNifEnv* env, unsigned long i)
1815
1816              Creates an integer term from an unsigned long int.
1817
1818       ERL_NIF_TERM enif_make_unique_integer(ErlNifEnv
1819               *env, ErlNifUniqueInteger properties)
1820
1821              Returns a unique integer with the same properties  as  specified
1822              by erlang:unique_integer/1.
1823
1824              env is the environment to create the integer in.
1825
1826              ERL_NIF_UNIQUE_POSITIVE   and  ERL_NIF_UNIQUE_MONOTONIC  can  be
1827              passed as the second argument to change the  properties  of  the
1828              integer  returned. They can be combined by OR:ing the two values
1829              together.
1830
1831              See also ErlNifUniqueInteger.
1832
1833       int enif_map_iterator_create(ErlNifEnv *env,
1834               ERL_NIF_TERM map, ErlNifMapIterator *iter, ErlNifMapIteratorEn‐
1835       try
1836               entry)
1837
1838              Creates  an  iterator for the map map by initializing the struc‐
1839              ture pointed to by iter. Argument entry determines the start po‐
1840              sition    of   the   iterator:   ERL_NIF_MAP_ITERATOR_FIRST   or
1841              ERL_NIF_MAP_ITERATOR_LAST.
1842
1843              Returns true on success, or false if map is not a map.
1844
1845              A map iterator is only useful during the lifetime of environment
1846              env  that  the map belongs to. The iterator must be destroyed by
1847              calling enif_map_iterator_destroy:
1848
1849              ERL_NIF_TERM key, value;
1850              ErlNifMapIterator iter;
1851              enif_map_iterator_create(env, my_map, &iter, ERL_NIF_MAP_ITERATOR_FIRST);
1852
1853              while (enif_map_iterator_get_pair(env, &iter, &key, &value)) {
1854                  do_something(key,value);
1855                  enif_map_iterator_next(env, &iter);
1856              }
1857              enif_map_iterator_destroy(env, &iter);
1858
1859          Note:
1860              The key-value pairs of a map have no  defined  iteration  order.
1861              The  only  guarantee is that the iteration order of a single map
1862              instance is preserved during the  lifetime  of  the  environment
1863              that the map belongs to.
1864
1865
1866       void enif_map_iterator_destroy(ErlNifEnv *env,
1867               ErlNifMapIterator *iter)
1868
1869              Destroys a map iterator created by enif_map_iterator_create.
1870
1871       int enif_map_iterator_get_pair(ErlNifEnv *env,
1872               ErlNifMapIterator *iter, ERL_NIF_TERM *key, ERL_NIF_TERM
1873               *value)
1874
1875              Gets key and value terms at the current map iterator position.
1876
1877              On success, sets *key and *value and returns true. Returns false
1878              if the iterator is positioned at head (before  first  entry)  or
1879              tail (beyond last entry).
1880
1881       int enif_map_iterator_is_head(ErlNifEnv *env,
1882               ErlNifMapIterator *iter)
1883
1884              Returns true if map iterator iter is positioned before the first
1885              entry.
1886
1887       int enif_map_iterator_is_tail(ErlNifEnv *env,
1888               ErlNifMapIterator *iter)
1889
1890              Returns true if map iterator iter is positioned after  the  last
1891              entry.
1892
1893       int enif_map_iterator_next(ErlNifEnv *env,
1894               ErlNifMapIterator *iter)
1895
1896              Increments map iterator to point to the next key-value entry.
1897
1898              Returns  true  if the iterator is now positioned at a valid key-
1899              value entry, or false if the iterator is positioned at the  tail
1900              (beyond the last entry).
1901
1902       int enif_map_iterator_prev(ErlNifEnv *env,
1903               ErlNifMapIterator *iter)
1904
1905              Decrements  map  iterator to point to the previous key-value en‐
1906              try.
1907
1908              Returns true if the iterator is now positioned at a  valid  key-
1909              value  entry, or false if the iterator is positioned at the head
1910              (before the first entry).
1911
1912       int enif_monitor_process(ErlNifEnv* caller_env,
1913             void* obj, const ErlNifPid* target_pid, ErlNifMonitor* mon)
1914
1915              Starts monitoring a process from a resource. When a  process  is
1916              monitored, a process exit results in a call to the provided down
1917              callback associated with the resource type.
1918
1919              Argument obj is pointer to the resource to hold the monitor  and
1920              *target_pid identifies the local process to be monitored.
1921
1922              If mon is not NULL, a successful call stores the identity of the
1923              monitor in the ErlNifMonitor struct  pointed  to  by  mon.  This
1924              identifier  is  used  to  refer to the monitor for later removal
1925              with enif_demonitor_process or compare  with  enif_compare_moni‐
1926              tors.  A  monitor  is  automatically removed when it triggers or
1927              when the resource is deallocated.
1928
1929              Argument caller_env is the environment  of  the  calling  thread
1930              (process  bound or callback environment) or NULL if calling from
1931              a custom thread not spawned by ERTS.
1932
1933              Returns 0 on success, < 0 if no down callback is provided, and >
1934              0  if  the process is no longer alive or if target_pid is  unde‐
1935              fined.
1936
1937              This function is only thread-safe when  the  emulator  with  SMP
1938              support  is used. It can only be used in a non-SMP emulator from
1939              a NIF-calling thread.
1940
1941       ErlNifTime enif_monotonic_time(ErlNifTimeUnit time_unit)
1942
1943              Returns the current  Erlang monotonic time. Notice  that  it  is
1944              not uncommon with negative values.
1945
1946              time_unit is the time unit of the returned value.
1947
1948              Returns  ERL_NIF_TIME_ERROR  if called with an invalid time unit
1949              argument, or if called from a thread that  is  not  a  scheduler
1950              thread.
1951
1952              See also ErlNifTime and ErlNifTimeUnit.
1953
1954       ErlNifMutex *enif_mutex_create(char *name)
1955
1956              Same as erl_drv_mutex_create.
1957
1958       void enif_mutex_destroy(ErlNifMutex *mtx)
1959
1960              Same as erl_drv_mutex_destroy.
1961
1962       void enif_mutex_lock(ErlNifMutex *mtx)
1963
1964              Same as erl_drv_mutex_lock.
1965
1966       char*enif_mutex_name(ErlNifMutex* mtx)
1967
1968              Same as erl_drv_mutex_name.
1969
1970       int enif_mutex_trylock(ErlNifMutex *mtx)
1971
1972              Same as erl_drv_mutex_trylock.
1973
1974       void enif_mutex_unlock(ErlNifMutex *mtx)
1975
1976              Same as erl_drv_mutex_unlock.
1977
1978       ERL_NIF_TERM enif_now_time(ErlNifEnv *env)
1979
1980              Returns an erlang:now() time stamp.
1981
1982              This function is deprecated.
1983
1984       ErlNifResourceType *enif_open_resource_type(ErlNifEnv* env, const char*
1985               module_str, const char* name, ErlNifResourceDtor* dtor,
1986               ErlNifResourceFlags flags, ErlNifResourceFlags* tried)
1987
1988              Creates  or  takes over a resource type identified by the string
1989              name and gives it the destructor function pointed  to  by  dtor.
1990              Argument flags can have the following values:
1991
1992                ERL_NIF_RT_CREATE:
1993                  Creates a new resource type that does not already exist.
1994
1995                ERL_NIF_RT_TAKEOVER:
1996                  Opens  an existing resource type and takes over ownership of
1997                  all its instances. The supplied destructor  dtor  is  called
1998                  both  for  existing instances and new instances not yet cre‐
1999                  ated by the calling NIF library.
2000
2001              The two flag values can be combined with  bitwise  OR.  The  re‐
2002              source  type  name is local to the calling module. Argument mod‐
2003              ule_str is not (yet) used and must be NULL. dtor can be NULL  if
2004              no destructor is needed.
2005
2006              On  success, the function returns a pointer to the resource type
2007              and   *tried   is   set   to   either    ERL_NIF_RT_CREATE    or
2008              ERL_NIF_RT_TAKEOVER  to  indicate what was done. On failure, re‐
2009              turns NULL and sets *tried to flags. It is allowed to set  tried
2010              to NULL.
2011
2012              Notice that enif_open_resource_type is only allowed to be called
2013              in the two callbacks load and upgrade.
2014
2015              See also enif_open_resource_type_x.
2016
2017       ErlNifResourceType  *enif_open_resource_type_x(ErlNifEnv*  env,   const
2018       char* name,      const ErlNifResourceTypeInit* init,
2019               ErlNifResourceFlags flags, ErlNifResourceFlags* tried)
2020
2021              Same  as  enif_open_resource_type  except  it accepts additional
2022              callback functions for resource types  that  are  used  together
2023              with enif_select and enif_monitor_process.
2024
2025              Argument  init  is a pointer to an ErlNifResourceTypeInit struc‐
2026              ture that contains the function pointers  for  destructor,  down
2027              and stop callbacks for the resource type.
2028
2029       int enif_port_command(ErlNifEnv* env, const
2030              ErlNifPort* to_port, ErlNifEnv *msg_env, ERL_NIF_TERM msg)
2031
2032              Works  as  erlang:port_command/2,  except that it is always com‐
2033              pletely asynchronous.
2034
2035                env:
2036                  The environment of the calling process. Must not be NULL.
2037
2038                *to_port:
2039                  The port ID of the receiving port. The port ID is  to  refer
2040                  to a port on the local node.
2041
2042                msg_env:
2043                  The  environment of the message term. Can be a process inde‐
2044                  pendent environment allocated with enif_alloc_env or NULL.
2045
2046                msg:
2047                  The message term to send. The same limitations apply  as  on
2048                  the payload to erlang:port_command/2.
2049
2050              Using  a  msg_env  of  NULL is an optimization, which groups to‐
2051              gether calls to enif_alloc_env,  enif_make_copy,  enif_port_com‐
2052              mand, and enif_free_env into one call. This optimization is only
2053              useful when a majority of the terms are to be copied from env to
2054              msg_env.
2055
2056              Returns  true if the command is successfully sent. Returns false
2057              if the command fails, for example:
2058
2059                * *to_port does not refer to a local port.
2060
2061                * The currently executing process (that is, the sender) is not
2062                  alive.
2063
2064                * msg is invalid.
2065
2066              See also enif_get_local_port.
2067
2068       void *enif_priv_data(ErlNifEnv* env)
2069
2070              Returns  the pointer to the private data that was set by load or
2071              upgrade.
2072
2073       ERL_NIF_TERM enif_raise_exception(ErlNifEnv*
2074               env, ERL_NIF_TERM reason)
2075
2076              Creates an error exception with the term reason to  be  returned
2077              from  a  NIF, and associates it with environment env. Once a NIF
2078              or any function it calls invokes enif_raise_exception, the  run‐
2079              time  ensures  that  the exception it creates is raised when the
2080              NIF returns, even if the NIF attempts to return a  non-exception
2081              term instead.
2082
2083              The  return  value from enif_raise_exception can only be used as
2084              the return value from the NIF that invoked it (directly or indi‐
2085              rectly)  or be passed to enif_is_exception, but not to any other
2086              NIF API function.
2087
2088              See also enif_has_pending_exception and enif_make_badarg.
2089
2090       void *enif_realloc(void* ptr, size_t size)
2091
2092              Reallocates memory allocated by enif_alloc to size bytes.
2093
2094              Returns NULL if the reallocation fails.
2095
2096              The returned pointer is suitably aligned for any  built-in  type
2097              that fit in the allocated memory.
2098
2099       int enif_realloc_binary(ErlNifBinary* bin, size_t size)
2100
2101              Changes the size of a binary bin. The source binary can be read-
2102              only, in which case it is left untouched and a mutable  copy  is
2103              allocated and assigned to *bin.
2104
2105              Returns true on success, or false if memory allocation failed.
2106
2107       void enif_release_binary(ErlNifBinary* bin)
2108
2109              Releases a binary obtained from enif_alloc_binary.
2110
2111       void enif_release_resource(void* obj)
2112
2113              Removes  a  reference  to  resource  object  obj  obtained  from
2114              enif_alloc_resource. The resource object is destructed when  the
2115              last  reference  is  removed. Each call to enif_release_resource
2116              must correspond to a previous  call  to  enif_alloc_resource  or
2117              enif_keep_resource.  References  made  by enif_make_resource can
2118              only be removed by the garbage collector.
2119
2120              There are no guarantees exactly when the destructor of an unref‐
2121              erenced  resource  is  called.  It  could  be called directly by
2122              enif_release_resource but it  could  also  be  scheduled  to  be
2123              called at a later time possibly by another thread.
2124
2125       ErlNifRWLock *enif_rwlock_create(char *name)
2126
2127              Same as erl_drv_rwlock_create.
2128
2129       void enif_rwlock_destroy(ErlNifRWLock *rwlck)
2130
2131              Same as erl_drv_rwlock_destroy.
2132
2133       char*enif_rwlock_name(ErlNifRWLock* rwlck)
2134
2135              Same as erl_drv_rwlock_name.
2136
2137       void enif_rwlock_rlock(ErlNifRWLock *rwlck)
2138
2139              Same as erl_drv_rwlock_rlock.
2140
2141       void enif_rwlock_runlock(ErlNifRWLock *rwlck)
2142
2143              Same as erl_drv_rwlock_runlock.
2144
2145       void enif_rwlock_rwlock(ErlNifRWLock *rwlck)
2146
2147              Same as erl_drv_rwlock_rwlock.
2148
2149       void enif_rwlock_rwunlock(ErlNifRWLock *rwlck)
2150
2151              Same as erl_drv_rwlock_rwunlock.
2152
2153       int enif_rwlock_tryrlock(ErlNifRWLock *rwlck)
2154
2155              Same as erl_drv_rwlock_tryrlock.
2156
2157       int enif_rwlock_tryrwlock(ErlNifRWLock *rwlck)
2158
2159              Same as erl_drv_rwlock_tryrwlock.
2160
2161       ERL_NIF_TERM enif_schedule_nif(ErlNifEnv* env,
2162               const  char* fun_name, int flags, ERL_NIF_TERM (*fp)(ErlNifEnv*
2163       env, int
2164               argc, const ERL_NIF_TERM argv[]), int argc, const ERL_NIF_TERM
2165               argv[])
2166
2167              Schedules NIF fp to execute. This function allows an application
2168              to break up long-running work into multiple regular NIF calls or
2169              to schedule a  dirty NIF to execute on a dirty scheduler thread.
2170
2171                fun_name:
2172                  Provides a name for the NIF that is scheduled for execution.
2173                  If  it cannot be converted to an atom, enif_schedule_nif re‐
2174                  turns a badarg exception.
2175
2176                flags:
2177                  Must be set to 0 for a regular  NIF.  If  the  emulator  was
2178                  built with dirty scheduler support enabled, flags can be set
2179                  to either ERL_NIF_DIRTY_JOB_CPU_BOUND if the job is expected
2180                  to be CPU-bound, or ERL_NIF_DIRTY_JOB_IO_BOUND for jobs that
2181                  will be I/O-bound. If dirty scheduler threads are not avail‐
2182                  able  in the emulator, an attempt to schedule such a job re‐
2183                  sults in a notsup exception.
2184
2185                argc and argv:
2186                  Can either be the originals passed into the calling NIF,  or
2187                  can be values created by the calling NIF.
2188
2189              The  calling  NIF must use the return value of enif_schedule_nif
2190              as its own return value.
2191
2192              Be aware that  enif_schedule_nif,  as  its  name  implies,  only
2193              schedules the NIF for future execution. The calling NIF does not
2194              block waiting for the scheduled NIF to execute and return.  This
2195              means  that  the calling NIF cannot expect to receive the sched‐
2196              uled NIF return value and use it for further operations.
2197
2198       int enif_select(ErlNifEnv* env, ErlNifEvent event,  enum  ErlNifSelect‐
2199       Flags mode,      void* obj, const ErlNifPid* pid, ERL_NIF_TERM ref)
2200
2201              This  function can be used to receive asynchronous notifications
2202              when OS-specific event objects become ready for either  read  or
2203              write operations.
2204
2205              Argument event identifies the event object. On Unix systems, the
2206              functions select/poll are used.  The  event  object  must  be  a
2207              socket,  pipe  or  other file descriptor object that select/poll
2208              can use.
2209
2210              Argument mode describes the type of events to wait for.  It  can
2211              be  ERL_NIF_SELECT_READ,  ERL_NIF_SELECT_WRITE  or  a bitwise OR
2212              combination to wait for both. It can also be ERL_NIF_SELECT_STOP
2213              or ERL_NIF_SELECT_CANCEL which are described further below. When
2214              a read or write event is triggered, a notification message  like
2215              this is sent to the process identified by pid:
2216
2217              {select, Obj, Ref, ready_input | ready_output}
2218
2219              ready_input  or  ready_output  indicates  if the event object is
2220              ready for reading or writing.
2221
2222          Note:
2223              For complete control over the message format use the newer func‐
2224              tions   enif_select_read   or  enif_select_write  introduced  in
2225              erts-11.0 (OTP-22.0).
2226
2227
2228              Argument pid may be NULL to indicate  the  calling  process.  It
2229              must not be set as  undefined.
2230
2231              Argument  obj  is a resource object obtained from enif_alloc_re‐
2232              source. The purpose of the resource objects is as a container of
2233              the  event  object to manage its state and lifetime. A handle to
2234              the resource is received in the notification message as Obj.
2235
2236              Argument ref must  be  either  a  reference  obtained  from  er‐
2237              lang:make_ref/0  or the atom undefined. It will be passed as Ref
2238              in the notifications. If a selective receive statement  is  used
2239              to  wait  for the notification then a reference created just be‐
2240              fore the receive will exploit a runtime  optimization  that  by‐
2241              passes all earlier received messages in the queue.
2242
2243              The  notifications are one-shot only. To receive further notifi‐
2244              cations of the same type (read  or  write),  repeated  calls  to
2245              enif_select must be made after receiving each notification.
2246
2247              ERL_NIF_SELECT_CANCEL  can be used to cancel previously selected
2248              events. It must  be  used  in  a  bitwise  OR  combination  with
2249              ERL_NIF_SELECT_READ   and/or  ERL_NIF_SELECT_WRITE  to  indicate
2250              which type of event to cancel. Arguments pid and ref are ignored
2251              when  ERL_NIF_SELECT_CANCEL  is specified. The return value will
2252              tell if the event was actualy cancelled or if a notification may
2253              already have been sent.
2254
2255              Use  ERL_NIF_SELECT_STOP  as  mode  in  order to safely close an
2256              event object that has been passed to enif_select. The stop call‐
2257              back of the resource obj will be called when it is safe to close
2258              the event object. This safe way of closing event objects must be
2259              used even if all notifications have been received (or cancelled)
2260              and no further calls to enif_select have been made.  ERL_NIF_SE‐
2261              LECT_STOP  will first cancel any selected events before it calls
2262              or schedules the stop callback. Arguments pid and  ref  are  ig‐
2263              nored when ERL_NIF_SELECT_STOP is specified.
2264
2265              The  first  call to enif_select for a specific OS event will es‐
2266              tablish a relation between the event object and  the  containing
2267              resource.  All  subsequent calls for an event must pass its con‐
2268              taining resource as argument obj. The relation is dissolved when
2269              enif_select has been called with mode as ERL_NIF_SELECT_STOP and
2270              the corresponding stop callback has  returned.  A  resource  can
2271              contain  several  event objects but one event object can only be
2272              contained within one resource. A resource will not be destructed
2273              until all its contained relations have been dissolved.
2274
2275          Note:
2276              Use  enif_monitor_process  together  with  enif_select to detect
2277              failing Erlang processes and prevent them from causing permanent
2278              leakage of resources and their contained OS event objects.
2279
2280
2281              Returns a non-negative value on success where the following bits
2282              can be set:
2283
2284                ERL_NIF_SELECT_STOP_CALLED:
2285                  The stop callback was called directly by enif_select.
2286
2287                ERL_NIF_SELECT_STOP_SCHEDULED:
2288                  The stop callback was scheduled to run on some other  thread
2289                  or later by this thread.
2290
2291                ERL_NIF_SELECT_READ_CANCELLED:
2292                  A  read  event  was  cancelled  by  ERL_NIF_SELECT_CANCEL or
2293                  ERL_NIF_SELECT_STOP and is  guaranteed  not  to  generate  a
2294                  ready_input notification message.
2295
2296                ERL_NIF_SELECT_WRITE_CANCELLED:
2297                  A  write  event  was  cancelled  by ERL_NIF_SELECT_CANCEL or
2298                  ERL_NIF_SELECT_STOP and is  guaranteed  not  to  generate  a
2299                  ready_output notification message.
2300
2301              Returns  a negative value if the call failed where the following
2302              bits can be set:
2303
2304                ERL_NIF_SELECT_INVALID_EVENT:
2305                  Argument event is not a valid OS event object.
2306
2307                ERL_NIF_SELECT_FAILED:
2308                  The system call failed to add the event object to  the  poll
2309                  set.
2310
2311          Note:
2312              Use  bitwise  AND to test for specific bits in the return value.
2313              New significant bits may be added in  future  releases  to  give
2314              more  detailed information for both failed and successful calls.
2315              Do NOT use equality tests like ==, as that may cause your appli‐
2316              cation to stop working.
2317
2318              Example:
2319
2320              retval = enif_select(env, fd, ERL_NIF_SELECT_STOP, resource, ref);
2321              if (retval < 0) {
2322                  /* handle error */
2323              }
2324              /* Success! */
2325              if (retval & ERL_NIF_SELECT_STOP_CALLED) {
2326                  /* ... */
2327              }
2328
2329
2330
2331          Note:
2332              The   mode  flag  ERL_NIF_SELECT_CANCEL  and  the  return  flags
2333              ERL_NIF_SELECT_READ_CANCELLED and ERL_NIF_SELECT_WRITE_CANCELLED
2334              were introduced in erts-11.0 (OTP-22.0).
2335
2336
2337       int enif_select_read(ErlNifEnv* env, ErlNifEvent event, void* obj,
2338             const ErlNifPid* pid, ERL_NIF_TERM msg, ErlNifEnv* msg_env)
2339       int enif_select_write(ErlNifEnv* env, ErlNifEvent event, void* obj,
2340             const ErlNifPid* pid, ERL_NIF_TERM msg, ErlNifEnv* msg_env)
2341
2342              These  are variants of enif_select where you can supply your own
2343              message term msg that will be sent to the process instead of the
2344              predefined tuple {select,_,_,_}.
2345
2346              Argument  msg_env  must either be NULL or the environment of msg
2347              allocated with enif_alloc_env. If argument msg_env is  NULL  the
2348              term  msg will be copied, otherwise both msg and msg_env will be
2349              invalidated by a successful call to enif_select_read or enif_se‐
2350              lect_write.  The  environment  is  then  to either be freed with
2351              enif_free_env or cleared for reuse with enif_clear_env.  An  un‐
2352              successful call will leave msg and msg_env still valid.
2353
2354              Apart  from  the  message  format  enif_select_read and enif_se‐
2355              lect_write behaves exactly the same as enif_select with argument
2356              mode  as  either ERL_NIF_SELECT_READ or ERL_NIF_SELECT_WRITE. To
2357              cancel or close events use enif_select.
2358
2359       ErlNifPid *enif_self(ErlNifEnv* caller_env, ErlNifPid* pid)
2360
2361              Initializes the ErlNifPid variable  at  *pid  to  represent  the
2362              calling process.
2363
2364              Returns  pid  if  successful,  or  NULL  if  caller_env is not a
2365              process bound environment.
2366
2367       int enif_send(ErlNifEnv* caller_env,
2368             ErlNifPid* to_pid, ErlNifEnv* msg_env, ERL_NIF_TERM msg)
2369
2370              Sends a message to a process.
2371
2372                caller_env:
2373                   The environment of the calling  thread  (process  bound  or
2374                  callback  environment)  or  NULL  if  calling  from a custom
2375                  thread not spawned by ERTS.
2376
2377                *to_pid:
2378                  The pid of the receiving process. The pid is to refer  to  a
2379                  process on the local node.
2380
2381                msg_env:
2382                  The environment of the message term. Must be a process inde‐
2383                  pendent environment allocated with enif_alloc_env or NULL.
2384
2385                msg:
2386                  The message term to send.
2387
2388              Returns true if the message is successfully sent. Returns  false
2389              if the send operation fails, that is:
2390
2391                * *to_pid does not refer to an alive local process.
2392
2393                * The currently executing process (that is, the sender) is not
2394                  alive.
2395
2396              The message environment msg_env with all  its  terms  (including
2397              msg) is invalidated by a successful call to enif_send. The envi‐
2398              ronment is to either be freed with enif_free_env or cleared  for
2399              reuse  with  enif_clear_env. An unsuccessful call will leave msg
2400              and msg_env still valid.
2401
2402              If msg_env is set to NULL, the msg term is copied and the origi‐
2403              nal term and its environment is still valid after the call.
2404
2405              This  function  is  only  thread-safe when the emulator with SMP
2406              support is used. It can only be used in a non-SMP emulator  from
2407              a NIF-calling thread.
2408
2409          Note:
2410              Passing  msg_env as NULL is only supported as from ERTS 8.0 (Er‐
2411              lang/OTP 19).
2412
2413
2414       void enif_set_pid_undefined(ErlNifPid* pid)
2415
2416              Sets an ErlNifPid variable as undefined.  See  enif_is_pid_unde‐
2417              fined.
2418
2419       unsigned enif_sizeof_resource(void* obj)
2420
2421              Gets  the  byte size of resource object obj obtained by enif_al‐
2422              loc_resource.
2423
2424       int enif_snprintf(char *str, size_t size, const
2425               char *format, ...)
2426
2427              Similar to snprintf but this format string  also  accepts  "%T",
2428              which formats Erlang terms of type ERL_NIF_TERM.
2429
2430              This function is primarily intended for debugging purpose. It is
2431              not recommended to print very large terms with %T. The  function
2432              may change errno, even if successful.
2433
2434       void enif_system_info(ErlNifSysInfo
2435               *sys_info_ptr, size_t size)
2436
2437              Same as driver_system_info.
2438
2439       int enif_term_to_binary(ErlNifEnv *env,
2440               ERL_NIF_TERM term, ErlNifBinary *bin)
2441
2442              Allocates a new binary with enif_alloc_binary and stores the re‐
2443              sult of encoding term according to the Erlang external term for‐
2444              mat.
2445
2446              Returns true on success, or false if the allocation fails.
2447
2448              See also erlang:term_to_binary/1 and enif_binary_to_term.
2449
2450       ErlNifTermType enif_term_type(ErlNifEnv *env, ERL_NIF_TERM term)
2451
2452              Determines the type of the given term. The term must be an ordi‐
2453              nary Erlang term and not one of the special  terms  returned  by
2454              enif_raise_exception, enif_schedule_nif, or similar.
2455
2456              The following types are defined at the moment:
2457
2458                ERL_NIF_TERM_TYPE_ATOM:
2459
2460
2461                ERL_NIF_TERM_TYPE_BITSTRING:
2462                  A bitstring or binary
2463
2464                ERL_NIF_TERM_TYPE_FLOAT:
2465
2466
2467                ERL_NIF_TERM_TYPE_FUN:
2468
2469
2470                ERL_NIF_TERM_TYPE_INTEGER:
2471
2472
2473                ERL_NIF_TERM_TYPE_LIST:
2474                  A list, empty or not
2475
2476                ERL_NIF_TERM_TYPE_MAP:
2477
2478
2479                ERL_NIF_TERM_TYPE_PID:
2480
2481
2482                ERL_NIF_TERM_TYPE_PORT:
2483
2484
2485                ERL_NIF_TERM_TYPE_REFERENCE:
2486
2487
2488                ERL_NIF_TERM_TYPE_TUPLE:
2489
2490
2491              Note  that  new  types may be added in the future, so the caller
2492              must be prepared to handle unknown types.
2493
2494       int enif_thread_create(char *name,ErlNifTid
2495               *tid,void * (*func)(void *),void *args,ErlNifThreadOpts
2496               *opts)
2497
2498              Same as erl_drv_thread_create.
2499
2500       void enif_thread_exit(void *resp)
2501
2502              Same as erl_drv_thread_exit.
2503
2504       int enif_thread_join(ErlNifTid, void **respp)
2505
2506              Same as erl_drv_thread_join.
2507
2508       char*enif_thread_name(ErlNifTid tid)
2509
2510              Same as erl_drv_thread_name.
2511
2512       ErlNifThreadOpts *enif_thread_opts_create(char *name)
2513
2514              Same as erl_drv_thread_opts_create.
2515
2516       void enif_thread_opts_destroy(ErlNifThreadOpts *opts)
2517
2518              Same as erl_drv_thread_opts_destroy.
2519
2520       ErlNifTid enif_thread_self(void)
2521
2522              Same as erl_drv_thread_self.
2523
2524       int enif_thread_type(void)
2525
2526              Determine the type of currently  executing  thread.  A  positive
2527              value  indicates  a  scheduler  thread while a negative value or
2528              zero indicates another type of thread. Currently  the  following
2529              specific types exist (which may be extended in the future):
2530
2531                ERL_NIF_THR_UNDEFINED:
2532                  Undefined thread that is not a scheduler thread.
2533
2534                ERL_NIF_THR_NORMAL_SCHEDULER:
2535                  A normal scheduler thread.
2536
2537                ERL_NIF_THR_DIRTY_CPU_SCHEDULER:
2538                  A dirty CPU scheduler thread.
2539
2540                ERL_NIF_THR_DIRTY_IO_SCHEDULER:
2541                  A dirty I/O scheduler thread.
2542
2543       ErlNifTime enif_time_offset(ErlNifTimeUnit time_unit)
2544
2545              Returns  the  current time offset between  Erlang monotonic time
2546              and  Erlang system time converted into the time_unit  passed  as
2547              argument.
2548
2549              time_unit is the time unit of the returned value.
2550
2551              Returns  ERL_NIF_TIME_ERROR  if called with an invalid time unit
2552              argument or if called from a thread  that  is  not  a  scheduler
2553              thread.
2554
2555              See also ErlNifTime and ErlNifTimeUnit.
2556
2557       void *enif_tsd_get(ErlNifTSDKey key)
2558
2559              Same as erl_drv_tsd_get.
2560
2561       int enif_tsd_key_create(char *name, ErlNifTSDKey *key)
2562
2563              Same as erl_drv_tsd_key_create.
2564
2565       void enif_tsd_key_destroy(ErlNifTSDKey key)
2566
2567              Same as erl_drv_tsd_key_destroy.
2568
2569       void enif_tsd_set(ErlNifTSDKey key, void *data)
2570
2571              Same as erl_drv_tsd_set.
2572
2573       int enif_vfprintf(FILE *stream, const char *format, va_list ap)
2574
2575
2576              Equivalent to enif_fprintf except that its called with a va_list
2577              instead of a variable number of arguments.
2578
2579       int enif_vsnprintf(char *str, size_t size, const char *format,  va_list
2580       ap)
2581
2582
2583              Equivalent  to  enif_snprintf  except  that  its  called  with a
2584              va_list instead of a variable number of arguments.
2585
2586       int enif_whereis_pid(ErlNifEnv *caller_env,
2587                 ERL_NIF_TERM name, ErlNifPid *pid)
2588
2589              Looks up a process by its registered name.
2590
2591                caller_env:
2592                  The environment of the  calling  thread  (process  bound  or
2593                  callback  environment)  or  NULL  if  calling  from a custom
2594                  thread not spawned by ERTS.
2595
2596                name:
2597                  The name of a registered process, as an atom.
2598
2599                *pid:
2600                  The ErlNifPid in which the resolved process id is stored.
2601
2602              On success, sets *pid to the local process registered with  name
2603              and returns true. If name is not a registered process, or is not
2604              an atom, false is returned and *pid is unchanged.
2605
2606              Works as erlang:whereis/1,  but  restricted  to  processes.  See
2607              enif_whereis_port to resolve registered ports.
2608
2609       int enif_whereis_port(ErlNifEnv *caller_env,
2610                 ERL_NIF_TERM name, ErlNifPort *port)
2611
2612              Looks up a port by its registered name.
2613
2614                caller_env:
2615                  The  environment  of  the  calling  thread (process bound or
2616                  callback environment) or  NULL  if  calling  from  a  custom
2617                  thread not spawned by ERTS.
2618
2619                name:
2620                  The name of a registered port, as an atom.
2621
2622                *port:
2623                  The ErlNifPort in which the resolved port id is stored.
2624
2625              On  success, sets *port to the port registered with name and re‐
2626              turns true. If name is not a registered port, or is not an atom,
2627              false is returned and *port is unchanged.
2628
2629              Works   as   erlang:whereis/1,  but  restricted  to  ports.  See
2630              enif_whereis_pid to resolve registered processes.
2631

SEE ALSO

2633       erlang:load_nif/2
2634
2635
2636
2637Ericsson AB                      erts 11.2.2.2                      erl_nif(3)
Impressum