From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 62EE97FFE1 for ; Thu, 6 Oct 2016 11:19:42 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=goswin-v-b@web.de; spf=Pass smtp.mailfrom=goswin-v-b@web.de; spf=None smtp.helo=postmaster@mout.web.de Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=pra; client-ip=212.227.17.11; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of goswin-v-b@web.de designates 212.227.17.11 as permitted sender) identity=mailfrom; client-ip=212.227.17.11; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mout.web.de) identity=helo; client-ip=212.227.17.11; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="postmaster@mout.web.de"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AE2HqDxxBEI9k2PLXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0ekQIJqq85mqBkHD//Il1AaPBtSBrakewLKH+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2WVTerzWI4CIIHV2nbEwu?= =?us-ascii?q?d76zQdSZ15X//tvx0qWbWx9Piju5bOE6BzSNhiKViPMrh5B/IL060BrDrygAUe?= =?us-ascii?q?1XwWR1OQDbxE6ktY/jtKJkpi9ZvvZk889bTY37eb45RPpWFmcIKWcwseLsrgbC?= =?us-ascii?q?S0OR4X1UeH8bnhdSBw/apEX1WYzusy2/qK938DaXN4v6QOZnCnyZ8653RUqw22?= =?us-ascii?q?88PDkj/TSPhw=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0ChAAAgFvZXhwsR49ReHQEFAQsBGBgHg?= =?us-ascii?q?wYBAQEBAXUtT40yln+Le4gxggsmhXoCgXY4FAEBAQEBAQEBAQEBEgEBAQgNCQk?= =?us-ascii?q?ZL4IyGIIYAQEEIw8BVgsYAgIFIQICDwUoNBqIGQEeCrBxiDwfhBMBAQgBAQEBJ?= =?us-ascii?q?IEHigqFE4I4LIIvBZl/hieJR4F5ToQZgwYKJ4VojHaDfx6DbBELgVVwAYg/AQE?= =?us-ascii?q?B?= X-IPAS-Result: =?us-ascii?q?A0ChAAAgFvZXhwsR49ReHQEFAQsBGBgHgwYBAQEBAXUtT40?= =?us-ascii?q?yln+Le4gxggsmhXoCgXY4FAEBAQEBAQEBAQEBEgEBAQgNCQkZL4IyGIIYAQEEI?= =?us-ascii?q?w8BVgsYAgIFIQICDwUoNBqIGQEeCrBxiDwfhBMBAQgBAQEBJIEHigqFE4I4LII?= =?us-ascii?q?vBZl/hieJR4F5ToQZgwYKJ4VojHaDfx6DbBELgVVwAYg/AQEB?= X-IronPort-AV: E=Sophos;i="5.31,453,1473112800"; d="scan'208";a="195817479" Received: from mout.web.de ([212.227.17.11]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Oct 2016 11:19:41 +0200 Received: from frosties.localnet ([78.43.233.192]) by smtp.web.de (mrweb102) with ESMTPSA (Nemesis) id 0M6V1T-1az07D2god-00yNzp for ; Thu, 06 Oct 2016 11:19:40 +0200 Received: from mrvn by frosties.localnet with local (Exim 4.87) (envelope-from ) id 1bs4q4-0007mx-0M for caml-list@inria.fr; Thu, 06 Oct 2016 11:19:40 +0200 Date: Thu, 6 Oct 2016 11:19:39 +0200 From: Goswin von Brederlow To: caml-list@inria.fr Message-ID: <20161006091938.GC29850@frosties> References: <090F68D4-7CA4-4091-BC5D-5F2C9BFB2AEC@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.6.0 (2016-04-01) X-Provags-ID: V03:K0:6mpUIsASVHohN91i+qG/LaztrqByd/EJpLKqcE4zRUb3bSTDytE RrZBL4H2XWUbVH0eEZLpHD+7v9g1bJr2f5WDwdcK7rFMLwYEN9Dutpt8mKWqGQNrnXDqMLn swvcGACQveQKgm9cZ4cpp+0qvKzYkdkmiEKGEo3OG+gzjj1NNkF6BCMV2HnVXRaWlmhVluv jQ0DgRFWiaybxr70K5RkA== X-UI-Out-Filterresults: notjunk:1;V01:K0:cBCH661HKUA=:oiL/vd8Ig34IoDvWTa0YLY ixOuvTsk0dzxkc3LeFJTB6nCspGnOGVOxmdizI7BTgjou22IhAITdL74A96LDQLAQEprks3ox SdalwCrGu4Y9g8DaKHDCiB50PQqMdU9Y3TTkKNg+wOTO8V5S1vbza3C9FTrTBD5HCvCCV9Kr6 RQtR0J48PTecfaZvf2Etq9fC+3ID/+q2mQCR5MtPEIYl1Fl08fnbQlozN/mssA/ylcRESpgGv 0rpFlrZOn94/EKgx9xkUDeNFruDHbhIp7VeA4gcoSe+wGcertoRdlGx4l25MslOsmauWFLFOL L3MZaGwEdFwApWJbu78/Q+aFrIODKsgs6J9HNHJm0aG6DDsCDO3LOzwKIo2LHTtccogGEUgUw NkV9fvzG4Sl2uk49/rD5S436Uq1nG/nBzAh3P/WdJX1IrYp4hJXF5AmtBMas7MAM99KZJAAZ7 k2RFiJII/WkCDyTNx457OdJuS/AvTZhR/Y/yF5w7k2zavNlOHvs6iecXiei2sHAp7d4Jfj0Ql k18vyQMixzqUO3dsRxlYc9ApEMli7JqImRhQAJD9oW7kgy+3Rbtjrsav34jeFdMs/gyKiDZlI OQEFa70Gh3iBG3k2kJBE5Ei3E79P+9GbvIJQzHj5DLcgQAluq+9E70IPZmq+Al9iTF6z8dfSJ I3tGLJB95DPJ6H8QZCWHYjejMoPozrMQLiXA523KJATkQMB7CO36c6FmTmv3uiBavIzpN9tRi vsqu+8O0X+FyMZIErnP2bigguSc8ndna6RXVXimkmoZ+K+mL1Kszocd1wcE= Subject: Re: [Caml-list] Getting some C values over to the OCaml side correctly On Sun, Sep 25, 2016 at 12:28:20PM +0000, David Allsopp wrote: > Edgar A wrote: > > On Sep 25, 2016, at 1:40 AM, David Allsopp > > wrote: > > > Edgar A wrote: > > > > Greetings, > > > > > > > > I have some C values that I want to get over to the OCaml side using > > > > the FFI. > > > > > > > > Specifically I have `uint32_t`, `intptr_t` and `int32_t` and I’m not > > > > sure how to get it over to the OCaml side correctly. > > > > > > > > Would the uint32_t be expressed as a Int32.t? Do I really need to put > > > > an integer as block just to get it to the OCaml side, because of the > > > > 31 bit integers on the OCaml side right? > > > > > > Yes, you do, unless you can guarantee that you don't need bit 31 ever. > > > The Unix library, for example, uses a 31/63-bit int for Unix FDs > > > (https://github.com/ocaml/ocaml/blob/trunk/otherlibs/unix/open.c) on the > > > realistic assumption that bit 31 will never be set. > > > > > > You should use a nativeint (still boxed) for an intptr_t, as that will > > > transparently work for 32/64-bit variants. > > > > > > > Been looking for examples but can’t find any, example code greatly > > > > appreciated, the C side and the ml side please. > > > > > > There's quite a lot in the manual for this > > > (http://caml.inria.fr/pub/docs/manual-ocaml/intfc.html) - Int32_val and > > > Nativeint_val are the two functions for converting an OCaml value to a C > > > type and caml_copy_int32 and caml_copy_nativeint are the equivalents for > > > converting the C type back to an OCaml value. > > > > > > Putting caml_copy_nativeint into a GitHub search reveals, for example, > > > https://github.com/yallop/ocaml-unix-type-representations/blob/master/ > > > lib/unix_type_representation_stubs.c (you can find many more examples > > > by searching for caml_copy_nativeint or caml_copy_int32) > > > > Thank you! Yes, did read through the manual but I was concerned about the > > unsigned part, does that not matter? > > Oh ah, I missed that part. Indeed it will - obviously, for uint32_int, you can use an int64 - the only way to use an unsigned type in OCaml is to use a bigger signed type. For unsigned 64-bit ints, that's obviously more painful. > > However, it can depend on what you want to do with it (for the C library you're binding). You don't lose/change any bits of a uint32_int by storing it in an int32, it just gets interpreted differently so, especially for unsigned ints being used only as bitmasks, you may be able to get away with leaving it as a signed OCaml value. > > > David I recommend looking at the ctypes module. Takes care of a lot of the repetitive and error prone work interfacing with C. MfG Goswin