From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Bob Matcuk <Hamartiology@squeg.net>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] va_arg values
Date: Sun, 21 Jan 2007 18:25:33 +0100 [thread overview]
Message-ID: <45B3A20D.1090304@inria.fr> (raw)
In-Reply-To: <200701132310.l0DNArug026315@discorde.inria.fr>
> value func(value obj, char *method, int n, ...)
> {
> va_list ap;
> int i;
> value *args = calloc(n + 1, sizeof(value));
> value r;
>
> args[0] = obj;
> va_start(ap, n);
> for (i = 0; i < n; i++)
> args[i + 1] = va_arg(ap, value);
> va_end(ap);
>
> r = caml_callbackN(caml_get_public_method(obj, hash_variant(method)),
> n + 1, args);
> free(args);
> return r;
> }
>
> Is it an invalid assumption that it is unnecessary to bother with the
> CAMLparam/CAMLlocal stuff since there's nothing to trip the GC?
It's a valid assumption. The hard rule is that you must register as
root all local variables that need to survive a call to the GC or to a
function that might trigger the GC. In general it's hard to keep
track of those variables, which is why the OCaml manual advocates
registering everything that has type "value". But in your case you
cannot, and the code fragment above look safe to me. (Make sure you
document this in a big comment, though...) As others pointed out,
you're also relying on the fact that caml_get_public_method
and hash_variant never trigger a GC, which is true now and in the
foreseeable future, but again a big comment is warranted.
> Should probably check calloc for success and maybe throw an exception
> if it failed... Which actually brings me to another quick question: if
> I throw an exception, say caml_failwith("..."), is it necessary to
> still call CAMLreturn after it? Or will the exception cause the
> function to exit?
The latter: the exception triggers something like a longjmp(), causing
immediate return from the C function (and de-registration of the local
roots, if any).
> One last quick question: is the line "args[i + 1] = va_arg(ap, value);"
> above legal? args[] is an array of value and va_arg(ap, value) will
> return a value. So, essentially, it's the same thing as the assignment
> in the following example:
>
> value func(value v1)
> {
> value v2 = v1;
> ...
> }
>
> I know values are just pointers so it is syntactically correct, but
> what I'm asking is: is it safe to do? Should I be using some function
> instead to create a copy of the value?
No copying is necessary.
Hope this helps,
- Xavier Leroy
prev parent reply other threads:[~2007-01-21 17:25 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-01-14 0:18 Bob Matcuk
2007-01-15 10:44 ` [Caml-list] " Richard Jones
2007-01-15 23:46 ` Bob Matcuk
2007-01-16 4:18 ` Chris King
2007-01-16 7:50 ` Bob Matcuk
2007-01-16 10:47 ` Mattias Engdegård
2007-01-21 17:25 ` Xavier Leroy [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=45B3A20D.1090304@inria.fr \
--to=xavier.leroy@inria.fr \
--cc=Hamartiology@squeg.net \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox