Skip to content

Commit c06ba96

Browse files
committed
term: rename and clarify BOXED_INTN_SIZE macro
Rename it to BOXED_BIGINT_HEAP_SIZE, and clarify that it must be always used, in order to have the suitable size for allocating space for the bigint term with its boxed header. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 5d59e2c commit c06ba96

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

src/libAtomVM/bif.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ static term make_bigint(Context *ctx, uint32_t fail_label, uint32_t live,
812812
term_bigint_size_requirements(count, &intn_data_size, &rounded_res_len);
813813

814814
if (UNLIKELY(memory_ensure_free_with_roots(
815-
ctx, BOXED_INTN_SIZE(intn_data_size), live, ctx->x, MEMORY_CAN_SHRINK)
815+
ctx, BOXED_BIGINT_HEAP_SIZE(intn_data_size), live, ctx->x, MEMORY_CAN_SHRINK)
816816
!= MEMORY_GC_OK)) {
817817
RAISE_ERROR_BIF(fail_label, OUT_OF_MEMORY_ATOM);
818818
}

src/libAtomVM/externalterm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ static int calculate_heap_usage(const uint8_t *external_term_buf, size_t remaini
10121012
size_t data_size;
10131013
size_t unused_rounded_len;
10141014
term_bigint_size_requirements(required_digits, &data_size, &unused_rounded_len);
1015-
return BOXED_INTN_SIZE(data_size);
1015+
return BOXED_BIGINT_HEAP_SIZE(data_size);
10161016
}
10171017

10181018
case ATOM_UTF8_EXT:

src/libAtomVM/jit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ static term jit_alloc_big_integer_fragment(
629629
size_t rounded_res_len;
630630
term_bigint_size_requirements(digits_len, &intn_data_size, &rounded_res_len);
631631

632-
if (UNLIKELY(memory_init_heap(&heap, BOXED_INTN_SIZE(intn_data_size)) != MEMORY_GC_OK)) {
632+
if (UNLIKELY(memory_init_heap(&heap, BOXED_BIGINT_HEAP_SIZE(intn_data_size)) != MEMORY_GC_OK)) {
633633
ctx->x[0] = ERROR_ATOM;
634634
ctx->x[1] = OUT_OF_MEMORY_ATOM;
635635
return term_invalid_term();

src/libAtomVM/nifs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ static term make_bigint(Context *ctx, const intn_digit_t bigres[], size_t bigres
20592059
size_t rounded_res_len;
20602060
term_bigint_size_requirements(bigres_len, &intn_data_size, &rounded_res_len);
20612061

2062-
if (UNLIKELY(memory_ensure_free(ctx, BOXED_INTN_SIZE(intn_data_size)) != MEMORY_GC_OK)) {
2062+
if (UNLIKELY(memory_ensure_free(ctx, BOXED_BIGINT_HEAP_SIZE(intn_data_size)) != MEMORY_GC_OK)) {
20632063
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
20642064
}
20652065

src/libAtomVM/opcodesswitch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ static bool maybe_call_native(Context *ctx, atom_index_t module_name, atom_index
18441844

18451845
Heap heap;
18461846
if (UNLIKELY(
1847-
memory_init_heap(&heap, BOXED_INTN_SIZE(intn_data_size)) != MEMORY_GC_OK)) {
1847+
memory_init_heap(&heap, BOXED_BIGINT_HEAP_SIZE(intn_data_size)) != MEMORY_GC_OK)) {
18481848
ctx->x[0] = ERROR_ATOM;
18491849
ctx->x[1] = OUT_OF_MEMORY_ATOM;
18501850
*out_term = term_invalid_term();

src/libAtomVM/term.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ extern "C" {
145145
#define FUNCTION_REFERENCE_SIZE 4
146146
#define BOXED_INT_SIZE (BOXED_TERMS_REQUIRED_FOR_INT + 1)
147147
#define BOXED_INT64_SIZE (BOXED_TERMS_REQUIRED_FOR_INT64 + 1)
148-
#define BOXED_INTN_SIZE(term_size) ((term_size) + 1)
149148
#define BOXED_FUN_SIZE 3
150149
#define FLOAT_SIZE (sizeof(float_term_t) / sizeof(term) + 1)
151150
#define REF_SIZE ((int) ((sizeof(uint64_t) / sizeof(term)) + 1))
@@ -188,6 +187,17 @@ extern "C" {
188187
#define TERM_BINARY_HEAP_SIZE(size) \
189188
(TERM_BINARY_DATA_SIZE_IN_TERMS(size) + BINARY_HEADER_SIZE)
190189

190+
/**
191+
* @def BOXED_BIGINT_HEAP_SIZE(term_size)
192+
* @brief Calculate total heap allocation size for a bigint including header
193+
*
194+
* @param term_size Data size in terms (from \c term_bigint_size_requirements())
195+
* @return Total heap size needed including 1 term for boxed header
196+
*
197+
* @see term_bigint_size_requirements() which provides the term_size parameter
198+
*/
199+
#define BOXED_BIGINT_HEAP_SIZE(term_size) ((term_size) + 1)
200+
191201
#define TERM_DEBUG_ASSERT(...)
192202

193203
#define TERM_FROM_ATOM_INDEX(atom_index) ((atom_index << TERM_IMMED2_TAG_SIZE) | TERM_IMMED2_ATOM)
@@ -1308,7 +1318,7 @@ static inline size_t term_boxed_integer_size(avm_int64_t value)
13081318
* boxed header with size and sign information. The digit data area is left
13091319
* uninitialized and must be filled using \c term_initialize_bigint().
13101320
*
1311-
* @param n Size of data area in terms (not digits)
1321+
* @param n Size of data area in terms (not digits), from \c term_bigint_size_requirements()
13121322
* @param sign Sign of the integer (\c TERM_INTEGER_POSITIVE or \c TERM_INTEGER_NEGATIVE)
13131323
* @param heap Heap to allocate from
13141324
* @return Newly created uninitialized bigint term
@@ -1320,11 +1330,15 @@ static inline size_t term_boxed_integer_size(avm_int64_t value)
13201330
* @post Header contains size and sign information
13211331
* @post Data area is uninitialized and must be filled before use
13221332
*
1333+
* @warning When ensuring heap space, use \c BOXED_BIGINT_HEAP_SIZE(n) to include
1334+
* the header term in the allocation size
1335+
*
13231336
* @note The size n is in terms, not \c intn_digit_t digits
13241337
* @note Use \c term_bigint_size_requirements() to calculate appropriate n value
13251338
*
13261339
* @see term_initialize_bigint() to fill the allocated data area
13271340
* @see term_bigint_size_requirements() to calculate required size
1341+
* @see BOXED_BIGINT_HEAP_SIZE() to calculate total heap allocation including header
13281342
*/
13291343
static inline term term_create_uninitialized_bigint(size_t n, term_integer_sign_t sign, Heap *heap)
13301344
{
@@ -1376,7 +1390,7 @@ static inline void term_initialize_bigint(
13761390
* systems (where term = 2 × intn_digit_t), including alignment requirements.
13771391
*
13781392
* @param n Number of non-zero \c intn_digit_t digits in the integer
1379-
* @param[out] intn_data_size Number of terms needed for storage
1393+
* @param[out] intn_data_size Number of terms needed for storage (excludes header)
13801394
* @param[out] rounded_num_len Rounded number of digits for zero-padding
13811395
*
13821396
* @pre n > 0
@@ -1386,6 +1400,9 @@ static inline void term_initialize_bigint(
13861400
* @post *intn_data_size > \c BOXED_TERMS_REQUIRED_FOR_INT64 (ensures bigint distinction)
13871401
* @post *rounded_num_len >= n (includes padding for alignment)
13881402
*
1403+
* @warning The returned intn_data_size does NOT include the boxed header term.
1404+
* Use \c BOXED_BIGINT_HEAP_SIZE(intn_data_size) when allocating heap space.
1405+
*
13891406
* @note Forces minimum size > \c BOXED_TERMS_REQUIRED_FOR_INT64 to distinguish
13901407
* bigints from regular boxed int64 values (which use two's complement)
13911408
* @note Rounds up to 8-byte boundaries for alignment
@@ -1396,9 +1413,15 @@ static inline void term_initialize_bigint(
13961413
* size_t count = intn_count_digits(bigint, bigint_len);
13971414
* size_t intn_data_size, rounded_res_len;
13981415
* term_bigint_size_requirements(count, &intn_data_size, &rounded_res_len);
1416+
*
1417+
* // Ensure heap has space for data + header
1418+
* memory_ensure_free(ctx, BOXED_BIGINT_HEAP_SIZE(intn_data_size));
1419+
*
13991420
* term t = term_create_uninitialized_bigint(intn_data_size, sign, heap);
14001421
* term_initialize_bigint(t, bigint, count, rounded_res_len);
14011422
* @endcode
1423+
*
1424+
* @see BOXED_BIGINT_HEAP_SIZE() to include header in heap allocation
14021425
*/
14031426
static inline void term_bigint_size_requirements(
14041427
size_t n, size_t *intn_data_size, size_t *rounded_num_len)

0 commit comments

Comments
 (0)