Q: external data in Ocaml 1.03

Basile STARYNKEVITCH (Basile.Starynkevitch@cea.fr)
Thu, 19 Dec 1996 16:40:15 +0100

Date: Thu, 19 Dec 1996 16:40:15 +0100
Message-Id: <199612191540.QAA32485@soleil.serma.cea.fr>
From: Basile STARYNKEVITCH <Basile.Starynkevitch@cea.fr>
To: caml-list@inria.fr
Subject: Q: external data in Ocaml 1.03

On some occasions, it would be useful to have external (foreign) data
in Ocaml.

I'll take a specific example: a small X11 Xlib encapsulation.

The ocaml 1.03 manual says (§13.2) that "a pointer to an object
outside the heap (eg a pointer to a block allocated by malloc, or to a
C variable)" is a valide object of type value.

So X11 Display* pointers (which are malloc-ed by the Xlib) are valid
ocaml objects. In particular, I suppose that the null (Display*)0 is a
valid ocaml object. But I don't know how to get it!

In Ocaml I'll code naturally:

type x11_display_t;; (*opaque type for Display* Xlib pointer *)

external x11_nulldisplay: x11_display_t = "caml_x11_nulldisplay";;

This does compile with Ocamlopt 1.03. Of course, the compiled code
contains a call to the C routine caml_x11_nulldisplay which is not
what I expect.

While in C I want to code:

const Display* caml_x11_nulldisplay=(Display*)0;;

This doesn't work as expected.

Of course, purist will rightly say that the null pointer is not a
valid C pointer! But I might also code (a useless but valid example)
in Caml

type c_file_t;; (*opaque type for FILE* stdio pointer*)

external c_stdout: c_file_t = "caml_stdout";;

with the C code

const FILE* caml_stdout=stdout;

Actually I think that the runtime might have a routine that converts a
C pointer (possibly Null) to None when it is 0 and to Some ptr when it
is a valid ptr.

My question is: how do I get external constant in Caml? I think that
the only hacky way is to define a C routine returning this constant:

in Caml:

external x11_get_null_display : unit -> x11_display_t
= "caml_get_x11_nulldisplay";;

val x11_nulldisplay = x11_get_null_display () ;;

in C

const Display*caml_get_x11_newdisplay(void)
{
return (Display*)0;
}

Thanks for reading

-- 

N.B. Any opinions expressed here are solely mine, and not of my organization. N.B. Les opinions exprimees ici me sont personnelles et n engagent pas le CEA.

---------------------------------------------------------------------- Basile STARYNKEVITCH ---- Commissariat à l Energie Atomique DRN/DMT/SERMA * CEA/Saclay bat.470 * 91191 GIF/YVETTE CEDEX * France fax: (33) 01,69.08.85.68; phone: 01,69.08.40.66; home: 01,46.65.45.53 email: Basile . Starynkevitch @ cea . fr (but remove white space) I speak french, english, russian. Je parle français, anglais, russe. ----------------------------------------------------------------------