[mercury-users] Making a Mercury shared library

Guillaume Yziquel guillaume.yziquel at citycable.ch
Sun Jun 5 08:56:14 AEST 2011

Le Sunday 05 Jun 2011 à 02:04:13 (+1000), Julien Fischer a écrit :
> Hi,


> On Sat, 4 Jun 2011, Guillaume Yziquel wrote:
> >
> >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.)

I have little control over how OCaml's GC compacts the heap. So
that's not really an option. For now, hardcoding it as a global static

> >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.

I'll likely be getting in this last use case at one point in time.

For now, I'm hitting another problem concerning the stackbottom argument
of the mercury_init() function call. My code is:

	#include <caml/mlvalues.h>
	#include <caml/alloc.h>
	#include "hello.mh"

	static value stackbottom;

	static char * mercury_args [1] = {"ocaml-mercury"};

	CAMLprim value ocaml_mercury_init (value ml_unit) {
	        stackbottom = caml_alloc_small (1, 0);
	        mercury_init(1, mercury_args, &Field(stackbottom, 0));
	        return Val_unit;

	CAMLprim value ocaml_fibonacci (value ml_n) {
	        MR_Integer n = Long_val(ml_n);
	        MR_Integer fib_n;
	        fibonacci(n, &fib_n);
	        return Val_long(fib_n);

What I'm doing here is boxing stackbottom into an OCaml heap-allocated
block. This is very likely wrong, as I gather that stackbottom really
should be something on the stack.

However, putting stackbottom on the stack would mean either:

-1- When calling ocaml_mercury_init, put stackbottom on the call stack,
then somehow make a callback to OCaml code from within
ocaml_mercury_init. This isn't so hard to do, but doing so means writing
awkward OCaml code and unnatural linking of OCaml modules.

-2- Write some C code that calls mercury_init() and then starts OCaml
code independently. The latter means calling OCaml's equivalent of
mercury_init() just after calling mercury_init.

The second option seems the most robust solution, but I'd like to avoid
it if possible. Moreover, in the user guide, it is written:

	"stackbottom is the address of the base of the stack. In grades
	that use conservative garbage collection this is used to tell
	the collector where to begin tracing."

This seems to suggest that if I chose the right garbage collecting
scheme for Mercury code, I might be able not to worry too much about
stackbottom. Which would be nice for starters.

So what GC mechanism would be satisfying? Do I really need Mercury's GC
around? (After all, interfacing GCs is usually rather error-prone and a
time waster if not necessary).

Thoughts welcome.

Best regards,

     Guillaume Yziquel

mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au

More information about the users mailing list