caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Allsopp <dra-news@metastack.com>
To: Edgar A <edgar.factorial@gmail.com>,
	"caml-list@inria.fr" <caml-list@inria.fr>
Subject: RE: [Caml-list] Getting some C values over to the OCaml side correctly
Date: Sun, 25 Sep 2016 08:40:38 +0000	[thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D901351FA074@Remus.metastack.local> (raw)
In-Reply-To: <090F68D4-7CA4-4091-BC5D-5F2C9BFB2AEC@gmail.com>

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)


David

  reply	other threads:[~2016-09-25  8:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-25  8:02 Edgar A
2016-09-25  8:40 ` David Allsopp [this message]
2016-09-25  8:43   ` Edgar A
2016-09-25 12:28     ` David Allsopp
2016-10-06  9:19       ` Goswin von Brederlow

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=E51C5B015DBD1348A1D85763337FB6D901351FA074@Remus.metastack.local \
    --to=dra-news@metastack.com \
    --cc=caml-list@inria.fr \
    --cc=edgar.factorial@gmail.com \
    /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).