[mercury-users] Making a Mercury shared library

Julien Fischer juliensf at csse.unimelb.edu.au
Sun Jun 5 02:04:13 AEST 2011


On Sat, 4 Jun 2011, Guillaume Yziquel wrote:

> Le Friday 03 Jun 2011 à 13:58:10 (+1000), Julien Fischer a écrit :
>>> What should I do to properly initialise the engine from foreign code?
>> See the ``Stand-alone Interfaces'' section of the User's guide for
>> details of how to do this.  There is an example in
>> samples/c_interface/standalone_c.
>> Julien.
> Thank you so much. Just a small question as I have a heap compacting GC
> that moves values around... argv is a char **.
> Should the array remain at a specific location in memory after the call
> to mercury_init() finishes?

Yes, the Mercury runtime will maintain a reference to it.

> Or may OCaml's GC freely move this array around in the OCaml heap?

It should not move it.

(In principle I guess you could move it so long as you updated the
runtime's reference to it -- the global variable mercury_argv - appropriately,
see runtime/mercury_wrapper.c for further details.)

> And this is perhaps a more general question, but how careful should I be
> when sharing values between OCaml and Mercury given the fact that
> OCaml's GC may move values around in the heap?

In the absence of a more detailed description of what you intend doing
then I would say that you need to be very careful.

If you're just calling Mercury from OCaml and the addresses of the
shared values are guaranteed not to move while the Mercury code is
running, i.e. OCaml doesn't do a garbage collection during a call to
Mercury procedure, then things should be okay.

Things are likely to be a good deal more complicated if Mercury
maintains references to OCaml values across multilple calls into
Mercury, e.g. inside a mutable, or f the Mercury code you are calling
makes calls back into OCaml.


More information about the users mailing list