examples cleaned up
[CommonLispStat.git] / external / cffi.darcs / src / cffi-ecl.lisp
blobf538a4fc4edf59ed523ca2c41c99b7948329f424
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; cffi-ecl.lisp --- ECL backend for CFFI.
4 ;;;
5 ;;; Copyright (C) 2005-2006, James Bielman <jamesjb@jamesjb.com>
6 ;;;
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:
14 ;;;
15 ;;; The above copyright notice and this permission notice shall be
16 ;;; included in all copies or substantial portions of the Software.
17 ;;;
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.
26 ;;;
28 ;;;# Administrivia
30 (defpackage #:cffi-sys
31 (:use #:common-lisp #:cffi-utils #:alexandria)
32 (:export
33 #:canonicalize-symbol-name-case
34 #:foreign-pointer
35 #:pointerp
36 #:pointer-eq
37 #:%foreign-alloc
38 #:foreign-free
39 #:with-foreign-pointer
40 #:null-pointer
41 #:null-pointer-p
42 #:inc-pointer
43 #:make-pointer
44 #:pointer-address
45 #:%mem-ref
46 #:%mem-set
47 #:%foreign-funcall
48 #:%foreign-funcall-pointer
49 #:%foreign-type-alignment
50 #:%foreign-type-size
51 #:%load-foreign-library
52 #:native-namestring
53 #:make-shareable-byte-vector
54 #:with-pointer-to-vector-data
55 #:%defcallback
56 #:%callback
57 #:%foreign-symbol-pointer))
59 (in-package #:cffi-sys)
61 ;;;# Mis-features
63 (eval-when (:compile-toplevel :load-toplevel :execute)
64 (mapc (lambda (feature) (pushnew feature *features*))
65 '(cffi-features:no-long-long
66 cffi-features:flat-namespace)))
68 ;;;# Symbol Case
70 (defun canonicalize-symbol-name-case (name)
71 (declare (string name))
72 (string-upcase name))
74 ;;;# Allocation
76 (defun %foreign-alloc (size)
77 "Allocate SIZE bytes of foreign-addressable memory."
78 (si:allocate-foreign-data :void size))
80 (defun foreign-free (ptr)
81 "Free a pointer PTR allocated by FOREIGN-ALLOC."
82 (si:free-foreign-data ptr))
84 (defmacro with-foreign-pointer ((var size &optional size-var) &body body)
85 "Bind VAR to SIZE bytes of foreign memory during BODY. The
86 pointer in VAR is invalid beyond the dynamic extent of BODY, and
87 may be stack-allocated if supported by the implementation. If
88 SIZE-VAR is supplied, it will be bound to SIZE during BODY."
89 (unless size-var
90 (setf size-var (gensym "SIZE")))
91 `(let* ((,size-var ,size)
92 (,var (%foreign-alloc ,size-var)))
93 (unwind-protect
94 (progn ,@body)
95 (foreign-free ,var))))
97 ;;;# Misc. Pointer Operations
99 (deftype foreign-pointer ()
100 'si:foreign-data)
102 (defun null-pointer ()
103 "Construct and return a null pointer."
104 (si:allocate-foreign-data :void 0))
106 (defun null-pointer-p (ptr)
107 "Return true if PTR is a null pointer."
108 (si:null-pointer-p ptr))
110 (defun inc-pointer (ptr offset)
111 "Return a pointer OFFSET bytes past PTR."
112 (ffi:make-pointer (+ (ffi:pointer-address ptr) offset) :void))
114 (defun pointerp (ptr)
115 "Return true if PTR is a foreign pointer."
116 (typep ptr 'si:foreign-data))
118 (defun pointer-eq (ptr1 ptr2)
119 "Return true if PTR1 and PTR2 point to the same address."
120 (= (ffi:pointer-address ptr1) (ffi:pointer-address ptr2)))
122 (defun make-pointer (address)
123 "Return a pointer pointing to ADDRESS."
124 (ffi:make-pointer address :void))
126 (defun pointer-address (ptr)
127 "Return the address pointed to by PTR."
128 (ffi:pointer-address ptr))
130 ;;;# Dereferencing
132 (defun %mem-ref (ptr type &optional (offset 0))
133 "Dereference an object of TYPE at OFFSET bytes from PTR."
134 (let* ((type (cffi-type->ecl-type type))
135 (type-size (ffi:size-of-foreign-type type)))
136 (si:foreign-data-ref-elt
137 (si:foreign-data-recast ptr (+ offset type-size) :void) offset type)))
139 (defun %mem-set (value ptr type &optional (offset 0))
140 "Set an object of TYPE at OFFSET bytes from PTR."
141 (let* ((type (cffi-type->ecl-type type))
142 (type-size (ffi:size-of-foreign-type type)))
143 (si:foreign-data-set-elt
144 (si:foreign-data-recast ptr (+ offset type-size) :void)
145 offset type value)))
147 ;;;# Type Operations
149 (defconstant +translation-table+
150 '((:char :byte "char")
151 (:unsigned-char :unsigned-byte "unsigned char")
152 (:short :short "short")
153 (:unsigned-short :unsigned-short "unsigned short")
154 (:int :int "int")
155 (:unsigned-int :unsigned-int "unsigned int")
156 (:long :long "long")
157 (:unsigned-long :unsigned-long "unsigned long")
158 (:float :float "float")
159 (:double :double "double")
160 (:pointer :pointer-void "void*")
161 (:void :void "void")))
163 (defun cffi-type->ecl-type (type-keyword)
164 "Convert a CFFI type keyword to an ECL type keyword."
165 (or (second (find type-keyword +translation-table+ :key #'first))
166 (error "~S is not a valid CFFI type" type-keyword)))
168 (defun ecl-type->c-type (type-keyword)
169 "Convert a CFFI type keyword to an valid C type keyword."
170 (or (third (find type-keyword +translation-table+ :key #'second))
171 (error "~S is not a valid CFFI type" type-keyword)))
173 (defun %foreign-type-size (type-keyword)
174 "Return the size in bytes of a foreign type."
175 (nth-value 0 (ffi:size-of-foreign-type
176 (cffi-type->ecl-type type-keyword))))
178 (defun %foreign-type-alignment (type-keyword)
179 "Return the alignment in bytes of a foreign type."
180 (nth-value 1 (ffi:size-of-foreign-type
181 (cffi-type->ecl-type type-keyword))))
183 ;;;# Calling Foreign Functions
185 (defconstant +ecl-inline-codes+ "#0,#1,#2,#3,#4,#5,#6,#7,#8,#9,#a,#b,#c,#d,#e,#f,#g,#h,#i,#j,#k,#l,#m,#n,#o,#p,#q,#r,#s,#t,#u,#v,#w,#x,#y,#z")
187 (defun produce-function-pointer-call (pointer types values return-type)
188 #-dffi
189 (if (stringp pointer)
190 (produce-function-pointer-call
191 `(%foreign-symbol-pointer ,pointer nil) types values return-type)
192 `(ffi:c-inline
193 ,(list* pointer values)
194 ,(list* :pointer-void types) ,return-type
195 ,(with-output-to-string (s)
196 (let ((types (mapcar #'ecl-type->c-type types)))
197 ;; On AMD64, the following code only works with the extra
198 ;; argument ",...". If this is not present, functions
199 ;; like sprintf do not work
200 (format s "((~A (*)(~@[~{~A,~}...~]))(#0))(~A)"
201 (ecl-type->c-type return-type) types
202 (subseq +ecl-inline-codes+ 3
203 (max 3 (+ 2 (* (length values) 3)))))))
204 :one-liner t :side-effects t))
205 #+dffi
206 (progn
207 (when (stringp pointer)
208 (setf pointer `(%foreign-symbol-pointer ,pointer nil)))
209 `(si:call-cfun ,pointer ,return-type (list ,@types) (list ,@values))))
212 (defun foreign-funcall-parse-args (args)
213 "Return three values, lists of arg types, values, and result type."
214 (let ((return-type :void))
215 (loop for (type arg) on args by #'cddr
216 if arg collect (cffi-type->ecl-type type) into types
217 and collect arg into values
218 else do (setf return-type (cffi-type->ecl-type type))
219 finally (return (values types values return-type)))))
221 (defmacro %foreign-funcall (name args &key library calling-convention)
222 "Call a foreign function."
223 (declare (ignore library calling-convention))
224 (multiple-value-bind (types values return-type)
225 (foreign-funcall-parse-args args)
226 (produce-function-pointer-call name types values return-type)))
228 (defmacro %foreign-funcall-pointer (ptr args &key calling-convention)
229 "Funcall a pointer to a foreign function."
230 (declare (ignore calling-convention))
231 (multiple-value-bind (types values return-type)
232 (foreign-funcall-parse-args args)
233 (produce-function-pointer-call ptr types values return-type)))
235 ;;;# Foreign Libraries
237 (defun %load-foreign-library (name path)
238 "Load a foreign library."
239 (declare (ignore name))
240 #-dffi (error "LOAD-FOREIGN-LIBRARY requires ECL's DFFI support. Use ~
241 FFI:LOAD-FOREIGN-LIBRARY with a constant argument instead.")
242 #+dffi (si:load-foreign-module path))
244 (defun %close-foreign-library (handle)
245 (error "%CLOSE-FOREIGN-LIBRARY unimplemented."))
247 (defun native-namestring (pathname)
248 (namestring pathname))
250 ;;;# Callbacks
252 ;;; Create a package to contain the symbols for callback functions.
253 ;;; We want to redefine callbacks with the same symbol so the internal
254 ;;; data structures are reused.
255 (defpackage #:cffi-callbacks
256 (:use))
258 (defvar *callbacks* (make-hash-table))
260 ;;; Intern a symbol in the CFFI-CALLBACKS package used to name the
261 ;;; internal callback for NAME.
262 (eval-when (:compile-toplevel :load-toplevel :execute)
263 (defun intern-callback (name)
264 (intern (format nil "~A::~A" (package-name (symbol-package name))
265 (symbol-name name))
266 '#:cffi-callbacks)))
268 (defmacro %defcallback (name rettype arg-names arg-types body
269 &key calling-convention)
270 (declare (ignore calling-convention))
271 (let ((cb-name (intern-callback name)))
272 `(progn
273 (ffi:defcallback (,cb-name :cdecl)
274 ,(cffi-type->ecl-type rettype)
275 ,(mapcar #'list arg-names
276 (mapcar #'cffi-type->ecl-type arg-types))
277 ,body)
278 (setf (gethash ',name *callbacks*) ',cb-name))))
280 (defun %callback (name)
281 (multiple-value-bind (symbol winp)
282 (gethash name *callbacks*)
283 (unless winp
284 (error "Undefined callback: ~S" name))
285 (ffi:callback symbol)))
287 ;;;# Foreign Globals
289 (defun %foreign-symbol-pointer (name library)
290 "Returns a pointer to a foreign symbol NAME."
291 (declare (ignore library))
292 (si:find-foreign-symbol name :default :pointer-void 0))