1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-16 08:40:19 +02:00

Bignums avoid both custom GMP allocator and finalizers

* libguile/deprecated.c (make_bignum): Move here from numbers.c, to
support scm_i_long2big etc.
(scm_i_big2dbl):
(scm_i_long2big):
(scm_i_ulong2big):
(scm_i_clonebig):
(scm_i_normbig): Deprecate.
(scm_install_gmp_memory_functions): Deprecate, happily!  SCM bignums now
have digits allocated inline with the bignum itself, so they are
completely transparent to the GC already.  The price is that if GMP ever
allocates digits via the MPZ API, those digits then have to be copied
back into managed memory.  But we avoid having to install finalizers and
we avoid having to muck with GMP's allocator.
* libguile/numbers.c (scm_from_mpz): Use scm_integer_from_mpz.
(scm_init_numbers): Never muck with GMP's allocators.
* doc/ref/guile-invoke.texi (Environment Variables): Remove note about
GUILE_INSTALL_GMP_MEMORY_FUNCTIONS.
* meta/build-env.in: No need to set GUILE_INSTALL_GMP_MEMORY_FUNCTIONS.
This commit is contained in:
Andy Wingo 2022-01-07 14:01:52 +01:00
parent a0765f564a
commit aa5455ea98
6 changed files with 113 additions and 201 deletions

View file

@ -36,6 +36,7 @@
#include "dynl.h"
#include "eval.h"
#include "foreign.h"
#include "finalizers.h"
#include "generalized-vectors.h"
#include "gc.h"
#include "gsubr.h"
@ -690,6 +691,95 @@ SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0, (SCM obj), "")
#undef FUNC_NAME
static void
finalize_bignum (void *ptr, void *data)
{
SCM bignum;
bignum = SCM_PACK_POINTER (ptr);
mpz_clear (SCM_I_BIG_MPZ (bignum));
}
static SCM
make_bignum (void)
{
scm_t_bits *p;
/* Allocate one word for the type tag and enough room for an `mpz_t'. */
p = scm_gc_malloc_pointerless (sizeof (scm_t_bits) + sizeof (mpz_t),
"bignum");
p[0] = scm_tc16_big;
scm_i_set_finalizer (p, finalize_bignum, NULL);
return SCM_PACK (p);
}
/* scm_i_big2dbl() rounds to the closest representable double,
in accordance with R5RS exact->inexact. */
double
scm_i_big2dbl (SCM b)
{
scm_c_issue_deprecation_warning
("scm_i_big2dbl is deprecated. Use scm_to_double instead.");
return scm_to_double (b);
}
SCM
scm_i_long2big (long x)
{
scm_c_issue_deprecation_warning
("scm_i_long2big is deprecated. Use scm_from_long instead.");
/* Return a newly created bignum initialized to X. */
SCM z = make_bignum ();
mpz_init_set_si (SCM_I_BIG_MPZ (z), x);
return z;
}
SCM
scm_i_ulong2big (unsigned long x)
{
scm_c_issue_deprecation_warning
("scm_i_ulong2big is deprecated. Use scm_from_ulong instead.");
/* Return a newly created bignum initialized to X. */
SCM z = make_bignum ();
mpz_init_set_ui (SCM_I_BIG_MPZ (z), x);
return z;
}
SCM
scm_i_clonebig (SCM src_big, int same_sign_p)
{
scm_c_issue_deprecation_warning
("scm_i_clonebig is deprecated. Use scm_to_mpz/scm_from_mpz instead.");
/* Copy src_big's value, negate it if same_sign_p is false, and return. */
SCM z = make_bignum ();
scm_to_mpz (src_big, SCM_I_BIG_MPZ (z));
if (!same_sign_p)
mpz_neg (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (z));
return z;
}
SCM
scm_i_normbig (SCM b)
{
scm_c_issue_deprecation_warning
("scm_i_normbig is deprecated. Direct bignum bit manipulation is not "
"supported.");
/* convert a big back to a fixnum if it'll fit */
/* presume b is a bignum */
if (mpz_fits_slong_p (SCM_I_BIG_MPZ (b)))
{
scm_t_inum val = mpz_get_si (SCM_I_BIG_MPZ (b));
if (SCM_FIXABLE (val))
b = SCM_I_MAKINUM (val);
}
return b;
}
int scm_install_gmp_memory_functions;
void