Commit 1add4951

1add49514304Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · ~2 modified

Description

feat: integrate bigint with Spinel GC

Bigint objects are now allocated via sp_gc_alloc when available, with a finalizer that calls mpz_clear to free internal limb buffers.

  • sp_bigint_alloc() uses weak extern sp_gc_alloc: if linked with Spinel runtime, uses GC; otherwise falls back to calloc
  • sp_gc_alloc made non-static when @needs_bigint (extern visible)
  • Intermediate bigint results are now collected by GC

Memory improvement (pidigits n=5): Before: 339 blocks (7,940 bytes) definitely lost After: 16 blocks (188 bytes) — only GC free list residuals

67 tests pass, bootstrap clean.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
1add49514304
Parent
177d70f1452f

lib/sp_bigint.cmodifiedView file

@@ -5262,6 +5262,9 @@
52625262 mpz_t mpz;
52635263 } sp_Bigint;
52645264
5265+/* GC integration: use Spinel's GC allocator when available */
5266+extern void *sp_gc_alloc(size_t sz, void(*fin)(void*), void(*scn)(void*)) __attribute__((weak));
5267+
52655268 static mpz_ctx_t sp_mpz_ctx_storage;
52665269 static mpz_ctx_t *sp_mpz_ctx = NULL;
52675270
@@ -5274,16 +5277,27 @@
52745277 }
52755278 }
52765279
5280+static void sp_bigint_finalizer(void *p) {
5281+ sp_Bigint *b = (sp_Bigint*)p;
5282+ mpz_clear(sp_mpz_ctx, &b->mpz);
5283+}
5284+
5285+static sp_Bigint *sp_bigint_alloc(void) {
5286+ sp_bigint_init_ctx();
5287+ if (sp_gc_alloc) {
5288+ return (sp_Bigint*)sp_gc_alloc(sizeof(sp_Bigint), sp_bigint_finalizer, NULL);
5289+ }
5290+ return (sp_Bigint*)calloc(1, sizeof(sp_Bigint));
5291+}
5292+
52775293 sp_Bigint *sp_bigint_new_int(int64_t v) {
5278- sp_bigint_init_ctx();
5279- sp_Bigint *b = (sp_Bigint*)malloc(sizeof(sp_Bigint));
5294+ sp_Bigint *b = sp_bigint_alloc();
52805295 mpz_init_set_int(sp_mpz_ctx, &b->mpz, (mrb_int)v);
52815296 return b;
52825297 }
52835298
52845299 sp_Bigint *sp_bigint_new_str(const char *s, int base) {
5285- sp_bigint_init_ctx();
5286- sp_Bigint *b = (sp_Bigint*)malloc(sizeof(sp_Bigint));
5300+ sp_Bigint *b = sp_bigint_alloc();
52875301 mpz_init(sp_mpz_ctx, &b->mpz);
52885302 mpz_init_set_str(sp_mpz_ctx, &b->mpz, s, (mrb_int)strlen(s), base);
52895303 return b;
@@ -5292,47 +5306,47 @@
52925306 void sp_bigint_free(sp_Bigint *b) {
52935307 if (b) {
52945308 mpz_clear(sp_mpz_ctx, &b->mpz);
5295- free(b);
5309+ if (!sp_gc_alloc) free(b);
52965310 }
52975311 }
52985312
52995313 sp_Bigint *sp_bigint_add(sp_Bigint *a, sp_Bigint *b) {
5300- sp_Bigint *r = (sp_Bigint*)malloc(sizeof(sp_Bigint));
5314+ sp_Bigint *r = sp_bigint_alloc();
53015315 mpz_init(sp_mpz_ctx, &r->mpz);
53025316 mpz_add(sp_mpz_ctx, &r->mpz, &a->mpz, &b->mpz);
53035317 return r;
53045318 }
53055319
53065320 sp_Bigint *sp_bigint_sub(sp_Bigint *a, sp_Bigint *b) {
5307- sp_Bigint *r = (sp_Bigint*)malloc(sizeof(sp_Bigint));
5321+ sp_Bigint *r = sp_bigint_alloc();
53085322 mpz_init(sp_mpz_ctx, &r->mpz);
53095323 mpz_sub(sp_mpz_ctx, &r->mpz, &a->mpz, &b->mpz);
53105324 return r;
53115325 }
53125326
53135327 sp_Bigint *sp_bigint_mul(sp_Bigint *a, sp_Bigint *b) {
5314- sp_Bigint *r = (sp_Bigint*)malloc(sizeof(sp_Bigint));
5328+ sp_Bigint *r = sp_bigint_alloc();
53155329 mpz_init(sp_mpz_ctx, &r->mpz);
53165330 mpz_mul(sp_mpz_ctx, &r->mpz, &a->mpz, &b->mpz);
53175331 return r;
53185332 }
53195333
53205334 sp_Bigint *sp_bigint_div(sp_Bigint *a, sp_Bigint *b) {
5321- sp_Bigint *r = (sp_Bigint*)malloc(sizeof(sp_Bigint));
5335+ sp_Bigint *r = sp_bigint_alloc();
53225336 mpz_init(sp_mpz_ctx, &r->mpz);
53235337 mpz_mdiv(sp_mpz_ctx, &r->mpz, &a->mpz, &b->mpz);
53245338 return r;
53255339 }
53265340
53275341 sp_Bigint *sp_bigint_mod(sp_Bigint *a, sp_Bigint *b) {
5328- sp_Bigint *r = (sp_Bigint*)malloc(sizeof(sp_Bigint));
5342+ sp_Bigint *r = sp_bigint_alloc();
53295343 mpz_init(sp_mpz_ctx, &r->mpz);
53305344 mpz_mmod(sp_mpz_ctx, &r->mpz, &a->mpz, &b->mpz);
53315345 return r;
53325346 }
53335347
53345348 sp_Bigint *sp_bigint_pow(sp_Bigint *base, int64_t exp) {
5335- sp_Bigint *r = (sp_Bigint*)malloc(sizeof(sp_Bigint));
5349+ sp_Bigint *r = sp_bigint_alloc();
53365350 mpz_init(sp_mpz_ctx, &r->mpz);
53375351 mpz_pow(sp_mpz_ctx, &r->mpz, &base->mpz, exp);
53385352 return r;