On Aug 23, 4:08 pm, Martin Albrecht <m...@informatik.uni-bremen.de> wrote: > > > Both sage and ecl use GMP for their multi-precision arithmetic, and > > > both call mp_set_memory_functions to register their memory managers. > > > This obviously doesn't work, since GMP only keeps track of one set of > > > memory management routines. Ecl needs to use its memory manager, since > > > it relies on Boehm for garbage collection and sage needs to use its > > > own because python relies on reference counting. > > This is not correct, last time I checked we basically told GMP to use system > malloc (1). There is no issue of reference counting at this stage. Can't we > just steal the memory management functions back after ECL was initialised? > This is what we do after libSingular is loaded which also registers its own > malloc/free/remalloc functions.
Ah, that is very useful information. That means that at least SAGE will call mpz_clear etc. on any unused integers. When grepping through the ecl source, I was only able to find one call to mp.*clear, and that is in the rational-to-double routine: Rationals in ECL are stored as numerator-denominator pairs. In order to arrive at a double, ECL constructs the corresponding mpq, asks for the double and clears the auxiliary mpq. A few observations on how ECL seems to use GMP. If we can verify these assumptions, it may not be too big a problem to steal back the GMP memory management routines: 1. bignum allocation seems to avoid mpz_init and use ecl_alloc_object to get its memory directly 2. computational routines seem to end with "copy register into preallocated bignum". If this is true, then ECL bignums will always live in Boehm managed memory, even if the GMP memfunctions are set otherwise. Registers, on the other hand, could live entirely under GMP memory control. These assumptions are currently not entirely met: a. big_register_free does not call mpz_clear (perhaps it shouldn't! but then it shouldn't be zeroing out its contents either. The next time the register would already have space preallocated) b. I'm not sure about init_big_registers c. In big_register_copy, there is a shortcut that tries to avoid copying if the mpz has undergone a realloc. This would allow leaking out of GMP managed mpz's into bignum objects. However, it seems if all the violations are in one small file: big.d. So the questions for Juanjo now are: A. is my analysis above correct? so, are mpz routines only called to modify a "register"? B. am I correct in thinking that one would only need to change "big.d" to make assumptions 1,2 hold? C. would you be interested in having those assumptions hold in ECL in general (or perhaps have a compile-time option to make them hold)? D. would you be willing to write the patch? I might be able to do it, but for you it's probably easier to oversee ramifications of the changes. My compliments on a very clean codebase, by the way. I wasn't expecting that it would be so easy to figure out how ECL works (but then again, I might have misunderstood). Kind regards, Nils --~--~---------~--~----~------------~-------~--~----~ To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---