From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by sympa.inria.fr (Postfix) with ESMTPS id E10EB7EE94 for ; Sun, 30 Dec 2012 15:20:08 +0100 (CET) Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of marek@xivilization.net) identity=pra; client-ip=178.63.18.39; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="marek@xivilization.net"; x-sender="marek@xivilization.net"; x-conformance=sidf_compatible Received-SPF: Pass (mail4-smtp-sop.national.inria.fr: domain of marek@xivilization.net designates 178.63.18.39 as permitted sender) identity=mailfrom; client-ip=178.63.18.39; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="marek@xivilization.net"; x-sender="marek@xivilization.net"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@coaxial.xivilization.net) identity=helo; client-ip=178.63.18.39; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="marek@xivilization.net"; x-sender="postmaster@coaxial.xivilization.net"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApEJAKZM4FCyPxIn/2dsb2JhbAAqGhaDMroVFnOCHgEBBAEMNAEBNwEECwtGVwYTiA0KCCykU4Q6AQV2jAcGjFeEQ5YOgR2PLIJ1 X-IronPort-AV: E=Sophos;i="4.84,381,1355094000"; d="scan'208";a="167101693" Received: from coaxial.xivilization.net ([178.63.18.39]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/AES256-SHA; 30 Dec 2012 15:20:08 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xivilization.net; s=xiv; h=Content-Transfer-Encoding:Content-Type:Mime-Version:References:In-Reply-To:Message-ID:Subject:Cc:To:From:Date; bh=Q9z2g/mAxlazAMMWa1Bm8cmkRZ95lpfS2j1QkXqEfC8=; b=NvvRDf+Jl6J/fhpDi5gZ93MSinEXF3lBQJNFU5VWY2VRic5O+j4Fmo81OWefImBgRegZyde4Q4cRJC+If/KLvCQgsDr8C3t7359+vxwtC5T0hXDUUkKGZ5NUPC3QbgZA; Received: from ppp-46-244-134-46.dynamic.mnet-online.de ([46.244.134.46] helo=localhost.localdomain) by coaxial.xivilization.net with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1TpJkE-0002kU-II; Sun, 30 Dec 2012 15:20:06 +0100 Date: Sun, 30 Dec 2012 15:19:41 +0100 From: Marek Kubica To: =?ISO-8859-1?B?VPZy9ms=?= Edwin Cc: caml-list@inria.fr Message-ID: <20121230151941.7d2261d1@xivilization.net> In-Reply-To: <50E04922.207@etorok.net> References: <20121230140823.20f39bcd@xivilization.net> <50E04922.207@etorok.net> X-Mailer: Claws Mail 3.9.0 (GTK+ 2.24.14; x86_64-unknown-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [Caml-list] C interop: Return values in parameters Hi, Thanks for your help. On Sun, 30 Dec 2012 16:01:06 +0200 T=F6r=F6k Edwin wrote: > Use a ' 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 =3D (struct archive*)archive; struct archive_entry* ent =3D (struct archive_entry*)entry; printf("ent: %p\n", ent); int retval =3D archive_read_next_header(handle, &ent); // ent changed printf("ent: %p\n", ent); entry =3D (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 =3D 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