Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
va_arg values
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-01-16 (06:42)
From: Bob Matcuk <Hamartiology@s...>
Subject: Re: [Caml-list] va_arg values
Thank you for your long reply! This is exactly the information I was
looking for. Cleared up the role of the GC for me. I'm not used to
working with any kind of automatic GC; I'm somewhat of a hardcore C
fanatic. It didn't occur to me that the GC might move things around,
though I feel I should have known! Doh!

On Mon, 15 Jan 2007 23:18:51 -0500
"Chris King" <> wrote:

> True, false.  caml_callbackN executes arbitrary code, which may or may
> not trip the GC.  hash_variant and caml_get_public_method are
> questionable also (since they return values), but looking at the Caml
> source code, it seems that they are safe (but I don't think the docs
> guarantee this).

Indeed. I was a bit weary of that myself.

> (BTW you should use caml_hash_variant rather than hash_variant; the
> comment for caml_get_public_method in caml/mlvalues.h should probably
> be updated to this effect also.)

Aye - I grabbed that from the documentation. I've noticed there are a
couple places where the documentation is missing the "caml_" but for
some reason, I didn't even think twice about that one.

> You could do this with caml_stat_alloc and caml_stat_free (in
> caml/memory.h).  These are equivalent to malloc/free but throw Caml's
> out-of-memory exception if they fail.  However in this case, I would
> simply declare args as an array.  Otherwise, if the callback throws an
> exception, args will not be freed unless you explicitly catch
> exceptions via caml_callbackN_exn, free it, and then re-raise the
> exception.

I hadn't even thought of that! Thanks! Not used to functions that don't
return other than the exec's and exit.

> Note that if you have no control over the C functions higher up the
> call chain (say an external library which calls your function), they
> could exhibit similar problems if they are unaware of the possibility
> of your function raising an exception.  The best thing to do in such a
> case would be to return an error condition if possible, or at the very
> least, print a warning and return or exit gracefully (the functions in
> caml/printexc.h help here).

Excellent advice; thanks again.

> K&R C doesn't, but GCC does.  If you're using another compiler or some
> compatibility flag, then the alloca function (usually found in
> alloca.h) should do the trick.  It allocates space on the stack
> exactly like an array declaration does, so the guts of CAMLlocalN
> should apply to it.

The problem with alloca is that it is not as portable (though, I can't
see what the problem is - I believe most, if not all architectures
could implement it as a single instruction). Still, given your previous
comment about the callback throwing an exception, perhaps it is the
best way to go...

Thank you again for your clarifications.

Bob Matcuk

Explanation of My Return Address, GPG Key: