cffi-sys
1 Introduction
2 Built-In Foreign Types
3 Operations on Built-in Foreign Types
4 Basic Pointer Operations
5 Foreign Memory Allocation
6 Memory Access
7 Foreign Function Calling
8 Loading Foreign Libraries
9 Foreign Globals
Symbol Index
cffi-sys
********

Copyright (C) 2005-2006, James Bielman <jamesjb at jamesjb.com>

     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 *note Built-In Foreign Types::.  Support for higher-level types
     is always defined in terms of those lower-level types in 'CFFI'
     proper.

2 Built-In Foreign Types
************************

 -- Foreign Type: :char
 -- Foreign Type: :unsigned-char
 -- Foreign Type: :short
 -- Foreign Type: :unsigned-short
 -- Foreign Type: :int
 -- Foreign Type: :unsigned-int
 -- Foreign Type: :long
 -- Foreign Type: :unsigned-long
 -- Foreign Type: :long-long
 -- Foreign Type: :unsigned-long-long
     These types correspond to the native C integer types according to
     the ABI of the system the Lisp implementation is compiled against.

 -- Foreign Type: :int8
 -- Foreign Type: :uint8
 -- Foreign Type: :int16
 -- Foreign Type: :uint16
 -- Foreign Type: :int32
 -- Foreign Type: :uint32
 -- Foreign Type: :int64
 -- Foreign Type: :uint64
     Foreign integer types of specific sizes, corresponding to the C
     types defined in 'stdint.h'.

 -- Foreign Type: :size
 -- Foreign Type: :ssize
 -- Foreign Type: :ptrdiff
 -- Foreign Type: :time
     Foreign integer types corresponding to the standard C types
     (without the '_t' suffix).

   _Implementor's note: I'm sure there are more of these that could be
useful, let's add any types that can't be defined portably to this list
as necessary._

 -- Foreign Type: :float
 -- Foreign Type: :double
     The ':float' type represents a C 'float' and a Lisp 'single-float'.
     ':double' represents a C 'double' and a Lisp 'double-float'.

 -- Foreign Type: :pointer
     A foreign pointer to an object of any type, corresponding to 'void
     *'.

 -- Foreign Type: :void
     No type at all.  Only valid as the return type of a function.

3 Operations on Built-in Foreign Types
**************************************

 -- Function: %foreign-type-size type => size
     Return the SIZE, in bytes, of objects having foreign type TYPE.  An
     error is signalled if TYPE is not a known built-in foreign type.

 -- Function: %foreign-type-alignment type => alignment
     Return the default alignment in bytes for structure members of
     foreign type TYPE.  An error is signalled if TYPE is not a known
     built-in foreign type.

     _Implementor's note: Maybe this should take an optional keyword
     argument specifying an alternate alignment system, eg.  :mac68k for
     68000-compatible alignment on Darwin._

4 Basic Pointer Operations
**************************

 -- Function: pointerp ptr => boolean
     Return true if PTR is a foreign pointer.

 -- Function: null-pointer => pointer
     Return a null foreign pointer.

 -- Function: null-pointer-p ptr => boolean
     Return true if PTR is a null foreign pointer.

 -- Function: make-pointer address => pointer
     Return a pointer corresponding to the numeric integer ADDRESS.

 -- Function: inc-pointer ptr offset => pointer
     Return the result of numerically incrementing PTR by OFFSET.

5 Foreign Memory Allocation
***************************

 -- Function: foreign-alloc size => pointer
     Allocate SIZE bytes of foreign-addressable memory and return a
     POINTER to the allocated block.  An implementation-specific error
     is signalled if the memory cannot be allocated.

 -- Function: foreign-free ptr => unspecified
     Free a pointer PTR allocated by 'foreign-alloc'.  The results are
     undefined if PTR is used after being freed.

 -- Macro: with-foreign-pointer (var size &optional size-var) &body body
     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.

6 Memory Access
***************

 -- Accessor: %mem-ref ptr type &optional offset
     Dereference a pointer OFFSET bytes from PTR to an object for
     reading (or writing when used with 'setf') of built-in type TYPE.

Example
=======

     ;; An impractical example, since time returns the time as well,
     ;; but it demonstrates %MEM-REF. Better (simple) examples wanted!
     (with-foreign-pointer (p (foreign-type-size :time))
       (foreign-funcall "time" :pointer p :time)
       (%mem-ref p :time))

7 Foreign Function Calling
**************************

 -- Macro: %foreign-funcall name {arg-type arg}* &optional result-type
          => object
 -- Macro: %foreign-funcall-pointer ptr {arg-type arg}* &optional
          result-type => object
     Invoke a foreign function called NAME in the foreign source code.

     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
========

     ;; Calling a standard C library function:
     (%foreign-funcall "sqrtf" :float 16.0 :float) => 4.0

     ;; 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._

9 Foreign Globals
*****************

 -- Function: foreign-symbol-pointer name => pointer
     Return a pointer to a foreign symbol NAME.

Symbol Index
************

* Menu:

* %foreign-funcall:                      Foreign Function Calling.
                                                              (line 187)
* %foreign-funcall-pointer:              Foreign Function Calling.
                                                              (line 189)
* %foreign-type-alignment:               Operations on Foreign Types.
                                                              (line 122)
* %foreign-type-size:                    Operations on Foreign Types.
                                                              (line 118)
* %load-foreign-library:                 Loading Foreign Libraries.
                                                              (line 217)
* %mem-ref:                              Memory Access.       (line 171)
* :char:                                 Built-In Foreign Types.
                                                              (line  68)
* :double:                               Built-In Foreign Types.
                                                              (line 104)
* :float:                                Built-In Foreign Types.
                                                              (line 103)
* :int:                                  Built-In Foreign Types.
                                                              (line  72)
* :int16:                                Built-In Foreign Types.
                                                              (line  83)
* :int32:                                Built-In Foreign Types.
                                                              (line  85)
* :int64:                                Built-In Foreign Types.
                                                              (line  87)
* :int8:                                 Built-In Foreign Types.
                                                              (line  81)
* :long:                                 Built-In Foreign Types.
                                                              (line  74)
* :long-long:                            Built-In Foreign Types.
                                                              (line  76)
* :pointer:                              Built-In Foreign Types.
                                                              (line 108)
* :ptrdiff:                              Built-In Foreign Types.
                                                              (line  94)
* :short:                                Built-In Foreign Types.
                                                              (line  70)
* :size:                                 Built-In Foreign Types.
                                                              (line  92)
* :ssize:                                Built-In Foreign Types.
                                                              (line  93)
* :time:                                 Built-In Foreign Types.
                                                              (line  95)
* :uint16:                               Built-In Foreign Types.
                                                              (line  84)
* :uint32:                               Built-In Foreign Types.
                                                              (line  86)
* :uint64:                               Built-In Foreign Types.
                                                              (line  88)
* :uint8:                                Built-In Foreign Types.
                                                              (line  82)
* :unsigned-char:                        Built-In Foreign Types.
                                                              (line  69)
* :unsigned-int:                         Built-In Foreign Types.
                                                              (line  73)
* :unsigned-long:                        Built-In Foreign Types.
                                                              (line  75)
* :unsigned-long-long:                   Built-In Foreign Types.
                                                              (line  77)
* :unsigned-short:                       Built-In Foreign Types.
                                                              (line  71)
* :void:                                 Built-In Foreign Types.
                                                              (line 112)
* foreign-alloc:                         Foreign Memory Allocation.
                                                              (line 152)
* foreign-free:                          Foreign Memory Allocation.
                                                              (line 157)
* foreign-symbol-pointer:                Foreign Globals.     (line 228)
* inc-pointer:                           Basic Pointer Operations.
                                                              (line 146)
* make-pointer:                          Basic Pointer Operations.
                                                              (line 143)
* null-pointer:                          Basic Pointer Operations.
                                                              (line 137)
* null-pointer-p:                        Basic Pointer Operations.
                                                              (line 140)
* pointerp:                              Basic Pointer Operations.
                                                              (line 134)
* with-foreign-pointer:                  Foreign Memory Allocation.
                                                              (line 161)

