caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Marek Kubica <marek@xivilization.net>
To: "Török Edwin" <edwin+ml-ocaml@etorok.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] C interop: Return values in parameters
Date: Sun, 30 Dec 2012 15:19:41 +0100	[thread overview]
Message-ID: <20121230151941.7d2261d1@xivilization.net> (raw)
In-Reply-To: <50E04922.207@etorok.net>

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

  reply	other threads:[~2012-12-30 14:20 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 [this message]
2012-12-30 14:36     ` Gabriel Scherer
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=20121230151941.7d2261d1@xivilization.net \
    --to=marek@xivilization.net \
    --cc=caml-list@inria.fr \
    --cc=edwin+ml-ocaml@etorok.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).