-
Notifications
You must be signed in to change notification settings - Fork 95
/
cstubs.mli
214 lines (153 loc) · 7.25 KB
/
cstubs.mli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
(*
* Copyright (c) 2014 Jeremy Yallop.
*
* This file is distributed under the terms of the MIT License.
* See the file LICENSE for details.
*)
(** Operations for generating C bindings stubs. *)
module Types :
sig
module type TYPE =
sig
include Ctypes_types.TYPE
type 'a const
val constant : string -> 'a typ -> 'a const
(** [constant name typ] retrieves the value of the compile-time constant
[name] of type [typ]. It can be used to retrieve enum constants,
#defined values and other integer constant expressions.
The type [typ] must be either an integer type such as [bool], [char],
[int], [uint8], etc., or a view (or perhaps multiple views) where the
underlying type is an integer type.
When the value of the constant cannot be represented in the type there
will typically be a diagnostic from either the C compiler or the OCaml
compiler. For example, gcc will say
warning: overflow in implicit constant conversion *)
val enum : string -> ?typedef:bool ->
?unexpected:(int64 -> 'a) -> ('a * int64 const) list -> 'a typ
(** [enum name ?unexpected alist] builds a type representation for the
enum named [name]. The size and alignment are retrieved so that the
resulting type can be used everywhere an integer type can be used: as
an array element or struct member, as an argument or return value,
etc.
The value [alist] is an association list of OCaml values and values
retrieved by the [constant] function. For example, to expose the enum
enum letters \{ A, B, C = 10, D \};
you might first retrieve the values of the enumeration constants:
{[
let a = constant "A" int64_t
and b = constant "B" int64_t
and c = constant "C" int64_t
and d = constant "D" int64_t
]}
and then build the enumeration type
{[
let letters = enum "letters" [
`A, a;
`B, b;
`C, c;
`D, d;
] ~unexpected:(fun i -> `E i)
]}
The [unexpected] function specifies the value to return in the case
that some unexpected value is encountered -- for example, if a
function with the return type 'enum letters' actually returns the
value [-1].
The optional flag [typedef] specifies whether the first argument,
[name], indicates an tag or an alias. If [typedef] is [false] (the
default) then [name] is treated as an enumeration tag:
[enum letters { ... }]
If [typedef] is [true] then [name] is instead treated as an alias:
[typedef enum { ... } letters] *)
end
module type BINDINGS = functor (F : TYPE) -> sig end
val write_c : Format.formatter -> (module BINDINGS) -> unit
end
module type FOREIGN =
sig
type 'a fn
type 'a return
val (@->) : 'a Ctypes.typ -> 'b fn -> ('a -> 'b) fn
val returning : 'a Ctypes.typ -> 'a return fn
type 'a result
val foreign : string -> ('a -> 'b) fn -> ('a -> 'b) result
val foreign_value : string -> 'a Ctypes.typ -> 'a Ctypes.ptr result
end
module type BINDINGS = functor (F : FOREIGN with type 'a result = unit) -> sig end
type errno_policy
(** Values of the [errno_policy] type specify the errno support provided by
the generated code. See {!ignore_errno} for the available option.
*)
val ignore_errno : errno_policy
(** Generate code with no special support for errno. This is the default. *)
val return_errno : errno_policy
(** Generate code that returns errno in addition to the return value of each function.
Passing [return_errno] as the [errno] argument to {!Cstubs.write_c} and
{!Cstubs.write_ml} changes the return type of bound functions from a
single value to a pair of values. For example, the binding
specification
[let realpath = foreign "reaplath" (string @-> string @-> returning string)]
generates a value of the following type by default:
[val realpath : string -> string -> stirng]
but when using [return_errno] the generated type is as follows:
[val realpath : string -> string -> stirng * int]
and when using both [return_errno] and [lwt_jobs] the generated type is as
follows:
[val realpath : string -> string -> (stirng * int) Lwt.t]
*)
type concurrency_policy
(** Values of the [concurrency_policy] type specify the concurrency support
provided by the generated code. See {!sequential} and {!lwt_jobs} for the
available options.
*)
val sequential : concurrency_policy
(** Generate code with no special support for concurrency. This is the
default.
*)
val unlocked : concurrency_policy
(** Generate code that releases the runtime lock during C calls.
*)
val lwt_preemptive : concurrency_policy
(** Generate code which runs C function calls with the Lwt_preemptive module:
http://ocsigen.org/lwt/2.5.1/api/Lwt_preemptive
Passing [lwt_preemptive] as the [concurrency] argument to {!Cstubs.write_c} and
{!Cstubs.write_ml} changes the return type of bound functions to include
the {!Lwt.t} constructor. For example, the binding specification
[let unlink = foreign "unlink" (string @-> returning int)]
generates a value of the following type by default:
[val unlink : string -> int]
but when using [lwt_preemptive] the generated type is as follows:
[val unlink : string -> int Lwt.t]
Additionally, the OCaml runtime lock is released during calls to functions
bound with [lwt_preemptive].
*)
val lwt_jobs : concurrency_policy
(** Generate code which implements C function calls as Lwt jobs:
http://ocsigen.org/lwt/2.5.1/api/Lwt_unix#TYPEjob
Passing [lwt_jobs] as the [concurrency] argument to {!Cstubs.write_c} and
{!Cstubs.write_ml} changes the return type of bound functions to include
the {!Lwt.t} constructor. For example, the binding specification
[let unlink = foreign "unlink" (string @-> returning int)]
generates a value of the following type by default:
[val unlink : string -> int]
but when using [lwt_jobs] the generated type is as follows:
[val unlink : string -> int Lwt.t]
*)
val write_c : ?concurrency:concurrency_policy -> ?errno:errno_policy ->
Format.formatter -> prefix:string -> (module BINDINGS) -> unit
(** [write_c fmt ~prefix bindings] generates C stubs for the functions bound
with [foreign] in [bindings]. The stubs are intended to be used in
conjunction with the ML code generated by {!write_ml}.
The optional argument [concurrency] specifies the concurrency support
provided by the generated code. The default is [sequential].
The generated code uses definitions exposed in the header file
[ctypes_cstubs_internals.h].
*)
val write_ml : ?concurrency:concurrency_policy -> ?errno:errno_policy ->
Format.formatter -> prefix:string -> (module BINDINGS) -> unit
(** [write_ml fmt ~prefix bindings] generates ML bindings for the functions
bound with [foreign] in [bindings]. The generated code conforms to the
{!FOREIGN} interface.
The optional argument [concurrency] specifies the concurrency support
provided by the generated code. The default is [sequential].
The generated code uses definitions exposed in the module
[Cstubs_internals]. *)