__gmp_allocate_func and garbage collection

Torbjorn Granlund tg at gmplib.org
Tue May 5 10:41:49 CEST 2009

Marc Glisse <marc.glisse at normalesup.org> writes:

  some garbage collectors (take the one by Boehm-Demers-Weiser) provide
  2 malloc variants, one for pure "data", and one for stuff that may
  include pointers to garbage collected memory (the GC 0-initializes
  memory in this case).
  On some private mailing list, someone asked about using a garbage
  collector with gmp. I was thus wondering whether it was safe to use
  mp_set_memory_functions with the first ("pure data") variant of
  malloc, or whether we had to use the safe general variant.
  Scanning through the GMP sources, and excluding tune/, tests/ and
  tal-debug, I found only one problematic file (I may have missed
  others, obviously). In randlc2x.c, __gmp_allocate_func is used to
  allocate a structure that contains several mpz_t.
  So it looks like if you don't use random numbers, it is safe to use
  the fast allocation routine. It would be nice to have some more formal
  guarantees though. I understand this message would be more convincing
  with benchmarks, but now is not the best time, I just wanted to get
  the idea out before I forgot.

Are you saying that a struct that contains a pointer is the rub?  So,
any time we malloc an mpz_t (not just declare it) these garbage
collectors will become unhappy?

I really don't know where this happens in GMP, but it should be quite


More information about the gmp-discuss mailing list