term.h
Include dependency graph for term.h:
This graph shows which files directly or indirectly include term.h:
term manipulation functions
This header implements term manipulation functions.
Defines
-
TERM_BOXED_VALUE_TAG 0x2
-
TERM_INTEGER_TAG 0xF
-
TERM_CATCH_TAG 0x1B
-
TERM_BOXED_TAG_MASK 0x3F
-
TERM_BOXED_TUPLE 0x0
-
TERM_BOXED_BIN_MATCH_STATE 0x4
-
TERM_BOXED_POSITIVE_INTEGER 0x8
-
TERM_BOXED_REF 0x10
-
TERM_BOXED_FUN 0x14
-
TERM_BOXED_FLOAT 0x18
-
TERM_BOXED_REFC_BINARY 0x20
-
TERM_BOXED_HEAP_BINARY 0x24
-
TERM_BOXED_MAP 0x3C
-
TERM_BOXED_SUB_BINARY 0x28
-
TERM_UNUSED 0x2B
-
TERM_RESERVED_MARKER(x) ((x << 6) | TERM_UNUSED)
-
TERM_BOXED_REFC_BINARY_SIZE 6
-
TERM_BOXED_BIN_MATCH_STATE_SIZE 4
-
TERM_BOXED_SUB_BINARY_SIZE 4
-
TERM_BOXED_RESOURCE_SIZE TERM_BOXED_REFC_BINARY_SIZE
-
REFC_BINARY_MIN 32
-
SUB_BINARY_MIN 8
-
BINARY_HEADER_SIZE 2
-
FUNCTION_REFERENCE_SIZE 4
-
BOXED_INT_SIZE (BOXED_TERMS_REQUIRED_FOR_INT + 1)
-
BOXED_INT64_SIZE (BOXED_TERMS_REQUIRED_FOR_INT64 + 1)
-
BOXED_FUN_SIZE 3
-
FLOAT_SIZE (sizeof(float_term_t) / sizeof(term) + 1)
-
TUPLE_SIZE(elems) ((int) (elems + 1))
-
CONS_SIZE 2
-
REFC_BINARY_CONS_OFFSET 4
-
TERM_MAP_SIZE(num_elements) (3 + 2 * (num_elements))
-
TERM_MAP_SHARED_SIZE(num_elements) (2 + (num_elements))
-
LIST_HEAD_INDEX 1
-
LIST_TAIL_INDEX 0
-
TERM_BINARY_SIZE_IS_HEAP(size) ((size) < REFC_BINARY_MIN)
-
TERM_BINARY_DATA_SIZE_IN_TERMS(size) (TERM_BINARY_SIZE_IS_HEAP(size) ? (((size) + 4 - 1) >> 2) + 1 : TERM_BOXED_REFC_BINARY_SIZE)
-
TERM_BINARY_HEAP_SIZE(size) (TERM_BINARY_DATA_SIZE_IN_TERMS(size) + BINARY_HEADER_SIZE)
-
TERM_DEBUG_ASSERT(...)
-
TERM_FROM_ATOM_INDEX(atom_index) ((atom_index << 6) | 0xB)
-
REF_AS_CSTRING_LEN 33
-
PID_AS_CSTRING_LEN 17
-
TYPEDEF_GLOBALCONTEXT
-
TERM_MAP_NOT_FOUND -1
-
TERM_MAP_MEMORY_ALLOC_FAIL -2
Typedefs
-
typedef struct GlobalContext GlobalContext
-
typedef struct PrinterFun PrinterFun
- int(* printer_function_t )(PrinterFun *fun, const char *fmt,...) PRINTF_FORMAT_ARGS(2
Enums
Functions
-
TermCompareResult term_compare(term t, term other, TermCompareOpts opts, GlobalContext *global)
Compares two terms.
Tells if first term is >, < or == to the second term.
- Parameters:
t – the first term.
other – the second term.
opts – a value of 1 will compare exact equality, 0 for less strict equality.
global – the global context.
- Returns:
any of TermEquals, TermLessThan, TermGreaterThan or TermCompareMemoryAllocFail error.
-
term term_alloc_refc_binary(size_t size, bool is_const, Heap *heap, GlobalContext *glb)
Create a reference-counted binary on the heap.
This function will create a reference-counted binary on the heap. If the data supplied is “const” (e.g., read from a literal in a BEAM file), then the returned term will point directly to the supplied data, and will not technically be reference-counted. Otherwise, a block of memory will be allocated to contain a copy of the data, in addition to a reference counter, so that the block can be free’d when no other terms reference the created object. (The reference count will be initialized to 1). If the data is non-NULL, it will be copied into the newly allocated block of memory.
- Parameters:
size – the size (in bytes) of the data to allocate
is_const – designates whether the data pointed to is “const”, such as a term literal
heap – the heap to allocate the binary in
glb – the global context as refc binaries are global
- Returns:
a term (reference) pointing to the newly allocated binary in the process heap.
-
term term_alloc_sub_binary(term binary, size_t offset, size_t len, Heap *heap)
Create a sub-binary.
This function will create a sub-binary on the heap, using the supplied binary, offset into the binary, and length of the sub-binary. This function assumes the length of the referenced binary is greater or equal to offset + len.
- Parameters:
binary – the referenced binary
offset – the offset into the referenced binary to start the sub-binary
len – the length (in bytes) of the sub-binary
heap – the heap to allocate the binary in
- Returns:
a term (reference) pointing to the newly allocated sub-binary in the process heap.
-
static inline term *term_to_term_ptr(term t)
Gets a pointer to a term stored on the heap.
Casts a term to a term * that points to a value stored on the heap. Be aware: terms are assumed to be immutable.
- Parameters:
t – the term that will be casted, it must be valid.
- Returns:
a pointer to a term.
-
static inline const term *term_to_const_term_ptr(term t)
Gets a const pointer to a term stored on the heap.
Casts a term to a const term * that points to a value stored on the heap.
- Parameters:
t – the term that will be casted, it must be valid.
- Returns:
a const pointer to a term.
-
static inline int term_is_atom(term t)
Checks if a term is an atom.
Returns 1 if a term is an atom, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_invalid_term(term t)
Check if a term is an invalid term.
Returns 1 if a term is an invalid term, otherwise 0 is returned.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_nil(term t)
Checks if a term is nil.
Returns 1 if a term is nil, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_nonempty_list(term t)
Checks if a term is a non empty list.
Returns 1 if a term is a non empty list (cons), otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_list(term t)
Checks if a term is a list.
Returns 1 if a term is a list (cons) or an empty list (nil term), otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_boxed(term t)
Checks if a term is a boxed value.
Returns 1 if a term is a boxed value stored on the heap, such as a tuple, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_movable_boxed(term t)
Checks if a term is a movable boxed value.
Returns 1 if a term is a boxed value that can be safely copied with memcpy.
- Parameters:
t – the term that will checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline size_t term_get_size_from_boxed_header(term header)
Returns size of a boxed term from its header.
Returns the size that is stored in boxed term header most significant bits.
- Parameters:
header – the boxed term header.
- Returns:
the size of the boxed term that follows the header. 0 is returned if the boxed term is just the header.
-
static inline size_t term_boxed_size(term t)
Returns size of a boxed term.
Returns the size of a boxed term in term units.
- Parameters:
t – the boxed term.
- Returns:
size of given term.
-
static inline int term_is_binary(term t)
Checks if a term is a binary.
Returns 1 if a term is a binary stored on the heap, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline bool term_is_refc_binary(term t)
Checks if a term is a binary.
Returns 1 if a term is a binary stored on the heap, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline bool term_is_sub_binary(term t)
Checks if a term is a sub-binary.
Returns true if a term is a sub-binary; false, otherwise.
- Parameters:
t – the term that will be checked.
- Returns:
true if check succeeds; false, otherwise.
-
static inline int term_is_integer(term t)
Checks if a term is an integer value.
Returns 1 if a term is an integer value, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline uint8_t term_is_uint8(term t)
Checks if a term is a uint8_t.
Returns 1 if a term is an integer value in the [0, 255] range, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_pid(term t)
Checks if a term is a pid.
Returns 1 if a term is a process id, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_tuple(term t)
Checks if a term is a tuple.
Returns 1 if a term is a tuple, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_reference(term t)
Checks if a term is a reference.
Returns 1 if a term is a reference, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline int term_is_function(term t)
Checks if a term is a fun.
Returns 1 if a term is a fun, otherwise 0.
- Deprecated:
renamed to term_is_fun.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline bool term_is_fun(term t)
Checks if a term is a fun.
Returns true if a term is a fun, otherwise false.
- Parameters:
t – the term that will be checked.
- Returns:
true if check succeeds, false otherwise.
-
static inline bool term_is_external_fun(term t)
Checks if a term is an external fun.
Returns true if a term is an external fun such as “fun m:f/a”, otherwise false.
- Parameters:
t – the term that will be checked.
- Returns:
true if check succeeds, false otherwise.
-
static inline int term_is_cp(term t)
Checks if a term is a saved CP.
Returns 1 if a term is a saved continuation pointer, otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline term term_invalid_term()
Gets invalid term.
Returns always an invalid term.
- Returns:
invalid term.
-
static inline term term_nil()
Gets nil value.
Returns always the nil value.
- Returns:
nil value term.
-
static inline int term_to_atom_index(term t)
Gets global atom table index.
Returns atom table index for given atom term.
- Parameters:
t – the term that will be converted to atom table index. t must be a valid atom term.
- Returns:
a global atom table index.
-
static inline term term_from_atom_index(int atom_index)
Term from global atom table index.
Returns a term from the given global atom table index.
- Parameters:
atom_index – global atoms table index.
- Returns:
a term that encapsulates the atom.
-
static inline uint8_t term_to_uint8(term t)
Term to uint8.
Returns an uint8 for a given term. No overflow check is executed.
- Parameters:
t – the term that will be converted to uint8.
- Returns:
an uint8_t value.
-
static inline int32_t term_to_int32(term t)
Term to int32.
Returns an int32 for a given term. No overflow check is executed.
- Parameters:
t – the term that will be converted to int32, term type is checked.
- Returns:
a int32 value.
-
static inline int32_t term_to_local_process_id(term t)
Gets process table index.
Returns local process table index for given atom term.
- Parameters:
t – the term that will be converted to local process table index, term type is checked.
- Returns:
a local process table index.
-
static inline term term_from_int4(int8_t value)
Term from int4.
Returns a term for a given 4 bits integer value.
- Parameters:
value – the value that will be converted to a term.
- Returns:
a term that encapsulates the integer value.
-
static inline term term_from_int11(int16_t value)
Term from int11.
Returns a term for a given 11 bits integer value.
- Parameters:
value – the value that will be converted to a term.
- Returns:
a term that encapsulates the integer value.
-
static inline term term_from_int32(int32_t value)
Term from int32.
Returns a term for a given 32 bits integer value.
- Parameters:
value – the value that will be converted to a term.
- Returns:
a term that encapsulates the integer value.
-
static inline avm_int64_t term_unbox_int64(term boxed_long)
-
static inline avm_int64_t term_maybe_unbox_int64(term maybe_boxed_int)
-
static inline term term_make_boxed_int64(avm_int64_t large_int64, Heap *heap)
-
static inline term term_make_maybe_boxed_int64(avm_int64_t value, Heap *heap)
-
static inline size_t term_boxed_integer_size(avm_int64_t value)
-
static inline term term_from_local_process_id(uint32_t local_process_id)
Term from local process id.
Returns a term for a given local process table index.
- Parameters:
local_process_id – the local process table index that will be converted to a term.
- Returns:
a term that encapsulates a PID.
-
static inline bool term_binary_size_is_heap_binary(size_t size)
Determine whether a binary should be a heap binary or not.
Returns true if a binary of the specified size should be allocated in the process heap (as opposed to being a refc binary)
- Parameters:
size – the intended binary size
- Returns:
true if the binary should be allocated in the process heap; false, otherwise.
-
static inline size_t term_binary_data_size_in_terms(size_t size)
The count of terms needed to store the given amount of bytes.
Returns the count of terms needed to store the given size in bytes.
- Parameters:
size – the size in bytes
- Returns:
the count of terms
-
static inline size_t term_binary_heap_size(size_t size)
The size (in terms) of a binary of size-many bytes in the heap.
Returns the number of terms needed in the heap to store a binary of a given size (in bytes)
- Parameters:
size – the size of the binary (in bytes)
- Returns:
the size (in terms) of a binary of size-many bytes in the heap
-
static inline unsigned long term_binary_size(term t)
Gets binary size.
Returns binary size for a given binary term.
- Parameters:
t – a term pointing to binary data. Fails if t is not a binary term.
- Returns:
binary size in bytes.
-
static inline void *term_refc_binary_ptr(term refc_binary)
Get the pointer off heap refc binary.
Returns address
- Returns:
offset (in words).
-
static inline const char *term_binary_data(term t)
Gets binary data.
Returns a pointer to stored binary data.
- Parameters:
t – a term pointing to binary data. Fails if t is not a binary term.
- Returns:
a const char * pointing to binary internal data.
-
static inline term term_create_uninitialized_binary(size_t size, Heap *heap, GlobalContext *glb)
Create an uninitialized binary.
Allocates a binary on the heap, and returns a term pointing to it. Note that the data in the binary is uninitialized and could contain any garbage. Make sure to initialize before use, if needed (e.g., via memset). The binary may be allocated as a refc if it is large enough.
- Parameters:
size – size of binary data buffer.
heap – the heap to allocate the binary in
glb – the global context as refc binaries are global
- Returns:
a term pointing to the boxed binary pointer.
-
static inline term term_from_literal_binary(const void *data, size_t size, Heap *heap, GlobalContext *glb)
Term from binary data.
Allocates a binary on the heap, and returns a term pointing to it.
- Parameters:
data – binary data.
size – size of binary data buffer.
heap – the heap to allocate the binary in
glb – the global context as refc binaries are global
- Returns:
a term pointing to the boxed binary pointer.
-
static inline size_t term_sub_binary_heap_size(term binary, size_t len)
Get the number of words in the heap to allocate for a sub-binary.
This function is used to compute the number of words needed on the heap to allocate for a sub-binary. This function is typically used in conjunction with term_maybe_create_sub_binary
- Parameters:
binary – source binary
len – desired length of the sub-binary
- Returns:
the number of words needed to allocate on the process heap for the desired sub-binary
-
static inline term term_maybe_create_sub_binary(term binary, size_t offset, size_t len, Heap *heap, GlobalContext *glb)
(Maybe) create a sub-binary — if not, create a heap binary.
Allocates a sub-binary if the source binary is reference-counted binary and if the length of the sub-binary is sufficiently large.
- Parameters:
binary – source binary
offset – offset into the source binary marking the start of the sub-binary
len – desired length of the sub-binary
heap – the heap to allocate memory in
glb – the global context as refc binaries are global
- Returns:
a term pointing to the boxed binary pointer.
-
static inline term term_from_const_binary(const void *data, size_t size, Heap *heap, GlobalContext *glb)
-
static inline term term_create_empty_binary(size_t size, Heap *heap, GlobalContext *glb)
Create an empty binary. All bytes in the binary are initialized to 0x00.
Allocates a binary on the heap, and returns a term pointing to it.
- Parameters:
size – size of binary data buffer.
heap – the heap to allocate memory in
glb – the global context as refc binaries are global
- Returns:
a term pointing to the boxed binary pointer.
-
static inline int term_bs_insert_binary(term t, int offset, term src, int n)
Insert an binary into a binary (using bit syntax).
Insert the data from the input binary, starting at the bit position starting in offset.
- Parameters:
t – a term pointing to binary data. Fails if t is not a binary term.
offset – the bitwise offset in t at which to start writing the integer value
src – binary source to insert binary data into.
n – the number of low-order bits from value to write.
- Returns:
0 on success; non-zero value if: t is not a binary term n is greater than the number of bits in an integer there is insufficient capacity in the binary to write these bits In general, none of these conditions should apply, if this function is being called in the context of generated bit syntax instructions.
-
static inline term term_from_ref_ticks(uint64_t ref_ticks, Heap *heap)
Get a ref term from ref ticks.
- Parameters:
ref_ticks – an unique uint64 value that will be used to create ref term.
heap – the heap to allocate memory in
- Returns:
a ref term created using given ref ticks.
-
static inline term term_alloc_tuple(uint32_t size, Heap *heap)
Allocates a tuple on a context heap.
Allocates an uninitialized tuple on the heap with given arity.
- Parameters:
size – tuple arity (count of tuple elements).
heap – the heap to allocate memory in
- Returns:
a term pointing on an empty tuple allocated on the heap.
-
static inline void term_put_tuple_element(term t, uint32_t elem_index, term put_value)
Replaces the content of a tuple element.
Destructively replaces the nth element of an existing tuple, it should be used only on newly allocated tuples.
- Parameters:
t – the term pointing to the target tuple, fails if not a tuple.
elem_index – the index of the element that will be replaced.
put_value – the term that will be put on the nth tuple element.
-
static inline term term_get_tuple_element(term t, int elem_index)
Returns the nth tuple element.
Returns the nth element for a given tuple pointed by a term.
- Parameters:
t – a term that points to a tuple, fails otherwise.
elem_index – index of the nth element that will be returned.
- Returns:
nth tuple term.
-
static inline term term_from_string(const uint8_t *data, uint16_t size, Heap *heap)
Allocates a new list using string data.
Returns a term that points to a list (cons) that will be created using a string.
- Parameters:
data – a pointer to a string, it doesn’t need to be NULL terminated.
size – of the string/list that will be read and allocated.
heap – the heap to allocate memory in
- Returns:
a term pointing to a list.
-
static inline term *term_get_list_ptr(term t)
Gets a term * pointing to a list.
Returns a term * pointer to a list (cons) from a given term.
- Parameters:
t – a term that points to a valid cons.
- Returns:
a term * pointing to the head of the first cell of a list.
-
static inline term term_list_from_list_ptr(term *list_elem)
Gets list term from pointer.
Return given list term from a list element pointer.
- Parameters:
list_elem – a pointer to a list element.
- Returns:
a list term
-
static inline term term_get_list_head(term t)
Gets list head.
Returns given list head term
- Parameters:
t – a term pointing to a valid list (cons)
- Returns:
list head term
-
static inline term term_get_list_tail(term t)
Gets list item tail.
Returns the tail, which is either a pointer to the next list item or nil, of the given list (that is not list tail).
- Returns:
list item tail term.
- static inline MALLOC_LIKE term * term_list_alloc (Heap *heap)
Allocate uninitialized memory for a list item.
Allocates a memory area that will be used to store a list item.
- Parameters:
heap – the heap to allocate memory in
- Returns:
a pointer to a newly allocated memory area.
-
static inline term term_list_init_prepend(term *list_elem, term head, term tail)
Prepends a term to an existing list.
Initializes a list item, set head to the given term and points tail to the given next item (that might be nil).
- Parameters:
head – term, the encapsulated list item value.
tail – either nil or next list item.
list_elem – the memory area that will be initialized.
- Returns:
a term pointing to the newly initialized list item.
-
static inline term term_list_prepend(term head, term tail, Heap *heap)
Prepends a term to an existing list.
Allocates a new list item, set head to the given term and points tail to the given next item (that might be nil).
- Parameters:
head – term, the encapsulated list item value.
tail – either nil or next list item.
heap – the heap to allocate memory in
- Returns:
a term pointing to the newly created list item.
-
static inline int term_list_length(term t, int *proper)
Returns list length.
Counts the number of list items
- Returns:
number of list items
-
static inline term term_from_float(avm_float_t f, Heap *heap)
-
static inline avm_float_t term_to_float(term t)
-
static inline avm_float_t term_conv_to_float(term t)
-
void term_display(FILE *fd, term t, const Context *ctx)
Prints a term to stdout.
Print any term to the given file.
- Parameters:
fd – the file where the term will be printed.
t – the term that will be printed.
ctx – the context.
-
int term_funprint(PrinterFun *pf, term t, const GlobalContext *global)
Prints a term using given printer fun.
Print any given term using a printer fun
- Parameters:
pf – function that will handle printing.
t – the term that will be printed.
global – the
GlobalContext
.
- Returns:
the number of printed characters.
-
int term_fprint(FILE *fd, term t, const GlobalContext *global)
Prints a term to the given file.
Print any given term to the given file.
- Parameters:
fd – the file where the term will be printed.
t – the term that will be printed.
global – the
GlobalContext
.
- Returns:
the number of printed characters.
-
int term_snprint(char *buf, size_t size, term t, const GlobalContext *global)
Write a term to a string as text.
Print any given term to the given buffer.
- Parameters:
buf – the buffer where the term will be printed.
size – the buffer size.
t – the term that will be printed.
global – the
GlobalContext
.
- Returns:
the number of printed characters.
-
static inline int term_is_string(term t)
Checks if a term is a string (i.e., a list of characters)
Returns 1 if a term is a proper (nil-terminated) list of characters or an empty list (nil term), otherwise 0.
- Parameters:
t – the term that will be checked.
- Returns:
1 if check succeeds, 0 otherwise.
-
static inline void term_truncate_boxed(term boxed, size_t new_size, Heap *heap)
truncates last allocated boxed term to given size
This function can be used to shrink last allocated boxed term
- Parameters:
boxed – the boxed term that will be shrinked (it must be the last allocated)
new_size – in terms
heap – the heap where the term has been allocated
-
static inline size_t term_get_map_keys_offset()
-
static inline size_t term_get_map_value_offset()
-
static inline size_t term_map_size_in_terms(size_t num_entries)
-
static inline term term_alloc_map(avm_uint_t size, Heap *heap)
-
static inline void term_set_map_assoc(term map, avm_uint_t pos, term key, term value)
-
static inline term term_get_map_key(term map, avm_uint_t pos)
-
static inline term term_get_map_value(term map, avm_uint_t pos)
-
static inline void term_set_map_value(term map, avm_uint_t pos, term value)
-
static inline int term_find_map_pos(term map, term key, GlobalContext *global)
-
term term_get_map_assoc(term map, term key, GlobalContext *glb)
-
static inline term term_get_map_assoc_default(term map, term key, term default_value, GlobalContext *glb)
-
static inline term term_from_resource(void *resource, Heap *heap)
Create a resource on the heap.
This function creates a resource (obtained from
enif_alloc_resource
) on the heap which must haveTERM_BOXED_RESOURCE_SIZE
free terms.Unlike
enif_make_resource
, this function doesn’t increment the reference counter but instead makes the heap own the resource. It will be garbage collected when the heap is destroyed.- Parameters:
resource – resource obtained from
enif_alloc_resource
heap – the heap to allocate the resource in
- Returns:
a term pointing to the resource
Variables
-
const term empty_tuple
All empty tuples will reference this.
-
struct PrinterFun
- #include <term.h>
Collaboration diagram for PrinterFun:
Public Members
-
printer_function_t print
-
printer_function_t print