From 879cba79761d7af456fb6f6f7311edcd3a885e99 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 29 Jul 2025 09:14:09 +0900 Subject: [PATCH] mruby-bigint: refactor pool_save and pool_restore to use mpz_ctx_t Refactor `pool_save` and `pool_restore` functions to accept `mpz_ctx_t *ctx` directly, aligning their signature with other context-aware functions. This change improves consistency and simplifies calls to these functions within `udiv`, `mpz_powm`, `mpz_powm_i`, and `mpz_gcd`. Co-authored-by: Gemini --- mrbgems/mruby-bigint/core/bigint.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/mrbgems/mruby-bigint/core/bigint.c b/mrbgems/mruby-bigint/core/bigint.c index 8602a35fa..8d5ec82de 100644 --- a/mrbgems/mruby-bigint/core/bigint.c +++ b/mrbgems/mruby-bigint/core/bigint.c @@ -50,14 +50,16 @@ typedef struct mpz_context { /* Pool allocation functions */ static size_t -pool_save(mpz_pool_t *pool) +pool_save(mpz_ctx_t *ctx) { + mpz_pool_t *pool = MPZ_POOL(ctx); return pool ? pool->used : 0; } static void -pool_restore(mpz_pool_t *pool, size_t state) +pool_restore(mpz_ctx_t *ctx, size_t state) { + mpz_pool_t *pool = MPZ_POOL(ctx); if (pool) { pool->used = state; } @@ -836,7 +838,7 @@ udiv(mpz_ctx_t *ctx, mpz_t *qq, mpz_t *rr, mpz_t *xx, mpz_t *yy) mrb_assert(yy->sz > 0); /* divided by zero */ /* Use new context architecture with automatic pool/heap management */ - size_t pool_state = pool_save(MPZ_POOL(ctx)); + size_t pool_state = pool_save(ctx); mpz_t q, x, y; mpz_init_temp(ctx, &q, xx->sz - yy->sz + 1); /* Quotient size estimate */ mpz_init_temp(ctx, &x, xx->sz + 1); /* Dividend with potential carry */ @@ -947,7 +949,7 @@ udiv(mpz_ctx_t *ctx, mpz_t *qq, mpz_t *rr, mpz_t *xx, mpz_t *yy) mpz_clear(ctx, &q); mpz_clear(ctx, &x); mpz_clear(ctx, &y); - pool_restore(MPZ_POOL(ctx), pool_state); + pool_restore(ctx, pool_state); } static void @@ -1698,7 +1700,7 @@ mpz_powm(mpz_ctx_t *ctx, mpz_t *zz, mpz_t *x, mpz_t *ex, mpz_t *n) return; } - size_t pool_state = pool_save(MPZ_POOL(ctx)); + size_t pool_state = pool_save(ctx); mpz_t t, b; mpz_init_set_int(ctx, &t, 1); mpz_init_set(ctx, &b, x); @@ -1743,10 +1745,9 @@ mpz_powm(mpz_ctx_t *ctx, mpz_t *zz, mpz_t *x, mpz_t *ex, mpz_t *n) if (use_barrett) { mpz_clear(ctx, &mu); } - pool_restore(MPZ_POOL(ctx), pool_state); + pool_restore(ctx, pool_state); } - static void mpz_powm_i(mpz_ctx_t *ctx, mpz_t *zz, mpz_t *x, mrb_int ex, mpz_t *n) { @@ -1759,7 +1760,7 @@ mpz_powm_i(mpz_ctx_t *ctx, mpz_t *zz, mpz_t *x, mrb_int ex, mpz_t *n) return; } - size_t pool_state = pool_save(MPZ_POOL(ctx)); + size_t pool_state = pool_save(ctx); mpz_t t, b; mpz_init_set_int(ctx, &t, 1); mpz_init_set(ctx, &b, x); @@ -1802,7 +1803,7 @@ mpz_powm_i(mpz_ctx_t *ctx, mpz_t *zz, mpz_t *x, mrb_int ex, mpz_t *n) if (use_barrett) { mpz_clear(ctx, &mu); } - pool_restore(MPZ_POOL(ctx), pool_state); + pool_restore(ctx, pool_state); } /* Helper functions for pool-based GCD operations */ @@ -1945,7 +1946,7 @@ mpz_power_of_2_p(mpz_t *x) static void mpz_gcd(mpz_ctx_t *ctx, mpz_t *gg, mpz_t *aa, mpz_t *bb) { - size_t pool_state = pool_save(MPZ_POOL(ctx)); + size_t pool_state = pool_save(ctx); mpz_t a, b; /* Handle special cases */ @@ -2046,7 +2047,7 @@ mpz_gcd(mpz_ctx_t *ctx, mpz_t *gg, mpz_t *aa, mpz_t *bb) mpz_clear(ctx, &a); mpz_clear(ctx, &b); cleanup: - pool_restore(MPZ_POOL(ctx), pool_state); + pool_restore(ctx, pool_state); }