Lazy mpz allocation

Marco Bodrato bodrato at mail.dm.unipi.it
Tue Sep 8 05:06:01 UTC 2015


Ciao,

Il Mar, 1 Settembre 2015 6:46 am, Marco Bodrato ha scritto:
> I'd like a sort of "copy-on-write" interface, that allocates the needed
> limbs when we need to store a new value into a variable and possibly
> (MPZ_REALLOC should, MPZ_NEWALLOC should not) copy the previous SIZ(x)
> limbs to the new area...

I mean, code like the following inserted in mpz/init.c :

  if (ALLOC (m) == 0)
    {
      mp = __GMP_ALLOCATE_FUNC_LIMBS (new_alloc);
      if (SIZ (m) != 0)
	if (ABSIZ(m) > new_alloc)
	  SIZ (m) = 0;
	else
	  MPN_COPY (mp, PTR (m), ABSIZ (m));
    }
  else
    {
      mp = __GMP_REALLOCATE_FUNC_LIMBS (PTR (m), ALLOC (m), new_alloc);
      if (ABSIZ (m) > new_alloc)
	SIZ (m) = 0;
    }
  PTR (m) = mp;
  ALLOC(m) = new_alloc;
  return (void *) mp;

This way, we can for example define mpq_init (x) with:

  ALLOC(NUM(x)) = ALLOC(DEN(x)) = 0;
  PTR(NUM(x)) = PTR(DEN(x)) = ptr_to_static_one;
  SIZ(NUM(x)) = 0;
  SIZ(DEN(x)) = 1;

Regards,
m

-- 
http://bodrato.it/papers/



More information about the gmp-devel mailing list