1 \input texinfo @c -*-texinfo-*-
3 @setfilename cffi-sys.info
4 @settitle CFFI-SYS Interface Specification
6 @c Show types in the same index as the functions.
10 Copyright @copyright{} 2005-2006, James Bielman <jamesjb at jamesjb.com>
13 Permission is hereby granted, free of charge, to any person
14 obtaining a copy of this software and associated documentation
15 files (the ``Software''), to deal in the Software without
16 restriction, including without limitation the rights to use, copy,
17 modify, merge, publish, distribute, sublicense, and/or sell copies
18 of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
21 The above copyright notice and this permission notice shall be
22 included in all copies or substantial portions of the Software.
24 @sc{The software is provided ``as is'', without warranty of any kind,
25 express or implied, including but not limited to the warranties of
26 merchantability, fitness for a particular purpose and
27 noninfringement. In no event shall the authors or copyright
28 holders be liable for any claim, damages or other liability,
29 whether in an action of contract, tort or otherwise, arising from,
30 out of or in connection with the software or the use or other
31 dealings in the software.}
36 @emph{Implementor's note: \text\}
40 @dircategory Software development
42 * CFFI Sys spec: (cffi-sys-spec). CFFI Sys spec.
46 @title CFFI-SYS Interface Specification
47 @c @subtitle Version X.X
48 @c @author James Bielman
51 @vskip 0pt plus 1filll
65 * Built-In Foreign Types::
66 * Operations on Foreign Types::
67 * Basic Pointer Operations::
68 * Foreign Memory Allocation::
70 * Foreign Function Calling::
71 * Loading Foreign Libraries::
79 @acronym{CFFI}, the Common Foreign Function Interface, purports to be
80 a portable foreign function interface for Common Lisp.
82 This specification defines a set of low-level primitives that must be
83 defined for each Lisp implementation supported by @acronym{CFFI}.
84 These operators are defined in the @code{CFFI-SYS} package.
86 The @code{CFFI} package uses the @code{CFFI-SYS} interface
87 to implement an extensible foreign type system with support for
88 typedefs, structures, and unions, a declarative interface for
89 defining foreign function calls, and automatic conversion of
90 foreign function arguments to/from Lisp types.
92 Please note the following conventions that apply to everything in
97 Functions in @code{CFFI-SYS} that are low-level versions of functions
98 exported from the @code{CFFI} package begin with a leading
99 percent-sign (eg. @code{%mem-ref}).
102 Where ``foreign type'' is mentioned as the kind of an argument, the
103 meaning is restricted to that subset of all foreign types defined in
104 @ref{Built-In Foreign Types}. Support for higher-level types is
105 always defined in terms of those lower-level types in @code{CFFI}
110 @node Built-In Foreign Types
111 @chapter Built-In Foreign Types
113 @deftp {Foreign Type} :char
114 @deftpx {Foreign Type} :unsigned-char
115 @deftpx {Foreign Type} :short
116 @deftpx {Foreign Type} :unsigned-short
117 @deftpx {Foreign Type} :int
118 @deftpx {Foreign Type} :unsigned-int
119 @deftpx {Foreign Type} :long
120 @deftpx {Foreign Type} :unsigned-long
121 @deftpx {Foreign Type} :long-long
122 @deftpx {Foreign Type} :unsigned-long-long
123 These types correspond to the native C integer types according to the
124 ABI of the system the Lisp implementation is compiled against.
127 @deftp {Foreign Type} :int8
128 @deftpx {Foreign Type} :uint8
129 @deftpx {Foreign Type} :int16
130 @deftpx {Foreign Type} :uint16
131 @deftpx {Foreign Type} :int32
132 @deftpx {Foreign Type} :uint32
133 @deftpx {Foreign Type} :int64
134 @deftpx {Foreign Type} :uint64
135 Foreign integer types of specific sizes, corresponding to the C types
136 defined in @code{stdint.h}.
139 @deftp {Foreign Type} :size
140 @deftpx {Foreign Type} :ssize
141 @deftpx {Foreign Type} :ptrdiff
142 @deftpx {Foreign Type} :time
143 Foreign integer types corresponding to the standard C types (without
144 the @code{_t} suffix).
147 @impnote{I'm sure there are more of these that could be useful, let's
148 add any types that can't be defined portably to this list as
151 @deftp {Foreign Type} :float
152 @deftpx {Foreign Type} :double
153 The @code{:float} type represents a C @code{float} and a Lisp
154 @code{single-float}. @code{:double} represents a C @code{double} and a
155 Lisp @code{double-float}.
158 @deftp {Foreign Type} :pointer
159 A foreign pointer to an object of any type, corresponding to
163 @deftp {Foreign Type} :void
164 No type at all. Only valid as the return type of a function.
168 @node Operations on Foreign Types
169 @chapter Operations on Built-in Foreign Types
171 @defun %foreign-type-size type @result{} size
172 Return the @var{size}, in bytes, of objects having foreign type
173 @var{type}. An error is signalled if @var{type} is not a known
174 built-in foreign type.
177 @defun %foreign-type-alignment type @result{} alignment
178 Return the default alignment in bytes for structure members of foreign
179 type @var{type}. An error is signalled if @var{type} is not a known
180 built-in foreign type.
182 @impnote{Maybe this should take an optional keyword argument specifying an
183 alternate alignment system, eg. :mac68k for 68000-compatible alignment
188 @node Basic Pointer Operations
189 @chapter Basic Pointer Operations
191 @defun pointerp ptr @result{} boolean
192 Return true if @var{ptr} is a foreign pointer.
195 @defun null-pointer @result{} pointer
196 Return a null foreign pointer.
199 @defun null-pointer-p ptr @result{} boolean
200 Return true if @var{ptr} is a null foreign pointer.
203 @defun make-pointer address @result{} pointer
204 Return a pointer corresponding to the numeric integer @var{address}.
207 @defun inc-pointer ptr offset @result{} pointer
208 Return the result of numerically incrementing @var{ptr} by @var{offset}.
212 @node Foreign Memory Allocation
213 @chapter Foreign Memory Allocation
215 @defun foreign-alloc size @result{} pointer
216 Allocate @var{size} bytes of foreign-addressable memory and return
217 a @var{pointer} to the allocated block. An implementation-specific
218 error is signalled if the memory cannot be allocated.
221 @defun foreign-free ptr @result{} unspecified
222 Free a pointer @var{ptr} allocated by @code{foreign-alloc}. The
223 results are undefined if @var{ptr} is used after being freed.
226 @defmac with-foreign-pointer (var size &optional size-var) &body body
227 Bind @var{var} to a pointer to @var{size} bytes of
228 foreign-accessible memory during @var{body}. Both @var{ptr} and the
229 memory block it points to have dynamic extent and may be stack
230 allocated if supported by the implementation. If @var{size-var} is
231 supplied, it will be bound to @var{size} during @var{body}.
236 @chapter Memory Access
238 @deffn {Accessor} %mem-ref ptr type &optional offset
239 Dereference a pointer @var{offset} bytes from @var{ptr} to an object
240 for reading (or writing when used with @code{setf}) of built-in type
247 ;; An impractical example, since time returns the time as well,
248 ;; but it demonstrates %MEM-REF. Better (simple) examples wanted!
249 (with-foreign-pointer (p (foreign-type-size :time))
250 (foreign-funcall "time" :pointer p :time)
255 @node Foreign Function Calling
256 @chapter Foreign Function Calling
258 @defmac %foreign-funcall name @{arg-type arg@}* &optional result-type @result{} object
259 @defmacx %foreign-funcall-pointer ptr @{arg-type arg@}* &optional result-type @result{} object
260 Invoke a foreign function called @var{name} in the foreign source code.
262 Each @var{arg-type} is a foreign type specifier, followed by
263 @var{arg}, Lisp data to be converted to foreign data of type
264 @var{arg-type}. @var{result-type} is the foreign type of the
265 function's return value, and is assumed to be @code{:void} if not
268 @code{%foreign-funcall-pointer} takes a pointer @var{ptr} to the
269 function, as returned by @code{foreign-symbol-pointer}, rather than a
273 @defmac %foreign-funcall-varargs name (@{fixed-type arg@}*) @{vararg-type arg@}* &optional result-type @result{} object
274 @defmacx %foreign-funcall-varargs-pointer ptr (@{fixed-type arg@}*) @{vararg-type arg@}* &optional result-type @result{} object
275 Invoke a foreign variadic function called @var{name} in the foreign
278 Each @var{fixed-type} and @var{vararg-type} is a foreign type
279 specifier, followed by @var{arg}, Lisp data to be converted to foreign
280 data of type @var{arg-type}. @var{result-type} is the foreign type of
281 the function's return value, and is assumed to be @code{:void} if not
284 @code{%foreign-funcall-pointer-varargs} takes a pointer @var{ptr} to
285 the variadic function, as returned by @code{foreign-symbol-pointer},
286 rather than a string @var{name}.
288 Both functions have default implementation which call
289 @code{%foreign-funcall} and @code{%foreign-funcall-pointer}
296 ;; Calling a standard C library function:
297 (%foreign-funcall "sqrtf" :float 16.0 :float) @result{} 4.0
301 ;; Dynamic allocation of a buffer and passing to a function:
302 (with-foreign-ptr (buf 255 buf-size)
303 (%foreign-funcall "gethostname" :pointer buf :size buf-size :int)
304 ;; Convert buf to a Lisp string using MAKE-STRING and %MEM-REF or
305 ;; a portable CFFI function such as CFFI:FOREIGN-STRING-TO-LISP.
310 @node Loading Foreign Libraries
311 @chapter Loading Foreign Libraries
313 @defun %load-foreign-library name @result{} unspecified
314 Load the foreign shared library @var{name}.
316 @impnote{There is a lot of behavior to decide here. Currently I lean
317 toward not requiring NAME to be a full path to the library so
318 we can search the system library directories (maybe even get
319 LD_LIBRARY_PATH from the environment) as necessary.}
323 @node Foreign Globals
324 @chapter Foreign Globals
326 @defun foreign-symbol-pointer name @result{} pointer
327 Return a pointer to a foreign symbol @var{name}.
331 @unnumbered Symbol Index