ffi.q

Call C from q: .ffi.bind turns a symbol in any shared library into a q function, .ffi.callFunction is the one-shot form. The surface and type letters are KX ffikdb's, so its published examples run unchanged.

Examples

pow:.ffi.bind[`libm.so.6`pow;"ff";"f"]
pow (2f;10f;::)

Entity Summary

EntitiesShort Description
.ffi.bind[funcname;argtypes;returntype]Resolve a C function once and answer a q function that calls it.
.ffi.callFunction[returnfunc;arglist]Call a C function, resolving it every time, with argument types inferred from the values.
.ffi.cvar[returnvar]Read a C global variable.
.ffi.extension[]This platform's shared-library extension.
.ffi.os[]This platform's OS letter, as ffikdb examples branch on it.
.ffi.ptrsize[]The width of a pointer on this platform, in bytes.
.ffi.setErrno[errnum]Set errno and answer its previous value; anything but an int atom reads without setting.

Entity Details

.ffi.bind[funcname;argtypes;returntype]

Resolve a C function once and answer a q function that calls it. The result is unary: it takes ONE list of arguments ending in (::) - the trailing :: stops q collapsing same-type arguments into a vector.

Parameters:
  • funcname - `fn resolvable in this process, or `lib`fn to load a library
  • argtypes - one type letter per argument (i j f C ... uppercase for a pointer, "k" a callback), "" for none
  • returntype - one type letter as a char atom, " " for void
throws: os funcname did not resolve

Examples

.ffi.bind[`libm.so.6`sqrt;"f";"f"] (16f;::)

.ffi.callFunction[returnfunc;arglist]

Call a C function, resolving it every time, with argument types inferred from the values.

Parameters:
  • arglist - the arguments, ending in (::)
  • returnfunc - `fn, `lib`fn, or (returnletter;fn) - the letter defaults to "i"

Examples

.ffi.callFunction[("f";`libm.so.6`sqrt)] (16f;::)

.ffi.cvar[returnvar]

Read a C global variable.

Parameters:
  • returnvar - `var, `lib`var, or (typeletter;var)

.ffi.extension[]

This platform's shared-library extension.

Return: `so, `dll or `dylib

Examples

` sv `libmylib,.ffi.extension[]

.ffi.os[]

This platform's OS letter, as ffikdb examples branch on it.

Return: "l", "w" or "m"

Examples

"l"=.ffi.os[]

.ffi.ptrsize[]

The width of a pointer on this platform, in bytes.

Return: 8i, or 4i on a 32-bit host

.ffi.setErrno[errnum]

Set errno and answer its previous value; anything but an int atom reads without setting.

Return: the previous errno

Examples

.ffi.setErrno[]