externalterm.c

Include dependency graph for externalterm.c:

digraph {
    graph [bgcolor="#00000000"]
    node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2]
    edge [color="#1414CE"]
    "4" [label="stdbool.h" tooltip="stdbool.h"]
    "17" [label="refc_binary.h" tooltip="refc_binary.h"]
    "22" [label="atom.h" tooltip="atom.h"]
    "12" [label="assert.h" tooltip="assert.h"]
    "15" [label="utils.h" tooltip="utils.h"]
    "25" [label="synclist.h" tooltip="synclist.h"]
    "18" [label="list.h" tooltip="list.h"]
    "5" [label="stdint.h" tooltip="stdint.h"]
    "7" [label="stdlib.h" tooltip="stdlib.h"]
    "20" [label="context.h" tooltip="context.h"]
    "9" [label="memory.h" tooltip="memory.h"]
    "19" [label="resources.h" tooltip="resources.h"]
    "3" [label="term.h" tooltip="term.h"]
    "16" [label="stddef.h" tooltip="stddef.h"]
    "10" [label="erl_nif.h" tooltip="erl_nif.h"]
    "13" [label="limits.h" tooltip="limits.h"]
    "1" [label="/__w/AtomVM/AtomVM/src/libAtomVM/externalterm.c" tooltip="/__w/AtomVM/AtomVM/src/libAtomVM/externalterm.c" fillcolor="#BFBFBF"]
    "23" [label="atom_table.h" tooltip="atom_table.h"]
    "2" [label="externalterm.h" tooltip="externalterm.h"]
    "8" [label="string.h" tooltip="string.h"]
    "11" [label="term_typedef.h" tooltip="term_typedef.h"]
    "21" [label="globalcontext.h" tooltip="globalcontext.h"]
    "29" [label="bitstring.h" tooltip="bitstring.h"]
    "26" [label="smp.h" tooltip="smp.h"]
    "30" [label="unicode.h" tooltip="unicode.h"]
    "28" [label="timer_list.h" tooltip="timer_list.h"]
    "27" [label="mailbox.h" tooltip="mailbox.h"]
    "6" [label="stdio.h" tooltip="stdio.h"]
    "24" [label="ets.h" tooltip="ets.h"]
    "14" [label="inttypes.h" tooltip="inttypes.h"]
    "17" -> "4" [dir=forward tooltip="include"]
    "17" -> "7" [dir=forward tooltip="include"]
    "17" -> "18" [dir=forward tooltip="include"]
    "17" -> "19" [dir=forward tooltip="include"]
    "22" -> "5" [dir=forward tooltip="include"]
    "22" -> "7" [dir=forward tooltip="include"]
    "15" -> "16" [dir=forward tooltip="include"]
    "15" -> "6" [dir=forward tooltip="include"]
    "15" -> "7" [dir=forward tooltip="include"]
    "25" -> "6" [dir=forward tooltip="include"]
    "25" -> "18" [dir=forward tooltip="include"]
    "25" -> "26" [dir=forward tooltip="include"]
    "18" -> "4" [dir=forward tooltip="include"]
    "20" -> "21" [dir=forward tooltip="include"]
    "20" -> "18" [dir=forward tooltip="include"]
    "20" -> "27" [dir=forward tooltip="include"]
    "20" -> "26" [dir=forward tooltip="include"]
    "20" -> "3" [dir=forward tooltip="include"]
    "20" -> "28" [dir=forward tooltip="include"]
    "9" -> "5" [dir=forward tooltip="include"]
    "9" -> "7" [dir=forward tooltip="include"]
    "9" -> "10" [dir=forward tooltip="include"]
    "9" -> "11" [dir=forward tooltip="include"]
    "9" -> "15" [dir=forward tooltip="include"]
    "19" -> "7" [dir=forward tooltip="include"]
    "19" -> "10" [dir=forward tooltip="include"]
    "19" -> "18" [dir=forward tooltip="include"]
    "19" -> "9" [dir=forward tooltip="include"]
    "3" -> "4" [dir=forward tooltip="include"]
    "3" -> "5" [dir=forward tooltip="include"]
    "3" -> "6" [dir=forward tooltip="include"]
    "3" -> "7" [dir=forward tooltip="include"]
    "3" -> "8" [dir=forward tooltip="include"]
    "3" -> "9" [dir=forward tooltip="include"]
    "3" -> "17" [dir=forward tooltip="include"]
    "3" -> "15" [dir=forward tooltip="include"]
    "3" -> "11" [dir=forward tooltip="include"]
    "10" -> "11" [dir=forward tooltip="include"]
    "1" -> "2" [dir=forward tooltip="include"]
    "1" -> "20" [dir=forward tooltip="include"]
    "1" -> "18" [dir=forward tooltip="include"]
    "1" -> "4" [dir=forward tooltip="include"]
    "1" -> "5" [dir=forward tooltip="include"]
    "1" -> "6" [dir=forward tooltip="include"]
    "1" -> "7" [dir=forward tooltip="include"]
    "1" -> "29" [dir=forward tooltip="include"]
    "1" -> "30" [dir=forward tooltip="include"]
    "1" -> "15" [dir=forward tooltip="include"]
    "23" -> "4" [dir=forward tooltip="include"]
    "23" -> "22" [dir=forward tooltip="include"]
    "2" -> "3" [dir=forward tooltip="include"]
    "11" -> "12" [dir=forward tooltip="include"]
    "11" -> "13" [dir=forward tooltip="include"]
    "11" -> "14" [dir=forward tooltip="include"]
    "11" -> "5" [dir=forward tooltip="include"]
    "21" -> "5" [dir=forward tooltip="include"]
    "21" -> "22" [dir=forward tooltip="include"]
    "21" -> "23" [dir=forward tooltip="include"]
    "21" -> "10" [dir=forward tooltip="include"]
    "21" -> "24" [dir=forward tooltip="include"]
    "21" -> "18" [dir=forward tooltip="include"]
    "21" -> "27" [dir=forward tooltip="include"]
    "21" -> "26" [dir=forward tooltip="include"]
    "21" -> "25" [dir=forward tooltip="include"]
    "21" -> "3" [dir=forward tooltip="include"]
    "21" -> "28" [dir=forward tooltip="include"]
    "29" -> "3" [dir=forward tooltip="include"]
    "29" -> "4" [dir=forward tooltip="include"]
    "29" -> "5" [dir=forward tooltip="include"]
    "26" -> "4" [dir=forward tooltip="include"]
    "30" -> "4" [dir=forward tooltip="include"]
    "30" -> "16" [dir=forward tooltip="include"]
    "30" -> "5" [dir=forward tooltip="include"]
    "28" -> "4" [dir=forward tooltip="include"]
    "28" -> "5" [dir=forward tooltip="include"]
    "28" -> "18" [dir=forward tooltip="include"]
    "27" -> "4" [dir=forward tooltip="include"]
    "27" -> "18" [dir=forward tooltip="include"]
    "27" -> "11" [dir=forward tooltip="include"]
    "27" -> "15" [dir=forward tooltip="include"]
    "24" -> "18" [dir=forward tooltip="include"]
    "24" -> "25" [dir=forward tooltip="include"]
    "24" -> "3" [dir=forward tooltip="include"]
}

Defines

NEW_FLOAT_EXT 70
SMALL_INTEGER_EXT 97
INTEGER_EXT 98
ATOM_EXT 100
SMALL_TUPLE_EXT 104
LARGE_TUPLE_EXT 105
NIL_EXT 106
STRING_EXT 107
LIST_EXT 108
BINARY_EXT 109
SMALL_BIG_EXT 110
EXPORT_EXT 113
MAP_EXT 116
SMALL_ATOM_UTF8_EXT 119
INVALID_TERM_SIZE -1
NEW_FLOAT_EXT_SIZE 9
SMALL_INTEGER_EXT_SIZE 2
INTEGER_EXT_SIZE 5
SMALL_BIG_EXT_BASE_SIZE 3
ATOM_EXT_BASE_SIZE 3
STRING_EXT_BASE_SIZE 3
LIST_EXT_BASE_SIZE 5
BINARY_EXT_BASE_SIZE 5
MAP_EXT_BASE_SIZE 5
SMALL_ATOM_EXT_BASE_SIZE 2
REMOVE_SIGN(val, unsigned_type)     ((val) < 0 ? ~((unsigned_type) (val)) + 1 : (unsigned_type) (val))

Functions

static term parse_external_terms(const uint8_t *external_term_buf, size_t *eterm_size, bool copy, Heap *heap, GlobalContext *glb)
static int calculate_heap_usage(const uint8_t *external_term_buf, size_t remaining, size_t *eterm_size, bool copy)
static size_t compute_external_size(term t, GlobalContext *glb)
static int externalterm_from_term(uint8_t **buf, size_t *len, term t, GlobalContext *glb)
static int serialize_term(uint8_t *buf, term t, GlobalContext *glb)
static term externalterm_to_term_internal(const void *external_term, size_t size, Context *ctx, ExternalTermOpts opts, size_t *bytes_read, bool copy)
Parameters:
  • external_term – buffer containing external term

  • size – size of the external_term

  • ctx – current context in which terms may be stored

  • opts – additional opts, such as ExternalTermToHeapFragment for storing parsed terms in a heap fragment. are stored in the context heap.

  • bytes_read – the number of bytes read off external_term in order to yield a term

  • copy – whether to copy binary data and atom strings (pass true, unless external_term is a const binary and will not be deallocated)

Returns:

the parsed term

term externalterm_to_term(const void *external_term, size_t size, Context *ctx, ExternalTermOpts opts)

Gets a term from external term data.

Deserialize an external term from external format and returns a term.

Parameters:
  • external_term – the external term that will be deserialized.

  • size – to allocate for term.

  • ctx – the context that owns the memory that will be allocated.

  • opts – if non-zero, use a heap fragment to store the generated terms. Otherwise, use the heap in the provided context. Note that when using the context heap, this function may call the GC, if there is insufficient space to store the generated terms.

Returns:

a term.

term externalterm_to_term_copy(const void *external_term, size_t size, Context *ctx, ExternalTermOpts opts)

Gets a term from external term data, and makes a copy of all data.

Deserialize an external term from external format and returns a term.

Parameters:
  • external_term – the external term that will be deserialized.

  • size – to allocate for term.

  • ctx – the context that owns the memory that will be allocated.

  • opts – if non-zero, use a heap fragment to store the generated terms. Otherwise, use the heap in the provided context. Note that when using the context heap, this function may call the GC, if there is insufficient space to store the generated terms.

Returns:

a term.

enum ExternalTermResult externalterm_from_binary(Context *ctx, term *dst, term binary, size_t *bytes_read)

Create a term from a binary.

Deserialize a binary term that stores term data in Erlang external term format, and instantiate the serialized terms. The heap from the context will be used to allocate the instantiated terms. This function is the complement of externalterm_to_binary. WARNING: This function may call the GC, which may render the input binary invalid.

Parameters:
  • ctx – the context that owns the memory that will be allocated.

  • dst – a pointer to a term that will contain the binary encoded term.

  • binary – the binary.

  • bytes_read – the number of bytes read from the input binary.

Returns:

the term deserialized from the input term, or an invalid term, if deserialization fails.

term externalterm_to_binary(Context *ctx, term t)

Create a binary from a term.

Serialize a term in Erlang external term format, and store the result in a binary term. The heap from the context will be used to allocate the hydrated terms. This function is the complement of externalterm_to_binary. WARNING: This function may call the GC, which may render the input binary invalid.

Parameters:
  • ctx – the context that owns the memory that will be allocated.

  • t – the term to return as binary.

Returns:

the term deserialized from the input term, or an invalid term, if deserialization fails.

static uint8_t get_num_bytes(avm_uint64_t val)
static void write_bytes(uint8_t *buf, avm_uint64_t val)
static avm_uint64_t read_bytes(const uint8_t *buf, uint8_t num_bytes)
enum ExternalTermResult externalterm_compute_external_size_raw(term t, size_t *size, GlobalContext *glb)

Computes the size required for a external term (tag excluded)

This function should be called in order to calculate the required buffer size to store a serialized term in external term format. This function doesn’t prepend the external term 1 byte tag.

Parameters:
  • t – the term for which size is calculated

  • size – the required buffer size (tag excluded)

  • glb – the global context

Returns:

EXTERNAL_TERM_OK in case of success

enum ExternalTermResult externalterm_serialize_term_raw(void *buf, term t, GlobalContext *glb)

Serialize a term (tag excluded)

This function serializes in external term format given term, and writes it to the given buffer. This function doesn’t prepend the external term 1 byte tag.

Parameters:
  • buf – the buffer where the external term is written

  • t – the term that will be serialized

  • glb – the global context

Returns:

EXTERNAL_TERM_OK in case of success