Module Foreign
High-level bindings for C functions and values
val foreign : ?abi:Libffi_abi.abi -> ?from:Dl.library -> ?stub:bool -> ?check_errno:bool -> ?release_runtime_lock:bool -> string -> ('a -> 'b) Ctypes.fn -> 'a -> 'bforeign name typexposes the C function of typetypnamed bynameas an OCaml value.The argument
?from, if supplied, is a library handle returned byDl.dlopen.The argument
?stub, iftrue(defaults tofalse), indicates that the function should not raise an exception ifnameis not found but return an OCaml value that raises an exception when called.The value
?check_errno, which defaults tofalse, indicates whetherUnix.Unix_errorshould be raised if the C function modifieserrno. 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 tofalse, 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 usingCtypes.ocaml_stringandCtypes.ocaml_bytes, and must not call back into OCaml.- raises Dl.DL_error
if
nameis not found in?fromand?stubisfalse.
val foreign_value : ?from:Dl.library -> string -> 'a Ctypes.typ -> 'a Ctypes.ptrforeign_value name typexposes the C value of typetypnamed bynameas an OCaml value. The argument?from, if supplied, is a library handle returned byDl.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.typConstruct 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 tofalse, indicates whetherUnix.Unix_errorshould be raised if the C function modifieserrno.The value
?runtime_lock, which defaults tofalse, 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 usingCtypes.ocaml_stringandCtypes.ocaml_bytes, and must not call back into OCaml. If the function pointer is used to call into OCaml from C then the?runtime_lockargument indicates whether the lock should be acquired and held during the call.- raises Dl.DL_error
if
nameis not found in?fromand?stubisfalse.
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.typConstruct a function pointer type from a function type.
This behaves like
funptr, except that null pointers appear in OCaml asNone.