From: malc <av1474@comtv.ru>
To: Guillaume Yziquel <guillaume.yziquel@citycable.ch>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Val_int vs caml_copy_nativeint
Date: Mon, 8 Aug 2011 10:22:21 +0400 (MSD) [thread overview]
Message-ID: <alpine.LNX.2.00.1108081022140.15208@linmac> (raw)
In-Reply-To: <20110808060103.GX29083@localhost>
On Mon, 8 Aug 2011, Guillaume Yziquel wrote:
> Le Monday 08 Aug 2011 ? 09:20:17 (+0400), malc a ?crit :
> > On Mon, 8 Aug 2011, Erik de Castro Lopo wrote:
> >
> > > Hi all,
> > >
> > > I'm writing a C stub function to allow the calling of a C library
> > > function from ocaml. The return from the stub is a tuple and I'm
> > > doing this:
> > >
> > > /* 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) ;
> > >
> > > The above works now, but didn't work when I was using
> > > caml_copy_nativeint() instead of Val_int() and I'd like to know
> > > why. I found it especially confusing because caml_copy_string()
> > > worked and was obvioulsy the right thing to do.
> >
> > 18.5.2
> >
> > Rule 5
> >
> > After a structured block (a block with tag less than No_scan_tag)
> > is allocated with the low-level functions, all fields of this block must
> > be filled with well-formed values before the next allocation operation. If
> > the block has been allocated with caml_alloc_small, filling is performed
> > by direct assignment to the fields of the block:
> > Field(v, n) = vn;
> > ...
> >
> > I'd say rule 5 has been violated here.
>
> No. caml_alloc_tuple is considered to be part of the simplified
> interface, not part of the low-level interface. Rule 5 shouldn't apply
> in this case.
>
> One of the reasons for rule 5 is that the contents of the allocated
> block may not satisfy GC constraints. So you should not allocate with
> the blocks item pointing to inconsistent garbage as the GC may the run
> over them.
>
> 18.4.4
>
> caml_alloc(n, t) returns a fresh block of size n with tag t.
> If t is less than No_scan_tag, then the fields of the block
> are initialized with a valid value in order to satisfy the
> GC constraints.
>
> In caml_alloc function in alloc.c:
>
> if (tag < No_scan_tag){
> for (i = 0; i < wosize; i++) Field (result, i) = 0;
> }
>
> and caml_alloc_tuple is roughly caml_alloc (in alloc.c) so definitely
> part of the simplified interface:
>
> CAMLexport value caml_alloc_tuple(mlsize_t n)
> {
> return caml_alloc(n, 0);
> }
>
>
I stand corrected.
--
mailto:av1474@comtv.ru
prev parent reply other threads:[~2011-08-08 6:22 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
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 [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=alpine.LNX.2.00.1108081022140.15208@linmac \
--to=av1474@comtv.ru \
--cc=caml-list@inria.fr \
--cc=guillaume.yziquel@citycable.ch \
/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