1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; cffi-sbcl.lisp --- CFFI-SYS implementation for SBCL.
5 ;;; Copyright (C) 2005-2006, James Bielman <jamesjb@jamesjb.com>
7 ;;; Permission is hereby granted, free of charge, to any person
8 ;;; obtaining a copy of this software and associated documentation
9 ;;; files (the "Software"), to deal in the Software without
10 ;;; restriction, including without limitation the rights to use, copy,
11 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
12 ;;; of the Software, and to permit persons to whom the Software is
13 ;;; furnished to do so, subject to the following conditions:
15 ;;; The above copyright notice and this permission notice shall be
16 ;;; included in all copies or substantial portions of the Software.
18 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 ;;; DEALINGS IN THE SOFTWARE.
30 (defpackage #:cffi-sys
31 (:use
#:common-lisp
#:sb-alien
#:cffi-utils
#:alexandria
)
33 #:canonicalize-symbol-name-case
44 #:with-foreign-pointer
46 #:%foreign-funcall-pointer
47 #:%foreign-type-alignment
49 #:%load-foreign-library
50 #:%close-foreign-library
54 #:make-shareable-byte-vector
55 #:with-pointer-to-vector-data
56 #:%foreign-symbol-pointer
60 (in-package #:cffi-sys
)
64 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
65 (mapc (lambda (feature) (pushnew feature
*features
*))
66 '(cffi-features:flat-namespace
)))
70 (declaim (inline canonicalize-symbol-name-case
))
71 (defun canonicalize-symbol-name-case (name)
72 (declare (string name
))
75 ;;;# Basic Pointer Operations
77 (deftype foreign-pointer
()
78 'sb-sys
:system-area-pointer
)
80 (declaim (inline pointerp
))
82 "Return true if PTR is a foreign pointer."
83 (sb-sys:system-area-pointer-p ptr
))
85 (declaim (inline pointer-eq
))
86 (defun pointer-eq (ptr1 ptr2
)
87 "Return true if PTR1 and PTR2 point to the same address."
88 (declare (type system-area-pointer ptr1 ptr2
))
89 (sb-sys:sap
= ptr1 ptr2
))
91 (declaim (inline null-pointer
))
92 (defun null-pointer ()
93 "Construct and return a null pointer."
96 (declaim (inline null-pointer-p
))
97 (defun null-pointer-p (ptr)
98 "Return true if PTR is a null pointer."
99 (declare (type system-area-pointer ptr
))
100 (zerop (sb-sys:sap-int ptr
)))
102 (declaim (inline inc-pointer
))
103 (defun inc-pointer (ptr offset
)
104 "Return a pointer pointing OFFSET bytes past PTR."
105 (declare (type system-area-pointer ptr
)
106 (type integer offset
))
107 (sb-sys:sap
+ ptr offset
))
109 (declaim (inline make-pointer
))
110 (defun make-pointer (address)
111 "Return a pointer pointing to ADDRESS."
112 ;; (declare (type (unsigned-byte 32) address))
113 (sb-sys:int-sap address
))
115 (declaim (inline pointer-address
))
116 (defun pointer-address (ptr)
117 "Return the address pointed to by PTR."
118 (declare (type system-area-pointer ptr
))
119 (sb-sys:sap-int ptr
))
123 ;;; Functions and macros for allocating foreign memory on the stack
124 ;;; and on the heap. The main CFFI package defines macros that wrap
125 ;;; FOREIGN-ALLOC and FOREIGN-FREE in UNWIND-PROTECT for the common usage
126 ;;; when the memory has dynamic extent.
128 (declaim (inline %foreign-alloc
))
129 (defun %foreign-alloc
(size)
130 "Allocate SIZE bytes on the heap and return a pointer."
131 ;; (declare (type (unsigned-byte 32) size))
132 (alien-sap (make-alien (unsigned 8) size
)))
134 (declaim (inline foreign-free
))
135 (defun foreign-free (ptr)
136 "Free a PTR allocated by FOREIGN-ALLOC."
137 (declare (type system-area-pointer ptr
))
138 (free-alien (sap-alien ptr
(* (unsigned 8)))))
140 (defmacro with-foreign-pointer
((var size
&optional size-var
) &body body
)
141 "Bind VAR to SIZE bytes of foreign memory during BODY. The
142 pointer in VAR is invalid beyond the dynamic extent of BODY, and
143 may be stack-allocated if supported by the implementation. If
144 SIZE-VAR is supplied, it will be bound to SIZE during BODY."
146 (setf size-var
(gensym "SIZE")))
147 ;; If the size is constant we can stack-allocate.
149 (let ((alien-var (gensym "ALIEN")))
150 `(with-alien ((,alien-var
(array (unsigned 8) ,(eval size
))))
151 (let ((,size-var
,(eval size
))
152 (,var
(alien-sap ,alien-var
)))
153 (declare (ignorable ,size-var
))
155 `(let* ((,size-var
,size
)
156 (,var
(%foreign-alloc
,size-var
)))
159 (foreign-free ,var
)))))
161 ;;;# Shareable Vectors
163 ;;; This interface is very experimental. WITH-POINTER-TO-VECTOR-DATA
164 ;;; should be defined to perform a copy-in/copy-out if the Lisp
165 ;;; implementation can't do this.
167 (declaim (inline make-shareable-byte-vector
))
168 (defun make-shareable-byte-vector (size)
169 "Create a Lisp vector of SIZE bytes can passed to
170 WITH-POINTER-TO-VECTOR-DATA."
171 ; (declare (type sb-int:index size))
172 (make-array size
:element-type
'(unsigned-byte 8)))
174 (defmacro with-pointer-to-vector-data
((ptr-var vector
) &body body
)
175 "Bind PTR-VAR to a foreign pointer to the data in VECTOR."
176 (let ((vector-var (gensym "VECTOR")))
177 `(let ((,vector-var
,vector
))
178 (declare (type (sb-kernel:simple-unboxed-array
(*)) ,vector-var
))
179 (sb-sys:with-pinned-objects
(,vector-var
)
180 (let ((,ptr-var
(sb-sys:vector-sap
,vector-var
)))
185 ;;; Define the %MEM-REF and %MEM-SET functions, as well as compiler
186 ;;; macros that optimize the case where the type keyword is constant
188 (defmacro define-mem-accessors
(&body pairs
)
190 (defun %mem-ref
(ptr type
&optional
(offset 0))
192 ,@(loop for
(keyword fn
) in pairs
193 collect
`(,keyword
(,fn ptr offset
)))))
194 (defun %mem-set
(value ptr type
&optional
(offset 0))
196 ,@(loop for
(keyword fn
) in pairs
197 collect
`(,keyword
(setf (,fn ptr offset
) value
)))))
198 (define-compiler-macro %mem-ref
199 (&whole form ptr type
&optional
(offset 0))
202 ,@(loop for
(keyword fn
) in pairs
203 collect
`(,keyword
`(,',fn
,ptr
,offset
))))
205 (define-compiler-macro %mem-set
206 (&whole form value ptr type
&optional
(offset 0))
210 ,@(loop for
(keyword fn
) in pairs
211 collect
`(,keyword
`(setf (,',fn
,ptr
,offset
)
215 (define-mem-accessors
216 (:char sb-sys
:signed-sap-ref-8
)
217 (:unsigned-char sb-sys
:sap-ref-8
)
218 (:short sb-sys
:signed-sap-ref-16
)
219 (:unsigned-short sb-sys
:sap-ref-16
)
220 (:int sb-sys
:signed-sap-ref-32
)
221 (:unsigned-int sb-sys
:sap-ref-32
)
222 (:long sb-sys
:signed-sap-ref-word
)
223 (:unsigned-long sb-sys
:sap-ref-word
)
224 (:long-long sb-sys
:signed-sap-ref-64
)
225 (:unsigned-long-long sb-sys
:sap-ref-64
)
226 (:float sb-sys
:sap-ref-single
)
227 (:double sb-sys
:sap-ref-double
)
228 (:pointer sb-sys
:sap-ref-sap
))
230 ;;;# Calling Foreign Functions
232 (defun convert-foreign-type (type-keyword)
233 "Convert a CFFI type keyword to an SB-ALIEN type."
236 (:unsigned-char
'unsigned-char
)
238 (:unsigned-short
'unsigned-short
)
240 (:unsigned-int
'unsigned-int
)
242 (:unsigned-long
'unsigned-long
)
243 (:long-long
'long-long
)
244 (:unsigned-long-long
'unsigned-long-long
)
245 (:float
'single-float
)
246 (:double
'double-float
)
247 (:pointer
'system-area-pointer
)
250 (defun %foreign-type-size
(type-keyword)
251 "Return the size in bytes of a foreign type."
252 (/ (sb-alien-internals:alien-type-bits
253 (sb-alien-internals:parse-alien-type
254 (convert-foreign-type type-keyword
) nil
)) 8))
256 (defun %foreign-type-alignment
(type-keyword)
257 "Return the alignment in bytes of a foreign type."
258 #+(and darwin ppc
(not ppc64
))
260 ((:double
:long-long
:unsigned-long-long
)
261 (return-from %foreign-type-alignment
8)))
262 ;; No override necessary for other types...
263 (/ (sb-alien-internals:alien-type-alignment
264 (sb-alien-internals:parse-alien-type
265 (convert-foreign-type type-keyword
) nil
)) 8))
267 (defun foreign-funcall-type-and-args (args)
268 "Return an SB-ALIEN function type for ARGS."
269 (let ((return-type 'void
))
270 (loop for
(type arg
) on args by
#'cddr
271 if arg collect
(convert-foreign-type type
) into types
272 and collect arg into fargs
273 else do
(setf return-type
(convert-foreign-type type
))
274 finally
(return (values types fargs return-type
)))))
276 (defmacro %%foreign-funcall
(name types fargs rettype
)
277 "Internal guts of %FOREIGN-FUNCALL."
279 (extern-alien ,name
(function ,rettype
,@types
))
282 (defmacro %foreign-funcall
(name args
&key library calling-convention
)
283 "Perform a foreign function call, document it more later."
284 (declare (ignore library calling-convention
))
285 (multiple-value-bind (types fargs rettype
)
286 (foreign-funcall-type-and-args args
)
287 `(%%foreign-funcall
,name
,types
,fargs
,rettype
)))
289 (defmacro %foreign-funcall-pointer
(ptr args
&key calling-convention
)
290 "Funcall a pointer to a foreign function."
291 (declare (ignore calling-convention
))
292 (multiple-value-bind (types fargs rettype
)
293 (foreign-funcall-type-and-args args
)
294 (with-unique-names (function)
295 `(with-alien ((,function
(* (function ,rettype
,@types
)) ,ptr
))
296 (alien-funcall ,function
,@fargs
)))))
300 ;;; The *CALLBACKS* hash table contains a direct mapping of CFFI
301 ;;; callback names to SYSTEM-AREA-POINTERs obtained by ALIEN-LAMBDA.
302 ;;; SBCL will maintain the addresses of the callbacks across saved
303 ;;; images, so it is safe to store the pointers directly.
304 (defvar *callbacks
* (make-hash-table))
306 (defmacro %defcallback
(name rettype arg-names arg-types body
307 &key calling-convention
)
308 (declare (ignore calling-convention
))
309 `(setf (gethash ',name
*callbacks
*)
311 (sb-alien::alien-lambda
,(convert-foreign-type rettype
)
312 ,(mapcar (lambda (sym type
)
313 (list sym
(convert-foreign-type type
)))
317 (defun %callback
(name)
318 (or (gethash name
*callbacks
*)
319 (error "Undefined callback: ~S" name
)))
321 ;;;# Loading and Closing Foreign Libraries
323 (declaim (inline %load-foreign-library
))
324 (defun %load-foreign-library
(name path
)
325 "Load a foreign library."
326 (declare (ignore name
))
327 (load-shared-object path
))
329 (defun %close-foreign-library
(handle)
330 "Closes a foreign library."
331 (sb-alien::dlclose-or-lose
332 (find (sb-ext:native-namestring handle
) sb-alien
::*shared-objects
*
333 :key
#'sb-alien
::shared-object-file
336 (defun native-namestring (pathname)
337 (sb-ext:native-namestring pathname
))
341 (defun %foreign-symbol-pointer
(name library
)
342 "Returns a pointer to a foreign symbol NAME."
343 (declare (ignore library
))
344 (when-let (address (sb-sys:find-foreign-symbol-address name
))
345 (sb-sys:int-sap address
)))