Erlang -- erl_eterm

User's Guide
Reference Manual
Release Notes
PDF
Top

Erlang Interface
Reference Manual
Version 3.7.3

Expand All
Contract All

Table of Contents

- ei
  - Top of manual page
  - ei_set_compat_rel()
  - ei_encode_version()
  - ei_x_encode_version()
  - ei_encode_long()
  - ei_x_encode_long()
  - ei_encode_ulong()
  - ei_x_encode_ulong()
  - ei_encode_longlong()
  - ei_x_encode_longlong()
  - ei_encode_ulonglong()
  - ei_x_encode_ulonglong()
  - ei_encode_bignum()
  - ei_x_encode_bignum()
  - ei_encode_double()
  - ei_x_encode_double()
  - ei_encode_boolean()
  - ei_x_encode_boolean()
  - ei_encode_char()
  - ei_x_encode_char()
  - ei_encode_string()
  - ei_encode_string_len()
  - ei_x_encode_string()
  - ei_x_encode_string_len()
  - ei_encode_atom()
  - ei_encode_atom_len()
  - ei_x_encode_atom()
  - ei_x_encode_atom_len()
  - ei_encode_binary()
  - ei_x_encode_binary()
  - ei_encode_pid()
  - ei_x_encode_pid()
  - ei_encode_fun()
  - ei_x_encode_fun()
  - ei_encode_port()
  - ei_x_encode_port()
  - ei_encode_ref()
  - ei_x_encode_ref()
  - ei_encode_term()
  - ei_x_encode_term()
  - ei_encode_trace()
  - ei_x_encode_trace()
  - ei_encode_tuple_header()
  - ei_x_encode_tuple_header()
  - ei_encode_list_header()
  - ei_x_encode_list_header()
  - ei_encode_empty_list()
  - ei_x_encode_empty_list()
  - ei_get_type()
  - ei_decode_version()
  - ei_decode_long()
  - ei_decode_ulong()
  - ei_decode_longlong()
  - ei_decode_ulonglong()
  - ei_decode_bignum()
  - ei_decode_double()
  - ei_decode_boolean()
  - ei_decode_char()
  - ei_decode_string()
  - ei_decode_atom()
  - ei_decode_binary()
  - ei_decode_fun()
  - free_fun()
  - ei_decode_pid()
  - ei_decode_port()
  - ei_decode_ref()
  - ei_decode_trace()
  - ei_decode_tuple_header()
  - ei_decode_list_header()
  - ei_decode_ei_term()
  - ei_decode_term()
  - ei_print_term()
  - ei_s_print_term()
  - ei_x_format()
  - ei_x_format_wo_ver()
  - ei_x_new()
  - ei_x_new_with_version()
  - ei_x_free()
  - ei_x_append()
  - ei_x_append_buf()
  - ei_skip_term()
- ei_connect
  - Top of manual page
  - ei_connect_init()
  - ei_connect_xinit()
  - ei_connect()
  - ei_xconnect()
  - ei_connect_tmo()
  - ei_xconnect_tmo()
  - ei_receive()
  - ei_receive_tmo()
  - ei_receive_msg()
  - ei_xreceive_msg()
  - ei_receive_msg_tmo()
  - ei_xreceive_msg_tmo()
  - ei_receive_encoded()
  - ei_receive_encoded_tmo()
  - ei_send()
  - ei_send_tmo()
  - ei_send_encoded()
  - ei_send_encoded_tmo()
  - ei_reg_send()
  - ei_reg_send_tmo()
  - ei_send_reg_encoded()
  - ei_send_reg_encoded_tmo()
  - ei_rpc()
  - ei_rpc_to()
  - ei_rpc_from()
  - ei_publish()
  - ei_publish_tmo()
  - ei_accept()
  - ei_accept_tmo()
  - ei_unpublish()
  - ei_unpublish_tmo()
  - ei_thisnodename()
  - ei_thishostname()
  - ei_thisalivename()
  - ei_self()
  - *ei_gethostbyname()
  - *ei_gethostbyaddr()
  - *ei_gethostbyname_r()
  - *ei_gethostbyaddr_r()
  - ei_get_tracelevel()
  - ei_set_tracelevel()
- registry
  - Top of manual page
  - ei_reg_open()
  - ei_reg_resize()
  - ei_reg_close()
  - ei_reg_setival()
  - ei_reg_setfval()
  - ei_reg_setsval()
  - ei_reg_setpval()
  - ei_reg_setval()
  - ei_reg_getival()
  - ei_reg_getfval()
  - ei_reg_getsval()
  - ei_reg_getpval()
  - ei_reg_getval()
  - ei_reg_markdirty()
  - ei_reg_delete()
  - ei_reg_stat()
  - ei_reg_tabstat()
  - ei_reg_dump()
  - ei_reg_restore()
  - ei_reg_purge()
- erl_connect
  - Top of manual page
  - erl_connect_init()
  - erl_connect_xinit()
  - erl_connect()
  - erl_xconnect()
  - erl_close_connection()
  - erl_receive()
  - erl_receive_msg()
  - erl_xreceive_msg()
  - erl_send()
  - erl_reg_send()
  - erl_rpc()
  - erl_rpc_to()
  - erl_rpc_from()
  - erl_publish()
  - erl_accept()
  - erl_thiscookie()
  - erl_thisnodename()
  - erl_thishostname()
  - erl_thisalivename()
  - erl_thiscreation()
  - erl_unpublish()
  - *erl_gethostbyname()
  - *erl_gethostbyaddr()
  - *erl_gethostbyname_r()
  - *erl_gethostbyaddr_r()
- erl_error
  - Top of manual page
  - erl_err_msg()
  - erl_err_quit()
  - erl_err_ret()
  - erl_err_sys()
  - erl_errno()
- erl_eterm
  - Top of manual page
  - erl_cons()
  - erl_copy_term()
  - erl_element()
  - erl_init()
  - erl_hd()
  - erl_iolist_to_binary()
  - erl_iolist_to_string()
  - erl_iolist_length()
  - erl_length()
  - erl_mk_atom()
  - erl_mk_binary()
  - erl_mk_empty_list()
  - erl_mk_estring()
  - erl_mk_float()
  - erl_mk_int()
  - erl_mk_list()
  - erl_mk_pid()
  - erl_mk_port()
  - erl_mk_ref()
  - erl_mk_long_ref()
  - erl_mk_string()
  - erl_mk_tuple()
  - erl_mk_uint()
  - erl_mk_var()
  - erl_print_term()
  - erl_set_compat_rel()
  - erl_size()
  - erl_tl()
  - erl_var_content()
- erl_format
  - Top of manual page
  - erl_format()
  - erl_match()
- erl_global
  - Top of manual page
  - erl_global_names()
  - erl_global_register()
  - erl_global_unregister()
  - erl_global_whereis()
- erl_malloc
  - Top of manual page
  - erl_alloc_eterm()
  - erl_eterm_release()
  - erl_eterm_statistics()
  - erl_free_array()
  - erl_free_term()
  - erl_free_compound()
  - erl_malloc()
  - erl_free()
- erl_marshal
  - Top of manual page
  - erl_compare_ext()
  - erl_decode()
  - erl_decode_buf()
  - erl_encode()
  - erl_encode_buf()
  - erl_ext_size()
  - erl_ext_type()
  - erl_peek_ext()
  - erl_term_len()
- erl_call

# erl_eterm

### C LIBRARY

erl_eterm

### LIBRARY SUMMARY

Functions for Erlang Term Construction

### DESCRIPTION

This module contains functions for creating and manipulating Erlang terms.

An Erlang term is represented by a C structure of type ETERM. Applications should not reference any fields in this structure directly, because it may be changed in future releases to provide faster and more compact term storage. Instead, applications should us the macros and functions provided.

The following macros each take a single ETERM pointer as an argument. They return a non-zero value if the test is true, and 0 otherwise:

ERL_IS_INTEGER(t)
  True if t is an integer.
ERL_IS_UNSIGNED_INTEGER(t)
  True if t is an integer.
ERL_IS_FLOAT(t)
  True if t is a floating point number.
ERL_IS_ATOM(t)
  True if t is an atom.
ERL_IS_PID(t)
  True if t is a Pid (process identifier).
ERL_IS_PORT(t)
  True if t is a port.
ERL_IS_REF(t)
  True if t is a reference.
ERL_IS_TUPLE(t)
  True if t is a tuple.
ERL_IS_BINARY(t)
  True if t is a binary.
ERL_IS_LIST(t)
  True if t is a list with zero or more elements.
ERL_IS_EMPTY_LIST(t)
  True if t is an empty list.
ERL_IS_CONS(t)
  True if t is a list with at least one element.

The following macros can be used for retrieving parts of Erlang terms. None of these do any type checking; results are undefined if you pass an ETERM* containing the wrong type. For example, passing a tuple to ERL_ATOM_PTR() will likely result in garbage.

char *ERL_ATOM_PTR(t)
  A string representing atom t.
int ERL_ATOM_SIZE(t)
  The length (in characters) of atom t.
void *ERL_BIN_PTR(t)
  A pointer to the contents of t
int ERL_BIN_SIZE(t)
  The length (in bytes) of binary object t.
int ERL_INT_VALUE(t)
  The integer of t.
unsigned int ERL_INT_UVALUE(t)
  The unsigned integer value of t.
double ERL_FLOAT_VALUE(t)
  The floating point value of t.
ETERM *ERL_PID_NODE(t)
  The Node in pid t.
int ERL_PID_NUMBER(t)
  The sequence number in pid t.
int ERL_PID_SERIAL(t)
  The serial number in pid t.
int ERL_PID_CREATION(t)
  The creation number in pid t.
int ERL_PORT_NUMBER(t)
  The sequence number in port t.
int ERL_PORT_CREATION(t)
  The creation number in port t.
ETERM *ERL_PORT_NODE(t)
  The node in port t.
int ERL_REF_NUMBER(t)
  The first part of the reference number in ref t. Use only for compatibility.
int ERL_REF_NUMBERS(t)
  Pointer to the array of reference numbers in ref t.
int ERL_REF_LEN(t)
  The number of used reference numbers in ref t.
int ERL_REF_CREATION(t)
  The creation number in ref t.
int ERL_TUPLE_SIZE(t)
  The number of elements in tuple t.
ETERM *ERL_CONS_HEAD(t)
  The head element of list t.
ETERM *ERL_CONS_TAIL(t)
  A List representing the tail elements of list t.

### EXPORTS

ETERM * erl_cons(head, tail)

Types:

ETERM *head;
ETERM *tail;

This function concatenates two Erlang terms, prepending head onto tail and thereby creating a cons cell. To make a proper list, tail should always be a list or an empty list. Note that NULL is not a valid list.

head is the new term to be added.

tail is the existing list to which head will be concatenated.

The function returns a new list.

ERL_CONS_HEAD(list) and ERL_CONS_TAIL(list) can be used to retrieve the head and tail components from the list. erl_hd(list) and erl_tl(list) will do the same thing, but check that the argument really is a list.

For example:

    ETERM *list,*anAtom,*anInt;
    anAtom = erl_mk_atom("madonna");
    anInt  = erl_mk_int(21);
    list   = erl_mk_empty_list();
    list   = erl_cons(anAtom, list);
    list   = erl_cons(anInt, list);
     ... /* do some work */
    erl_free_compound(list);

ETERM * erl_copy_term(term)

Types:

ETERM *term;

This function creates and returns a copy of the Erlang term term.

ETERM * erl_element(position, tuple)

Types:

int position;
ETERM *tuple;

This function extracts a specified element from an Erlang tuple.

position specifies which element to retrieve from tuple. The elements are numbered starting from 1.

tuple is an Erlang term containing at least position elements.

The function returns a new Erlang term corresponding to the requested element, or NULL if position was greater than the arity of tuple.

void erl_init(NULL, 0)

Types:

void *NULL;
int 0;

This function must be called before any of the others in the erl_interface library in order to initialize the library functions. The arguments must be specified as erl_init(NULL,0).

ETERM * erl_hd(list)

Types:

ETERM *list;

Extracts the first element from a list.

list is an Erlang term containing a list.

The function returns an Erlang term corresponding to the head element in the list, or a NULL pointer if list was not a list.

ETERM * erl_iolist_to_binary(term)

Types:

ETERM *list;

This function converts an IO list to a binary term.

list is an Erlang term containing a list.

This function an Erlang binary term, or NULL if list was not an IO list.

Informally, an IO list is a deep list of characters and binaries which can be sent to an Erlang port. In BNF, an IO list is formally defined as follows:

    iolist ::= []
            |   Binary
            |   [iohead | iolist]
            ;
    iohead ::= Binary
            |   Byte (integer in the range [0..255])
            |   iolist
            ;

char * erl_iolist_to_string(list)

Types:

ETERM *list;

This function converts an IO list to a '\0' terminated C string.

list is an Erlang term containing an IO list. The IO list must not contain the integer 0, since C strings may not contain this value except as a terminating marker.

This function returns a pointer to a dynamically allocated buffer containing a string. If list is not an IO list, or if list contains the integer 0, NULL is returned. It is the caller's responsibility free the allocated buffer with erl_free().

Refer to erl_iolist_to_binary() for the definition of an IO list.

int erl_iolist_length(list)

Types:

ETERM *list;

Returns the length of an IO list.

list is an Erlang term containing an IO list.

The function returns the length of list, or -1 if list is not an IO list.

Refer to erl_iolist_to_binary() for the definition of an IO list.

int erl_length(list)

Types:

ETERM *list;

Determines the length of a proper list.

list is an Erlang term containing proper list. In a proper list, all tails except the last point to another list cell, and the last tail points to an empty list.

Returns -1 if list is not a proper list.

ETERM * erl_mk_atom(string)

Types:

char *string;

Creates an atom.

string is the sequence of characters that will be used to create the atom.

Returns an Erlang term containing an atom. Note that it is the callers responsibility to make sure that string contains a valid name for an atom.

ERL_ATOM_PTR(atom) can be used to retrieve the atom name (as a string). Note that the string is not 0-terminated in the atom. ERL_ATOM_SIZE(atom)returns the length of the atom name.

ETERM * erl_mk_binary(bptr, size)

Types:

char *bptr;
int size;

This function produces an Erlang binary object from a buffer containing a sequence of bytes.

bptr is a pointer to a buffer containing data to be converted.

size indicates the length of bptr.

The function returns an Erlang binary object.

ERL_BIN_PTR(bin) retrieves a pointer to the binary data. ERL_BIN_SIZE(bin) retrieves the size.

ETERM * erl_mk_empty_list()

This function creates and returns an empty Erlang list. Note that NULL is not used to represent an empty list; Use this function instead.

ETERM * erl_mk_estring(string, len)

Types:

char *string;
int len;

This function creates a list from a sequence of bytes.

string is a buffer containing a sequence of bytes. The buffer does not need to be zero-terminated.

len is the length of string.

The function returns an Erlang list object corresponding to the character sequence in string.

ETERM * erl_mk_float(f)

Types:

double f;

Creates an Erlang float.

f is a value to be converted to an Erlang float.

The function returns an Erlang float object with the value specified in f.

ERL_FLOAT_VALUE(t) can be used to retrieve the value from an Erlang float.

ETERM * erl_mk_int(n)

Types:

int n;

Creates an Erlang integer.

n is a value to be converted to an Erlang integer.

The function returns an Erlang integer object with the value specified in n.

ERL_INT_VALUE(t) can be used to retrieve the value value from an Erlang integer.

ETERM * erl_mk_list(array, arrsize)

Types:

ETERM **array;
int arrsize;

Creates an Erlang list from an array of Erlang terms, such that each element in the list corresponds to one element in the array.

array is an array of Erlang terms.

arrsize is the number of elements in array.

The function creates an Erlang list object, whose length arrsize and whose elements are taken from the terms in array.

ETERM * erl_mk_pid(node, number, serial, creation)

Types:

const char *node;
unsigned int number;
unsigned int serial;
unsigned int creation;

This function creates an Erlang process identifier. The resulting pid can be used by Erlang processes wishing to communicate with the C node.

node is the name of the C node.

number, serial and creation are arbitrary numbers. Note though, that these are limited in precision, so only the low 15, 3 and 2 bits of these numbers are actually used.

The function returns an Erlang pid object.

ERL_PID_NODE(pid), ERL_PID_NUMBER(pid), ERL_PID_SERIAL(pid) and ERL_PID_CREATION(pid) can be used to retrieve the four values used to create the pid.

ETERM * erl_mk_port(node, number, creation)

Types:

const char *node;
unsigned int number;
unsigned int creation;

This function creates an Erlang port identifier.

node is the name of the C node.

number and creation are arbitrary numbers. Note though, that these are limited in precision, so only the low 18 and 2 bits of these numbers are actually used.

The function returns an Erlang port object.

ERL_PORT_NODE(port), ERL_PORT_NUMBER(port) and ERL_PORT_CREATION can be used to retrieve the three values used to create the port.

ETERM * erl_mk_ref(node, number, creation)

Types:

const char *node;
unsigned int number;
unsigned int creation;

This function creates an old Erlang reference, with only 18 bits - use erl_mk_long_ref instead.

node is the name of the C node.

number should be chosen uniquely for each reference created for a given C node.

creation is an arbitrary number.

Note that number and creation are limited in precision, so only the low 18 and 2 bits of these numbers are actually used.

The function returns an Erlang reference object.

ERL_REF_NODE(ref), ERL_REF_NUMBER(ref), and ERL_REF_CREATION(ref) to retrieve the three values used to create the reference.

ETERM * erl_mk_long_ref(node, n1, n2, n3, creation)

Types:

const char *node;
unsigned int n1, n2, n3;
unsigned int creation;

This function creates an Erlang reference, with 82 bits.

node is the name of the C node.

n1, n2 and n3 can be seen as one big number n1*2^64+n2*2^32+n3 which should be chosen uniquely for each reference created for a given C node.

creation is an arbitrary number.

Note that n3 and creation are limited in precision, so only the low 18 and 2 bits of these numbers are actually used.

The function returns an Erlang reference object.

ERL_REF_NODE(ref), ERL_REF_NUMBERS(ref), ERL_REF_LEN(ref) and ERL_REF_CREATION(ref) to retrieve the values used to create the reference.

ETERM * erl_mk_string(string)

Types:

char *string;

This function creates a list from a zero terminated string.

string is the zero-terminated sequence of characters (i.e. a C string) from which the list will be created.

The function returns an Erlang list.

ETERM * erl_mk_tuple(array, arrsize)

Types:

ETERM **array;
int arrsize;

Creates an Erlang tuple from an array of Erlang terms.

array is an array of Erlang terms.

arrsize is the number of elements in array.

The function creates an Erlang tuple, whose arity is size and whose elements are taken from the terms in array.

To retrieve the size of a tuple, either use the erl_size function (which checks the type of the checked term and works for a binary as well as for a tuple), or the ERL_TUPLE_SIZE(tuple) returns the arity of a tuple. erl_size() will do the same thing, but it checks that the argument really is a tuple. erl_element(index,tuple) returns the element corresponding to a given position in the tuple.

ETERM * erl_mk_uint(n)

Types:

unsigned int n;

Creates an Erlang unsigned integer.

n is a value to be converted to an Erlang unsigned integer.

The function returns an Erlang unsigned integer object with the value specified in n.

ERL_INT_UVALUE(t) can be used to retrieve the value from an Erlang unsigned integer.

ETERM * erl_mk_var(name)

Types:

char *name;

This function creates an unbound Erlang variable. The variable can later be bound through pattern matching or assignment.

name specifies a name for the variable.

The function returns an Erlang variable object with the name name.

int erl_print_term(stream, term)

Types:

FILE *stream;
ETERM *term;

This function prints the specified Erlang term to the given output stream.

stream indicates where the function should send its output.

term is the Erlang term to print.

The function returns the number of characters written, or a negative value if there was an error.

void erl_set_compat_rel(release_number)

Types:

unsigned release_number;

By default, the erl_interface library is only guaranteed to be compatible with other Erlang/OTP components from the same release as the erl_interface library itself. For example, erl_interface from the OTP R10 release is not compatible with an Erlang emulator from the OTP R9 release by default.

A call to erl_set_compat_rel(release_number) sets the erl_interface library in compatibility mode of release release_number. Valid range of release_number is [7, current release]. This makes it possible to communicate with Erlang/OTP components from earlier releases.

Note

If this function is called, it may only be called once directly after the call to the erl_init() function.

Warning

You may run into trouble if this feature is used carelessly. Always make sure that all communicating components are either from the same Erlang/OTP release, or from release X and release Y where all components from release Y are in compatibility mode of release X.

int erl_size(term)

Types:

ETERM *term;

Returns the arity of an Erlang tuple, or the number of bytes in an Erlang binary object.

term is an Erlang tuple or an Erlang binary object.

The function returns the size of term as described above, or -1 if term is not one of the two supported types.

ETERM * erl_tl(list)

Types:

ETERM *list;

Extracts the tail from a list.

list is an Erlang term containing a list.

The function returns an Erlang list corresponding to the original list minus the first element, or NULL pointer if list was not a list.

ETERM * erl_var_content(term, name)

Types:

ETERM *term;
char *name;

This function returns the contents of the specified variable in an Erlang term.

term is an Erlang term. In order for this function to succeed, term must be an Erlang variable with the specified name, or it must be an Erlang list or tuple containing a variable with the specified name. Other Erlang types cannot contain variables.

name is the name of an Erlang variable.

Returns the Erlang object corresponding to the value of name in term. If no variable with the name name was found in term, or if term is not a valid Erlang term, NULL is returned.