GHCI/FFI/GMP/Me madness

Simon Marlow simonmar at microsoft.com
Fri Aug 13 10:29:45 EDT 2004


On 13 August 2004 14:04, Remi Turk wrote:

>> How do you arrange to free a GMP integer when it is no longer
>> referenced from the heap?  You'd need finalizers, and that way lies
>> madness.  The memory allocation tricks we play with GMP are all to
>> support GC of Integers.
> 
> Errr, I guess I'll have to start looking for a straitjacket then ;)
> Finalisers where exactly what I was thinking about, and though
> below implementation without doubt contains a bunch of bugs,
> perhaps even show-stoppers, the following code _does_ print True:
> 
> import Mpz
> 
> main= print $ show (z::Integer) == show (z::Mpz)
>     where
>         -- some random calculations
>         x, y, z :: Blub a => a
>         x   = 2^64 - sum (take 100 $ iterate (63*) 3)
>         y   = 3^200 * 234233432 - (34 `pow` 38) + sum [1,87..20000]
>         z   = fac 5000 * (x + y) `div` (2^100)
> 
> Or are finalisers simply going to be too slow to take seriously
> for this?

You can't use mallocForeignPtr (because GMP needs to allocate its own
memory), so you have to use full-blown ForeignPtrs with malloc/free and
finalizers.  Yes, these are very slow compared to using GC'd memory.

Cheers,
	Simon



More information about the Glasgow-haskell-users mailing list