1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; cffi-lispworks.lisp --- Lispworks CFFI-SYS implementation.
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
#:cl
#: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
57 #:defcfun-helper-forms
61 (in-package #:cffi-sys
)
65 #-lispworks-64bit
(pushnew 'no-long-long
*features
*)
69 (defun canonicalize-symbol-name-case (name)
70 (declare (string name
))
73 ;;;# Basic Pointer Operations
75 (deftype foreign-pointer
()
79 "Return true if PTR is a foreign pointer."
82 (defun pointer-eq (ptr1 ptr2
)
83 "Return true if PTR1 and PTR2 point to the same address."
84 (fli:pointer-eq ptr1 ptr2
))
86 ;; We use FLI:MAKE-POINTER here instead of FLI:*NULL-POINTER* since old
87 ;; versions of Lispworks don't seem to have it.
88 (defun null-pointer ()
89 "Return a null foreign pointer."
90 (fli:make-pointer
:address
0 :type
:void
))
92 (defun null-pointer-p (ptr)
93 "Return true if PTR is a null pointer."
94 (check-type ptr fli
::pointer
)
95 (fli:null-pointer-p ptr
))
97 ;; FLI:INCF-POINTER won't work on FLI pointers to :void so we
98 ;; increment "manually."
99 (defun inc-pointer (ptr offset
)
100 "Return a pointer OFFSET bytes past PTR."
101 (fli:make-pointer
:type
:void
:address
(+ (fli:pointer-address ptr
) offset
)))
103 (defun make-pointer (address)
104 "Return a pointer pointing to ADDRESS."
105 (fli:make-pointer
:type
:void
:address address
))
107 (defun pointer-address (ptr)
108 "Return the address pointed to by PTR."
109 (fli:pointer-address ptr
))
113 (defun %foreign-alloc
(size)
114 "Allocate SIZE bytes of memory and return a pointer."
115 (fli:allocate-foreign-object
:type
:byte
:nelems size
))
117 (defun foreign-free (ptr)
118 "Free a pointer PTR allocated by FOREIGN-ALLOC."
119 (fli:free-foreign-object ptr
))
121 (defmacro with-foreign-pointer
((var size
&optional size-var
) &body body
)
122 "Bind VAR to SIZE bytes of foreign memory during BODY. Both the
123 pointer in VAR and the memory it points to have dynamic extent and may
124 be stack allocated if supported by the implementation."
126 (setf size-var
(gensym "SIZE")))
127 `(fli:with-dynamic-foreign-objects
()
128 (let* ((,size-var
,size
)
129 (,var
(fli:alloca
:type
:byte
:nelems
,size-var
)))
132 ;;;# Shareable Vectors
134 (defun make-shareable-byte-vector (size)
135 "Create a shareable byte vector."
136 #+(or lispworks3 lispworks4 lispworks5.0
)
138 (make-array size
:element-type
'(unsigned-byte 8)))
139 #-
(or lispworks3 lispworks4 lispworks5.0
)
140 (make-array size
:element-type
'(unsigned-byte 8) :allocation
:static
))
142 (defmacro with-pointer-to-vector-data
((ptr-var vector
) &body body
)
143 "Bind PTR-VAR to a pointer at the data in VECTOR."
144 `(fli:with-dynamic-lisp-array-pointer
(,ptr-var
,vector
)
149 (defun convert-foreign-type (cffi-type)
150 "Convert a CFFI type keyword to an FLI type."
153 (:unsigned-char
'(:unsigned
:byte
))
155 (:unsigned-short
'(:unsigned
:short
))
157 (:unsigned-int
'(:unsigned
:int
))
159 (:unsigned-long
'(:unsigned
:long
))
160 ;; On 32-bit platforms, Lispworks 5.0+ supports long-long for
161 ;; DEFCFUN and FOREIGN-FUNCALL.
162 (:long-long
'(:long
:long
))
163 (:unsigned-long-long
'(:unsigned
:long
:long
))
169 ;;; Convert a CFFI type keyword to a symbol suitable for passing to
170 ;;; FLI:FOREIGN-TYPED-AREF.
171 #+#.
(cl:if
(cl:find-symbol
"FOREIGN-TYPED-AREF" "FLI") '(and) '(or))
172 (defun convert-foreign-typed-aref-type (cffi-type)
174 ((:char
:short
:int
:long
#+lispworks-64bit
:long-long
)
175 `(signed-byte ,(* 8 (%foreign-type-size cffi-type
))))
176 ((:unsigned-char
:unsigned-short
:unsigned-int
:unsigned-long
177 #+lispworks-64bit
:unsigned-long-long
)
178 `(unsigned-byte ,(* 8 (%foreign-type-size cffi-type
))))
179 (:float
'single-float
)
180 (:double
'double-float
)))
182 (defun %mem-ref
(ptr type
&optional
(offset 0))
183 "Dereference an object of type TYPE OFFSET bytes from PTR."
184 (unless (zerop offset
)
185 (setf ptr
(inc-pointer ptr offset
)))
186 (fli:dereference ptr
:type
(convert-foreign-type type
)))
188 ;; Lispworks 5.0 on 64-bit platforms doesn't have [u]int64 support in
189 ;; FOREIGN-TYPED-AREF. That was implemented in 5.1.
190 #+(and lispworks-64bit lispworks5.0
)
191 (defun 64-bit-type-p (type)
192 (member type
'(:long
:unsigned-long
:long-long
:unsigned-long-long
)))
194 ;;; In LispWorks versions where FLI:FOREIGN-TYPED-AREF is fbound, use
195 ;;; it instead of FLI:DEREFERENCE in the optimizer for %MEM-REF.
196 #+#.
(cl:if
(cl:find-symbol
"FOREIGN-TYPED-AREF" "FLI") '(and) '(or))
197 (define-compiler-macro %mem-ref
(&whole form ptr type
&optional
(off 0))
199 (let ((type (eval type
)))
200 (if (or #+(and lispworks-64bit lispworks5.0
) (64-bit-type-p type
)
202 (let ((fli-type (convert-foreign-type type
))
203 (ptr-form (if (eql off
0) ptr
`(inc-pointer ,ptr
,off
))))
204 `(fli:dereference
,ptr-form
:type
',fli-type
))
205 (let ((lisp-type (convert-foreign-typed-aref-type type
)))
207 (declare (optimize (speed 3) (safety 0)))
208 (fli:foreign-typed-aref
',lisp-type
,ptr
(the fixnum
,off
))))))
211 ;;; Open-code the call to FLI:DEREFERENCE when TYPE is constant at
212 ;;; macroexpansion time, when FLI:FOREIGN-TYPED-AREF is not available.
213 #-
#.
(cl:if
(cl:find-symbol
"FOREIGN-TYPED-AREF" "FLI") '(and) '(or))
214 (define-compiler-macro %mem-ref
(&whole form ptr type
&optional
(off 0))
216 (let ((ptr-form (if (eql off
0) ptr
`(inc-pointer ,ptr
,off
)))
217 (type (convert-foreign-type (eval type
))))
218 `(fli:dereference
,ptr-form
:type
',type
))
221 (defun %mem-set
(value ptr type
&optional
(offset 0))
222 "Set the object of TYPE at OFFSET bytes from PTR."
223 (unless (zerop offset
)
224 (setf ptr
(inc-pointer ptr offset
)))
225 (setf (fli:dereference ptr
:type
(convert-foreign-type type
)) value
))
227 ;;; In LispWorks versions where FLI:FOREIGN-TYPED-AREF is fbound, use
228 ;;; it instead of FLI:DEREFERENCE in the optimizer for %MEM-SET.
229 #+#.
(cl:if
(cl:find-symbol
"FOREIGN-TYPED-AREF" "FLI") '(and) '(or))
230 (define-compiler-macro %mem-set
(&whole form val ptr type
&optional
(off 0))
233 (let ((type (eval type
)))
234 (if (or #+(and lispworks-64bit lispworks5.0
) (64-bit-type-p type
)
236 (let ((fli-type (convert-foreign-type type
))
237 (ptr-form (if (eql off
0) ptr
`(inc-pointer ,ptr
,off
))))
238 `(setf (fli:dereference
,ptr-form
:type
',fli-type
) ,val
))
239 (let ((lisp-type (convert-foreign-typed-aref-type type
)))
241 (declare (optimize (speed 3) (safety 0)))
242 (setf (fli:foreign-typed-aref
',lisp-type
,ptr
247 ;;; Open-code the call to (SETF FLI:DEREFERENCE) when TYPE is constant
248 ;;; at macroexpansion time.
249 #-
#.
(cl:if
(cl:find-symbol
"FOREIGN-TYPED-AREF" "FLI") '(and) '(or))
250 (define-compiler-macro %mem-set
(&whole form val ptr type
&optional
(off 0))
253 (let ((ptr-form (if (eql off
0) ptr
`(inc-pointer ,ptr
,off
)))
254 (type (convert-foreign-type (eval type
))))
255 `(setf (fli:dereference
,ptr-form
:type
',type
) ,val
)))
258 ;;;# Foreign Type Operations
260 (defun %foreign-type-size
(type)
261 "Return the size in bytes of a foreign type."
262 (fli:size-of
(convert-foreign-type type
)))
264 (defun %foreign-type-alignment
(type)
265 "Return the structure alignment in bytes of foreign type."
266 #+(and darwin harp
::powerpc
)
267 (when (eq type
:double
)
268 (return-from %foreign-type-alignment
8))
269 ;; Override not necessary for the remaining types...
270 (fli:align-of
(convert-foreign-type type
)))
272 ;;;# Calling Foreign Functions
274 (defvar *foreign-funcallable-cache
* (make-hash-table :test
'equal
)
275 "Caches foreign funcallables created by %FOREIGN-FUNCALL or
276 %FOREIGN-FUNCALL-POINTER. We only need to have one per each
279 (defun foreign-funcall-type-and-args (args)
280 "Returns a list of types, list of args and return type."
281 (let ((return-type :void
))
282 (loop for
(type arg
) on args by
#'cddr
283 if arg collect
(convert-foreign-type type
) into types
284 and collect arg into fargs
285 else do
(setf return-type
(convert-foreign-type type
))
286 finally
(return (values types fargs return-type
)))))
288 (defun create-foreign-funcallable (types rettype convention
)
289 "Creates a foreign funcallable for the signature TYPES -> RETTYPE."
290 #+mac
(declare (ignore convention
))
291 (format t
"~&Creating foreign funcallable for signature ~S -> ~S~%"
293 ;; yes, ugly, this most likely wants to be a top-level form...
294 (let ((internal-name (gensym)))
298 (fli:define-foreign-funcallable
,internal-name
299 ,(loop for type in types
300 collect
(list (gensym) type
))
301 :result-type
,rettype
303 ;; avoid warning about cdecl not being supported on mac
304 #-mac
,@(list :calling-convention convention
)))))
307 (defun get-foreign-funcallable (types rettype convention
)
308 "Returns a foreign funcallable for the signature TYPES -> RETTYPE -
309 either from the cache or newly created."
310 (let ((signature (cons rettype types
)))
311 (or (gethash signature
*foreign-funcallable-cache
*)
312 ;; (SETF GETHASH) is supposed to be thread-safe
313 (setf (gethash signature
*foreign-funcallable-cache
*)
314 (create-foreign-funcallable types rettype convention
)))))
316 (defmacro %%foreign-funcall
(foreign-function args convention
)
317 "Does the actual work for %FOREIGN-FUNCALL-POINTER and %FOREIGN-FUNCALL.
318 Checks if a foreign funcallable which fits ARGS already exists and creates
319 and caches it if necessary. Finally calls it."
320 (multiple-value-bind (types fargs rettype
)
321 (foreign-funcall-type-and-args args
)
322 `(funcall (load-time-value
323 (get-foreign-funcallable ',types
',rettype
',convention
))
324 ,foreign-function
,@fargs
)))
326 (defmacro %foreign-funcall
(name args
&key library convention
)
327 "Calls a foreign function named NAME passing arguments ARGS."
329 (fli:make-pointer
:symbol-name
,name
330 :module
',(if (eq library
:default
) nil library
))
333 (defmacro %foreign-funcall-pointer
(ptr args
&key convention
)
334 "Calls a foreign function pointed at by PTR passing arguments ARGS."
335 `(%%foreign-funcall
,ptr
,args
,convention
))
337 (defun defcfun-helper-forms (name lisp-name rettype args types options
)
338 "Return 2 values for DEFCFUN. A prelude form and a caller form."
339 (let ((ff-name (intern (format nil
"%cffi-foreign-function/~A" lisp-name
))))
341 `(fli:define-foreign-function
(,ff-name
,name
:source
)
342 ,(mapcar (lambda (ty) (list (gensym) (convert-foreign-type ty
)))
344 :result-type
,(convert-foreign-type rettype
)
346 :module
',(let ((lib (getf options
:library
)))
347 (if (eq lib
:default
) nil lib
))
348 ;; avoid warning about cdecl not being supported on mac platforms
349 #-mac
,@(list :calling-convention
(getf options
:convention
)))
350 `(,ff-name
,@args
))))
354 (defvar *callbacks
* (make-hash-table))
356 ;;; Create a package to contain the symbols for callback functions. We
357 ;;; want to redefine callbacks with the same symbol so the internal data
358 ;;; structures are reused.
359 (defpackage #:cffi-callbacks
362 ;;; Intern a symbol in the CFFI-CALLBACKS package used to name the internal
363 ;;; callback for NAME.
364 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
365 (defun intern-callback (name)
366 (intern (format nil
"~A::~A"
367 (if-let (package (symbol-package name
))
368 (package-name package
)
373 (defmacro %defcallback
(name rettype arg-names arg-types body
375 (let ((cb-name (intern-callback name
)))
377 (fli:define-foreign-callable
378 (,cb-name
:encode
:lisp
379 :result-type
,(convert-foreign-type rettype
)
380 :calling-convention
,convention
383 ,(mapcar (lambda (sym type
)
384 (list sym
(convert-foreign-type type
)))
387 (setf (gethash ',name
*callbacks
*) ',cb-name
))))
389 (defun %callback
(name)
390 (multiple-value-bind (symbol winp
)
391 (gethash name
*callbacks
*)
393 (error "Undefined callback: ~S" name
))
394 (fli:make-pointer
:symbol-name symbol
:module
:callbacks
)))
396 ;;;# Loading Foreign Libraries
398 (defun %load-foreign-library
(name path
)
399 "Load the foreign library NAME."
400 (fli:register-module
(or name path
) :connection-style
:immediate
403 (defun %close-foreign-library
(name)
404 "Close the foreign library NAME."
405 (fli:disconnect-module name
:remove t
))
407 (defun native-namestring (pathname)
408 (namestring pathname
))
412 (defun %foreign-symbol-pointer
(name library
)
413 "Returns a pointer to a foreign symbol NAME."
416 (fli:make-pointer
:symbol-name name
:type
:void
417 :module
(if (eq library
:default
) nil library
)))))