@@ -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 */
13291343static 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 */
14031426static inline void term_bigint_size_requirements (
14041427 size_t n , size_t * intn_data_size , size_t * rounded_num_len )
0 commit comments