memory allocation

Torbjorn Granlund tg at gmplib.org
Sat Jun 5 18:04:35 CEST 2010


  > > Why does GMP allocate 3 limbs for z (which fits into 2 limbs), and allocate
  > > 3 limbs for r (which fits into 1 limb)?
  > 
  > does it cause trouble somehow? It is only a constant number (1 for the 
  > string, 2 for the division, in the worst case) of extra limbs, the 
  > documentation mentions this can happen and many gmp functions do it (the 
  > worst being maybe z-z which allocates size(z)+1).
  
  yes it causes trouble in MPFR, where we use mpz functions with our own
  allocation function instead of mpz_init, to replace calls to malloc() by
  calls to alloca(). However this approch works only if we are able to bound
  the maximal allocated size, since if mpz_ functions call realloc() we are dead.
  The problem is that the maximal allocated size (wrt actual size) in mpz is
  not documented, and we can only guess it.
  
Then MPFR uses GMP in a way that GMP does not and can not realistically
support.  If we promised some specific allocation for all functions,
then we would have to stay within the envelope set for all future
implementations of the mpz functions, something we really do not want to
tdo.

I believe MPFR should stay away from mpz instead of using it against its
design.  We have mpn functions intented for the software such as MPFR.

-- 
Torbjörn


More information about the gmp-devel mailing list