examples cleaned up
[CommonLispStat.git] / external / cffi.darcs / src / cffi-corman.lisp
blobf71264886f4f5f8de0f0fbddc2debfdeb5714e64
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; cffi-corman.lisp --- CFFI-SYS implementation for Corman Lisp.
4 ;;;
5 ;;; Copyright (C) 2005-2006, Luis Oliveira <loliveira(@)common-lisp.net>
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 ;;; This port is suffering from bitrot as of 2007-03-29. Corman Lisp
29 ;;; is too funky with ASDF, crashes easily, makes it very painful to
30 ;;; do any testing. -- luis
32 ;;;# Administrivia
34 (defpackage #:cffi-sys
35 (:use #:common-lisp #:c-types #:cffi-utils #:alexandria)
36 (:export
37 #:canonicalize-symbol-name-case
38 #:foreign-pointer
39 #:pointerp
40 #:pointer-eq
41 #:null-pointer
42 #:null-pointer-p
43 #:inc-pointer
44 #:make-pointer
45 #:pointer-address
46 #:%foreign-alloc
47 #:foreign-free
48 #:with-foreign-pointer
49 #:%foreign-funcall
50 #:%foreign-type-alignment
51 #:%foreign-type-size
52 #:%load-foreign-library
53 #:native-namestring
54 #:%mem-ref
55 #:%mem-set
56 ;#:make-shareable-byte-vector
57 ;#:with-pointer-to-vector-data
58 #:foreign-symbol-pointer
59 #:defcfun-helper-forms
60 #:%defcallback
61 #:%callback))
63 (in-package #:cffi-sys)
65 ;;;# Mis-features
67 (eval-when (:compile-toplevel :load-toplevel :execute)
68 (mapc (lambda (feature) (pushnew feature *features*))
69 '(cffi-features:no-long-long
70 cffi-features:no-foreign-funcall)))
72 ;;;$ Symbol Case
74 (defun canonicalize-symbol-name-case (name)
75 (declare (string name))
76 (string-upcase name))
78 ;;;# Basic Pointer Operations
80 (deftype foreign-pointer ()
81 'cl::foreign)
83 (defun pointerp (ptr)
84 "Return true if PTR is a foreign pointer."
85 (cpointerp ptr))
87 (defun pointer-eq (ptr1 ptr2)
88 "Return true if PTR1 and PTR2 point to the same address."
89 (cpointer= ptr1 ptr2))
91 (defun null-pointer ()
92 "Return a null pointer."
93 (create-foreign-ptr))
95 (defun null-pointer-p (ptr)
96 "Return true if PTR is a null pointer."
97 (cpointer-null ptr))
99 (defun inc-pointer (ptr offset)
100 "Return a pointer pointing OFFSET bytes past PTR."
101 (let ((new-ptr (create-foreign-ptr)))
102 (setf (cpointer-value new-ptr)
103 (+ (cpointer-value ptr) offset))
104 new-ptr))
106 (defun make-pointer (address)
107 "Return a pointer pointing to ADDRESS."
108 (int-to-foreign-ptr address))
110 (defun pointer-address (ptr)
111 "Return the address pointed to by PTR."
112 (foreign-ptr-to-int ptr))
114 ;;;# Allocation
116 ;;; Functions and macros for allocating foreign memory on the stack
117 ;;; and on the heap. The main CFFI package defines macros that wrap
118 ;;; FOREIGN-ALLOC and FOREIGN-FREE in UNWIND-PROTECT for the common usage
119 ;;; when the memory has dynamic extent.
121 (defun %foreign-alloc (size)
122 "Allocate SIZE bytes on the heap and return a pointer."
123 (malloc size))
125 (defun foreign-free (ptr)
126 "Free a PTR allocated by FOREIGN-ALLOC."
127 (free ptr))
129 (defmacro with-foreign-pointer ((var size &optional size-var) &body body)
130 "Bind VAR to SIZE bytes of foreign memory during BODY. The
131 pointer in VAR is invalid beyond the dynamic extent of BODY, and
132 may be stack-allocated if supported by the implementation. If
133 SIZE-VAR is supplied, it will be bound to SIZE during BODY."
134 (unless size-var
135 (setf size-var (gensym "SIZE")))
136 `(let* ((,size-var ,size)
137 (,var (malloc ,size-var)))
138 (unwind-protect
139 (progn ,@body)
140 (free ,var))))
142 ;;;# Shareable Vectors
144 ;;; This interface is very experimental. WITH-POINTER-TO-VECTOR-DATA
145 ;;; should be defined to perform a copy-in/copy-out if the Lisp
146 ;;; implementation can't do this.
148 ;(defun make-shareable-byte-vector (size)
149 ; "Create a Lisp vector of SIZE bytes can passed to
150 ;WITH-POINTER-TO-VECTOR-DATA."
151 ; (make-array size :element-type '(unsigned-byte 8)))
153 ;(defmacro with-pointer-to-vector-data ((ptr-var vector) &body body)
154 ; "Bind PTR-VAR to a foreign pointer to the data in VECTOR."
155 ; `(sb-sys:without-gcing
156 ; (let ((,ptr-var (sb-sys:vector-sap ,vector)))
157 ; ,@body)))
159 ;;;# Dereferencing
161 ;;; According to the docs, Corman's C Function Definition Parser
162 ;;; converts int to long, so we'll assume that.
163 (defun convert-foreign-type (type-keyword)
164 "Convert a CFFI type keyword to a CormanCL type."
165 (ecase type-keyword
166 (:char :char)
167 (:unsigned-char :unsigned-char)
168 (:short :short)
169 (:unsigned-short :unsigned-short)
170 (:int :long)
171 (:unsigned-int :unsigned-long)
172 (:long :long)
173 (:unsigned-long :unsigned-long)
174 (:float :single-float)
175 (:double :double-float)
176 (:pointer :handle)
177 (:void :void)))
179 (defun %mem-ref (ptr type &optional (offset 0))
180 "Dereference an object of TYPE at OFFSET bytes from PTR."
181 (unless (eql offset 0)
182 (setq ptr (inc-pointer ptr offset)))
183 (ecase type
184 (:char (cref (:char *) ptr 0))
185 (:unsigned-char (cref (:unsigned-char *) ptr 0))
186 (:short (cref (:short *) ptr 0))
187 (:unsigned-short (cref (:unsigned-short *) ptr 0))
188 (:int (cref (:long *) ptr 0))
189 (:unsigned-int (cref (:unsigned-long *) ptr 0))
190 (:long (cref (:long *) ptr 0))
191 (:unsigned-long (cref (:unsigned-long *) ptr 0))
192 (:float (cref (:single-float *) ptr 0))
193 (:double (cref (:double-float *) ptr 0))
194 (:pointer (cref (:handle *) ptr 0))))
196 ;(define-compiler-macro %mem-ref (&whole form ptr type &optional (offset 0))
197 ; (if (constantp type)
198 ; `(cref (,(convert-foreign-type type) *) ,ptr ,offset)
199 ; form))
201 (defun %mem-set (value ptr type &optional (offset 0))
202 "Set the object of TYPE at OFFSET bytes from PTR."
203 (unless (eql offset 0)
204 (setq ptr (inc-pointer ptr offset)))
205 (ecase type
206 (:char (setf (cref (:char *) ptr 0) value))
207 (:unsigned-char (setf (cref (:unsigned-char *) ptr 0) value))
208 (:short (setf (cref (:short *) ptr 0) value))
209 (:unsigned-short (setf (cref (:unsigned-short *) ptr 0) value))
210 (:int (setf (cref (:long *) ptr 0) value))
211 (:unsigned-int (setf (cref (:unsigned-long *) ptr 0) value))
212 (:long (setf (cref (:long *) ptr 0) value))
213 (:unsigned-long (setf (cref (:unsigned-long *) ptr 0) value))
214 (:float (setf (cref (:single-float *) ptr 0) value))
215 (:double (setf (cref (:double-float *) ptr 0) value))
216 (:pointer (setf (cref (:handle *) ptr 0) value))))
218 ;;;# Calling Foreign Functions
220 (defun %foreign-type-size (type-keyword)
221 "Return the size in bytes of a foreign type."
222 (sizeof (convert-foreign-type type-keyword)))
224 ;;; Couldn't find anything in sys/ffi.lisp and the C declaration parser
225 ;;; doesn't seem to care about alignment so we'll assume that it's the
226 ;;; same as its size.
227 (defun %foreign-type-alignment (type-keyword)
228 (sizeof (convert-foreign-type type-keyword)))
230 (defun find-dll-containing-function (name)
231 "Searches for NAME in the loaded DLLs. If found, returns
232 the DLL's name (a string), else returns NIL."
233 (dolist (dll ct::*dlls-loaded*)
234 (when (ignore-errors
235 (ct::get-dll-proc-address name (ct::dll-record-handle dll)))
236 (return (ct::dll-record-name dll)))))
238 ;;; This won't work at all...
240 (defmacro %foreign-funcall (name &rest args)
241 (let ((sym (gensym)))
242 `(let (,sym)
243 (ct::install-dll-function ,(find-dll-containing-function name)
244 ,name ,sym)
245 (funcall ,sym ,@(loop for (type arg) on args by #'cddr
246 if arg collect arg)))))
249 ;;; It *might* be possible to implement by copying most of the code
250 ;;; from Corman's DEFUN-DLL. Alternatively, it could implemented the
251 ;;; same way as Lispworks' foreign-funcall. In practice, nobody uses
252 ;;; Corman with CFFI, apparently. :)
253 (defmacro %foreign-funcall (name &rest args)
254 "Call a foreign function NAME passing arguments ARGS."
255 `(format t "~&;; Calling ~A with args ~S.~%" ,name ',args))
257 (defun defcfun-helper-forms (name lisp-name rettype args types)
258 "Return 2 values for DEFCFUN. A prelude form and a caller form."
259 (let ((ff-name (intern (format nil "%cffi-foreign-function/~A" lisp-name)))
260 ;; XXX This will only work if the dll is already loaded, fix this.
261 (dll (find-dll-containing-function name)))
262 (values
263 `(defun-dll ,ff-name
264 ,(mapcar (lambda (type)
265 (list (gensym) (convert-foreign-type type)))
266 types)
267 :return-type ,(convert-foreign-type rettype)
268 :library-name ,dll
269 :entry-name ,name
270 ;; we want also :pascal linkage type to access
271 ;; the win32 api for instance..
272 :linkage-type :c)
273 `(,ff-name ,@args))))
275 ;;;# Callbacks
277 ;;; defun-c-callback vs. defun-direct-c-callback?
278 ;;; same issue as Allegro, no return type declaration, should we coerce?
279 (defmacro %defcallback (name rettype arg-names arg-types body-form)
280 (declare (ignore rettype))
281 (with-unique-names (cb-sym)
282 `(progn
283 (defun-c-callback ,cb-sym
284 ,(mapcar (lambda (sym type) (list sym (convert-foreign-type type)))
285 arg-names arg-types)
286 ,body-form)
287 (setf (get ',name 'callback-ptr)
288 (get-callback-procinst ',cb-sym)))))
290 ;;; Just continue to use the plist for now even though this really
291 ;;; should use a *CALLBACKS* hash table and not define the callbacks
292 ;;; as gensyms. Someone with access to Corman should update this.
293 (defun %callback (name)
294 (get name 'callback-ptr))
296 ;;;# Loading Foreign Libraries
298 (defun %load-foreign-library (name)
299 "Load the foreign library NAME."
300 (ct::get-dll-record name))
302 (defun %close-foreign-library (name)
303 "Close the foreign library NAME."
304 (error "Not implemented."))
306 (defun native-namestring (pathname)
307 (namestring pathname)) ; TODO: confirm
309 ;;;# Foreign Globals
311 ;;; FFI to GetProcAddress from the Win32 API.
312 ;;; "The GetProcAddress function retrieves the address of an exported
313 ;;; function or variable from the specified dynamic-link library (DLL)."
314 (defun-dll get-proc-address
315 ((module HMODULE)
316 (name LPCSTR))
317 :return-type FARPROC
318 :library-name "Kernel32.dll"
319 :entry-name "GetProcAddress"
320 :linkage-type :pascal)
322 (defun foreign-symbol-pointer (name)
323 "Returns a pointer to a foreign symbol NAME."
324 (let ((str (lisp-string-to-c-string name)))
325 (unwind-protect
326 (dolist (dll ct::*dlls-loaded*)
327 (let ((ptr (get-proc-address
328 (int-to-foreign-ptr (ct::dll-record-handle dll))
329 str)))
330 (when (not (cpointer-null ptr))
331 (return ptr))))
332 (free str))))