Skip to content

Commit 9624f63

Browse files
committed
externalterm: add externalterm_to_term_copy
Works like externalterm_to_term, but it makes a copy of data stored in the buffer, so it can be safely used from NIFs. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 74775d4 commit 9624f63

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
4747
- Make external term serialize functions available without using `externalterm_to_binary` so terms
4848
can be written directly to a buffer.
4949
- Support for `erlang:list_to_integer/2`
50+
- Add `externalterm_to_term_copy` that can be safely used from NIFs taking temporary buffers
5051

5152
### Changed
5253

src/libAtomVM/externalterm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ term externalterm_to_term(const void *external_term, size_t size, Context *ctx,
133133
return externalterm_to_term_internal(external_term, size, ctx, opts, &bytes_read, false);
134134
}
135135

136+
term externalterm_to_term_copy(const void *external_term, size_t size, Context *ctx, ExternalTermOpts opts)
137+
{
138+
size_t bytes_read = 0;
139+
return externalterm_to_term_internal(external_term, size, ctx, opts, &bytes_read, true);
140+
}
141+
136142
enum ExternalTermResult externalterm_from_binary(Context *ctx, term *dst, term binary, size_t *bytes_read)
137143
{
138144
if (!term_is_binary(binary)) {

src/libAtomVM/externalterm.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ typedef enum
6666
term externalterm_to_term(
6767
const void *external_term, size_t size, Context *ctx, ExternalTermOpts opts);
6868

69+
/**
70+
* @brief Gets a term from external term data, and makes a copy of all data.
71+
*
72+
* @details Deserialize an external term from external format and returns a term.
73+
* @param external_term the external term that will be deserialized.
74+
* @param size to allocate for term.
75+
* @param ctx the context that owns the memory that will be allocated.
76+
* @param opts if non-zero, use a heap fragment to store the generated
77+
* terms. Otherwise, use the heap in the provided context. Note that when using the
78+
* context heap, this function may call the GC, if there is insufficient space to
79+
* store the generated terms.
80+
* @returns a term.
81+
*/
82+
term externalterm_to_term_copy(
83+
const void *external_term, size_t size, Context *ctx, ExternalTermOpts opts);
84+
6985
/**
7086
* @brief Create a term from a binary.
7187
*

0 commit comments

Comments
 (0)