caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Marek Kubica <marek@xivilization.net>
Cc: "Török Edwin" <edwin+ml-ocaml@etorok.net>, caml-list@inria.fr
Subject: Re: [Caml-list] C interop: Return values in parameters
Date: Sun, 30 Dec 2012 15:36:16 +0100	[thread overview]
Message-ID: <CAPFanBFe6bgnhjuVf01+O0Y0BH+Z0pVd=UFZQt+GLiRD-87t1A@mail.gmail.com> (raw)
In-Reply-To: <20121230151941.7d2261d1@xivilization.net>

References are a derived concept defined as:

  type 'a ref = { mutable contents : 'a }

You can update them from the C side just as you would handle a
polymorphic record, with Field and Store_field.

In the code you show, the OCaml value corresponding to the pointer is
exactly the pointer, hidden as a 'value' type. This is correct as
OCaml detects out-of-(OCaml)-heap pointer. However, if you used a
custom block instead (
http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual033.html#toc150
), OCaml would do the boxing for you: Data_custom_val(v) already
returns a pointer than can be dereferenced or mutated.

I think you have a choice between using references explicitly for
those functions of the API that mutate input references, or uniformly
representing this type of data as a custom block. The latter option
may be valuable if you have uses for the other features of custom
blocks, eg. the user-defined comparison and finalization operations,
and probably not worth the trouble otherwise. Finally, explicitly
using references to signal mutability in some part of your API is
probably clearer and a better design.

On Sun, Dec 30, 2012 at 3:19 PM, Marek Kubica <marek@xivilization.net> wrote:
> Hi,
>
> Thanks for your help.
>
> On Sun, 30 Dec 2012 16:01:06 +0200
> Török Edwin <edwin+ml-ocaml@etorok.net> wrote:
>
>> Use a '<yourtype> ref' for the parameter (or a record with a mutable
>> field) on the OCaml side, and you can update the field on the C side
>> then.
>
> I was thinking about the same thing and checked
>
> http://www.linux-nantes.org/~fmonnier/ocaml/ocaml-wrapping-c.php
> and
> http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual033.html
>
> and couldn't find how to modify a ref value from C.
>
> My code looks like this:
>
> CAMLprim value ost_read_next_header(value archive, value entry)
> {
>     struct archive* handle = (struct archive*)archive;
>     struct archive_entry* ent = (struct archive_entry*)entry;
>     printf("ent: %p\n", ent);
>     int retval = archive_read_next_header(handle, &ent);
>     // ent changed
>     printf("ent: %p\n", ent);
>     entry = (value)ent;
>     return Val_int(retval);
> }
>
> And the second parameter is defined as "entry ref", yet when I look at
> the resulting value from OCaml, the ref's value did not change:
>
> let entry = ref (Archive.entry_new ()) in
>     Archive.print_pointer !entry;
>     ...
>     ignore (Archive.read_next_header handle entry);
>     Archive.print_pointer !entry;
>
> It still points to the same value that my Archive.entry_new returned.
>
>> Or if your C type is not actually void*, and your C function doesn't
>> have side-effects (besides updating ptr) you can also make the OCaml
>> function return the actual value, and raise an exception if the
>> function failed.
>
> I thought about this, but I have a number of these functions and some
> have more than one return parameter, so I'd need to return a tuple at
> least. I plan to make this wrapper as close to C and low-level, so I
> can write a proper high-level wrapper on top.
>
> If the ref-appoach does not get me anywhere, I might still do this.
>
> regards,
> Marek
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2012-12-30 14:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-30 13:08 Marek Kubica
2012-12-30 14:01 ` Török Edwin
2012-12-30 14:19   ` Marek Kubica
2012-12-30 14:36     ` Gabriel Scherer [this message]
2012-12-31  1:48       ` Marek Kubica

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='CAPFanBFe6bgnhjuVf01+O0Y0BH+Z0pVd=UFZQt+GLiRD-87t1A@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=edwin+ml-ocaml@etorok.net \
    --cc=marek@xivilization.net \
    /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;
as well as URLs for NNTP newsgroup(s).