ctypes

Combinators for binding to C libraries without writing any C
Library ctypes-foreign
Module type Foreign . Funptr
type fn

fn is the signature of the underlying OCaml function.

type t

Handle to an OCaml function that can be passed to C for use in callbacks.

Each value of type t allocated by of_fun must be deallocated by calling free. Alternatively with_fun encapsulates both allocation and deallocation.

val t : t Ctypes.typ

A type representation for a function pointer type with explicit lifetime management.

val t_opt : t option Ctypes.typ

This behaves like t, except that null pointers appear in OCaml as None.

val free : t -> unit

Indicate that the fptr is no longer needed.

Once free has been called any C calls to this Dynamic_funptr.t are unsafe. Only call free once the callback is no longer used from C.

val of_fun : fn -> t

Turn an OCaml closure into a function pointer that can be passed to C.

The function pointer returned by of_fun should be deallocated by a call to free once it is no longer in use. Failure to call free is an error.

Alternatively, with_fun encapsulates both allocation and deallocation.

Implementation detail: to avoid crashes, if free is not called then the implementation will retain a reference to the OCaml closure and report a warning. See report_leaked_funptr.

val with_fun : fn -> ( t -> 'c ) -> 'c

with_fun fn (fun fptr -> e) - Turn an OCaml closure into a function pointer and perform simple life cycle management.

with_fun fn (fun fptr -> e) will call free fptr after e completes.

with_fun is not safe to use if the C function ptr fptr may still be used after e completes.