Legend:
Library
Module
Module type
Parameter
Class
Class type
Library
Module
Module type
Parameter
Class
Class type
include module type of struct include Ctypes end
type ('a, 'b) pointer = ('a, 'b) Ctypes_static.pointer
The type of pointer values. A value of type ('a, [`C]) pointer
contains a C-compatible pointer, and a value of type ('a, [`OCaml]) pointer
contains a pointer to a value that can be moved by OCaml runtime.
type 'a ptr = ('a, [ `C ]) pointer
The type of C-compatible pointer values. A value of type t ptr
can be used to read and write values of type t
at particular addresses.
type 'a ocaml = 'a Ctypes_static.ocaml
The type of pointer values pointing directly into OCaml values. Pointers of this type should never be captured by external code. In particular, functions accepting 'a ocaml
pointers must not invoke any OCaml code.
type 'a carray = 'a Ctypes_static.carray
The type of C array values. A value of type t carray
can be used to read and write array objects in C-managed storage.
type 'a bigarray_class = 'a Ctypes_static.bigarray_class
The type of Bigarray classes. There are four instances, one for each of the Bigarray submodules.
val genarray :
< element : 'a
; layout : 'l
; ba_repr : 'b
; bigarray : ('a, 'b, 'l) Bigarray.Genarray.t
; carray : 'a carray
; dims : int array >
bigarray_class
The class of Bigarray.Genarray.t
values
val array1 :
< element : 'a
; layout : 'l
; ba_repr : 'b
; bigarray : ('a, 'b, 'l) Bigarray.Array1.t
; carray : 'a carray
; dims : int >
bigarray_class
The class of Bigarray.Array1.t
values
val array2 :
< element : 'a
; layout : 'l
; ba_repr : 'b
; bigarray : ('a, 'b, 'l) Bigarray.Array2.t
; carray : 'a carray carray
; dims : int * int >
bigarray_class
The class of Bigarray.Array2.t
values
val array3 :
< element : 'a
; layout : 'l
; ba_repr : 'b
; bigarray : ('a, 'b, 'l) Bigarray.Array3.t
; carray : 'a carray carray carray
; dims : int * int * int >
bigarray_class
The class of Bigarray.Array3.t
values
type ('a, 'kind) structured = ('a, 'kind) Ctypes_static.structured
The base type of values representing C struct and union types. The 'kind
parameter is a polymorphic variant type indicating whether the type represents a struct (`Struct
) or a union (`Union
).
type 'a structure = ('a, [ `Struct ]) structured
The type of values representing C struct types.
type 'a union = ('a, [ `Union ]) structured
The type of values representing C union types.
type ('a, 't) field = ('a, 't) Ctypes_static.field
The type of values representing C struct or union members (called "fields" here). A value of type (a, s) field
represents a field of type a
in a struct or union of type s
.
type 'a abstract = 'a Ctypes_static.abstract
The type of abstract values. The purpose of the abstract
type is to represent values whose type varies from platform to platform.
For example, the type pthread_t
is a pointer on some platforms, an integer on other platforms, and a struct on a third set of platforms. One way to deal with this kind of situation is to have possibly-platform-specific code which interrogates the C type in some way to help determine an appropriate representation. Another way is to use abstract
, leaving the representation opaque.
(Note, however, that although pthread_t
is a convenient example, since the type used to implement it varies significantly across platforms, it's not actually a good match for abstract
, since values of type pthread_t
are passed and returned by value.)
include Ctypes_types.TYPE
with type 'a typ = 'a Ctypes_static.typ
and type ('a, 's) field := ('a, 's) field
type 'a typ = 'a Ctypes_static.typ
The type of values representing C types. There are two types associated with each typ
value: the C type used to store and pass values, and the corresponding OCaml type. The type parameter indicates the OCaml type, so a value of type t typ
is used to read and write OCaml values of type t
. There are various uses of typ
values, including
Foreign.foreign
ptr
val void : unit typ
Value representing the C void type. Void values appear in OCaml as the unit type, so using void in an argument or result type specification produces a function which accepts or returns unit.
Dereferencing a pointer to void is an error, as in C, and will raise IncompleteType
.
The scalar types consist of the Arithmetic types and the Pointer types.
The arithmetic types consist of the signed and unsigned integer types (including character types) and the floating types. There are values representing both exact-width integer types (of 8, 16, 32 and 64 bits) and types whose size depend on the platform (signed and unsigned short, int, long, long long).
val char : char typ
Value representing the C type char
.
val schar : int typ
Value representing the C type signed char
.
val short : int typ
Value representing the C type (signed
) short
.
val int : int typ
Value representing the C type (signed
) int
.
val long : Signed.long typ
Value representing the C type (signed
) long
.
val llong : Signed.llong typ
Value representing the C type (signed
) long long
.
val nativeint : nativeint typ
Value representing the C type (signed
) int
.
val int8_t : int typ
Value representing an 8-bit signed integer C type.
val int16_t : int typ
Value representing a 16-bit signed integer C type.
val int32_t : int32 typ
Value representing a 32-bit signed integer C type.
val int64_t : int64 typ
Value representing a 64-bit signed integer C type.
module Intptr = Ctypes.Intptr
module Ptrdiff = Ctypes.Ptrdiff
val camlint : int typ
Value representing an integer type with the same storage requirements as an OCaml int
.
val uchar : Unsigned.uchar typ
Value representing the C type unsigned char
.
val bool : bool typ
Value representing the C type bool
.
val uint8_t : Unsigned.uint8 typ
Value representing an 8-bit unsigned integer C type.
val uint16_t : Unsigned.uint16 typ
Value representing a 16-bit unsigned integer C type.
val uint32_t : Unsigned.uint32 typ
Value representing a 32-bit unsigned integer C type.
val uint64_t : Unsigned.uint64 typ
Value representing a 64-bit unsigned integer C type.
val size_t : Unsigned.size_t typ
Value representing the C type size_t
, an alias for one of the unsigned integer types. The actual size and alignment requirements for size_t
vary between platforms.
val ushort : Unsigned.ushort typ
Value representing the C type unsigned short
.
val sint : Signed.sint typ
Value representing the C type int
.
val uint : Unsigned.uint typ
Value representing the C type unsigned int
.
val ulong : Unsigned.ulong typ
Value representing the C type unsigned long
.
val ullong : Unsigned.ullong typ
Value representing the C type unsigned long long
.
module Uintptr = Ctypes.Uintptr
val float : float typ
Value representing the C single-precision float
type.
val double : float typ
Value representing the C type double
.
val complexld : ComplexL.t typ
Value representing the C99 long-double-precision long double complex
type.
val ptr : 'a typ -> 'a Ctypes_static.ptr typ
Construct a pointer type from an existing type (called the reference type).
val ptr_opt : 'a typ -> 'a Ctypes_static.ptr option typ
Construct a pointer type from an existing type (called the reference type). This behaves like ptr
, except that null pointers appear in OCaml as None
.
val string : string typ
A high-level representation of the string type.
On the C side this behaves like char *
; on the OCaml side values read and written using string
are simply native OCaml strings.
To avoid problems with the garbage collector, values passed using string
are copied into immovable C-managed storage before being passed to C.
When the memory is not owned by the C code, -- i.e. when creating or initializing a struct in OCaml before passing it to C -- then the string
view isn't a good choice, because there's no way to manage the lifetime of the C copy of the generated OCaml string.
val string_opt : string option typ
A high-level representation of the string type. This behaves like string
, except that null pointers appear in OCaml as None
.
val ocaml_string : string Ctypes_static.ocaml typ
Value representing the directly mapped storage of an OCaml string.
val ocaml_bytes : bytes Ctypes_static.ocaml typ
Value representing the directly mapped storage of an OCaml byte array.
val array : int -> 'a typ -> 'a Ctypes_static.carray typ
Construct a sized array type from a length and an existing type (called the element type).
val bigarray :
< element : 'a
; layout : Bigarray.c_layout
; ba_repr : 'b
; dims : 'dims
; bigarray : 'bigarray
; carray : _ >
Ctypes_static.bigarray_class ->
'dims ->
('a, 'b) Bigarray.kind ->
'bigarray typ
Construct a sized C-layout bigarray type representation from a bigarray class, the dimensions, and the Bigarray.kind
.
val fortran_bigarray :
< element : 'a
; layout : Bigarray.fortran_layout
; ba_repr : 'b
; dims : 'dims
; bigarray : 'bigarray
; carray : _ >
Ctypes_static.bigarray_class ->
'dims ->
('a, 'b) Bigarray.kind ->
'bigarray typ
Construct a sized Fortran-layout bigarray type representation from a bigarray class, the dimensions, and the Bigarray.kind
.
val typ_of_bigarray_kind : ('a, 'b) Bigarray.kind -> 'a typ
typ_of_bigarray_kind k
is the type corresponding to the Bigarray kind k
.
val structure : string -> 's Ctypes_static.structure typ
Construct a new structure type. The type value returned is incomplete and can be updated using field
until it is passed to seal
, at which point the set of fields is fixed.
The type ('_s structure typ
) of the expression returned by the call structure tag
includes a weak type variable, which can be explicitly instantiated to ensure that the OCaml values representing different C structure types have incompatible types. Typical usage is as follows:
type tagname
let tagname : tagname structure typ = structure "tagname"
val union : string -> 's Ctypes_static.union typ
val field :
't typ ->
string ->
'a typ ->
('a, ('s, [< `Struct | `Union ]) Ctypes_static.structured as 't) field
field ty label ty'
adds a field of type ty'
with label label
to the structure or union type ty
and returns a field value that can be used to read and write the field in structure or union instances (e.g. using getf
and setf
).
Attempting to add a field to a union type that has been sealed with seal
is an error, and will raise ModifyingSealedType
.
val seal : (_, [< `Struct | `Union ]) Ctypes_static.structured typ -> unit
seal t
completes the struct or union type t
so that no further fields can be added. Struct and union types must be sealed before they can be used in a way that involves their size or alignment; see the documentation for IncompleteType
for further details.
val view :
?format_typ:((Format.formatter -> unit) -> Format.formatter -> unit) ->
?format:(Format.formatter -> 'b -> unit) ->
read:('a -> 'b) ->
write:('b -> 'a) ->
'a typ ->
'b typ
view ~read:r ~write:w t
creates a C type representation t'
which behaves like t
except that values read using t'
are subsequently transformed using the function r
and values written using t'
are first transformed using the function w
.
For example, given suitable definitions of string_of_char_ptr
and char_ptr_of_string
, the type representation
view ~read:string_of_char_ptr ~write:char_ptr_of_string (ptr char)
can be used to pass OCaml strings directly to and from bound C functions, or to read and write string members in structs and arrays. (In fact, the string
type representation is defined in exactly this way.)
The optional argument format_typ
is used by the Ctypes.format_typ
and string_of_typ
functions to print the type at the top level and elsewhere. If format_typ
is not supplied the printer for t
is used instead.
The optional argument format
is used by the Ctypes.format
and string_of
functions to print the values. If format_val
is not supplied the printer for t
is used instead.
typedef t name
creates a C type representation t'
which is equivalent to t
except its name is printed as name
.
This is useful when generating C stubs involving "anonymous" types, for example: typedef struct { int f } typedef_name;
val abstract :
name:string ->
size:int ->
alignment:int ->
'a Ctypes_static.abstract typ
Create an abstract type specification from the size and alignment requirements for the type.
val lift_typ : 'a Ctypes_static.typ -> 'a typ
lift_typ t
turns a concrete type representation into an abstract type representation.
For example, retrieving struct layout from C involves working with an abstract representation of types which do not support operations such as sizeof
. The lift_typ
function makes it possible to use concrete type representations wherever such abstract type representations are needed.
Abstract interface to C function type descriptions
type 'a fn = 'a Ctypes_static.fn
The type of values representing C function types. A value of type t fn
can be used to bind to C functions and to describe type of OCaml functions passed to C.
Construct a function type from a type and an existing function type. This corresponds to prepending a parameter to a C function parameter list. For example,
int @-> ptr void @-> returning float
describes a function type that accepts two arguments -- an integer and a pointer to void -- and returns a float.
type 'a static_funptr = 'a Ctypes_static.static_funptr
Function pointer types
The type of values representing C function pointer types.
val static_funptr : 'a fn -> 'a Ctypes_static.static_funptr typ
Construct a function pointer type from an existing function type (called the reference type).
val sizeof : 'a typ -> int
sizeof t
computes the size in bytes of the type t
. The exception IncompleteType
is raised if t
is incomplete.
val alignment : 'a typ -> int
alignment t
computes the alignment requirements of the type t
. The exception IncompleteType
is raised if t
is incomplete.
val format_typ : ?name:string -> Format.formatter -> 'a typ -> unit
Pretty-print a C representation of the type to the specified formatter.
val format_fn : ?name:string -> Format.formatter -> 'a fn -> unit
Pretty-print a C representation of the function type to the specified formatter.
val string_of_typ : ?name:string -> 'a typ -> string
Return a C representation of the type.
val string_of_fn : ?name:string -> 'a fn -> string
Return a C representation of the function type.
val format : 'a typ -> Format.formatter -> 'a -> unit
Pretty-print a representation of the C value to the specified formatter.
val string_of : 'a typ -> 'a -> string
Return a string representation of the C value.
val null : unit ptr
A null pointer.
val (!@) : 'a ptr -> 'a
!@ p
dereferences the pointer p
. If the reference type is a scalar type then dereferencing constructs a new value. If the reference type is an aggregate type then dereferencing returns a value that references the memory pointed to by p
.
val (<-@) : 'a ptr -> 'a -> unit
p <-@ v
writes the value v
to the address p
.
If p
is a pointer to an array element then p +@ n
computes the address of the n
th next element.
If p
is a pointer to an array element then p -@ n
computes the address of the nth previous element.
ptr_diff p q
computes q - p
. As in C, both p
and q
must point into the same array, and the result value is the difference of the subscripts of the two array elements.
allocate t v
allocates a fresh value of type t
, initialises it with v
and returns its address. The argument ?finalise
, if present, will be called just before the memory is freed. The value will be automatically freed after no references to the pointer remain within the calling OCaml program.
allocate_n t ~count:n
allocates a fresh array with element type t
and length n
, and returns its address. The argument ?finalise
, if present, will be called just before the memory is freed. The array will be automatically freed after no references to the pointer remain within the calling OCaml program. The memory is allocated with libc's calloc
and is guaranteed to be zero-filled.
If p
and q
are pointers to elements i
and j
of the same array then ptr_compare p q
compares the indexes of the elements. The result is negative if i
is less than j
, positive if i
is greater than j
, and zero if i
and j
are equal.
val is_null : 'a ptr -> bool
is_null p
is true when p
is a null pointer.
val ptr_of_raw_address : nativeint -> unit ptr
Convert the numeric representation of an address to a pointer
val funptr_of_raw_address :
nativeint ->
(unit -> unit) Ctypes_static.static_funptr
Convert the numeric representation of an address to a function pointer
val raw_address_of_ptr : unit ptr -> nativeint
raw_address_of_ptr p
returns the numeric representation of p.
Note that the return value remains valid only as long as the pointed-to object is alive. If p
is a managed object (e.g. a value returned by make
) then unless the caller retains a reference to p
, the object may be collected, invalidating the returned address.
val string_from_ptr : char ptr -> length:int -> string
string_from_ptr p ~length
creates a string initialized with the length
characters at address p
.
Raise Invalid_argument "Ctypes.string_from_ptr"
if length
is negative.
val ocaml_string_start : string -> string ocaml
ocaml_string_start s
allows to pass a pointer to the contents of an OCaml string directly to a C function.
val ocaml_bytes_start : bytes -> bytes ocaml
ocaml_bytes_start s
allows to pass a pointer to the contents of an OCaml byte array directly to a C function.
module CArray = Ctypes.CArray
Operations on C arrays.
val bigarray_start :
< element : 'a
; layout : 'l
; ba_repr : _
; bigarray : 'b
; carray : _
; dims : _ >
bigarray_class ->
'b ->
'a ptr
Return the address of the first element of the given Bigarray value.
val bigarray_of_ptr :
< element : 'a
; layout : Bigarray.c_layout
; ba_repr : 'f
; bigarray : 'b
; carray : _
; dims : 'i >
bigarray_class ->
'i ->
('a, 'f) Bigarray.kind ->
'a ptr ->
'b
bigarray_of_ptr c dims k p
converts the C pointer p
to a C-layout bigarray value. No copy is made; the bigarray references the memory pointed to by p
.
val fortran_bigarray_of_ptr :
< element : 'a
; layout : Bigarray.fortran_layout
; ba_repr : 'f
; bigarray : 'b
; carray : _
; dims : 'i >
bigarray_class ->
'i ->
('a, 'f) Bigarray.kind ->
'a ptr ->
'b
fortran_bigarray_of_ptr c dims k p
converts the C pointer p
to a Fortran-layout bigarray value. No copy is made; the bigarray references the memory pointed to by p
.
val array_of_bigarray :
< element : _
; layout : Bigarray.c_layout
; ba_repr : _
; bigarray : 'b
; carray : 'c
; dims : _ >
bigarray_class ->
'b ->
'c
array_of_bigarray c b
converts the bigarray value b
to a value of type CArray.t
. No copy is made; the result occupies the same memory as b
.
Convert a Bigarray value to a C array.
val bigarray_of_array :
< element : 'a
; layout : Bigarray.c_layout
; ba_repr : 'f
; bigarray : 'b
; carray : 'c carray
; dims : 'i >
bigarray_class ->
('a, 'f) Bigarray.kind ->
'c carray ->
'b
bigarray_of_array c k a
converts the CArray.t
value a
to a C-layout bigarray value. No copy is made; the result occupies the same memory as a
.
val make : ?finalise:('s -> unit) -> (_, _) structured as 's typ -> 's
Allocate a fresh, uninitialised structure or union value. The argument ?finalise
, if present, will be called just before the underlying memory is freed.
val setf : (_, _) structured as 's -> ('a, 's) field -> 'a -> unit
setf s f v
overwrites the value of the field f
in the structure or union s
with v
.
val getf : (_, _) structured as 's -> ('a, 's) field -> 'a
getf s f
retrieves the value of the field f
in the structure or union s
. The semantics for non-scalar types are non-copying, as for (!@)
.
val (@.) : (_, _) structured as 's -> ('a, 's) field -> 'a ptr
s @. f
computes the address of the field f
in the structure or union value s
.
val (|->) : (_, _) structured as 's ptr -> ('a, 's) field -> 'a ptr
p |-> f
computes the address of the field f
in the structure or union value pointed to by p
.
offsetof f
returns the offset, in bytes, of the field f
from the beginning of the associated struct type.
val field_name : (_, _) field -> string
field_name f
returns the name of the field f
.
val addr : (_, _) structured as 's -> 's ptr
addr s
returns the address of the structure or union s
.
coerce t1 t2
returns a coercion function between the types represented by t1
and t2
. If t1
cannot be coerced to t2
, coerce
raises Uncoercible
.
The following coercions are currently supported:
void
view
and another type t
(in either direction) if there is a coercion between the representation type underlying the view and t
.t1
is coercible to t2
and t2
is coercible to t3
, then t1
is directly coercible to t3
.The set of supported coercions is subject to change. Future versions of ctypes may both add new types of coercion and restrict the existing coercions.
coerce_fn f1 f2
returns a coercion function between the function types represented by f1
and f2
. If f1
cannot be coerced to f2
, coerce_fn
raises Uncoercible
.
A function type f1
may be coerced to another function type f2
if all of the following hold:
f1
and f2
have the same arityf2
may be coerced to the corresponding argument of f1
f1
may be coerced to the return type of f2
The set of supported coercions is subject to change. Future versions of ctypes may both add new types of coercion and restrict the existing coercions.
.
module type FOREIGN = Ctypes.FOREIGN
Foreign function binding interface.
module type TYPE = Ctypes.TYPE
Foreign types binding interface.
module Root = Ctypes.Root
An attempt was made to use a feature not currently supported by ctypes. In practice this refers to attempts to use an union, array or abstract type as an argument or return type of a function.
An attempt was made to modify a sealed struct or union type description.
An attempt was made to compute the size or alignment of an incomplete type.
The incomplete types are struct and union types that have not been sealed, and the void type.
It is not permitted to compute the size or alignment requirements of an incomplete type, to use it as a struct or union member, to read or write a value of the type through a pointer or to use it as the referenced type in pointer arithmetic. Additionally, incomplete struct and union types cannot be used as argument or return types.
type uncoercible_info = Ctypes.uncoercible_info
exception Uncoercible of uncoercible_info
An attempt was made to coerce between uncoercible types.
include module type of struct include Foreign end
val foreign :
?abi:Libffi_abi.abi ->
?from:Dl.library ->
?stub:bool ->
?check_errno:bool ->
?release_runtime_lock:bool ->
string ->
('a -> 'b) Ctypes.fn ->
'a ->
'b
foreign name typ
exposes the C function of type typ
named by name
as an OCaml value.
The argument ?from
, if supplied, is a library handle returned by Dl.dlopen
.
The argument ?stub
, if true
(defaults to false
), indicates that the function should not raise an exception if name
is not found but return an OCaml value that raises an exception when called.
The value ?check_errno
, which defaults to false
, indicates whether Unix.Unix_error
should be raised if the C function modifies errno
. Please note that a function that succeeds is allowed to change errno. So use this option with caution.
The value ?release_runtime_lock
, which defaults to false
, indicates whether the OCaml runtime lock should be released during the call to the C function, allowing other threads to run. If the runtime lock is released then the C function must not access OCaml heap objects, such as arguments passed using Ctypes.ocaml_string
and Ctypes.ocaml_bytes
, and must not call back into OCaml.
val foreign_value :
?from:Dl.library ->
string ->
'a Ctypes.typ ->
'a Ctypes.ptr
foreign_value name typ
exposes the C value of type typ
named by name
as an OCaml value. The argument ?from
, if supplied, is a library handle returned by Dl.dlopen
.
val funptr :
?abi:Libffi_abi.abi ->
?name:string ->
?check_errno:bool ->
?runtime_lock:bool ->
?thread_registration:bool ->
('a -> 'b) Ctypes.fn ->
('a -> 'b) Ctypes.typ
Construct a function pointer type from a function type.
The ctypes library, like C itself, distinguishes functions and function pointers. Functions are not first class: it is not possible to use them as arguments or return values of calls, or store them in addressable memory. Function pointers are first class, and so have none of these restrictions.
The value ?check_errno
, which defaults to false
, indicates whether Unix.Unix_error
should be raised if the C function modifies errno
.
The value ?runtime_lock
, which defaults to false
, indicates whether the OCaml runtime lock should be released during the call to the C function, allowing other threads to run. If the runtime lock is released then the C function must not access OCaml heap objects, such as arguments passed using Ctypes.ocaml_string
and Ctypes.ocaml_bytes
, and must not call back into OCaml. If the function pointer is used to call into OCaml from C then the ?runtime_lock
argument indicates whether the lock should be acquired and held during the call.
val funptr_opt :
?abi:Libffi_abi.abi ->
?name:string ->
?check_errno:bool ->
?runtime_lock:bool ->
?thread_registration:bool ->
('a -> 'b) Ctypes.fn ->
('a -> 'b) option Ctypes.typ
Construct a function pointer type from a function type.
This behaves like funptr
, except that null pointers appear in OCaml as None
.
A closure passed to C was collected by the OCaml garbage collector before it was called.
module type Funptr = Foreign.Funptr
val dynamic_funptr :
?abi:Libffi_abi.abi ->
?runtime_lock:bool ->
?thread_registration:bool ->
('a -> 'b) Ctypes.fn ->
(module Funptr
with type fn = 'a -> 'b)
Define a type representation for passing OCaml functions to C with explicit lifetime management.
(val (dynamic_funptr (foo @-> returning bar)))
corresponds to the C type bar( * )(foo)
.
Example:
module Progress_callback =
(val (dynamic_funptr (int @-> int @-> ptr void @-> returning void)))
let keygen = foreign "RSA_generate_key"
(int @-> int @-> Progress_callback.t @-> ptr void @-> returning rsa_key)
let secret_key =
Progress_callback.with_fun
(fun a b _ -> printf "progress: a:%d, b:%d\n" a b)
(fun progress ->
keygen 2048 65537 progress null)
val report_leaked_funptr : (string -> unit) Pervasives.ref
Hook called on collection of closures associated with dynamic_funptr
values that have not been deallocated with free
.
By default the ctypes library retains closures associated with function pointers that have not been freed and prints a warning to stderr.
You can use this hook to change how these error messages are reported.