Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Bob Matcuk <Hamartiology@squeg.net>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] va_arg values
Date: Tue, 16 Jan 2007 02:50:27 -0500	[thread overview]
Message-ID: <200701160642.l0G6gSZB023643@concorde.inria.fr> (raw)
In-Reply-To: <875c7e070701152018s4eca17b1i72a10440e0c1edef@mail.gmail.com>

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" <colanderman@gmail.com> 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
http://www.Squeg.Net/

Explanation of My Return Address, GPG Key:
http://www.Squeg.Net/returnAddr.php


  reply	other threads:[~2007-01-16  6:42 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 [this message]
2007-01-16 10:47   ` Mattias Engdegård
2007-01-21 17:25 ` Xavier Leroy

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=200701160642.l0G6gSZB023643@concorde.inria.fr \
    --to=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