cffi-sys

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

The software is provided “as is”, without warranty of any kind,
express or implied, including but not limited to the warranties of
merchantability, fitness for a particular purpose and
noninfringement. In no event shall the authors or copyright
holders be liable for any claim, damages or other liability,
whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the software or the use or other
dealings in the software.

1 Introduction

CFFI, the Common Foreign Function Interface, purports to be
a portable foreign function interface for Common Lisp.

This specification defines a set of low-level primitives that must be
defined for each Lisp implementation supported by CFFI.
These operators are defined in the CFFI-SYS package.

The CFFI package uses the CFFI-SYS interface
to implement an extensible foreign type system with support for
typedefs, structures, and unions, a declarative interface for
defining foreign function calls, and automatic conversion of
foreign function arguments to/from Lisp types.

Please note the following conventions that apply to everything in
CFFI-SYS:

Functions in CFFI-SYS that are low-level versions of functions
exported from the CFFI package begin with a leading
percent-sign (eg. %mem-ref).

Where “foreign type” is mentioned as the kind of an argument, the
meaning is restricted to that subset of all foreign types defined in
Built-In Foreign Types. Support for higher-level types is
always defined in terms of those lower-level types in CFFI
proper.

Bind var to a pointer to size bytes of
foreign-accessible memory during body. Both ptr and the
memory block it points to have dynamic extent and may be stack
allocated if supported by the implementation. If size-var is
supplied, it will be bound to size during body.

Each arg-type is a foreign type specifier, followed by
arg, Lisp data to be converted to foreign data of type
arg-type. result-type is the foreign type of the
function's return value, and is assumed to be :void if not
supplied.

%foreign-funcall-pointer takes a pointer ptr to the
function, as returned by foreign-symbol-pointer, rather than a
string name.

Examples

;; Dynamic allocation of a buffer and passing to a function:
(with-foreign-ptr(buf 255 buf-size)(%foreign-funcall "gethostname":pointer buf :size buf-size :int);; Convert buf to a Lisp string using MAKE-STRING and %MEM-REF or
;; a portable CFFI function such as CFFI:FOREIGN-STRING-TO-LISP.
)

8 Loading Foreign Libraries

— Function: %load-foreign-library name => unspecified

Load the foreign shared library name.

Implementor's note: There is a lot of behavior to decide here. Currently I lean
toward not requiring NAME to be a full path to the library so
we can search the system library directories (maybe even get
LD_LIBRARY_PATH from the environment) as necessary.