Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Leszek.Holenderski@gmd.de
To: caml-list@pauillac.inria.fr
Subject: Q: How to call a CSL function from C
Date: Fri, 15 Dec 1995 13:40:00 +0100	[thread overview]
Message-ID: <v01520d0bacf6f9220314@[129.26.12.124]> (raw)


Hi Camlers,

Is there any official way to call a CSL function from C? CSL 1.12 manual
doesn't seem to mention anything about the subject.

I need to call some f: 'a -> 'b (in fact, 'a and 'b are some concrete
types, but it doesn't matter here) from my C code linked with CSL 1.12. In
my Caml code, I'll have the function

  external register_callback: ('a -> 'b) -> unit = "register_callback"

such that 'register_callback f' will store, in my C code, enough
information to call f later from the C code.

I suppose the C function for calling f should have the following pattern:

  value invoke_callback (f_info, arg);
    ... f_info;
    value arg;
  {
    value closure;
    closure = ... extract/build f's closure from f_info ...;
    return callback(closure, arg);  /* 'callback' is from interp.h */
  }

A simple solution would be to keep f's closure as f_info:

  static value f_closure = NULL;

  value register_callback (f);
    value f;
  {f_closure = f; return Val_unit;}

  value invoke_callback (f, arg);
    value f, arg;
  {return callback(f, arg);}

  ... invoke_callback(f_closure, something); ...

but I suspect it's too naive, since f's closure could be reallocated by GC,
between register_callback and invoke_callback.

The following code, borrowed from CamlTk for CL 0.7 (thus, from Francois
Rouaix), seems to avoid the problem:

  static code_t f_code = NULL;

  value register_callback (f);
    value f;
  {f_code = Code_val(f); return Val_unit;}

  value invoke_callback (f, arg);
    code_t f;
    value arg;
  {
    value closure;
    closure = alloc(2, Closure_tag);
    Code_val(closure) = f;
    Env_val(closure) = Atom(0);
    return callback(closure, arg);
  }

  ... invoke_callback(f_code, something); ...

When trying to port the code from CL 0.7 to CSL 1.12, I encountered the
following problems:

1. How to port the innocently looking 'Env_val(closure) = Atom(0)' if
Env_val seems to disappear in CSL 1.12.

2. Does Atom(0) still represent the empty environment? Or maybe Val_int(0)
is used for the purpose? (The later possibility came to my mind since
Val_unit used to be Atom(0), in CL, but it is  Val_int(0), in CSL.)

3. What are atoms used for in CSL 1.12, anyway? (In CL 0.7, they were used
for representing constant constructors. In CSl 1.12, constant constructors
are represented by integers.)

4. What functions survive GC? More precisely, for what f, Code_val(f) is
not changed by GC? Only for those which are declared on the top level?

Any help will be appreciated.

Cheers,
Leszek







+---------------------------------+-------------------------+
| Leszek Holenderski              | room  C2-222            |
| GMD-I5.SKS                      | phone ++ 02241 142412   |
| Schloss Birlinghoven            | fax   ++ 02241 142035   |
| Postfach 1316                   | email lhol@gmdzi.gmd.de |
| 53757 Sankt Augustin 1, Germany |                         |
+---------------------------------+-------------------------+

GMD = Gesellschaft fuer Mathematik und Datenverarbeitung, i.e.
      National Research Centre for Comp. Sci.






             reply	other threads:[~1995-12-15 13:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-12-15 12:40 Leszek.Holenderski [this message]
1995-12-15 16:33 ` Xavier Leroy

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='v01520d0bacf6f9220314@[129.26.12.124]' \
    --to=leszek.holenderski@gmd.de \
    --cc=caml-list@pauillac.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