44
44
#include "externalterm.h"
45
45
#include "globalcontext.h"
46
46
#include "interop.h"
47
+ #include "intn.h"
47
48
#include "mailbox.h"
48
49
#include "memory.h"
49
50
#include "module.h"
@@ -2185,8 +2186,10 @@ static term nif_erlang_atom_to_list_1(Context *ctx, int argc, term argv[])
2185
2186
}
2186
2187
2187
2188
static term integer_to_buf (Context * ctx , int argc , term argv [], char * tmp_buf , size_t tmp_buf_size ,
2188
- char * * int_buf , size_t * int_len )
2189
+ char * * int_buf , size_t * int_len , bool * needs_cleanup )
2189
2190
{
2191
+ * needs_cleanup = false;
2192
+
2190
2193
term value = argv [0 ];
2191
2194
avm_int_t base = 10 ;
2192
2195
VALIDATE_VALUE (value , term_is_any_integer );
@@ -2225,8 +2228,13 @@ static term integer_to_buf(Context *ctx, int argc, term argv[], char *tmp_buf, s
2225
2228
break ;
2226
2229
}
2227
2230
#endif
2228
- default :
2229
- abort ();
2231
+ default : {
2232
+ size_t boxed_size = term_intn_size (value );
2233
+ size_t digits_per_term = sizeof (term ) / sizeof (intn_digit_t );
2234
+ intn_digit_t * dest_buf = (void * ) term_intn_data (value );
2235
+ * int_buf = intn_to_string (dest_buf , boxed_size * digits_per_term , base , int_len );
2236
+ * needs_cleanup = true;
2237
+ }
2230
2238
}
2231
2239
}
2232
2240
@@ -2235,17 +2243,14 @@ static term integer_to_buf(Context *ctx, int argc, term argv[], char *tmp_buf, s
2235
2243
2236
2244
static term nif_erlang_integer_to_binary_2 (Context * ctx , int argc , term argv [])
2237
2245
{
2238
- #ifdef INT64_TO_A_BUF_LEN
2239
2246
size_t tmp_buf_size = INT64_WRITE_TO_ASCII_BUF_LEN ;
2240
- #else
2241
- size_t tmp_buf_size = INTPTR_WRITE_TO_ASCII_BUF_LEN ;
2242
- #endif
2243
2247
char tmp_buf [tmp_buf_size ];
2244
2248
2245
2249
char * int_buf ;
2246
2250
size_t int_len ;
2251
+ bool needs_cleanup ;
2247
2252
term maybe_fail_ret
2248
- = integer_to_buf (ctx , argc , argv , tmp_buf , tmp_buf_size , & int_buf , & int_len );
2253
+ = integer_to_buf (ctx , argc , argv , tmp_buf , tmp_buf_size , & int_buf , & int_len , & needs_cleanup );
2249
2254
if (UNLIKELY (term_is_invalid_term (maybe_fail_ret ))) {
2250
2255
return maybe_fail_ret ;
2251
2256
}
@@ -2255,27 +2260,36 @@ static term nif_erlang_integer_to_binary_2(Context *ctx, int argc, term argv[])
2255
2260
RAISE_ERROR (OUT_OF_MEMORY_ATOM );
2256
2261
}
2257
2262
2258
- return term_from_literal_binary (int_buf , int_len , & ctx -> heap , ctx -> global );
2263
+ term ret = term_from_literal_binary (int_buf , int_len , & ctx -> heap , ctx -> global );
2264
+
2265
+ if (needs_cleanup ) {
2266
+ free (int_buf );
2267
+ }
2268
+
2269
+ return ret ;
2259
2270
}
2260
2271
2261
2272
static term nif_erlang_integer_to_list_2 (Context * ctx , int argc , term argv [])
2262
2273
{
2263
- #ifdef INT64_TO_A_BUF_LEN
2264
2274
size_t tmp_buf_size = INT64_WRITE_TO_ASCII_BUF_LEN ;
2265
- #else
2266
- size_t tmp_buf_size = INTPTR_WRITE_TO_ASCII_BUF_LEN ;
2267
- #endif
2268
2275
char tmp_buf [tmp_buf_size ];
2269
2276
2270
2277
char * int_buf ;
2271
2278
size_t int_len ;
2279
+ bool needs_cleanup ;
2272
2280
term maybe_fail_ret
2273
- = integer_to_buf (ctx , argc , argv , tmp_buf , tmp_buf_size , & int_buf , & int_len );
2281
+ = integer_to_buf (ctx , argc , argv , tmp_buf , tmp_buf_size , & int_buf , & int_len , & needs_cleanup );
2274
2282
if (UNLIKELY (term_is_invalid_term (maybe_fail_ret ))) {
2275
2283
return maybe_fail_ret ;
2276
2284
}
2277
2285
2278
- return make_list_from_ascii_buf ((uint8_t * ) int_buf , int_len , ctx );
2286
+ term ret = make_list_from_ascii_buf ((uint8_t * ) int_buf , int_len , ctx );
2287
+
2288
+ if (needs_cleanup ) {
2289
+ free (int_buf );
2290
+ }
2291
+
2292
+ return ret ;
2279
2293
}
2280
2294
2281
2295
static int format_float (term value , int scientific , int decimals , int compact , char * out_buf , int outbuf_len )
0 commit comments