Commit 822ba282
822ba282a8f9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · ~2 modified
Description
fix: bigint GC safety via sp_gc_alloc_nogc (no-collect allocator)
Root cause: nested bigint expressions like sp_bigint_mul(sp_bigint_add(a,b), c) have intermediate results on the C stack that are not GC-rooted. If the inner call triggers GC (via sp_gc_alloc threshold check), intermediates are collected.
Fix: bigint objects are allocated via sp_gc_alloc_nogc which adds objects to the old heap (with finalizer for mpz_clear) but never triggers GC collection. GC triggers at the next regular sp_gc_alloc call instead.
pidigits n=1000: correct output, 0.05s (Ruby 0.15s, 3x faster). 68 tests pass, bootstrap clean.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 822ba282a8f9
- Parent
- 166702795e7d
lib/sp_bigint.cmodifiedView file
mpz_t mpz;} sp_Bigint;/* GC integration: use Spinel's GC allocator when available */extern void *sp_gc_alloc(size_t sz, void(*fin)(void*), void(*scn)(void*)) __attribute__((weak));/* GC integration: use Spinel's no-collect allocator to avoid GC during nested bigint ops */extern void *sp_gc_alloc_nogc(size_t sz, void(*fin)(void*), void(*scn)(void*)) __attribute__((weak));static mpz_ctx_t sp_mpz_ctx_storage;static mpz_ctx_t *sp_mpz_ctx = NULL;static sp_Bigint *sp_bigint_alloc(void) { sp_bigint_init_ctx(); if (sp_gc_alloc) { return (sp_Bigint*)sp_gc_alloc(sizeof(sp_Bigint), sp_bigint_finalizer, NULL); if (sp_gc_alloc_nogc) { return (sp_Bigint*)sp_gc_alloc_nogc(sizeof(sp_Bigint), sp_bigint_finalizer, NULL); } return (sp_Bigint*)calloc(1, sizeof(sp_Bigint));}void sp_bigint_free(sp_Bigint *b) { if (b) { mpz_clear(sp_mpz_ctx, &b->mpz); if (!sp_gc_alloc) free(b); if (!sp_gc_alloc_nogc) free(b); }}spinel_codegen.rbmodifiedView file
Loading diff…