8
6
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
9
<META name="GENERATOR" content="hevea 1.08">
7
<META name="GENERATOR" content="hevea 1.09">
10
8
<LINK rel="stylesheet" type="text/css" href="manual.css">
12
Interfacing C with Objective Caml
9
<TITLE>Interfacing C with Objective Caml</TITLE>
16
<A HREF="manual031.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
17
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
18
<A HREF="manual033.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
12
<A HREF="manual031.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
13
<A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
14
<A HREF="manual033.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
21
<H1 CLASS="chapter"><A NAME="htoc204">Chapter 18</A> Interfacing C with Objective Caml</H1> <A NAME="c:intf-c"></A>
23
This chapter describes how user-defined primitives, written in C, can
24
be linked with Caml code and called from Caml functions.<BR>
26
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
27
Since 3.08.0 all global C identifiers have been prefixed with
28
"<KBD>caml_</KBD>" to avoid name clashes with other libraries (i.e.,
29
<KBD>caml_alloc_small</KBD> instead of <KBD>alloc_small</KBD>).
30
This change has been announced but has not been documented
31
before. These new "<KBD>caml_</KBD>" prefixes are not marked in
35
<H2 CLASS="section"><A NAME="htoc205">18.1</A> Overview and compilation information</H2>
38
<H3 CLASS="subsection"><A NAME="htoc206">18.1.1</A> Declaring primitives</H3>
16
<H1 CLASS="chapter"><A NAME="htoc204">Chapter�18</A>��Interfacing<A NAME="c:intf-c"></A> C with Objective Caml</H1><P>This chapter describes how user-defined primitives, written in C, can
17
be linked with Caml code and called from Caml functions.</P><H2 CLASS="section"><A NAME="htoc205">18.1</A>��Overview and compilation information</H2><H3 CLASS="subsection"><A NAME="htoc206">18.1.1</A>��Declaring primitives</H3><P>
39
18
User primitives are declared in an implementation file or
40
<TT>struct</TT>...<TT>end</TT> module expression using the <TT>external</TT> keyword:
19
<TT>struct</TT>…<TT>end</TT> module expression using the <TT>external</TT> keyword:
42
21
external <I>name</I> : <I>type</I> = <I>C-function-name</I>
44
23
This defines the value name <I>name</I> as a function with type
45
24
<I>type</I> that executes by calling the given C function.
46
25
For instance, here is how the <TT>input</TT> primitive is declared in the
47
26
standard library module <TT>Pervasives</TT>:
48
<PRE CLASS="verbatim">
49
external input : in_channel -> string -> int -> int -> int
27
</P><PRE CLASS="verbatim"> external input : in_channel -> string -> int -> int -> int
51
</PRE>Primitives with several arguments are always curried. The C function
52
does not necessarily have the same name as the ML function.<BR>
54
External functions thus defined can be specified in interface files or
55
<TT>sig</TT>...<TT>end</TT> signatures either as regular values
29
</PRE><P>Primitives with several arguments are always curried. The C function
30
does not necessarily have the same name as the ML function.</P><P>External functions thus defined can be specified in interface files or
31
<TT>sig</TT>…<TT>end</TT> signatures either as regular values
57
33
val <I>name</I> : <I>type</I>
59
35
thus hiding their implementation as a C function, or explicitly as
60
“manifest” external functions
36
“manifest” external functions
62
38
external <I>name</I> : <I>type</I> = <I>C-function-name</I>
64
40
The latter is slightly more efficient, as it allows clients of the
65
41
module to call directly the C function instead of going through the
66
corresponding Caml function. <BR>
68
The arity (number of arguments) of a primitive is automatically
42
corresponding Caml function. </P><P>The arity (number of arguments) of a primitive is automatically
69
43
determined from its Caml type in the <TT>external</TT> declaration, by
70
44
counting the number of function arrows in the type. For instance,
71
45
<TT>input</TT> above has arity 4, and the <TT>input</TT> C function is called with
72
46
four arguments. Similarly,
73
<PRE CLASS="verbatim">
74
external input2 : in_channel * string * int * int -> int = "input2"
75
</PRE>has arity 1, and the <TT>input2</TT> C function receives one argument (which
76
is a quadruple of Caml values).<BR>
78
Type abbreviations are not expanded when determining the arity of a
47
</P><PRE CLASS="verbatim"> external input2 : in_channel * string * int * int -> int = "input2"
48
</PRE><P>has arity 1, and the <TT>input2</TT> C function receives one argument (which
49
is a quadruple of Caml values).</P><P>Type abbreviations are not expanded when determining the arity of a
79
50
primitive. For instance,
80
<PRE CLASS="verbatim">
81
type int_endo = int -> int
51
</P><PRE CLASS="verbatim"> type int_endo = int -> int
82
52
external f : int_endo -> int_endo = "f"
83
53
external g : (int -> int) -> (int -> int) = "f"
84
</PRE><TT>f</TT> has arity 1, but <TT>g</TT> has arity 2. This allows a primitive to
54
</PRE><P><TT>f</TT> has arity 1, but <TT>g</TT> has arity 2. This allows a primitive to
85
55
return a functional value (as in the <TT>f</TT> example above): just remember
86
to name the functional return type in a type abbreviation.<BR>
89
<H3 CLASS="subsection"><A NAME="htoc207">18.1.2</A> Implementing primitives</H3>
90
User primitives with arity <I>n</I> ≤ 5 are implemented by C functions
56
to name the functional return type in a type abbreviation.</P><H3 CLASS="subsection"><A NAME="htoc207">18.1.2</A>��Implementing primitives</H3><P>User primitives with arity <I>n</I> ≤ 5 are implemented by C functions
91
57
that take <I>n</I> arguments of type <TT>value</TT>, and return a result of type
92
58
<TT>value</TT>. The type <TT>value</TT> is the type of the representations for Caml
93
59
values. It encodes objects of several base types (integers,
94
floating-point numbers, strings, ...), as well as Caml data
60
floating-point numbers, strings, …), as well as Caml data
95
61
structures. The type <TT>value</TT> and the associated conversion
96
62
functions and macros are described in details below. For instance,
97
63
here is the declaration for the C function implementing the <TT>input</TT>
99
<PRE CLASS="verbatim">
100
CAMLprim value input(value channel, value buffer, value offset, value length)
65
</P><PRE CLASS="verbatim">CAMLprim value input(value channel, value buffer, value offset, value length)
104
</PRE>When the primitive function is applied in a Caml program, the C
69
</PRE><P>When the primitive function is applied in a Caml program, the C
105
70
function is called with the values of the expressions to which the
106
71
primitive is applied as arguments. The value returned by the function is
107
72
passed back to the Caml program as the result of the function
110
User primitives with arity greater than 5 should be implemented by two
73
application.</P><P>User primitives with arity greater than 5 should be implemented by two
111
74
C functions. The first function, to be used in conjunction with the
112
75
bytecode compiler <TT>ocamlc</TT>, receives two arguments: a pointer to an
113
76
array of Caml values (the values for the arguments), and an
146
106
distinct C functions to implement these two tasks. The first function
147
107
actually implements the primitive, taking native C values as
148
108
arguments and returning a native C value. The second function,
149
often called the “stub code”, is a simple wrapper around the first
109
often called the “stub code”, is a simple wrapper around the first
150
110
function that converts its arguments from Caml values to C values,
151
111
call the first function, and convert the returned C value to Caml
152
112
value. For instance, here is the stub code for the <TT>input</TT>
154
<PRE CLASS="verbatim">
155
CAMLprim value input(value channel, value buffer, value offset, value length)
114
</P><PRE CLASS="verbatim">CAMLprim value input(value channel, value buffer, value offset, value length)
157
116
return Val_long(getblock((struct channel *) channel,
158
117
&Byte(buffer, Long_val(offset)),
159
118
Long_val(length)));
161
</PRE>(Here, <TT>Val_long</TT>, <TT>Long_val</TT> and so on are conversion macros for the
120
</PRE><P>(Here, <TT>Val_long</TT>, <TT>Long_val</TT> and so on are conversion macros for the
162
121
type <TT>value</TT>, that will be described later. The <TT>CAMLprim</TT> macro
163
122
expands to the required compiler directives to ensure that the
164
123
function following it is exported and accessible from Caml.)
165
124
The hard work is performed by the function <TT>getblock</TT>, which is
167
<PRE CLASS="verbatim">
168
long getblock(struct channel * channel, char * p, long n)
126
</P><PRE CLASS="verbatim">long getblock(struct channel * channel, char * p, long n)
173
To write C code that operates on Objective Caml values, the following
130
</PRE><P>To write C code that operates on Objective Caml values, the following
174
131
include files are provided:
177
<DIV CLASS="center"><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%">
178
<TR><TD ALIGN=center NOWRAP><B>Include file</B></TD>
179
<TD ALIGN=center NOWRAP><B>Provides</B></TD>
132
</P><DIV CLASS="center"><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%"><TR><TD ALIGN=center NOWRAP><B>Include file</B></TD><TD ALIGN=center NOWRAP><B>Provides</B></TD></TR>
181
133
<TR><TD VALIGN=top ALIGN=left NOWRAP>
182
<TT>caml/mlvalues.h</TT></TD>
183
<TD VALIGN=top ALIGN=left>definition of the <TT>value</TT> type, and conversion macros</TD>
185
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/alloc.h</TT></TD>
186
<TD VALIGN=top ALIGN=left>allocation functions (to create structured Caml
189
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/memory.h</TT></TD>
190
<TD VALIGN=top ALIGN=left>miscellaneous memory-related functions
191
and macros (for GC interface, in-place modification of structures, etc).</TD>
193
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/fail.h</TT></TD>
194
<TD VALIGN=top ALIGN=left>functions for raising exceptions
195
(see section <A HREF="#s:c-exceptions">18.4.5</A>)</TD>
197
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/callback.h</TT></TD>
198
<TD VALIGN=top ALIGN=left>callback from C to Caml (see
199
section <A HREF="#s:callback">18.7</A>).</TD>
201
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/custom.h</TT></TD>
202
<TD VALIGN=top ALIGN=left>operations on custom blocks (see
203
section <A HREF="#s:custom">18.9</A>).</TD>
205
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/intext.h</TT></TD>
206
<TD VALIGN=top ALIGN=left>operations for writing user-defined
134
<TT>caml/mlvalues.h</TT></TD><TD VALIGN=top ALIGN=left>definition of the <TT>value</TT> type, and conversion macros</TD></TR>
135
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/alloc.h</TT></TD><TD VALIGN=top ALIGN=left>allocation functions (to create structured Caml
137
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/memory.h</TT></TD><TD VALIGN=top ALIGN=left>miscellaneous memory-related functions
138
and macros (for GC interface, in-place modification of structures, etc).</TD></TR>
139
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/fail.h</TT></TD><TD VALIGN=top ALIGN=left>functions for raising exceptions
140
(see section�<A HREF="#s:c-exceptions">18.4.5</A>)</TD></TR>
141
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/callback.h</TT></TD><TD VALIGN=top ALIGN=left>callback from C to Caml (see
142
section�<A HREF="#s:callback">18.7</A>).</TD></TR>
143
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/custom.h</TT></TD><TD VALIGN=top ALIGN=left>operations on custom blocks (see
144
section�<A HREF="#s:custom">18.9</A>).</TD></TR>
145
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml/intext.h</TT></TD><TD VALIGN=top ALIGN=left>operations for writing user-defined
207
146
serialization and deserialization functions for custom blocks
208
(see section <A HREF="#s:custom">18.9</A>).</TD>
209
</TR></TABLE></DIV><BR>
147
(see section�<A HREF="#s:custom">18.9</A>).</TD></TR>
212
149
These files reside in the <TT>caml/</TT> subdirectory of the Objective Caml
213
standard library directory (usually <TT>/usr/local/lib/ocaml</TT>).<BR>
216
<H3 CLASS="subsection"><A NAME="htoc208">18.1.3</A> Statically linking C code with Caml code</H3>
217
<A NAME="staticlink-c-code"></A>
218
The Objective Caml runtime system comprises three main parts: the bytecode
150
standard library directory (usually <TT>/usr/local/lib/ocaml</TT>).</P><H3 CLASS="subsection"><A NAME="htoc208">18.1.3</A>��Statically linking C code with Caml code</H3><P>
151
<A NAME="staticlink-c-code"></A></P><P>The Objective Caml runtime system comprises three main parts: the bytecode
219
152
interpreter, the memory manager, and a set of C functions that
220
153
implement the primitive operations. Some bytecode instructions are
221
154
provided to call these C functions, designated by their offset in a
222
table of functions (the table of primitives).<BR>
224
In the default mode, the Caml linker produces bytecode for the
155
table of functions (the table of primitives).</P><P>In the default mode, the Caml linker produces bytecode for the
225
156
standard runtime system, with a standard set of primitives. References
226
157
to primitives that are not in this standard set result in the
227
“unavailable C primitive” error. (Unless dynamic loading of C
228
libraries is supported – see section <A HREF="#dynlink-c-code">18.1.4</A> below.)<BR>
230
In the “custom runtime” mode, the Caml linker scans the
158
“unavailable C primitive” error. (Unless dynamic loading of C
159
libraries is supported – see section�<A HREF="#dynlink-c-code">18.1.4</A> below.)</P><P>In the “custom runtime” mode, the Caml linker scans the
231
160
object files and determines the set of required primitives. Then, it
232
161
builds a suitable runtime system, by calling the native code linker with:
233
<UL CLASS="itemize"><LI CLASS="li-itemize">
162
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
234
163
the table of the required primitives;
235
<LI CLASS="li-itemize">a library that provides the bytecode interpreter, the
164
</LI><LI CLASS="li-itemize">a library that provides the bytecode interpreter, the
236
165
memory manager, and the standard primitives;
237
<LI CLASS="li-itemize">libraries and object code files (<TT>.o</TT> files) mentioned on the
166
</LI><LI CLASS="li-itemize">libraries and object code files (<TT>.o</TT> files) mentioned on the
238
167
command line for the Caml linker, that provide implementations
239
168
for the user's primitives.
241
170
This builds a runtime system with the required primitives. The Caml
242
171
linker generates bytecode for this custom runtime system. The
243
172
bytecode is appended to the end of the custom runtime system, so that
244
173
it will be automatically executed when the output file (custom
245
runtime + bytecode) is launched.<BR>
247
To link in “custom runtime” mode, execute the <TT>ocamlc</TT> command with:
248
<UL CLASS="itemize"><LI CLASS="li-itemize">
174
runtime + bytecode) is launched.</P><P>To link in “custom runtime” mode, execute the <TT>ocamlc</TT> command with:
175
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
249
176
the <TT>-custom</TT> option;
250
<LI CLASS="li-itemize">the names of the desired Caml object files (<TT>.cmo</TT> and <TT>.cma</TT> files) ;
251
<LI CLASS="li-itemize">the names of the C object files and libraries (<TT>.o</TT> and <TT>.a</TT>
177
</LI><LI CLASS="li-itemize">the names of the desired Caml object files (<TT>.cmo</TT> and <TT>.cma</TT> files) ;
178
</LI><LI CLASS="li-itemize">the names of the C object files and libraries (<TT>.o</TT> and <TT>.a</TT>
252
179
files) that implement the required primitives. Under Unix and Windows,
253
180
a library named <TT>lib</TT><I>name</I><TT>.a</TT> residing in one of the standard
254
181
library directories can also be specified as <TT>-cclib -l</TT><I>name</I>.
256
If you are using the native-code compiler <TT>ocamlopt</TT>, the <TT>-custom</TT>
182
</LI></UL><P>If you are using the native-code compiler <TT>ocamlopt</TT>, the <TT>-custom</TT>
257
183
flag is not needed, as the final linking phase of <TT>ocamlopt</TT> always
258
184
builds a standalone executable. To build a mixed Caml/C executable,
259
185
execute the <TT>ocamlopt</TT> command with:
260
<UL CLASS="itemize"><LI CLASS="li-itemize">
186
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
261
187
the names of the desired Caml native object files (<TT>.cmx</TT> and
262
188
<TT>.cmxa</TT> files);
263
<LI CLASS="li-itemize">the names of the C object files and libraries (<TT>.o</TT>, <TT>.a</TT>,
189
</LI><LI CLASS="li-itemize">the names of the C object files and libraries (<TT>.o</TT>, <TT>.a</TT>,
264
190
<TT>.so</TT> or <TT>.dll</TT> files) that implement the required primitives.
266
Starting with OCaml 3.00, it is possible to record the
191
</LI></UL><P>Starting with OCaml 3.00, it is possible to record the
267
192
<TT>-custom</TT> option as well as the names of C libraries in a Caml
268
193
library file <TT>.cma</TT> or <TT>.cmxa</TT>. For instance, consider a Caml library
269
194
<TT>mylib.cma</TT>, built from the Caml object files <TT>a.cmo</TT> and <TT>b.cmo</TT>,
270
195
which reference C code in <TT>libmylib.a</TT>. If the library is
271
196
built as follows:
273
198
ocamlc -a -o mylib.cma -custom a.cmo b.cmo -cclib -lmylib
275
200
users of the library can simply link with <TT>mylib.cma</TT>:
277
202
ocamlc -o myprog mylib.cma ...
279
204
and the system will automatically add the <TT>-custom</TT> and <TT>-cclib -lmylib</TT> options, achieving the same effect as
281
206
ocamlc -o myprog -custom a.cmo b.cmo ... -cclib -lmylib
283
208
The alternative, of course, is to build the library without extra
286
211
ocamlc -a -o mylib.cma a.cmo b.cmo
288
213
and then ask users to provide the <TT>-custom</TT> and <TT>-cclib -lmylib</TT>
289
214
options themselves at link-time:
291
216
ocamlc -o myprog -custom mylib.cma ... -cclib -lmylib
293
218
The former alternative is more convenient for the final users of the
294
library, however.<BR>
297
<H3 CLASS="subsection"><A NAME="htoc209">18.1.4</A> Dynamically linking C code with Caml code</H3>
298
<A NAME="dynlink-c-code"></A>
299
Starting with OCaml 3.03, an alternative to static linking of C code
219
library, however.</P><H3 CLASS="subsection"><A NAME="htoc209">18.1.4</A>��Dynamically linking C code with Caml code</H3><P>
220
<A NAME="dynlink-c-code"></A></P><P>Starting with OCaml 3.03, an alternative to static linking of C code
300
221
using the <TT>-custom</TT> code is provided. In this mode, the Caml linker
301
222
generates a pure bytecode executable (no embedded custom runtime
302
223
system) that simply records the names of dynamically-loaded libraries
303
224
containing the C code. The standard Caml runtime system <TT>ocamlrun</TT>
304
225
then loads dynamically these libraries, and resolves references to the
305
required primitives, before executing the bytecode.<BR>
307
This facility is currently supported and known to work well under
308
Linux and Windows (the native Windows port). It is supported, but not
226
required primitives, before executing the bytecode.</P><P>This facility is currently supported and known to work well under
227
Linux, MacOS�X, and Windows (the native Windows port). It is supported, but not
309
228
fully tested yet, under FreeBSD, Tru64, Solaris and Irix. It is not
310
supported yet under other Unixes, Cygwin for Windows, and MacOS.<BR>
312
To dynamically link C code with Caml code, the C code must first be
229
supported yet under other Unixes and under Cygwin for Windows.</P><P>To dynamically link C code with Caml code, the C code must first be
313
230
compiled into a shared library (under Unix) or DLL (under Windows).
314
231
This involves 1- compiling the C files with appropriate C compiler
315
232
flags for producing position-independent code, and 2- building a
316
233
shared library from the resulting object files. The resulting shared
317
234
library or DLL file must be installed in a place where <TT>ocamlrun</TT> can
318
235
find it later at program start-up time (see
319
section <A HREF="manual024.html#s-ocamlrun-dllpath">10.3</A>).
236
section�<A HREF="manual024.html#s-ocamlrun-dllpath">10.3</A>).
320
237
Finally (step 3), execute the <TT>ocamlc</TT> command with
321
<UL CLASS="itemize"><LI CLASS="li-itemize">
238
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
322
239
the names of the desired Caml object files (<TT>.cmo</TT> and <TT>.cma</TT> files) ;
323
<LI CLASS="li-itemize">the names of the C shared libraries (<TT>.so</TT> or <TT>.dll</TT> files) that
240
</LI><LI CLASS="li-itemize">the names of the C shared libraries (<TT>.so</TT> or <TT>.dll</TT> files) that
324
241
implement the required primitives. Under Unix and Windows,
325
242
a library named <TT>dll</TT><I>name</I><TT>.so</TT> (respectively, <TT>.dll</TT>) residing
326
243
in one of the standard library directories can also be specified as
327
244
<TT>-dllib -l</TT><I>name</I>.
329
246
Do <EM>not</EM> set the <TT>-custom</TT> flag, otherwise you're back to static linking
330
as described in section <A HREF="#staticlink-c-code">18.1.3</A>.
331
Under Unix, the <TT>ocamlmklib</TT> tool (see section <A HREF="#s-ocamlmklib">18.10</A>)
332
automates steps 2 and 3.<BR>
334
As in the case of static linking, it is possible (and recommended) to
247
as described in section�<A HREF="#staticlink-c-code">18.1.3</A>.
248
Under Unix, the <TT>ocamlmklib</TT> tool (see section�<A HREF="#s-ocamlmklib">18.10</A>)
249
automates steps 2 and 3.</P><P>As in the case of static linking, it is possible (and recommended) to
335
250
record the names of C libraries in a Caml <TT>.cmo</TT> library archive.
336
251
Consider again a Caml library
337
252
<TT>mylib.cma</TT>, built from the Caml object files <TT>a.cmo</TT> and <TT>b.cmo</TT>,
338
253
which reference C code in <TT>dllmylib.so</TT>. If the library is
339
254
built as follows:
341
256
ocamlc -a -o mylib.cma a.cmo b.cmo -dllib -lmylib
343
258
users of the library can simply link with <TT>mylib.cma</TT>:
345
260
ocamlc -o myprog mylib.cma ...
347
262
and the system will automatically add the <TT>-dllib -lmylib</TT> option,
348
263
achieving the same effect as
350
265
ocamlc -o myprog a.cmo b.cmo ... -dllib -lmylib
352
267
Using this mechanism, users of the library <TT>mylib.cma</TT> do not need to
353
268
known that it references C code, nor whether this C code must be
354
statically linked (using <TT>-custom</TT>) or dynamically linked.<BR>
357
<H3 CLASS="subsection"><A NAME="htoc210">18.1.5</A> Choosing between static linking and dynamic linking</H3>
358
After having described two different ways of linking C code with Caml
269
statically linked (using <TT>-custom</TT>) or dynamically linked.</P><H3 CLASS="subsection"><A NAME="htoc210">18.1.5</A>��Choosing between static linking and dynamic linking</H3><P>After having described two different ways of linking C code with Caml
359
270
code, we now review the pros and cons of each, to help developers of
360
mixed Caml/C libraries decide.<BR>
362
The main advantage of dynamic linking is that it preserves the
271
mixed Caml/C libraries decide.</P><P>The main advantage of dynamic linking is that it preserves the
363
272
platform-independence of bytecode executables. That is, the bytecode
364
273
executable contains no machine code, and can therefore be compiled on
365
platform <I>A</I> and executed on other platforms <I>B</I>, <I>C</I>, ..., as long
274
platform <I>A</I> and executed on other platforms <I>B</I>, <I>C</I>, …, as long
366
275
as the required shared libraries are available on all these
367
276
platforms. In contrast, executables generated by <TT>ocamlc -custom</TT> run
368
277
only on the platform on which they were created, because they embark a
369
278
custom-tailored runtime system specific to that platform. In
370
addition, dynamic linking results in smaller executables.<BR>
372
Another advantage of dynamic linking is that the final users of the
279
addition, dynamic linking results in smaller executables.</P><P>Another advantage of dynamic linking is that the final users of the
373
280
library do not need to have a C compiler, C linker, and C runtime
374
281
libraries installed on their machines. This is no big deal under
375
282
Unix and Cygwin, but many Windows users are reluctant to install
376
Microsoft Visual C just to be able to do <TT>ocamlc -custom</TT>.<BR>
378
There are two drawbacks to dynamic linking. The first is that the
283
Microsoft Visual C just to be able to do <TT>ocamlc -custom</TT>.</P><P>There are two drawbacks to dynamic linking. The first is that the
379
284
resulting executable is not stand-alone: it requires the shared
380
285
libraries, as well as <TT>ocamlrun</TT>, to be installed on the machine
381
286
executing the code. If you wish to distribute a stand-alone
382
287
executable, it is better to link it statically, using <TT>ocamlc -custom -ccopt -static</TT> or <TT>ocamlopt -ccopt -static</TT>. Dynamic linking also
383
raises the “DLL hell” problem: some care must be taken to ensure
288
raises the “DLL hell” problem: some care must be taken to ensure
384
289
that the right versions of the shared libraries are found at start-up
387
The second drawback of dynamic linking is that it complicates the
290
time.</P><P>The second drawback of dynamic linking is that it complicates the
388
291
construction of the library. The C compiler and linker flags to
389
292
compile to position-independent code and build a shared library vary
390
293
wildly between different Unix systems. Also, dynamic linking is not
391
294
supported on all Unix systems, requiring a fall-back case to static
392
295
linking in the Makefile for the library. The <TT>ocamlmklib</TT> command
393
(see section <A HREF="#s-ocamlmklib">18.10</A>) tries to hide some of these system
396
In conclusion: dynamic linking is highly recommended under the native
296
(see section�<A HREF="#s-ocamlmklib">18.10</A>) tries to hide some of these system
297
dependencies.</P><P>In conclusion: dynamic linking is highly recommended under the native
397
298
Windows port, because there are no portability problems and it is much
398
299
more convenient for the end users. Under Unix, dynamic linking should
399
300
be considered for mature, frequently used libraries because it
400
301
enhances platform-independence of bytecode executables. For new or
401
302
rarely-used libraries, static linking is much simpler to set up in a
405
<H3 CLASS="subsection"><A NAME="htoc211">18.1.6</A> Building standalone custom runtime systems</H3>
406
<A NAME="s:custom-runtime"></A>
407
It is sometimes inconvenient to build a custom runtime system each
303
portable way.</P><H3 CLASS="subsection"><A NAME="htoc211">18.1.6</A>��Building standalone custom runtime systems</H3><P>
304
<A NAME="s:custom-runtime"></A></P><P>It is sometimes inconvenient to build a custom runtime system each
408
305
time Caml code is linked with C libraries, like <TT>ocamlc -custom</TT> does.
409
306
For one thing, the building of the runtime system is slow on some
410
307
systems (that have bad linkers or slow remote file systems); for
411
308
another thing, the platform-independence of bytecode files is lost,
412
forcing to perform one <TT>ocamlc -custom</TT> link per platform of interest.<BR>
414
An alternative to <TT>ocamlc -custom</TT> is to build separately a custom
309
forcing to perform one <TT>ocamlc -custom</TT> link per platform of interest.</P><P>An alternative to <TT>ocamlc -custom</TT> is to build separately a custom
415
310
runtime system integrating the desired C libraries, then generate
416
“pure” bytecode executables (not containing their own runtime
311
“pure” bytecode executables (not containing their own runtime
417
312
system) that can run on this custom runtime. This is achieved by the
418
313
<TT>-make_runtime</TT> and <TT>-use_runtime</TT> flags to <TT>ocamlc</TT>. For example,
419
314
to build a custom runtime system integrating the C parts of the
420
“Unix” and “Threads” libraries, do:
421
<PRE CLASS="verbatim">
422
ocamlc -make-runtime -o /home/me/ocamlunixrun unix.cma threads.cma
423
</PRE>To generate a bytecode executable that runs on this runtime system,
315
“Unix” and “Threads” libraries, do:
316
</P><PRE CLASS="verbatim"> ocamlc -make-runtime -o /home/me/ocamlunixrun unix.cma threads.cma
317
</PRE><P>To generate a bytecode executable that runs on this runtime system,
426
320
ocamlc -use-runtime /home/me/ocamlunixrun -o myprog \
427
321
unix.cma threads.cma <I>your .cmo and .cma files</I>
429
323
The bytecode executable <TT>myprog</TT> can then be launched as usual:
430
<TT>myprog</TT> <I>args</I> or <TT>/home/me/ocamlunixrun myprog</TT> <I>args</I>.<BR>
432
Notice that the bytecode libraries <TT>unix.cma</TT> and <TT>threads.cma</TT> must
324
<TT>myprog</TT> <I>args</I> or <TT>/home/me/ocamlunixrun myprog</TT> <I>args</I>.</P><P>Notice that the bytecode libraries <TT>unix.cma</TT> and <TT>threads.cma</TT> must
433
325
be given twice: when building the runtime system (so that <TT>ocamlc</TT>
434
326
knows which C primitives are required) and also when building the
435
327
bytecode executable (so that the bytecode from <TT>unix.cma</TT> and
436
<TT>threads.cma</TT> is actually linked in).<BR>
439
<H2 CLASS="section"><A NAME="htoc212">18.2</A> The <TT>value</TT> type</H2>
441
All Caml objects are represented by the C type <TT>value</TT>,
328
<TT>threads.cma</TT> is actually linked in).</P><H2 CLASS="section"><A NAME="htoc212">18.2</A>��The <TT>value</TT> type</H2><P>All Caml objects are represented by the C type <TT>value</TT>,
442
329
defined in the include file <TT>caml/mlvalues.h</TT>, along with macros to
443
330
manipulate values of that type. An object of type <TT>value</TT> is either:
444
<UL CLASS="itemize"><LI CLASS="li-itemize">
331
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
445
332
an unboxed integer;
446
<LI CLASS="li-itemize">a pointer to a block inside the heap (such as the blocks
333
</LI><LI CLASS="li-itemize">a pointer to a block inside the heap (such as the blocks
447
334
allocated through one of the <CODE>caml_alloc_*</CODE> functions below);
448
<LI CLASS="li-itemize">a pointer to an object outside the heap (e.g., a pointer to a block
335
</LI><LI CLASS="li-itemize">a pointer to an object outside the heap (e.g., a pointer to a block
449
336
allocated by <TT>malloc</TT>, or to a C variable).
452
<H3 CLASS="subsection"><A NAME="htoc213">18.2.1</A> Integer values</H3>
453
Integer values encode 31-bit signed integers (63-bit on 64-bit
454
architectures). They are unboxed (unallocated).<BR>
457
<H3 CLASS="subsection"><A NAME="htoc214">18.2.2</A> Blocks</H3>
458
Blocks in the heap are garbage-collected, and therefore have strict
337
</LI></UL><H3 CLASS="subsection"><A NAME="htoc213">18.2.1</A>��Integer values</H3><P>Integer values encode 31-bit signed integers (63-bit on 64-bit
338
architectures). They are unboxed (unallocated).</P><H3 CLASS="subsection"><A NAME="htoc214">18.2.2</A>��Blocks</H3><P>Blocks in the heap are garbage-collected, and therefore have strict
459
339
structure constraints. Each block includes a header containing the
460
340
size of the block (in words), and the tag of the block.
461
341
The tag governs how the contents of the blocks are structured. A tag
516
372
pointer, formerly pointing outside the Caml heap, now points inside
517
373
the Caml heap, and this can confuse the garbage collector. To avoid
518
374
these problems, it is preferable to wrap the pointer in a Caml block
519
with tag <TT>Abstract_tag</TT> or <TT>Custom_tag</TT>.<BR>
522
<H2 CLASS="section"><A NAME="htoc216">18.3</A> Representation of Caml data types</H2>
524
This section describes how Caml data types are encoded in the
525
<TT>value</TT> type.<BR>
528
<H3 CLASS="subsection"><A NAME="htoc217">18.3.1</A> Atomic types</H3><BR>
530
<DIV CLASS="center"><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%">
531
<TR><TD ALIGN=center NOWRAP><B>Caml type</B></TD>
532
<TD ALIGN=center NOWRAP><B>Encoding</B></TD>
375
with tag <TT>Abstract_tag</TT> or <TT>Custom_tag</TT>.</P><H2 CLASS="section"><A NAME="htoc216">18.3</A>��Representation of Caml data types</H2><P>This section describes how Caml data types are encoded in the
376
<TT>value</TT> type.</P><H3 CLASS="subsection"><A NAME="htoc217">18.3.1</A>��Atomic types</H3><DIV CLASS="center"><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%"><TR><TD ALIGN=center NOWRAP><B>Caml type</B></TD><TD ALIGN=center NOWRAP><B>Encoding</B></TD></TR>
534
377
<TR><TD ALIGN=left NOWRAP>
536
<TD ALIGN=left NOWRAP>Unboxed integer values.</TD>
538
<TR><TD ALIGN=left NOWRAP><TT>char</TT></TD>
539
<TD ALIGN=left NOWRAP>Unboxed integer values (ASCII code).</TD>
541
<TR><TD ALIGN=left NOWRAP><TT>float</TT></TD>
542
<TD ALIGN=left NOWRAP>Blocks with tag <TT>Double_tag</TT>.</TD>
544
<TR><TD ALIGN=left NOWRAP><TT>string</TT></TD>
545
<TD ALIGN=left NOWRAP>Blocks with tag <TT>String_tag</TT>.</TD>
547
<TR><TD ALIGN=left NOWRAP><TT>int32</TT></TD>
548
<TD ALIGN=left NOWRAP>Blocks with tag <TT>Custom_tag</TT>.</TD>
550
<TR><TD ALIGN=left NOWRAP><TT>int64</TT></TD>
551
<TD ALIGN=left NOWRAP>Blocks with tag <TT>Custom_tag</TT>.</TD>
553
<TR><TD ALIGN=left NOWRAP><TT>nativeint</TT></TD>
554
<TD ALIGN=left NOWRAP>Blocks with tag <TT>Custom_tag</TT>.</TD>
555
</TR></TABLE></DIV><BR>
558
<H3 CLASS="subsection"><A NAME="htoc218">18.3.2</A> Tuples and records</H3>
559
Tuples are represented by pointers to blocks, with tag 0.<BR>
561
Records are also represented by zero-tagged blocks. The ordering of
378
<TT>int</TT></TD><TD ALIGN=left NOWRAP>Unboxed integer values.</TD></TR>
379
<TR><TD ALIGN=left NOWRAP><TT>char</TT></TD><TD ALIGN=left NOWRAP>Unboxed integer values (ASCII code).</TD></TR>
380
<TR><TD ALIGN=left NOWRAP><TT>float</TT></TD><TD ALIGN=left NOWRAP>Blocks with tag <TT>Double_tag</TT>.</TD></TR>
381
<TR><TD ALIGN=left NOWRAP><TT>string</TT></TD><TD ALIGN=left NOWRAP>Blocks with tag <TT>String_tag</TT>.</TD></TR>
382
<TR><TD ALIGN=left NOWRAP><TT>int32</TT></TD><TD ALIGN=left NOWRAP>Blocks with tag <TT>Custom_tag</TT>.</TD></TR>
383
<TR><TD ALIGN=left NOWRAP><TT>int64</TT></TD><TD ALIGN=left NOWRAP>Blocks with tag <TT>Custom_tag</TT>.</TD></TR>
384
<TR><TD ALIGN=left NOWRAP><TT>nativeint</TT></TD><TD ALIGN=left NOWRAP>Blocks with tag <TT>Custom_tag</TT>.</TD></TR>
385
</TABLE></DIV><H3 CLASS="subsection"><A NAME="htoc218">18.3.2</A>��Tuples and records</H3><P>Tuples are represented by pointers to blocks, with tag�0.</P><P>Records are also represented by zero-tagged blocks. The ordering of
562
386
labels in the record type declaration determines the layout of
563
387
the record fields: the value associated to the label
564
declared first is stored in field 0 of the block, the value associated
565
to the label declared next goes in field 1, and so on.<BR>
567
As an optimization, records whose fields all have static type <TT>float</TT>
388
declared first is stored in field�0 of the block, the value associated
389
to the label declared next goes in field�1, and so on.</P><P>As an optimization, records whose fields all have static type <TT>float</TT>
568
390
are represented as arrays of floating-point numbers, with tag
569
<TT>Double_array_tag</TT>. (See the section below on arrays.)<BR>
572
<H3 CLASS="subsection"><A NAME="htoc219">18.3.3</A> Arrays</H3>
573
Arrays of integers and pointers are represented like tuples,
574
that is, as pointers to blocks tagged 0. They are accessed with the
575
<TT>Field</TT> macro for reading and the <TT>modify</TT> function for writing.<BR>
577
Arrays of floating-point numbers (type <TT>float array</TT>)
391
<TT>Double_array_tag</TT>. (See the section below on arrays.)</P><H3 CLASS="subsection"><A NAME="htoc219">18.3.3</A>��Arrays</H3><P>Arrays of integers and pointers are represented like tuples,
392
that is, as pointers to blocks tagged�0. They are accessed with the
393
<TT>Field</TT> macro for reading and the <TT>modify</TT> function for writing.</P><P>Arrays of floating-point numbers (type <TT>float array</TT>)
578
394
have a special, unboxed, more efficient representation.
579
395
These arrays are represented by pointers to blocks with tag
580
396
<TT>Double_array_tag</TT>. They should be accessed with the <TT>Double_field</TT>
581
and <TT>Store_double_field</TT> macros.<BR>
584
<H3 CLASS="subsection"><A NAME="htoc220">18.3.4</A> Concrete types</H3>
585
Constructed terms are represented either by unboxed integers (for
397
and <TT>Store_double_field</TT> macros.</P><H3 CLASS="subsection"><A NAME="htoc220">18.3.4</A>��Concrete types</H3><P>Constructed terms are represented either by unboxed integers (for
586
398
constant constructors) or by blocks whose tag encode the constructor
587
399
(for non-constant constructors). The constant constructors and the
588
400
non-constant constructors for a given concrete type are numbered
592
404
constructors declared with a <I>n</I>-tuple as argument are represented by
593
405
a block of size <I>n</I>, tagged with the constructor number; the <I>n</I>
594
406
fields contain the components of its tuple argument. Other
595
non-constant constructors are represented by a block of size 1, tagged
596
with the constructor number; the field 0 contains the value of the
597
constructor argument. Example:<BR>
599
<DIV CLASS="center"><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%">
600
<TR><TD ALIGN=center NOWRAP><B>Constructed term</B></TD>
601
<TD ALIGN=center NOWRAP><B>Representation</B></TD>
407
non-constant constructors are represented by a block of size�1, tagged
408
with the constructor number; the field�0 contains the value of the
409
constructor argument. Example:</P><DIV CLASS="center"><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%"><TR><TD ALIGN=center NOWRAP><B>Constructed term</B></TD><TD ALIGN=center NOWRAP><B>Representation</B></TD></TR>
603
410
<TR><TD VALIGN=top ALIGN=left NOWRAP>
605
<TD VALIGN=top ALIGN=left><TT>Val_int(0)</TT></TD>
607
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>false</TT></TD>
608
<TD VALIGN=top ALIGN=left><TT>Val_int(0)</TT></TD>
610
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>true</TT></TD>
611
<TD VALIGN=top ALIGN=left><TT>Val_int(1)</TT></TD>
613
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>[]</TT></TD>
614
<TD VALIGN=top ALIGN=left><TT>Val_int(0)</TT></TD>
616
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>h::t</TT></TD>
617
<TD VALIGN=top ALIGN=left>Block with size = 2 and tag = 0; first field
618
contains <TT>h</TT>, second field <TT>t</TT></TD>
619
</TR></TABLE></DIV><BR>
621
As a convenience, <TT>caml/mlvalues.h</TT> defines the macros <TT>Val_unit</TT>,
622
<TT>Val_false</TT> and <TT>Val_true</TT> to refer to <TT>()</TT>, <TT>false</TT> and <TT>true</TT>.<BR>
625
<H3 CLASS="subsection"><A NAME="htoc221">18.3.5</A> Objects</H3>
626
Objects are represented as blocks
627
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
628
with tag <TT>Object_tag</TT>.
411
<TT>()</TT></TD><TD VALIGN=top ALIGN=left><TT>Val_int(0)</TT></TD></TR>
412
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>false</TT></TD><TD VALIGN=top ALIGN=left><TT>Val_int(0)</TT></TD></TR>
413
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>true</TT></TD><TD VALIGN=top ALIGN=left><TT>Val_int(1)</TT></TD></TR>
414
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>[]</TT></TD><TD VALIGN=top ALIGN=left><TT>Val_int(0)</TT></TD></TR>
415
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>h::t</TT></TD><TD VALIGN=top ALIGN=left>Block with size = 2 and tag = 0; first field
416
contains <TT>h</TT>, second field <TT>t</TT></TD></TR>
417
</TABLE></DIV><P>As a convenience, <TT>caml/mlvalues.h</TT> defines the macros <TT>Val_unit</TT>,
418
<TT>Val_false</TT> and <TT>Val_true</TT> to refer to <TT>()</TT>, <TT>false</TT> and <TT>true</TT>.</P><H3 CLASS="subsection"><A NAME="htoc221">18.3.5</A>��Objects</H3><P>Objects are represented as blocks with tag <TT>Object_tag</TT>. The first
631
419
field of the block refers to the object class and associated method
632
suite, in a format that cannot easily be exploited from C.
633
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
635
field contains a unique object ID, used for comparisons.
420
suite, in a format that cannot easily be exploited from C. The second
421
field contains a unique object ID, used for comparisons. The remaining
638
422
fields of the object contain the values of the instance variables of
639
the object. Instance variables are stored in the order in which they
640
appear in the class definition (taking inherited classes into
643
One may extract a public method from an object using the C function
423
the object. It is unsafe to access directly instance variables, as the
424
type system provides no guaranteee about the instance variables
425
contained by an object.
426
</P><P>One may extract a public method from an object using the C function
644
427
<TT>caml_get_public_method</TT> (declared in <TT><caml/mlvalues.h></TT>.)
645
428
Since public method tags are hashed in the same way as variant tags,
646
429
and methods are functions taking self as first argument, if you want
647
430
to do the method call <TT>foo#bar</TT> from the C side, you should call:
648
<PRE CLASS="verbatim">
649
callback(caml_get_public_method(foo, hash_variant("bar")), foo);
652
<H3 CLASS="subsection"><A NAME="htoc222">18.3.6</A> Variants</H3>
653
Like constructed terms, values of variant types are represented either
431
</P><PRE CLASS="verbatim"> callback(caml_get_public_method(foo, hash_variant("bar")), foo);
432
</PRE><H3 CLASS="subsection"><A NAME="htoc222">18.3.6</A>��Variants</H3><P>Like constructed terms, values of variant types are represented either
654
433
as integers (for variants without arguments), or as blocks (for
655
434
variants with an argument). Unlike constructed terms, variant
656
435
constructors are not numbered starting from 0, but identified by a
657
436
hash value (a Caml integer), as computed by the C function
658
437
<TT>hash_variant</TT> (declared in <TT><caml/mlvalues.h></TT>):
659
438
the hash value for a variant constructor named, say, <TT>VConstr</TT>
660
is <TT>hash_variant("VConstr")</TT>.<BR>
662
The variant value <TT>`VConstr</TT> is represented by
439
is <TT>hash_variant("VConstr")</TT>.</P><P>The variant value <TT>`VConstr</TT> is represented by
663
440
<TT>hash_variant("VConstr")</TT>. The variant value <TT>`VConstr(</TT><I>v</I><TT>)</TT> is
664
441
represented by a block of size 2 and tag 0, with field number 0
665
442
containing <TT>hash_variant("VConstr")</TT> and field number 1 containing
668
Unlike constructed values, variant values taking several arguments are
443
<I>v</I>.</P><P>Unlike constructed values, variant values taking several arguments are
669
444
not flattened. That is, <TT>`VConstr(</TT><I>v</I><TT>, </TT><I>v'</I><TT>)</TT> is
670
445
represented by a block of size 2, whose field number 1 contains
671
446
the representation of the pair <TT>(</TT><I>v</I><TT>, </TT><I>v'</I><TT>)</TT>, rather than a
672
block of size 3 containing <I>v</I> and <I>v'</I> in fields 1 and 2.<BR>
675
<H2 CLASS="section"><A NAME="htoc223">18.4</A> Operations on values</H2>
678
<H3 CLASS="subsection"><A NAME="htoc224">18.4.1</A> Kind tests</H3>
679
<UL CLASS="itemize"><LI CLASS="li-itemize">
447
block of size 3 containing <I>v</I> and <I>v'</I> in fields 1 and 2.</P><H2 CLASS="section"><A NAME="htoc223">18.4</A>��Operations on values</H2><H3 CLASS="subsection"><A NAME="htoc224">18.4.1</A>��Kind tests</H3><UL CLASS="itemize"><LI CLASS="li-itemize">
680
448
<TT>Is_long(</TT><I>v</I><TT>)</TT> is true if value <I>v</I> is an immediate integer,
682
<LI CLASS="li-itemize"><TT>Is_block(</TT><I>v</I><TT>)</TT> is true if value <I>v</I> is a pointer to a block,
450
</LI><LI CLASS="li-itemize"><TT>Is_block(</TT><I>v</I><TT>)</TT> is true if value <I>v</I> is a pointer to a block,
683
451
and false if it is an immediate integer.
686
<H3 CLASS="subsection"><A NAME="htoc225">18.4.2</A> Operations on integers</H3>
687
<UL CLASS="itemize"><LI CLASS="li-itemize">
452
</LI></UL><H3 CLASS="subsection"><A NAME="htoc225">18.4.2</A>��Operations on integers</H3><UL CLASS="itemize"><LI CLASS="li-itemize">
688
453
<TT>Val_long(</TT><I>l</I><TT>)</TT> returns the value encoding the <TT>long int</TT> <I>l</I>.
689
<LI CLASS="li-itemize"><TT>Long_val(</TT><I>v</I><TT>)</TT> returns the <TT>long int</TT> encoded in value <I>v</I>.
690
<LI CLASS="li-itemize"><TT>Val_int(</TT><I>i</I><TT>)</TT> returns the value encoding the <TT>int</TT> <I>i</I>.
691
<LI CLASS="li-itemize"><TT>Int_val(</TT><I>v</I><TT>)</TT> returns the <TT>int</TT> encoded in value <I>v</I>.
692
<LI CLASS="li-itemize"><TT>Val_bool(</TT><I>x</I><TT>)</TT> returns the Caml boolean representing the
454
</LI><LI CLASS="li-itemize"><TT>Long_val(</TT><I>v</I><TT>)</TT> returns the <TT>long int</TT> encoded in value <I>v</I>.
455
</LI><LI CLASS="li-itemize"><TT>Val_int(</TT><I>i</I><TT>)</TT> returns the value encoding the <TT>int</TT> <I>i</I>.
456
</LI><LI CLASS="li-itemize"><TT>Int_val(</TT><I>v</I><TT>)</TT> returns the <TT>int</TT> encoded in value <I>v</I>.
457
</LI><LI CLASS="li-itemize"><TT>Val_bool(</TT><I>x</I><TT>)</TT> returns the Caml boolean representing the
693
458
truth value of the C integer <I>x</I>.
694
<LI CLASS="li-itemize"><TT>Bool_val(</TT><I>v</I><TT>)</TT> returns 0 if <I>v</I> is the Caml boolean
459
</LI><LI CLASS="li-itemize"><TT>Bool_val(</TT><I>v</I><TT>)</TT> returns 0 if <I>v</I> is the Caml boolean
695
460
<TT>false</TT>, 1 if <I>v</I> is <TT>true</TT>.
696
<LI CLASS="li-itemize"><TT>Val_true</TT>, <TT>Val_false</TT> represent the Caml booleans <TT>true</TT> and <TT>false</TT>.
699
<H3 CLASS="subsection"><A NAME="htoc226">18.4.3</A> Accessing blocks</H3>
700
<UL CLASS="itemize"><LI CLASS="li-itemize">
461
</LI><LI CLASS="li-itemize"><TT>Val_true</TT>, <TT>Val_false</TT> represent the Caml booleans <TT>true</TT> and <TT>false</TT>.
462
</LI></UL><H3 CLASS="subsection"><A NAME="htoc226">18.4.3</A>��Accessing blocks</H3><UL CLASS="itemize"><LI CLASS="li-itemize">
701
463
<TT>Wosize_val(</TT><I>v</I><TT>)</TT> returns the size of the block <I>v</I>, in words,
702
464
excluding the header.
703
<LI CLASS="li-itemize"><TT>Tag_val(</TT><I>v</I><TT>)</TT> returns the tag of the block <I>v</I>.
704
<LI CLASS="li-itemize"><TT>Field(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT> returns the value contained in the
465
</LI><LI CLASS="li-itemize"><TT>Tag_val(</TT><I>v</I><TT>)</TT> returns the tag of the block <I>v</I>.
466
</LI><LI CLASS="li-itemize"><TT>Field(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT> returns the value contained in the
705
467
<I>n</I><FONT SIZE=2><SUP>th</SUP></FONT> field of the structured block <I>v</I>. Fields are numbered from 0 to
706
<TT>Wosize_val</TT>(<I>v</I>)−1.
707
<LI CLASS="li-itemize"><TT>Store_field(</TT><I>b</I><TT>, </TT><I>n</I><TT>, </TT><I>v</I><TT>)</TT> stores the value
468
<TT>Wosize_val</TT>(<I>v</I>)−1.
469
</LI><LI CLASS="li-itemize"><TT>Store_field(</TT><I>b</I><TT>, </TT><I>n</I><TT>, </TT><I>v</I><TT>)</TT> stores the value
708
470
<I>v</I> in the field number <I>n</I> of value <I>b</I>, which must be a
709
471
structured block.
710
<LI CLASS="li-itemize"><TT>Code_val(</TT><I>v</I><TT>)</TT> returns the code part of the closure <I>v</I>.
711
<LI CLASS="li-itemize"><TT>string_length(</TT><I>v</I><TT>)</TT> returns the length (number of characters)
472
</LI><LI CLASS="li-itemize"><TT>Code_val(</TT><I>v</I><TT>)</TT> returns the code part of the closure <I>v</I>.
473
</LI><LI CLASS="li-itemize"><TT>string_length(</TT><I>v</I><TT>)</TT> returns the length (number of characters)
712
474
of the string <I>v</I>.
713
<LI CLASS="li-itemize"><TT>Byte(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT> returns the <I>n</I><FONT SIZE=2><SUP>th</SUP></FONT> character of the string
475
</LI><LI CLASS="li-itemize"><TT>Byte(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT> returns the <I>n</I><FONT SIZE=2><SUP>th</SUP></FONT> character of the string
714
476
<I>v</I>, with type <TT>char</TT>. Characters are numbered from 0 to
715
<TT>string_length</TT>(<I>v</I>)−1.
716
<LI CLASS="li-itemize"><TT>Byte_u(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT> returns the <I>n</I><FONT SIZE=2><SUP>th</SUP></FONT> character of the string
477
<TT>string_length</TT>(<I>v</I>)−1.
478
</LI><LI CLASS="li-itemize"><TT>Byte_u(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT> returns the <I>n</I><FONT SIZE=2><SUP>th</SUP></FONT> character of the string
717
479
<I>v</I>, with type <TT>unsigned char</TT>. Characters are numbered from 0 to
718
<TT>string_length</TT>(<I>v</I>)−1.
719
<LI CLASS="li-itemize"><TT>String_val(</TT><I>v</I><TT>)</TT> returns a pointer to the first byte of the string
480
<TT>string_length</TT>(<I>v</I>)−1.
481
</LI><LI CLASS="li-itemize"><TT>String_val(</TT><I>v</I><TT>)</TT> returns a pointer to the first byte of the string
720
482
<I>v</I>, with type <TT>char *</TT>. This pointer is a valid C string: there is a
721
483
null character after the last character in the string. However, Caml
722
484
strings can contain embedded null characters, that will confuse
723
485
the usual C functions over strings.
724
<LI CLASS="li-itemize"><TT>Double_val(</TT><I>v</I><TT>)</TT> returns the floating-point number contained in
486
</LI><LI CLASS="li-itemize"><TT>Double_val(</TT><I>v</I><TT>)</TT> returns the floating-point number contained in
725
487
value <I>v</I>, with type <TT>double</TT>.
726
<LI CLASS="li-itemize"><TT>Double_field(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT> returns
488
</LI><LI CLASS="li-itemize"><TT>Double_field(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT> returns
727
489
the <I>n</I><FONT SIZE=2><SUP>th</SUP></FONT> element of the array of floating-point numbers <I>v</I> (a
728
490
block tagged <TT>Double_array_tag</TT>).
729
<LI CLASS="li-itemize"><TT>Store_double_field(</TT><I>v</I><TT>, </TT><I>n</I><TT>, </TT><I>d</I><TT>)</TT> stores the double precision floating-point number <I>d</I>
491
</LI><LI CLASS="li-itemize"><TT>Store_double_field(</TT><I>v</I><TT>, </TT><I>n</I><TT>, </TT><I>d</I><TT>)</TT> stores the double precision floating-point number <I>d</I>
730
492
in the <I>n</I><FONT SIZE=2><SUP>th</SUP></FONT> element of the array of floating-point numbers <I>v</I>.
731
<LI CLASS="li-itemize"><TT>Data_custom_val(</TT><I>v</I><TT>)</TT> returns a pointer to the data part
493
</LI><LI CLASS="li-itemize"><TT>Data_custom_val(</TT><I>v</I><TT>)</TT> returns a pointer to the data part
732
494
of the custom block <I>v</I>. This pointer has type <TT>void *</TT> and must
733
495
be cast to the type of the data contained in the custom block.
734
<LI CLASS="li-itemize"><TT>Int32_val(</TT><I>v</I><TT>)</TT> returns the 32-bit integer contained
496
</LI><LI CLASS="li-itemize"><TT>Int32_val(</TT><I>v</I><TT>)</TT> returns the 32-bit integer contained
735
497
in the <TT>int32</TT> <I>v</I>.
736
<LI CLASS="li-itemize"><TT>Int64_val(</TT><I>v</I><TT>)</TT> returns the 64-bit integer contained
498
</LI><LI CLASS="li-itemize"><TT>Int64_val(</TT><I>v</I><TT>)</TT> returns the 64-bit integer contained
737
499
in the <TT>int64</TT> <I>v</I>.
738
<LI CLASS="li-itemize"><TT>Nativeint_val(</TT><I>v</I><TT>)</TT> returns the long integer contained
500
</LI><LI CLASS="li-itemize"><TT>Nativeint_val(</TT><I>v</I><TT>)</TT> returns the long integer contained
739
501
in the <TT>nativeint</TT> <I>v</I>.
741
503
The expressions <TT>Field(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT>,
742
504
<TT>Byte(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT> and
743
505
<TT>Byte_u(</TT><I>v</I><TT>, </TT><I>n</I><TT>)</TT>
814
564
the fields of the block (initially containing garbage) must be initialized
815
565
with legal values (using the <TT>initialize</TT> function described below)
816
566
before the next allocation.
819
<H3 CLASS="subsection"><A NAME="htoc228">18.4.5</A> Raising exceptions</H3> <A NAME="s:c-exceptions"></A>
820
Two functions are provided to raise two standard exceptions:
821
<UL CLASS="itemize"><LI CLASS="li-itemize">
567
</LI></UL><H3 CLASS="subsection"><A NAME="htoc228">18.4.5</A>��Raising exceptions</H3><P> <A NAME="s:c-exceptions"></A></P><P>Two functions are provided to raise two standard exceptions:
568
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
822
569
<TT>caml_failwith(</TT><I>s</I><TT>)</TT>, where <I>s</I> is a null-terminated C string (with
823
570
type <CODE>char *</CODE>), raises exception <TT>Failure</TT> with argument <I>s</I>.
824
<LI CLASS="li-itemize"><TT>caml_invalid_argument(</TT><I>s</I><TT>)</TT>, where <I>s</I> is a null-terminated C
571
</LI><LI CLASS="li-itemize"><TT>caml_invalid_argument(</TT><I>s</I><TT>)</TT>, where <I>s</I> is a null-terminated C
825
572
string (with type <CODE>char *</CODE>), raises exception <TT>Invalid_argument</TT>
826
573
with argument <I>s</I>.
828
Raising arbitrary exceptions from C is more delicate: the
574
</LI></UL><P>Raising arbitrary exceptions from C is more delicate: the
829
575
exception identifier is dynamically allocated by the Caml program, and
830
576
therefore must be communicated to the C function using the
831
registration facility described below in section <A HREF="#s:register-exn">18.7.3</A>.
577
registration facility described below in section�<A HREF="#s:register-exn">18.7.3</A>.
832
578
Once the exception identifier is recovered in C, the following
833
579
functions actually raise the exception:
834
<UL CLASS="itemize"><LI CLASS="li-itemize">
580
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
835
581
<TT>caml_raise_constant(</TT><I>id</I><TT>)</TT> raises the exception <I>id</I> with
837
<LI CLASS="li-itemize"><TT>caml_raise_with_arg(</TT><I>id</I><TT>, </TT><I>v</I><TT>)</TT> raises the exception
583
</LI><LI CLASS="li-itemize"><TT>caml_raise_with_arg(</TT><I>id</I><TT>, </TT><I>v</I><TT>)</TT> raises the exception
838
584
<I>id</I> with the Caml value <I>v</I> as argument;
839
<LI CLASS="li-itemize"><TT>caml_raise_with_string(</TT><I>id</I><TT>, </TT><I>s</I><TT>)</TT>, where <I>s</I> is a
585
</LI><LI CLASS="li-itemize"><TT>caml_raise_with_string(</TT><I>id</I><TT>, </TT><I>s</I><TT>)</TT>, where <I>s</I> is a
840
586
null-terminated C string, raises the exception <I>id</I> with a copy of
841
587
the C string <I>s</I> as argument.
844
<H2 CLASS="section"><A NAME="htoc229">18.5</A> Living in harmony with the garbage collector</H2>
846
Unused blocks in the heap are automatically reclaimed by the garbage
588
</LI></UL><H2 CLASS="section"><A NAME="htoc229">18.5</A>��Living in harmony with the garbage collector</H2><P>Unused blocks in the heap are automatically reclaimed by the garbage
847
589
collector. This requires some cooperation from C code that
848
manipulates heap-allocated blocks.<BR>
851
<H3 CLASS="subsection"><A NAME="htoc230">18.5.1</A> Simple interface</H3>
852
All the macros described in this section are declared in the
853
<TT>memory.h</TT> header file.<BR>
855
<DIV CLASS="flushleft"><B>Rule 1</B> <EM>
590
manipulates heap-allocated blocks.</P><H3 CLASS="subsection"><A NAME="htoc230">18.5.1</A>��Simple interface</H3><P>All the macros described in this section are declared in the
591
<TT>memory.h</TT> header file.</P><DIV CLASS="theorem"><B>Rule�1</B>��<EM>
856
592
A function that has parameters or local variables of type <TT>value</TT> must
857
593
begin with a call to one of the <TT>CAMLparam</TT> macros and return with
858
<TT>CAMLreturn</TT> or <TT>CAMLreturn0</TT>.
861
There are six <TT>CAMLparam</TT> macros: <TT>CAMLparam0</TT> to <TT>CAMLparam5</TT>, which
594
<TT>CAMLreturn</TT>, <TT>CAMLreturn0</TT>, or <TT>CAMLreturnT</TT>.
595
</EM></DIV><P>There are six <TT>CAMLparam</TT> macros: <TT>CAMLparam0</TT> to <TT>CAMLparam5</TT>, which
862
596
take zero to five arguments respectively. If your function has fewer
863
597
than 5 parameters of type <TT>value</TT>, use the corresponding macros
864
598
with these parameters as arguments. If your function has more than 5
865
599
parameters of type <TT>value</TT>, use <TT>CAMLparam5</TT> with five of these
866
600
parameters, and use one or more calls to the <TT>CAMLxparam</TT> macros for
867
the remaining parameters (<TT>CAMLxparam1</TT> to <TT>CAMLxparam5</TT>).<BR>
869
The macros <TT>CAMLreturn</TT> and <TT>CAMLreturn0</TT> are used to replace the C
601
the remaining parameters (<TT>CAMLxparam1</TT> to <TT>CAMLxparam5</TT>).</P><P>The macros <TT>CAMLreturn</TT>, <TT>CAMLreturn0</TT>, and <TT>CAMLreturnT</TT> are used to
870
603
keyword <TT>return</TT>. Every occurence of <TT>return x</TT> must be replaced by
871
<TT>CAMLreturn (x)</TT>, every occurence of <TT>return</TT> without argument must be
604
<TT>CAMLreturn (x)</TT> if <TT>x</TT> has type <TT>value</TT>, or <TT>CAMLreturnT (t, x)</TT>
605
(where <TT>t</TT> is the type of <TT>x</TT>); every occurence of <TT>return</TT> without
872
607
replaced by <TT>CAMLreturn0</TT>. If your C function is a procedure (i.e. if
873
608
it returns void), you must insert <TT>CAMLreturn0</TT> at the end (to replace
874
C's implicit <TT>return</TT>).<BR>
877
<H5 CLASS="paragraph">Note:</H5> some C compilers give bogus warnings about unused
609
C's implicit <TT>return</TT>).</P><H5 CLASS="paragraph">Note:</H5><P> some C compilers give bogus warnings about unused
878
610
variables <TT>caml__dummy_xxx</TT> at each use of <TT>CAMLparam</TT> and
879
<TT>CAMLlocal</TT>. You should ignore them.<BR>
885
<PRE CLASS="verbatim">
886
void foo (value v1, value v2, value v3)
611
<TT>CAMLlocal</TT>. You should ignore them.</P><P><BR>
613
</P><PRE CLASS="verbatim">void foo (value v1, value v2, value v3)
888
615
CAMLparam3 (v1, v2, v3);
894
<H5 CLASS="paragraph">Note:</H5> if your function is a primitive with more than 5 arguments
619
</PRE><H5 CLASS="paragraph">Note:</H5><P> if your function is a primitive with more than 5 arguments
895
620
for use with the byte-code runtime, its arguments are not <TT>value</TT>s and
896
must not be declared (they have types <TT>value *</TT> and <TT>int</TT>).<BR>
898
<DIV CLASS="flushleft"><B>Rule 2</B> <EM>
621
must not be declared (they have types <TT>value *</TT> and <TT>int</TT>).</P><DIV CLASS="theorem"><B>Rule�2</B>��<EM>
899
622
Local variables of type <TT>value</TT> must be declared with one of the
900
623
<TT>CAMLlocal</TT> macros. Arrays of <TT>value</TT>s are declared with
902
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
903
These macros must be used at the beginning of the
624
<TT>CAMLlocalN</TT>. These macros must be used at the beginning of the
904
625
function, not in a nested block.
908
The macros <TT>CAMLlocal1</TT> to <TT>CAMLlocal5</TT> declare and initialize one to
626
</EM></DIV><P>The macros <TT>CAMLlocal1</TT> to <TT>CAMLlocal5</TT> declare and initialize one to
909
627
five local variables of type <TT>value</TT>. The variable names are given as
910
628
arguments to the macros. <TT>CAMLlocalN(</TT><I>x</I><TT>, </TT><I>n</I><TT>)</TT> declares
911
629
and initializes a local variable of type <TT>value [</TT><I>n</I><TT>]</TT>. You can
912
630
use several calls to these macros if you have more than 5 local
914
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
915
<STRIKE>You can also use them in nested C blocks within the
916
function.</STRIKE> (deleted)
921
<PRE CLASS="verbatim">
922
value bar (value v1, value v2, value v3)
631
variables.</P><P>Example:
632
</P><PRE CLASS="verbatim">value bar (value v1, value v2, value v3)
924
634
CAMLparam3 (v1, v2, v3);
925
635
CAMLlocal1 (result);
1230
</PRE>To compile and link this program, run:
1231
<PRE CLASS="verbatim">
1232
ocamlc -custom -o test <span style="background-color:yellow; color:red">unix.cma</span> test.ml curses_stubs.o -cclib -lcurses
1233
</PRE>(On some machines, you may need to put <TT>-cclib -ltermcap</TT> or
1234
<TT>-cclib -lcurses -cclib -ltermcap</TT> instead of <TT>-cclib -lcurses</TT>.)<BR>
1237
<H2 CLASS="section"><A NAME="htoc233">18.7</A> Advanced topic: callbacks from C to Caml</H2> <A NAME="s:callback"></A>
1239
So far, we have described how to call C functions from Caml. In this
889
</PRE><P>To compile and link this program, run:
890
</P><PRE CLASS="verbatim"> ocamlc -custom -o test unix.cma test.ml curses_stubs.o -cclib -lcurses
891
</PRE><P>(On some machines, you may need to put <TT>-cclib -ltermcap</TT> or
892
<TT>-cclib -lcurses -cclib -ltermcap</TT> instead of <TT>-cclib -lcurses</TT>.)</P><H2 CLASS="section"><A NAME="htoc233">18.7</A>��Advanced topic: callbacks from C to Caml</H2><P> <A NAME="s:callback"></A>
893
</P><P>So far, we have described how to call C functions from Caml. In this
1240
894
section, we show how C functions can call Caml functions, either as
1241
895
callbacks (Caml calls C which calls Caml), or because the main program
1242
is written in C.<BR>
1245
<H3 CLASS="subsection"><A NAME="htoc234">18.7.1</A> Applying Caml closures from C</H3> <A NAME="s:callbacks"></A>
1246
C functions can apply Caml functional values (closures) to Caml values.
896
is written in C.</P><H3 CLASS="subsection"><A NAME="htoc234">18.7.1</A>��Applying Caml closures from C</H3><P> <A NAME="s:callbacks"></A></P><P>C functions can apply Caml functional values (closures) to Caml values.
1247
897
The following functions are provided to perform the applications:
1248
<UL CLASS="itemize"><LI CLASS="li-itemize">
898
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
1249
899
<TT>caml_callback(</TT><I>f, a</I><TT>)</TT> applies the functional value <I>f</I> to
1250
the value <I>a</I> and return the value returned by <I>f</I>.
1251
<LI CLASS="li-itemize"><TT>caml_callback2(</TT><I>f, a, b</I><TT>)</TT> applies the functional value <I>f</I>
900
the value <I>a</I> and return the value returned by�<I>f</I>.
901
</LI><LI CLASS="li-itemize"><TT>caml_callback2(</TT><I>f, a, b</I><TT>)</TT> applies the functional value <I>f</I>
1252
902
(which is assumed to be a curried Caml function with two arguments) to
1253
903
<I>a</I> and <I>b</I>.
1254
<LI CLASS="li-itemize"><TT>caml_callback3(</TT><I>f, a, b, c</I><TT>)</TT> applies the functional value <I>f</I>
904
</LI><LI CLASS="li-itemize"><TT>caml_callback3(</TT><I>f, a, b, c</I><TT>)</TT> applies the functional value <I>f</I>
1255
905
(a curried Caml function with three arguments) to <I>a</I>, <I>b</I> and <I>c</I>.
1256
<LI CLASS="li-itemize"><TT>caml_callbackN(</TT><I>f, n, args</I><TT>)</TT> applies the functional value <I>f</I>
906
</LI><LI CLASS="li-itemize"><TT>caml_callbackN(</TT><I>f, n, args</I><TT>)</TT> applies the functional value <I>f</I>
1257
907
to the <I>n</I> arguments contained in the array of values <I>args</I>.
1259
909
If the function <I>f</I> does not return, but raises an exception that
1260
910
escapes the scope of the application, then this exception is
1261
911
propagated to the next enclosing Caml code, skipping over the C
1262
912
code. That is, if a Caml function <I>f</I> calls a C function <I>g</I> that
1263
913
calls back a Caml function <I>h</I> that raises a stray exception, then the
1264
914
execution of <I>g</I> is interrupted and the exception is propagated back
1267
If the C code wishes to catch exceptions escaping the Caml function,
915
into <I>f</I>.</P><P>If the C code wishes to catch exceptions escaping the Caml function,
1268
916
it can use the functions <TT>caml_callback_exn</TT>, <TT>caml_callback2_exn</TT>,
1269
917
<TT>caml_callback3_exn</TT>, <TT>caml_callbackN_exn</TT>. These functions take the same
1270
918
arguments as their non-<TT>_exn</TT> counterparts, but catch escaping
1271
919
exceptions and return them to the C code. The return value <I>v</I> of the
1272
920
<TT>caml_callback*_exn</TT> functions must be tested with the macro
1273
<TT>Is_exception_result(</TT><I>v</I><TT>)</TT>. If the macro returns “false”, no
921
<TT>Is_exception_result(</TT><I>v</I><TT>)</TT>. If the macro returns “false”, no
1274
922
exception occured, and <I>v</I> is the value returned by the Caml
1275
function. If <TT>Is_exception_result(</TT><I>v</I><TT>)</TT> returns “true”,
923
function. If <TT>Is_exception_result(</TT><I>v</I><TT>)</TT> returns “true”,
1276
924
an exception escaped, and its value (the exception descriptor) can be
1277
recovered using <TT>Extract_exception(</TT><I>v</I><TT>)</TT>.<BR>
1280
<H3 CLASS="subsection"><A NAME="htoc235">18.7.2</A> Registering Caml closures for use in C functions</H3>
1281
The main difficulty with the <TT>callback</TT> functions described above is
925
recovered using <TT>Extract_exception(</TT><I>v</I><TT>)</TT>.</P><H3 CLASS="subsection"><A NAME="htoc235">18.7.2</A>��Registering Caml closures for use in C functions</H3><P>The main difficulty with the <TT>callback</TT> functions described above is
1282
926
obtaining a closure to the Caml function to be called. For this
1283
927
purpose, Objective Caml provides a simple registration mechanism, by
1284
928
which Caml code can register Caml functions under some global name,
1285
929
and then C code can retrieve the corresponding closure by this global
1288
On the Caml side, registration is performed by evaluating
930
name.</P><P>On the Caml side, registration is performed by evaluating
1289
931
<TT>Callback.register</TT> <I>n v</I>. Here, <I>n</I> is the global name
1290
932
(an arbitrary string) and <I>v</I> the Caml value. For instance:
1291
<PRE CLASS="verbatim">
1292
let f x = print_string "f is applied to "; print_int n; print_newline()
933
</P><PRE CLASS="verbatim"> let f x = print_string "f is applied to "; print_int n; print_newline()
1293
934
let _ = Callback.register "test function" f
1295
On the C side, a pointer to the value registered under name <I>n</I> is
935
</PRE><P>On the C side, a pointer to the value registered under name <I>n</I> is
1296
936
obtained by calling <TT>caml_named_value(</TT><I>n</I><TT>)</TT>. The returned
1297
937
pointer must then be dereferenced to recover the actual Caml value.
1298
938
If no value is registered under the name <I>n</I>, the null pointer is
1299
939
returned. For example, here is a C wrapper that calls the Caml function <TT>f</TT>
1301
<PRE CLASS="verbatim">
1302
void call_caml_f(int arg)
941
</P><PRE CLASS="verbatim"> void call_caml_f(int arg)
1304
943
caml_callback(*caml_named_value("test function"), Val_int(arg));
1307
The pointer returned by <TT>caml_named_value</TT> is constant and can safely
945
</PRE><P>The pointer returned by <TT>caml_named_value</TT> is constant and can safely
1308
946
be cached in a C variable to avoid repeated name lookups. On the other
1309
947
hand, the value pointed to can change during garbage collection and
1310
948
must always be recomputed at the point of use. Here is a more
1311
949
efficient variant of <TT>call_caml_f</TT> above that calls <TT>caml_named_value</TT>
1313
<PRE CLASS="verbatim">
1314
void call_caml_f(int arg)
951
</P><PRE CLASS="verbatim"> void call_caml_f(int arg)
1316
953
static value * closure_f = NULL;
1317
954
if (closure_f == NULL) {
1321
958
caml_callback(*closure_f, Val_int(arg));
1325
<H3 CLASS="subsection"><A NAME="htoc236">18.7.3</A> Registering Caml exceptions for use in C functions</H3> <A NAME="s:register-exn"></A>
1326
The registration mechanism described above can also be used to
960
</PRE><H3 CLASS="subsection"><A NAME="htoc236">18.7.3</A>��Registering Caml exceptions for use in C functions</H3><P> <A NAME="s:register-exn"></A></P><P>The registration mechanism described above can also be used to
1327
961
communicate exception identifiers from Caml to C. The Caml code
1328
962
registers the exception by evaluating
1329
963
<TT>Callback.register_exception</TT> <I>n exn</I>, where <I>n</I> is an
1330
964
arbitrary name and <I>exn</I> is an exception value of the
1331
965
exception to register. For example:
1332
<PRE CLASS="verbatim">
1333
exception Error of string
966
</P><PRE CLASS="verbatim"> exception Error of string
1334
967
let _ = Callback.register_exception "test exception" (Error "any string")
1335
</PRE>The C code can then recover the exception identifier using
968
</PRE><P>The C code can then recover the exception identifier using
1336
969
<TT>caml_named_value</TT> and pass it as first argument to the functions
1337
970
<TT>raise_constant</TT>, <TT>raise_with_arg</TT>, and <TT>raise_with_string</TT> (described
1338
in section <A HREF="#s:c-exceptions">18.4.5</A>) to actually raise the exception. For
971
in section�<A HREF="#s:c-exceptions">18.4.5</A>) to actually raise the exception. For
1339
972
example, here is a C function that raises the <TT>Error</TT> exception with
1340
973
the given argument:
1341
<PRE CLASS="verbatim">
1342
void raise_error(char * msg)
974
</P><PRE CLASS="verbatim"> void raise_error(char * msg)
1344
976
caml_raise_with_string(*caml_named_value("test exception"), msg);
1348
<H3 CLASS="subsection"><A NAME="htoc237">18.7.4</A> Main program in C</H3> <A NAME="s:main-c"></A>
1349
In normal operation, a mixed Caml/C program starts by executing the
978
</PRE><H3 CLASS="subsection"><A NAME="htoc237">18.7.4</A>��Main program in C</H3><P> <A NAME="s:main-c"></A></P><P>In normal operation, a mixed Caml/C program starts by executing the
1350
979
Caml initialization code, which then may proceed to call C
1351
980
functions. We say that the main program is the Caml code. In some
1352
981
applications, it is desirable that the C code plays the role of the
1353
982
main program, calling Caml functions when needed. This can be achieved as
1355
<UL CLASS="itemize"><LI CLASS="li-itemize">
984
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
1356
985
The C part of the program must provide a <TT>main</TT> function,
1357
986
which will override the default <TT>main</TT> function provided by the Caml
1358
987
runtime system. Execution will start in the user-defined <TT>main</TT> function
1359
just like for a regular C program.<BR>
1361
<LI CLASS="li-itemize">At some point, the C code must call <TT>caml_main(argv)</TT> to
988
just like for a regular C program.</LI><LI CLASS="li-itemize">At some point, the C code must call <TT>caml_main(argv)</TT> to
1362
989
initialize the Caml code. The <TT>argv</TT> argument is a C array of strings
1363
990
(type <TT>char **</TT>), terminated with a <TT>NULL</TT> pointer,
1364
991
which represents the command-line arguments, as
1365
992
passed as second argument to <TT>main</TT>. The Caml array <TT>Sys.argv</TT> will
1366
993
be initialized from this parameter. For the bytecode compiler,
1367
994
<TT>argv[0]</TT> and <TT>argv[1]</TT> are also consulted to find the file containing
1370
<LI CLASS="li-itemize">The call to <TT>caml_main</TT> initializes the Caml runtime system,
995
the bytecode.</LI><LI CLASS="li-itemize">The call to <TT>caml_main</TT> initializes the Caml runtime system,
1371
996
loads the bytecode (in the case of the bytecode compiler), and
1372
997
executes the initialization code of the Caml program. Typically, this
1373
998
initialization code registers callback functions using <TT>Callback.register</TT>.
1374
999
Once the Caml initialization code is complete, control returns to the
1375
C code that called <TT>caml_main</TT>.<BR>
1377
<LI CLASS="li-itemize">The C code can then invoke Caml functions using the callback
1378
mechanism (see section <A HREF="#s:callbacks">18.7.1</A>).
1381
<H3 CLASS="subsection"><A NAME="htoc238">18.7.5</A> Embedding the Caml code in the C code</H3> <A NAME="s:embedded-code"></A>
1382
The bytecode compiler in custom runtime mode (<TT>ocamlc -custom</TT>)
1000
C code that called <TT>caml_main</TT>.</LI><LI CLASS="li-itemize">The C code can then invoke Caml functions using the callback
1001
mechanism (see section�<A HREF="#s:callbacks">18.7.1</A>).
1002
</LI></UL><H3 CLASS="subsection"><A NAME="htoc238">18.7.5</A>��Embedding the Caml code in the C code</H3><P> <A NAME="s:embedded-code"></A></P><P>The bytecode compiler in custom runtime mode (<TT>ocamlc -custom</TT>)
1383
1003
normally appends the bytecode to the executable file containing the
1384
1004
custom runtime. This has two consequences. First, the final linking
1385
1005
step must be performed by <TT>ocamlc</TT>. Second, the Caml runtime library
1386
1006
must be able to find the name of the executable file from the
1387
1007
command-line arguments. When using <TT>caml_main(argv)</TT> as in
1388
section <A HREF="#s:main-c">18.7.4</A>, this means that <TT>argv[0]</TT> or <TT>argv[1]</TT> must
1389
contain the executable file name.<BR>
1391
An alternative is to embed the bytecode in the C code. The
1008
section�<A HREF="#s:main-c">18.7.4</A>, this means that <TT>argv[0]</TT> or <TT>argv[1]</TT> must
1009
contain the executable file name.</P><P>An alternative is to embed the bytecode in the C code. The
1392
1010
<TT>-output-obj</TT> option to <TT>ocamlc</TT> is provided for this purpose.
1393
1011
It causes the <TT>ocamlc</TT> compiler to output a C object file (<TT>.o</TT> file)
1394
1012
containing the bytecode for the Caml part of the program, as well as a
1395
1013
<TT>caml_startup</TT> function. The C object file produced by <TT>ocamlc -output-obj</TT> can then be linked with C code using the standard C
1396
compiler, or stored in a C library.<BR>
1398
The <TT>caml_startup</TT> function must be called from the main C program in
1014
compiler, or stored in a C library.</P><P>The <TT>caml_startup</TT> function must be called from the main C program in
1399
1015
order to initialize the Caml runtime and execute the Caml
1400
1016
initialization code. Just like <TT>caml_main</TT>, it takes one <TT>argv</TT>
1401
1017
parameter containing the command-line parameters. Unlike <TT>caml_main</TT>,
1402
1018
this <TT>argv</TT> parameter is used only to initialize <TT>Sys.argv</TT>, but not
1403
for finding the name of the executable file.<BR>
1405
The native-code compiler <TT>ocamlopt</TT> also supports the <TT>-output-obj</TT>
1019
for finding the name of the executable file.</P><P>The native-code compiler <TT>ocamlopt</TT> also supports the <TT>-output-obj</TT>
1406
1020
option, causing it to output a C object file containing the native
1407
1021
code for all Caml modules on the command-line, as well as the Caml
1408
1022
startup code. Initialization is performed by calling <TT>caml_startup</TT> as
1409
in the case of the bytecode compiler.<BR>
1411
For the final linking phase, in addition to the object file produced
1023
in the case of the bytecode compiler.</P><P>For the final linking phase, in addition to the object file produced
1412
1024
by <TT>-output-obj</TT>, you will have to provide the Objective Caml runtime
1413
1025
library (<TT>libcamlrun.a</TT> for bytecode, <TT>libasmrun.a</TT> for native-code),
1414
1026
as well as all C libraries that are required by the Caml libraries
1415
1027
used. For instance, assume the Caml part of your program uses the
1416
1028
Unix library. With <TT>ocamlc</TT>, you should do:
1418
1030
ocamlc -output-obj -o camlcode.o unix.cma <I>other</I> .cmo <I>and</I> .cma <I>files</I>
1419
1031
cc -o myprog <I>C objects and libraries</I> \
1420
1032
camlcode.o -L/usr/local/lib/ocaml -lunix -lcamlrun
1422
1034
With <TT>ocamlopt</TT>, you should do:
1424
1036
ocamlopt -output-obj -o camlcode.o unix.cmxa <I>other</I> .cmx <I>and</I> .cmxa <I>files</I>
1425
1037
cc -o myprog <I>C objects and libraries</I> \
1426
1038
camlcode.o -L/usr/local/lib/ocaml -lunix -lasmrun
1429
<H5 CLASS="paragraph">Warning:</H5> On some ports, special options are required on the final
1039
</PRE><H5 CLASS="paragraph">Warning:</H5><P> On some ports, special options are required on the final
1430
1040
linking phase that links together the object file produced by the
1431
1041
<TT>-output-obj</TT> option and the remainder of the program. Those options
1432
1042
are shown in the configuration file <TT>config/Makefile</TT> generated during
1618
1174
returns the size in bytes of the data part of the custom block.
1619
1175
This size must be identical to the <TT>wsize_32</TT> result of
1620
1176
the <TT>serialize</TT> operation if the architecture is 32 bits, or
1621
<TT>wsize_64</TT> if the architecture is 64 bits.<BR>
1623
The <TT>deserialize</TT> field can be set to <TT>custom_deserialize_default</TT>
1177
<TT>wsize_64</TT> if the architecture is 64 bits.<P>The <TT>deserialize</TT> field can be set to <TT>custom_deserialize_default</TT>
1624
1178
to indicate that deserialization is not supported. In this case,
1625
1179
do not register the <TT>struct custom_operations</TT> with the deserializer
1626
1180
using <TT>register_custom_operations</TT> (see below).
1629
<H3 CLASS="subsection"><A NAME="htoc242">18.9.2</A> Allocating custom blocks</H3>
1630
Custom blocks must be allocated via the <TT>caml_alloc_custom</TT> function.
1181
</P></LI></UL><P>Note: the <TT>finalize</TT>, <TT>compare</TT>, <TT>hash</TT>, <TT>serialize</TT> and <TT>deserialize</TT>
1182
functions attached to custom block descriptors must never trigger a
1183
garbage collection. Within these functions, do not call any of the
1184
Caml allocation functions, and do not perform a callback into Caml
1185
code. Do not use <TT>CAMLparam</TT> to register the parameters to these
1186
functions, and do not use <TT>CAMLreturn</TT> to return the result.</P><H3 CLASS="subsection"><A NAME="htoc242">18.9.2</A>��Allocating custom blocks</H3><P>Custom blocks must be allocated via the <TT>caml_alloc_custom</TT> function.
1631
1187
<TT>caml_alloc_custom(</TT><I>ops</I><TT>, </TT><I>size</I><TT>, </TT><I>used</I><TT>, </TT><I>max</I><TT>)</TT>
1632
1188
returns a fresh custom block, with room for <I>size</I> bytes of user
1633
1189
data, and whose associated operations are given by <I>ops</I> (a
1634
1190
pointer to a <TT>struct custom_operations</TT>, usually statically allocated
1635
as a C global variable). <BR>
1637
The two parameters <I>used</I> and <I>max</I> are used to control the
1191
as a C global variable). </P><P>The two parameters <I>used</I> and <I>max</I> are used to control the
1638
1192
speed of garbage collection when the finalized object contains
1639
1193
pointers to out-of-heap resources. Generally speaking, the
1640
1194
Caml incremental major collector adjusts its speed relative to the
1641
1195
allocation rate of the program. The faster the program allocates, the
1642
1196
harder the GC works in order to reclaim quickly unreachable blocks
1643
and avoid having large amount of “floating garbage” (unreferenced
1644
objects that the GC has not yet collected).<BR>
1646
Normally, the allocation rate is measured by counting the in-heap size
1197
and avoid having large amount of “floating garbage” (unreferenced
1198
objects that the GC has not yet collected).</P><P>Normally, the allocation rate is measured by counting the in-heap size
1647
1199
of allocated blocks. However, it often happens that finalized
1648
1200
objects contain pointers to out-of-heap memory blocks and other resources
1649
1201
(such as file descriptors, X Windows bitmaps, etc.). For those
1650
1202
blocks, the in-heap size of blocks is not a good measure of the
1651
quantity of resources allocated by the program.<BR>
1653
The two arguments <I>used</I> and <I>max</I> give the GC an idea of how
1203
quantity of resources allocated by the program.</P><P>The two arguments <I>used</I> and <I>max</I> give the GC an idea of how
1654
1204
much out-of-heap resources are consumed by the finalized block
1655
1205
being allocated: you give the amount of resources allocated to this
1656
1206
object as parameter <I>used</I>, and the maximum amount that you want
1657
1207
to see in floating garbage as parameter <I>max</I>. The units are
1658
arbitrary: the GC cares only about the ratio <I>used</I> / <I>max</I>.<BR>
1660
For instance, if you are allocating a finalized block holding an X
1208
arbitrary: the GC cares only about the ratio <I>used</I> / <I>max</I>.</P><P>For instance, if you are allocating a finalized block holding an X
1661
1209
Windows bitmap of <I>w</I> by <I>h</I> pixels, and you'd rather not
1662
1210
have more than 1 mega-pixels of unreclaimed bitmaps, specify
1663
<I>used</I> = <I>w</I> * <I>h</I> and <I>max</I> = 1000000.<BR>
1665
Another way to describe the effect of the <I>used</I> and <I>max</I>
1211
<I>used</I> = <I>w</I> * <I>h</I> and <I>max</I> = 1000000.</P><P>Another way to describe the effect of the <I>used</I> and <I>max</I>
1666
1212
parameters is in terms of full GC cycles. If you allocate many custom
1667
1213
blocks with <I>used</I> / <I>max</I> = 1 / <I>N</I>, the GC will then do one
1668
1214
full cycle (examining every object in the heap and calling
1669
1215
finalization functions on those that are unreachable) every <I>N</I>
1670
1216
allocations. For instance, if <I>used</I> = 1 and <I>max</I> = 1000,
1671
1217
the GC will do one full cycle at least every 1000 allocations of
1674
If your finalized blocks contain no pointers to out-of-heap resources,
1218
custom blocks.</P><P>If your finalized blocks contain no pointers to out-of-heap resources,
1675
1219
or if the previous discussion made little sense to you, just take
1676
1220
<I>used</I> = 0 and <I>max</I> = 1. But if you later find that the
1677
finalization functions are not called “often enough”, consider
1678
increasing the <I>used</I> / <I>max</I> ratio.<BR>
1681
<H3 CLASS="subsection"><A NAME="htoc243">18.9.3</A> Accessing custom blocks</H3>
1682
The data part of a custom block <I>v</I> can be
1221
finalization functions are not called “often enough”, consider
1222
increasing the <I>used</I> / <I>max</I> ratio.</P><H3 CLASS="subsection"><A NAME="htoc243">18.9.3</A>��Accessing custom blocks</H3><P>The data part of a custom block <I>v</I> can be
1683
1223
accessed via the pointer <TT>Data_custom_val(</TT><I>v</I><TT>)</TT>. This pointer
1684
1224
has type <TT>void *</TT> and should be cast to the actual type of the data
1685
stored in the custom block.<BR>
1687
The contents of custom blocks are not scanned by the garbage
1225
stored in the custom block.</P><P>The contents of custom blocks are not scanned by the garbage
1688
1226
collector, and must therefore not contain any pointer inside the Caml
1689
1227
heap. In other terms, never store a Caml <TT>value</TT> in a custom block,
1690
1228
and do not use <TT>Field</TT>, <TT>Store_field</TT> nor <TT>modify</TT> to access the data
1691
1229
part of a custom block. Conversely, any C data structure (not
1692
containing heap pointers) can be stored in a custom block.<BR>
1695
<H3 CLASS="subsection"><A NAME="htoc244">18.9.4</A> Writing custom serialization and deserialization functions</H3>
1696
The following functions, defined in <TT><caml/intext.h></TT>, are provided to
1230
containing heap pointers) can be stored in a custom block.</P><H3 CLASS="subsection"><A NAME="htoc244">18.9.4</A>��Writing custom serialization and deserialization functions</H3><P>The following functions, defined in <TT><caml/intext.h></TT>, are provided to
1697
1231
write and read back the contents of custom blocks in a portable way.
1698
1232
Those functions handle endianness conversions when e.g. data is
1699
written on a little-endian machine and read back on a big-endian machine.<BR>
1701
<DIV CLASS="center"><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%">
1702
<TR><TD ALIGN=center NOWRAP><B>Function</B></TD>
1703
<TD ALIGN=center NOWRAP><B>Action</B></TD>
1233
written on a little-endian machine and read back on a big-endian machine.</P><DIV CLASS="center"><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%"><TR><TD ALIGN=center NOWRAP><B>Function</B></TD><TD ALIGN=center NOWRAP><B>Action</B></TD></TR>
1705
1234
<TR><TD VALIGN=top ALIGN=left NOWRAP>
1706
<TT>caml_serialize_int_1</TT></TD>
1707
<TD VALIGN=top ALIGN=left>Write a 1-byte integer</TD>
1709
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_int_2</TT></TD>
1710
<TD VALIGN=top ALIGN=left>Write a 2-byte integer</TD>
1712
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_int_4</TT></TD>
1713
<TD VALIGN=top ALIGN=left>Write a 4-byte integer</TD>
1715
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_int_8</TT></TD>
1716
<TD VALIGN=top ALIGN=left>Write a 8-byte integer</TD>
1718
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_float_4</TT></TD>
1719
<TD VALIGN=top ALIGN=left>Write a 4-byte float</TD>
1721
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_float_8</TT></TD>
1722
<TD VALIGN=top ALIGN=left>Write a 8-byte float</TD>
1724
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_block_1</TT></TD>
1725
<TD VALIGN=top ALIGN=left>Write an array of 1-byte quantities</TD>
1727
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_block_2</TT></TD>
1728
<TD VALIGN=top ALIGN=left>Write an array of 2-byte quantities</TD>
1730
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_block_4</TT></TD>
1731
<TD VALIGN=top ALIGN=left>Write an array of 4-byte quantities</TD>
1733
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_block_8</TT></TD>
1734
<TD VALIGN=top ALIGN=left>Write an array of 8-byte quantities</TD>
1736
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_uint_1</TT></TD>
1737
<TD VALIGN=top ALIGN=left>Read an unsigned 1-byte integer</TD>
1739
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_sint_1</TT></TD>
1740
<TD VALIGN=top ALIGN=left>Read a signed 1-byte integer</TD>
1742
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_uint_2</TT></TD>
1743
<TD VALIGN=top ALIGN=left>Read an unsigned 2-byte integer</TD>
1745
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_sint_2</TT></TD>
1746
<TD VALIGN=top ALIGN=left>Read a signed 2-byte integer</TD>
1748
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_uint_4</TT></TD>
1749
<TD VALIGN=top ALIGN=left>Read an unsigned 4-byte integer</TD>
1751
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_sint_4</TT></TD>
1752
<TD VALIGN=top ALIGN=left>Read a signed 4-byte integer</TD>
1754
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_uint_8</TT></TD>
1755
<TD VALIGN=top ALIGN=left>Read an unsigned 8-byte integer</TD>
1757
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_sint_8</TT></TD>
1758
<TD VALIGN=top ALIGN=left>Read a signed 8-byte integer</TD>
1760
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_float_4</TT></TD>
1761
<TD VALIGN=top ALIGN=left>Read a 4-byte float</TD>
1763
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_float_8</TT></TD>
1764
<TD VALIGN=top ALIGN=left>Read an 8-byte float</TD>
1766
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_block_1</TT></TD>
1767
<TD VALIGN=top ALIGN=left>Read an array of 1-byte quantities</TD>
1769
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_block_2</TT></TD>
1770
<TD VALIGN=top ALIGN=left>Read an array of 2-byte quantities</TD>
1772
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_block_4</TT></TD>
1773
<TD VALIGN=top ALIGN=left>Read an array of 4-byte quantities</TD>
1775
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_block_8</TT></TD>
1776
<TD VALIGN=top ALIGN=left>Read an array of 8-byte quantities</TD>
1778
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_error</TT></TD>
1779
<TD VALIGN=top ALIGN=left>Signal an error during deserialization;
1235
<TT>caml_serialize_int_1</TT></TD><TD VALIGN=top ALIGN=left>Write a 1-byte integer</TD></TR>
1236
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_int_2</TT></TD><TD VALIGN=top ALIGN=left>Write a 2-byte integer</TD></TR>
1237
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_int_4</TT></TD><TD VALIGN=top ALIGN=left>Write a 4-byte integer</TD></TR>
1238
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_int_8</TT></TD><TD VALIGN=top ALIGN=left>Write a 8-byte integer</TD></TR>
1239
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_float_4</TT></TD><TD VALIGN=top ALIGN=left>Write a 4-byte float</TD></TR>
1240
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_float_8</TT></TD><TD VALIGN=top ALIGN=left>Write a 8-byte float</TD></TR>
1241
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_block_1</TT></TD><TD VALIGN=top ALIGN=left>Write an array of 1-byte quantities</TD></TR>
1242
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_block_2</TT></TD><TD VALIGN=top ALIGN=left>Write an array of 2-byte quantities</TD></TR>
1243
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_block_4</TT></TD><TD VALIGN=top ALIGN=left>Write an array of 4-byte quantities</TD></TR>
1244
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_serialize_block_8</TT></TD><TD VALIGN=top ALIGN=left>Write an array of 8-byte quantities</TD></TR>
1245
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_uint_1</TT></TD><TD VALIGN=top ALIGN=left>Read an unsigned 1-byte integer</TD></TR>
1246
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_sint_1</TT></TD><TD VALIGN=top ALIGN=left>Read a signed 1-byte integer</TD></TR>
1247
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_uint_2</TT></TD><TD VALIGN=top ALIGN=left>Read an unsigned 2-byte integer</TD></TR>
1248
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_sint_2</TT></TD><TD VALIGN=top ALIGN=left>Read a signed 2-byte integer</TD></TR>
1249
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_uint_4</TT></TD><TD VALIGN=top ALIGN=left>Read an unsigned 4-byte integer</TD></TR>
1250
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_sint_4</TT></TD><TD VALIGN=top ALIGN=left>Read a signed 4-byte integer</TD></TR>
1251
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_uint_8</TT></TD><TD VALIGN=top ALIGN=left>Read an unsigned 8-byte integer</TD></TR>
1252
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_sint_8</TT></TD><TD VALIGN=top ALIGN=left>Read a signed 8-byte integer</TD></TR>
1253
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_float_4</TT></TD><TD VALIGN=top ALIGN=left>Read a 4-byte float</TD></TR>
1254
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_float_8</TT></TD><TD VALIGN=top ALIGN=left>Read an 8-byte float</TD></TR>
1255
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_block_1</TT></TD><TD VALIGN=top ALIGN=left>Read an array of 1-byte quantities</TD></TR>
1256
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_block_2</TT></TD><TD VALIGN=top ALIGN=left>Read an array of 2-byte quantities</TD></TR>
1257
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_block_4</TT></TD><TD VALIGN=top ALIGN=left>Read an array of 4-byte quantities</TD></TR>
1258
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_block_8</TT></TD><TD VALIGN=top ALIGN=left>Read an array of 8-byte quantities</TD></TR>
1259
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>caml_deserialize_error</TT></TD><TD VALIGN=top ALIGN=left>Signal an error during deserialization;
1780
1260
<TT>input_value</TT> or <TT>Marshal.from_...</TT> raise a <TT>Failure</TT> exception after
1781
cleaning up their internal data structures</TD>
1782
</TR></TABLE></DIV><BR>
1784
Serialization functions are attached to the custom blocks to which
1261
cleaning up their internal data structures</TD></TR>
1262
</TABLE></DIV><P>Serialization functions are attached to the custom blocks to which
1785
1263
they apply. Obviously, deserialization functions cannot be attached
1786
1264
this way, since the custom block does not exist yet when
1787
1265
deserialization begins! Thus, the <TT>struct custom_operations</TT> that
1822
1290
word is reserved for storing the custom operations; the other
1823
1291
<I>n</I>-1 words are available for your data. The two parameters
1824
1292
<I>used</I> and <I>max</I> are used to control the speed of garbage
1825
collection, as described for <TT>caml_alloc_custom</TT>.<BR>
1828
<H2 CLASS="section"><A NAME="htoc247">18.10</A> Building mixed C/Caml libraries: <TT>ocamlmklib</TT></H2> <A NAME="s-ocamlmklib"></A>
1829
The <TT>ocamlmklib</TT> command facilitates the construction of libraries
1293
collection, as described for <TT>caml_alloc_custom</TT>.</P><H2 CLASS="section"><A NAME="htoc247">18.10</A>��Building mixed C/Caml libraries: <TT>ocamlmklib</TT></H2><P> <A NAME="s-ocamlmklib"></A></P><P>The <TT>ocamlmklib</TT> command facilitates the construction of libraries
1830
1294
containing both Caml code and C code, and usable both in static
1831
linking and dynamic linking modes.<BR>
1834
<FONT COLOR=purple>Windows:</FONT>
1835
<BLOCKQUOTE CLASS="quote"> This command is available only under Cygwin, but not for the
1295
linking and dynamic linking modes.</P><BLOCKQUOTE CLASS="quote"><FONT COLOR=purple>Windows:</FONT>�� This command is available only under Cygwin, but not for the
1836
1296
native Win32 port.
1838
The <TT>ocamlmklib</TT> command takes three kinds of arguments:
1839
<UL CLASS="itemize"><LI CLASS="li-itemize">
1297
</BLOCKQUOTE><P>The <TT>ocamlmklib</TT> command takes three kinds of arguments:
1298
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
1840
1299
Caml source files and object files (<TT>.cmo</TT>, <TT>.cmx</TT>, <TT>.ml</TT>)
1841
1300
comprising the Caml part of the library;
1842
<LI CLASS="li-itemize">C object files (<TT>.o</TT>, <TT>.a</TT>) comprising the C part of the
1301
</LI><LI CLASS="li-itemize">C object files (<TT>.o</TT>, <TT>.a</TT>) comprising the C part of the
1844
<LI CLASS="li-itemize">Support libraries for the C part (<TT>-l</TT><I>lib</I>).
1303
</LI><LI CLASS="li-itemize">Support libraries for the C part (<TT>-l</TT><I>lib</I>).
1846
1305
It generates the following outputs:
1847
<UL CLASS="itemize"><LI CLASS="li-itemize">
1306
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
1848
1307
A Caml bytecode library <TT>.cma</TT> incorporating the <TT>.cmo</TT> and
1849
1308
<TT>.ml</TT> Caml files given as arguments, and automatically referencing the
1850
1309
C library generated with the C object files.
1851
<LI CLASS="li-itemize">A Caml native-code library <TT>.cmxa</TT> incorporating the <TT>.cmx</TT> and
1310
</LI><LI CLASS="li-itemize">A Caml native-code library <TT>.cmxa</TT> incorporating the <TT>.cmx</TT> and
1852
1311
<TT>.ml</TT> Caml files given as arguments, and automatically referencing the
1853
1312
C library generated with the C object files.
1854
<LI CLASS="li-itemize">If dynamic linking is supported on the target platform, a
1313
</LI><LI CLASS="li-itemize">If dynamic linking is supported on the target platform, a
1855
1314
<TT>.so</TT> shared library built from the C object files given as arguments,
1856
1315
and automatically referencing the support libraries.
1857
<LI CLASS="li-itemize">A C static library <TT>.a</TT> built from the C object files.
1316
</LI><LI CLASS="li-itemize">A C static library <TT>.a</TT> built from the C object files.
1859
1318
In addition, the following options are recognized:
1860
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
1861
<B><TT>-cclib</TT>, <TT>-ccopt</TT>, <TT>-I</TT>, <TT>-linkall</TT></B><DD CLASS="dd-description">
1319
</P><DL CLASS="description"><DT CLASS="dt-description">
1320
<B><TT>-cclib</TT>, <TT>-ccopt</TT>, <TT>-I</TT>, <TT>-linkall</TT></B></DT><DD CLASS="dd-description">
1862
1321
These options are passed as is to <TT>ocamlc</TT> or <TT>ocamlopt</TT>.
1863
1322
See the documentation of these commands.
1864
<DT CLASS="dt-description"><B><TT>-pthread</TT>, <TT>-rpath</TT>, <TT>-R</TT>, <TT>-Wl,-rpath</TT>, <TT>-Wl,-R</TT></B><DD CLASS="dd-description">
1323
</DD><DT CLASS="dt-description"><B><TT>-pthread</TT>, <TT>-rpath</TT>, <TT>-R</TT>, <TT>-Wl,-rpath</TT>, <TT>-Wl,-R</TT></B></DT><DD CLASS="dd-description">
1865
1324
These options are passed as is to the C compiler. Refer to the
1866
1325
documentation of the C compiler.
1867
<DT CLASS="dt-description"><TT><B>-custom</B></TT><DD CLASS="dd-description"> Force the construction of a statically linked library
1326
</DD><DT CLASS="dt-description"><TT><B>-custom</B></TT></DT><DD CLASS="dd-description"> Force the construction of a statically linked library
1868
1327
only, even if dynamic linking is supported.
1869
<DT CLASS="dt-description"><TT><B>-failsafe</B></TT><DD CLASS="dd-description"> Fall back to building a statically linked library
1328
</DD><DT CLASS="dt-description"><TT><B>-failsafe</B></TT></DT><DD CLASS="dd-description"> Fall back to building a statically linked library
1870
1329
if a problem occurs while building the shared library (e.g. some of
1871
1330
the support libraries are not available as shared libraries).
1872
<DT CLASS="dt-description"><B><TT>-L</TT><I>dir</I></B><DD CLASS="dd-description"> Add <I>dir</I> to the search path for support
1331
</DD><DT CLASS="dt-description"><B><TT>-L</TT><I>dir</I></B></DT><DD CLASS="dd-description"> Add <I>dir</I> to the search path for support
1873
1332
libraries (<TT>-l</TT><I>lib</I>).
1874
<DT CLASS="dt-description"><B><TT>-ocamlc</TT> <I>cmd</I></B><DD CLASS="dd-description"> Use <I>cmd</I> instead of <TT>ocamlc</TT> to call
1333
</DD><DT CLASS="dt-description"><B><TT>-ocamlc</TT> <I>cmd</I></B></DT><DD CLASS="dd-description"> Use <I>cmd</I> instead of <TT>ocamlc</TT> to call
1875
1334
the bytecode compiler.
1876
<DT CLASS="dt-description"><B><TT>-ocamlopt</TT> <I>cmd</I></B><DD CLASS="dd-description"> Use <I>cmd</I> instead of <TT>ocamlopt</TT> to call
1335
</DD><DT CLASS="dt-description"><B><TT>-ocamlopt</TT> <I>cmd</I></B></DT><DD CLASS="dd-description"> Use <I>cmd</I> instead of <TT>ocamlopt</TT> to call
1877
1336
the native-code compiler.
1878
<DT CLASS="dt-description"><B><TT>-o</TT> <I>output</I></B><DD CLASS="dd-description"> Set the name of the generated Caml library.
1337
</DD><DT CLASS="dt-description"><B><TT>-o</TT> <I>output</I></B></DT><DD CLASS="dd-description"> Set the name of the generated Caml library.
1879
1338
<TT>ocamlmklib</TT> will generate <I>output</I><TT>.cma</TT> and/or <I>output</I><TT>.cmxa</TT>.
1880
1339
If not specified, defaults to <TT>a</TT>.
1881
<DT CLASS="dt-description"><B><TT>-oc</TT> <I>outputc</I></B><DD CLASS="dd-description"> Set the name of the generated C library.
1340
</DD><DT CLASS="dt-description"><B><TT>-oc</TT> <I>outputc</I></B></DT><DD CLASS="dd-description"> Set the name of the generated C library.
1882
1341
<TT>ocamlmklib</TT> will generate <TT>lib</TT><I>outputc</I><TT>.so</TT> (if shared
1883
1342
libraries are supported) and <TT>lib</TT><I>outputc</I><TT>.a</TT>.
1884
1343
If not specified, defaults to the output name given with <TT>-o</TT>.
1887
<H5 CLASS="paragraph">Example</H5> Consider a Caml interface to the standard <TT>libz</TT>
1344
</DD></DL><H5 CLASS="paragraph">Example</H5><P> Consider a Caml interface to the standard <TT>libz</TT>
1888
1345
C library for reading and writing compressed files. Assume this
1889
1346
library resides in <TT>/usr/local/zlib</TT>. This interface is
1890
1347
composed of a Caml part <TT>zip.cmo</TT>/<TT>zip.cmx</TT> and a C part <TT>zipstubs.o</TT>
1891
1348
containing the stub code around the <TT>libz</TT> entry points. The
1892
1349
following command builds the Caml libraries <TT>zip.cma</TT> and <TT>zip.cmxa</TT>,
1893
1350
as well as the companion C libraries <TT>dllzip.so</TT> and <TT>libzip.a</TT>:
1894
<PRE CLASS="verbatim">
1895
ocamlmklib -o zip zip.cmo zip.cmx zipstubs.o -lz -L/usr/local/zlib
1896
</PRE>If shared libraries are supported, this performs the following
1351
</P><PRE CLASS="verbatim">ocamlmklib -o zip zip.cmo zip.cmx zipstubs.o -lz -L/usr/local/zlib
1352
</PRE><P>If shared libraries are supported, this performs the following
1898
<PRE CLASS="verbatim">
1899
ocamlc -a -o zip.cma zip.cmo -dllib -lzip \
1354
</P><PRE CLASS="verbatim">ocamlc -a -o zip.cma zip.cmo -dllib -lzip \
1900
1355
-cclib -lzip -cclib -lz -ccopt -L/usr/local/zlib
1901
1356
ocamlopt -a -o zip.cmxa zip.cmx -cclib -lzip \
1902
1357
-cclib -lzip -cclib -lz -ccopt -L/usr/local/zlib
1903
1358
gcc -shared -o dllzip.so zipstubs.o -lz -L/usr/local/zlib
1904
1359
ar rc libzip.a zipstubs.o
1905
</PRE>If shared libraries are not supported, the following commands are
1360
</PRE><P>If shared libraries are not supported, the following commands are
1906
1361
performed instead:
1907
<PRE CLASS="verbatim">
1908
ocamlc -a -custom -o zip.cma zip.cmo -cclib -lzip \
1362
</P><PRE CLASS="verbatim">ocamlc -a -custom -o zip.cma zip.cmo -cclib -lzip \
1909
1363
-cclib -lz -ccopt -L/usr/local/zlib
1910
1364
ocamlopt -a -o zip.cmxa zip.cmx -lzip \
1911
1365
-cclib -lz -ccopt -L/usr/local/zlib
1912
1366
ar rc libzip.a zipstubs.o
1913
</PRE>Instead of building simultaneously the bytecode library, the
1367
</PRE><P>Instead of building simultaneously the bytecode library, the
1914
1368
native-code library and the C libraries, <TT>ocamlmklib</TT> can be called
1915
1369
three times to build each separately. Thus,
1916
<PRE CLASS="verbatim">
1917
ocamlmklib -o zip zip.cmo -lz -L/usr/local/zlib
1918
</PRE>builds the bytecode library <TT>zip.cma</TT>, and
1919
<PRE CLASS="verbatim">
1920
ocamlmklib -o zip zip.cmx -lz -L/usr/local/zlib
1921
</PRE>builds the native-code library <TT>zip.cmxa</TT>, and
1922
<PRE CLASS="verbatim">
1923
ocamlmklib -o zip zipstubs.o -lz -L/usr/local/zlib
1924
</PRE>builds the C libraries <TT>dllzip.so</TT> and <TT>libzip.a</TT>. Notice that the
1370
</P><PRE CLASS="verbatim">ocamlmklib -o zip zip.cmo -lz -L/usr/local/zlib
1371
</PRE><P>builds the bytecode library <TT>zip.cma</TT>, and
1372
</P><PRE CLASS="verbatim">ocamlmklib -o zip zip.cmx -lz -L/usr/local/zlib
1373
</PRE><P>builds the native-code library <TT>zip.cmxa</TT>, and
1374
</P><PRE CLASS="verbatim">ocamlmklib -o zip zipstubs.o -lz -L/usr/local/zlib
1375
</PRE><P>builds the C libraries <TT>dllzip.so</TT> and <TT>libzip.a</TT>. Notice that the
1925
1376
support libraries (<TT>-lz</TT>) and the corresponding options
1926
1377
(<TT>-L/usr/local/zlib</TT>) must be given on all three invocations of <TT>ocamlmklib</TT>,
1927
1378
because they are needed at different times depending on whether shared
1928
libraries are supported.<BR>
1931
<A HREF="manual031.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
1932
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
1933
<A HREF="manual033.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
1379
libraries are supported.</P><HR>
1380
<A HREF="manual031.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
1381
<A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
1382
<A HREF="manual033.html"><IMG SRC="next_motif.gif" ALT="Next"></A>