Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: David Allsopp <dra-news@metastack.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: RE: [Caml-list] Val_int vs caml_copy_nativeint
Date: Tue, 9 Aug 2011 08:44:44 +0000	[thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D9C23E4B44@Remus.metastack.local> (raw)
In-Reply-To: <20110809113359.c306a95842c0c86ea160025d@mega-nerd.com>

Erik de Castro Lopo wrote:
> Xavier Leroy wrote:
> 
> > On 08/08/2011 10:03 AM, Guillaume Yziquel wrote:
> >
> > > Then I do not see anything wrong if the code snippet you sent.
> > > However, when you change Val_int to caml_copy_nativeint, the layout
> > > of the tuple is different. [...] So if you keep the same OCaml code
> > > when reading the result value, it's no surprise that the pointer is
> > > shown in place of the integer you expected.
> >
> > This is good advice indeed: make sure your Caml type declaration
> > matches the data representation that your Caml/C stub implements...
> >
> > >    /* Package up the result as a tuple. */
> > >    v_response = caml_alloc_tuple (3) ;
> > >    Store_field (v_response, 0, Val_int (width)) ;
> > >    Store_field (v_response, 1, Val_int (height)) ;
> > >    Store_field (v_response, 2, caml_copy_string (code)) ;
> > >    CAMLreturn (v_response) ;
> >
> > Additionally, do make sure that "v_response" is registered with the GC
> > (declared with one of the CAMLlocal macros).
> 
> This is strange, it wasn't declared with a CAMLlocal macro and it was
> working, but if I do declare it with one the program segfaults during
> garbage collection (caml_oldify_local_roots).

If you altered this but left the program exactly the same then you violated rule 1 in 18.5.1 of the manual: CAMLparamn must come first. The result is predictable - you attempt to allocate a local variable before the local root is properly initialised...

That said, in your specific example, my understanding is that it should be OK not using CAMLlocal because you return v_response with CAMLreturn before the GC could be triggered (no allocations or callbacks between caml_alloc_tuple and CAMLreturn). Whether that's a) correct and b) sensible (i.e. whether the performance "boost" justifies the commenting needed in the code to explain how brittle it is) is another matter!


David


  parent reply	other threads:[~2011-08-09  8:44 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-08  3:15 Erik de Castro Lopo
2011-08-08  3:48 ` Romain Beauxis
2011-08-08  3:59   ` Guillaume Yziquel
2011-08-08  7:49     ` Erik de Castro Lopo
2011-08-08  3:53 ` Guillaume Yziquel
2011-08-08  7:46   ` Erik de Castro Lopo
2011-08-08  8:03     ` Guillaume Yziquel
2011-08-08  9:17       ` Mathias Kende
2011-08-08 17:24       ` Xavier Leroy
2011-08-09  1:33         ` Erik de Castro Lopo
2011-08-09  1:40           ` Romain Beauxis
2011-08-09  8:44           ` David Allsopp [this message]
2011-08-09 10:07             ` malc
2011-08-09 10:43               ` David Allsopp
2011-08-09 22:53           ` Erik de Castro Lopo
2011-08-10  6:43             ` Gabriel Scherer
2011-08-08  5:20 ` malc
2011-08-08  6:01   ` Guillaume Yziquel
2011-08-08  6:22     ` malc

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=E51C5B015DBD1348A1D85763337FB6D9C23E4B44@Remus.metastack.local \
    --to=dra-news@metastack.com \
    --cc=caml-list@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