caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Harrison, John R" <john.r.harrison@intel.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Cc: "Harrison, John R" <john.r.harrison@intel.com>
Subject: [Caml-list] Non-toplevel C functions using OCaml values
Date: Mon, 9 Jan 2012 02:35:25 +0000	[thread overview]
Message-ID: <BD6BC2F330F5FB42AE6C31E84D7B76D725691790@ORSMSX101.amr.corp.intel.com> (raw)

I have a question about interfacing C and OCaml functions that I
didn't see an answer to in the manual, though maybe I just didn't look
carefully enough. I think I pretty well understand how to set up a C
function that is designed to be called directly by OCaml. But what
about a function that I want to use internally as a utility, callable
by other C functions that are themselves called by OCaml?

For example, if I want to implement a function designed to be called
directly by OCaml and basically equivalent to the OCaml definition

  let cons h t = h::t;;

then I think (correct me if I'm wrong) that I do the following:

  CAMLprim value caml_cons(value h,value t)
  { CAMLparam2(h,t);
    CAMLlocal1(newblock);
    newblock = caml_alloc(2,0);
    Store_field(newblock,0,h);
    Store_field(newblock,1,t);
    CAMLreturn(newblock);
  }

But what if I want to set up a utility that I'm only going to call
from other C functions, and which involves a mixture of standard C
types and OCaml values among its arguments or local variables, and/or
allocates OCaml values? For example, suppose I want to implement the
special case of the above "cons" for int lists where the first
argument is a C int (which I assume to be in range for an OCaml
int). I might guess that I would do something like this:

  value int_cons(int h,value t)
  { CAMLparam1(t);
    CAMLlocal1(newblock);
    newblock = caml_alloc(2,0);
    Store_field(newblock,0,Val_int(h));
    Store_field(newblock,1,t);
    CAMLreturn(newblock);
  }

and then just call it like any other C function. However, I can
imagine several other plausible alternatives, e.g. that I should skip
some or all of the special CAMLsomething functions, or that I should
just forget the idea and use OCaml values throughout, calling it as if
it were any other OCaml function. Can someone enlighten me?

John.



             reply	other threads:[~2012-01-09  2:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-09  2:35 Harrison, John R [this message]
2012-01-09  7:33 ` Gerd Stolpmann
2012-01-09  9:23   ` ygrek

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=BD6BC2F330F5FB42AE6C31E84D7B76D725691790@ORSMSX101.amr.corp.intel.com \
    --to=john.r.harrison@intel.com \
    --cc=caml-list@inria.fr \
    /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).