alloca usage

Torbjorn Granlund tg at swox.com
Thu Nov 20 22:58:32 CET 2003


[I trimmed pike-devel at lists.lysator.liu.se since I don't know
that they obfuscate email address in any web arhives.  I don't
want the spam flood to reach my new address.]

nisse at lysator.liu.se (Niels Möller) writes:

  It seems that the default configuration of GMP, with WANT_TMP_ALLOCA,
  allocates arbitrarily large temporaries on the stack. For example
  mpn_tdiv_qr allocates temporary storage proportional to the size of
  the input.
  
We are addressing this for the next major GMP release.

  Perhaps it would be a good idea to define TMP_ALLOC as something like
  
  #define TMP_ALLOC(size) \
   ((size) < PAGESIZE*2) ? alloca((size)) : heap_alloc((size), &tmp_marker)
  
It is not immediately apparent how to make that work, withuot
changing quite much.  I suppose it would be easier to just use
malloc (or the "default memory allocator") for large blocks, and
not involve the TMP_* allocation mechanism.

  if (tsize > BIG)
    tp = (*__gmp_allocate_func) (tsize);
  else
    tp = TMP_ALLOC (tsize)
  .
  .
  .
  if (tsize > BIG)
    (*__gmp_free_func) (tp, tsize);

In most places, we will know the allocation is going to be too
large for the stack, with will make the test unnecessary.

--
Torbjörn


More information about the gmp-devel mailing list