[m-users.] Too Slow

Julien Fischer jfischer at opturion.com
Sat May 2 00:52:22 AEST 2015


Hi,

On Fri, 1 May 2015, Mark Brown wrote:

> On Fri, May 1, 2015 at 5:54 PM, Peter Wang <novalazy at gmail.com> wrote:
>> On Fri, 01 May 2015 07:02:48 +0100, Matthias Guedemann <matthias.guedemann at googlemail.com> wrote:
>>> Hi Peter,
>>>
>>>> You should allocate the mp_ints like this:
>>>
>>>>     Mp_Int = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
>>>
>>>> For the string in mp_to_string you should use
>>>
>>>>     MR_allocate_aligned_string_msg(S, length, MR_ALLOC_ID);
>>>
>>>> or other macros in mercury_string.h
>>>
>>> ok, thank you for the hints. Could you point me to the relevant section
>>> in the user guide, or is the documentation only in the source files?
>>
>> Unfortunately the latter (mercury_memory.h and mercury_string.h).
>> I think no memory management interface has been documented because
>> the existing interface is specific to conservative GCs, if not Boehm
>> GC.  Nonetheless, practical code using the foreign language interface
>> invariably needs to allocate so I think we should document some of the
>> macros and functions, with caveats.
>
> That's done for lists and floats (section 14.3.1), and ought to be
> done for strings, too. Allocating foreign types on Mercury's heap
> needs one big caveat, though, namely that no finaliser will be called.
> If the foreign type needs to be explicitly freed, allocating it on
> Mercury's heap won't work.

The usual approach to such types is to allocate them in whatever manner
the underlying library prefers then allocate a wrapper structure on
Mercury's heap that points to the type.  The wrapper structure then has
a finalizer registered with the GC using MR_GC_register_finalizer, from
runtime/mercury_memory.h.  (The finalizer calls a user-defined function
that is reponsible for finalising the underlying type.)

Cheers,
Julien.



More information about the users mailing list