How store libGMP bigint&-ratnum:s within a parent Garbage Collected environment with movable objects so that libGMP values must be absent of any absolute pointers whatsoever?

Tinker tinkr at openmailbox.org
Sat Feb 28 19:45:34 UTC 2015


Hi Marc,

Thank you a lot for your response!


I'll define my problem rather roughly now:

I want to exact-precision arithmetics, example, i have the numbers A B C 
D which are each random integer values that are bigger than the 
machine's integer size, so say in the size range 10^100.

Then also I have two fractionals (you know what I mean e.g. the exact 
numebr 10/7 , some maybe call these just rationals) A/B and C/D, let's 
call them E and F.

What I want to do is all the standard math operations + - * / ^, 
comparisons, truncate remainder quotient modulo floor round ceiling abs, 
on them. (I understand that I may need to implement some of these 
myself, atop libGMP's exports.)


E.g., E + F, A + E, F * G, A / B, B + C.


Right, so, my GC would move around objects as it pleases, so memory 
pointer integrity applies only during individual math operations, and 
not between them.

So for this reason, if any libGMP value would need to contain a pointer 
to anything, then that would need to be represented in the GC's object 
reference format - so if that would be needed then just let me know, 
but, if I got you right, a libGMP big-integer is just a byte array, and 
for the fractionals i can just implement my own format for keeping them 
in a row.


Sure I'm fine with implementing mp_set_memory_functions.. so each math 
operation, like, generation of result of the E + F operation presuming 
the result is a bigint, will libGMP make one single alloc_func() for 
that only?


And maybe most importantly, just please re-confirm that libGMP will be 
fine with that I move around these byte arrays anywhere and use libGMP 
operations on the moved versions?

Are you aware of any example of any other libGMP binding that does this?


Thanks! :)



On 2015-02-28 17:22, Marc Glisse wrote:
> On Sat, 28 Feb 2015, Tinker wrote:
> 
>> I have a GC:ed heap wherein I want to store my libGMP 
>> bigint&-ratnum:s. This means that I provide the memory allocator, and 
>> also it means that libGMP must store one bigint/bigratnum as one 
>> single solid byte array only, i.e. one such value cannot be split up 
>> in more memory allocations, because between libGMP calls my GC changes 
>> memory location of heap objects frequently, and therefore the values 
>> may utilize no absolute memory pointers.
> 
> Your email is not very clear. How do you reference your array, if not
> through a pointer? Or does your GC go through memory and change all
> the pointers when it moves an array? Note that GMP already stores
> integers in a continuous memory block (a rational is a pair of
> integers).
> 
> Using mp_set_memory_functions and the mpn layer should be safe, but
> without more info I have no idea if you could use mpz/mpq.
> 
>> How do I use libGMP in this way?
>> 
>> 
>> As I see it it's extremely important that libGMP supports this, 
>> because this is the only way that a garbage collected environment.
>> 
>> (Methods with static memory addresses for values would risk memory 
>> fragmentation, and not being exposed to that risk is the exact reason 
>> I use a GC.)



More information about the gmp-discuss mailing list