1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; cffi-openmcl.lisp --- CFFI-SYS implementation for OpenMCL.
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
#:ccl
)
32 (:import-from
#:alexandria
#:once-only
#:if-let
)
34 #:canonicalize-symbol-name-case
36 #:pointerp
; ccl:pointerp
40 #:with-foreign-pointer
49 #:%foreign-funcall-pointer
50 #:%foreign-type-alignment
52 #:%load-foreign-library
53 #:%close-foreign-library
55 #:make-shareable-byte-vector
56 #:with-pointer-to-vector-data
57 #:%foreign-symbol-pointer
61 (in-package #:cffi-sys
)
65 (pushnew 'flat-namespace
*features
*)
69 (defun canonicalize-symbol-name-case (name)
70 (declare (string name
))
75 ;;; Functions and macros for allocating foreign memory on the stack
76 ;;; and on the heap. The main CFFI package defines macros that wrap
77 ;;; FOREIGN-ALLOC and FOREIGN-FREE in UNWIND-PROTECT for the common
78 ;;; usage when the memory has dynamic extent.
80 (defun %foreign-alloc
(size)
81 "Allocate SIZE bytes on the heap and return a pointer."
84 (defun foreign-free (ptr)
85 "Free a PTR allocated by FOREIGN-ALLOC."
86 ;; TODO: Should we make this a dead macptr?
89 (defmacro with-foreign-pointer
((var size
&optional size-var
) &body body
)
90 "Bind VAR to SIZE bytes of foreign memory during BODY. The
91 pointer in VAR is invalid beyond the dynamic extent of BODY, and
92 may be stack-allocated if supported by the implementation. If
93 SIZE-VAR is supplied, it will be bound to SIZE during BODY."
95 (setf size-var
(gensym "SIZE")))
96 `(let ((,size-var
,size
))
97 (%stack-block
((,var
,size-var
))
100 ;;;# Misc. Pointer Operations
102 (deftype foreign-pointer
()
105 (defun null-pointer ()
106 "Construct and return a null pointer."
109 (defun null-pointer-p (ptr)
110 "Return true if PTR is a null pointer."
111 (ccl:%null-ptr-p ptr
))
113 (defun inc-pointer (ptr offset
)
114 "Return a pointer OFFSET bytes past PTR."
115 (ccl:%inc-ptr ptr offset
))
117 (defun pointer-eq (ptr1 ptr2
)
118 "Return true if PTR1 and PTR2 point to the same address."
119 (ccl:%ptr-eql ptr1 ptr2
))
121 (defun make-pointer (address)
122 "Return a pointer pointing to ADDRESS."
123 (ccl:%int-to-ptr address
))
125 (defun pointer-address (ptr)
126 "Return the address pointed to by PTR."
127 (ccl:%ptr-to-int ptr
))
129 ;;;# Shareable Vectors
131 ;;; This interface is very experimental. WITH-POINTER-TO-VECTOR-DATA
132 ;;; should be defined to perform a copy-in/copy-out if the Lisp
133 ;;; implementation can't do this.
135 (defun make-shareable-byte-vector (size)
136 "Create a Lisp vector of SIZE bytes that can passed to
137 WITH-POINTER-TO-VECTOR-DATA."
138 (make-array size
:element-type
'(unsigned-byte 8)))
140 (defmacro with-pointer-to-vector-data
((ptr-var vector
) &body body
)
141 "Bind PTR-VAR to a foreign pointer to the data in VECTOR."
142 `(ccl:with-pointer-to-ivector
(,ptr-var
,vector
)
147 ;;; Define the %MEM-REF and %MEM-SET functions, as well as compiler
148 ;;; macros that optimize the case where the type keyword is constant
150 (defmacro define-mem-accessors
(&body pairs
)
152 (defun %mem-ref
(ptr type
&optional
(offset 0))
154 ,@(loop for
(keyword fn
) in pairs
155 collect
`(,keyword
(,fn ptr offset
)))))
156 (defun %mem-set
(value ptr type
&optional
(offset 0))
158 ,@(loop for
(keyword fn
) in pairs
159 collect
`(,keyword
(setf (,fn ptr offset
) value
)))))
160 (define-compiler-macro %mem-ref
161 (&whole form ptr type
&optional
(offset 0))
164 ,@(loop for
(keyword fn
) in pairs
165 collect
`(,keyword
`(,',fn
,ptr
,offset
))))
167 (define-compiler-macro %mem-set
168 (&whole form value ptr type
&optional
(offset 0))
172 ,@(loop for
(keyword fn
) in pairs
173 collect
`(,keyword
`(setf (,',fn
,ptr
,offset
)
177 (define-mem-accessors
178 (:char %get-signed-byte
)
179 (:unsigned-char %get-unsigned-byte
)
180 (:short %get-signed-word
)
181 (:unsigned-short %get-unsigned-word
)
182 (:int %get-signed-long
)
183 (:unsigned-int %get-unsigned-long
)
184 #+(or 32-bit-target windows-target
) (:long %get-signed-long
)
185 #+(and (not windows-target
) 64-bit-target
) (:long ccl
::%%get-signed-longlong
)
186 #+(or 32-bit-target windows-target
) (:unsigned-long %get-unsigned-long
)
187 #+(and 64-bit-target
(not windows-target
)) (:unsigned-long ccl
::%%get-unsigned-longlong
)
188 (:long-long ccl
::%get-signed-long-long
)
189 (:unsigned-long-long ccl
::%get-unsigned-long-long
)
190 (:float %get-single-float
)
191 (:double %get-double-float
)
194 ;;;# Calling Foreign Functions
196 (defun convert-foreign-type (type-keyword)
197 "Convert a CFFI type keyword to an OpenMCL type."
200 (:unsigned-char
:unsigned-byte
)
201 (:short
:signed-short
)
202 (:unsigned-short
:unsigned-short
)
204 (:unsigned-int
:unsigned-int
)
206 (:unsigned-long
:unsigned-long
)
207 (:long-long
:signed-doubleword
)
208 (:unsigned-long-long
:unsigned-doubleword
)
209 (:float
:single-float
)
210 (:double
:double-float
)
214 (defun %foreign-type-size
(type-keyword)
215 "Return the size in bytes of a foreign type."
216 (/ (ccl::foreign-type-bits
217 (ccl::parse-foreign-type
218 (convert-foreign-type type-keyword
)))
221 ;; There be dragons here. See the following thread for details:
222 ;; http://clozure.com/pipermail/openmcl-devel/2005-June/002777.html
223 (defun %foreign-type-alignment
(type-keyword)
224 "Return the alignment in bytes of a foreign type."
225 (/ (ccl::foreign-type-alignment
226 (ccl::parse-foreign-type
227 (convert-foreign-type type-keyword
))) 8))
229 (defun convert-foreign-funcall-types (args)
230 "Convert foreign types for a call to FOREIGN-FUNCALL."
231 (loop for
(type arg
) on args by
#'cddr
232 collect
(convert-foreign-type type
)
235 (defun convert-external-name (name)
236 "Add an underscore to NAME if necessary for the ABI."
237 #+darwin
(concatenate 'string
"_" name
)
240 (defmacro %foreign-funcall
(function-name args
&key library convention
)
241 "Perform a foreign function call, document it more later."
242 (declare (ignore library convention
))
244 ,(convert-external-name function-name
)
245 ,@(convert-foreign-funcall-types args
)))
247 (defmacro %foreign-funcall-pointer
(ptr args
&key convention
)
248 (declare (ignore convention
))
249 `(ff-call ,ptr
,@(convert-foreign-funcall-types args
)))
253 ;;; The *CALLBACKS* hash table maps CFFI callback names to OpenMCL "macptr"
254 ;;; entry points. It is safe to store the pointers directly because
255 ;;; OpenMCL will update the address of these pointers when a saved image
256 ;;; is loaded (see CCL::RESTORE-PASCAL-FUNCTIONS).
257 (defvar *callbacks
* (make-hash-table))
259 ;;; Create a package to contain the symbols for callback functions. We
260 ;;; want to redefine callbacks with the same symbol so the internal data
261 ;;; structures are reused.
262 (defpackage #:cffi-callbacks
265 ;;; Intern a symbol in the CFFI-CALLBACKS package used to name the internal
266 ;;; callback for NAME.
267 (defun intern-callback (name)
268 (intern (format nil
"~A::~A"
269 (if-let (package (symbol-package name
))
270 (package-name package
)
275 (defmacro %defcallback
(name rettype arg-names arg-types body
277 (let ((cb-name (intern-callback name
)))
279 (defcallback ,cb-name
280 (,@(when (eq convention
:stdcall
)
281 '(:discard-stack-args
))
282 ,@(mapcan (lambda (sym type
)
283 (list (convert-foreign-type type
) sym
))
285 ,(convert-foreign-type rettype
))
287 (setf (gethash ',name
*callbacks
*) (symbol-value ',cb-name
)))))
289 (defun %callback
(name)
290 (or (gethash name
*callbacks
*)
291 (error "Undefined callback: ~S" name
)))
293 ;;;# Loading Foreign Libraries
295 (defun %load-foreign-library
(name path
)
296 "Load the foreign library NAME."
297 (declare (ignore name
))
298 (open-shared-library path
))
300 (defun %close-foreign-library
(name)
301 "Close the foreign library NAME."
302 ;; C-S-L sometimes ends in an endless loop
303 ;; with :COMPLETELY T
304 (close-shared-library name
:completely nil
))
306 (defun native-namestring (pathname)
307 (ccl::native-translated-namestring pathname
))
311 (defun %foreign-symbol-pointer
(name library
)
312 "Returns a pointer to a foreign symbol NAME."
313 (declare (ignore library
))
314 (foreign-symbol-address (convert-external-name name
)))