[m-users.] Too Slow

Peter Wang novalazy at gmail.com
Fri May 1 10:22:10 AEST 2015


On Thu, 30 Apr 2015 18:54:29 +0100, Matthias Guedemann <matthias.guedemann at googlemail.com> wrote:
> I'd be happy to learn more on using FFI. In particular, I think my
> approach is heavy on memory leaks. Every time a new mp_int is created,
> memory is allocated which is never(?) freed. How does one handle this in
> Mercury normally when using FFI? 

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

You should add the attribute `thread_safe' to all of the foreign procs.


More information about the users mailing list