Refactor core package definitions
[cffi.git] / src / cffi-corman.lisp
blob8a2dfa4dfd02f08903dad48526af45c0c758e360
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; cffi-corman.lisp --- CFFI-SYS implementation for Corman Lisp.
4 ;;;
5 ;;; Copyright (C) 2005-2008, 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 (in-package #:cffi-sys)
34 ;;;# Misfeatures
36 (pushnew 'no-long-long *features*)
37 (pushnew 'no-foreign-funcall *features*)
39 ;;;$ Symbol Case
41 (defun canonicalize-symbol-name-case (name)
42 (declare (string name))
43 (string-upcase name))
45 ;;;# Basic Pointer Operations
47 (deftype foreign-pointer ()
48 'cl::foreign)
50 (defun pointerp (ptr)
51 "Return true if PTR is a foreign pointer."
52 (cpointerp ptr))
54 (defun pointer-eq (ptr1 ptr2)
55 "Return true if PTR1 and PTR2 point to the same address."
56 (cpointer= ptr1 ptr2))
58 (defun null-pointer ()
59 "Return a null pointer."
60 (create-foreign-ptr))
62 (defun null-pointer-p (ptr)
63 "Return true if PTR is a null pointer."
64 (cpointer-null ptr))
66 (defun inc-pointer (ptr offset)
67 "Return a pointer pointing OFFSET bytes past PTR."
68 (let ((new-ptr (create-foreign-ptr)))
69 (setf (cpointer-value new-ptr)
70 (+ (cpointer-value ptr) offset))
71 new-ptr))
73 (defun make-pointer (address)
74 "Return a pointer pointing to ADDRESS."
75 (int-to-foreign-ptr address))
77 (defun pointer-address (ptr)
78 "Return the address pointed to by PTR."
79 (foreign-ptr-to-int ptr))
81 ;;;# Allocation
82 ;;;
83 ;;; Functions and macros for allocating foreign memory on the stack
84 ;;; and on the heap. The main CFFI package defines macros that wrap
85 ;;; FOREIGN-ALLOC and FOREIGN-FREE in UNWIND-PROTECT for the common usage
86 ;;; when the memory has dynamic extent.
88 (defun %foreign-alloc (size)
89 "Allocate SIZE bytes on the heap and return a pointer."
90 (malloc size))
92 (defun foreign-free (ptr)
93 "Free a PTR allocated by FOREIGN-ALLOC."
94 (free ptr))
96 (defmacro with-foreign-pointer ((var size &optional size-var) &body body)
97 "Bind VAR to SIZE bytes of foreign memory during BODY. The
98 pointer in VAR is invalid beyond the dynamic extent of BODY, and
99 may be stack-allocated if supported by the implementation. If
100 SIZE-VAR is supplied, it will be bound to SIZE during BODY."
101 (unless size-var
102 (setf size-var (gensym "SIZE")))
103 `(let* ((,size-var ,size)
104 (,var (malloc ,size-var)))
105 (unwind-protect
106 (progn ,@body)
107 (free ,var))))
109 ;;;# Shareable Vectors
111 ;;; This interface is very experimental. WITH-POINTER-TO-VECTOR-DATA
112 ;;; should be defined to perform a copy-in/copy-out if the Lisp
113 ;;; implementation can't do this.
115 ;(defun make-shareable-byte-vector (size)
116 ; "Create a Lisp vector of SIZE bytes can passed to
117 ;WITH-POINTER-TO-VECTOR-DATA."
118 ; (make-array size :element-type '(unsigned-byte 8)))
120 ;(defmacro with-pointer-to-vector-data ((ptr-var vector) &body body)
121 ; "Bind PTR-VAR to a foreign pointer to the data in VECTOR."
122 ; `(sb-sys:without-gcing
123 ; (let ((,ptr-var (sb-sys:vector-sap ,vector)))
124 ; ,@body)))
126 ;;;# Dereferencing
128 ;;; According to the docs, Corman's C Function Definition Parser
129 ;;; converts int to long, so we'll assume that.
130 (defun convert-foreign-type (type-keyword)
131 "Convert a CFFI type keyword to a CormanCL type."
132 (ecase type-keyword
133 (:char :char)
134 (:unsigned-char :unsigned-char)
135 (:short :short)
136 (:unsigned-short :unsigned-short)
137 (:int :long)
138 (:unsigned-int :unsigned-long)
139 (:long :long)
140 (:unsigned-long :unsigned-long)
141 (:float :single-float)
142 (:double :double-float)
143 (:pointer :handle)
144 (:void :void)))
146 (defun %mem-ref (ptr type &optional (offset 0))
147 "Dereference an object of TYPE at OFFSET bytes from PTR."
148 (unless (eql offset 0)
149 (setq ptr (inc-pointer ptr offset)))
150 (ecase type
151 (:char (cref (:char *) ptr 0))
152 (:unsigned-char (cref (:unsigned-char *) ptr 0))
153 (:short (cref (:short *) ptr 0))
154 (:unsigned-short (cref (:unsigned-short *) ptr 0))
155 (:int (cref (:long *) ptr 0))
156 (:unsigned-int (cref (:unsigned-long *) ptr 0))
157 (:long (cref (:long *) ptr 0))
158 (:unsigned-long (cref (:unsigned-long *) ptr 0))
159 (:float (cref (:single-float *) ptr 0))
160 (:double (cref (:double-float *) ptr 0))
161 (:pointer (cref (:handle *) ptr 0))))
163 ;(define-compiler-macro %mem-ref (&whole form ptr type &optional (offset 0))
164 ; (if (constantp type)
165 ; `(cref (,(convert-foreign-type type) *) ,ptr ,offset)
166 ; form))
168 (defun %mem-set (value ptr type &optional (offset 0))
169 "Set the object of TYPE at OFFSET bytes from PTR."
170 (unless (eql offset 0)
171 (setq ptr (inc-pointer ptr offset)))
172 (ecase type
173 (:char (setf (cref (:char *) ptr 0) value))
174 (:unsigned-char (setf (cref (:unsigned-char *) ptr 0) value))
175 (:short (setf (cref (:short *) ptr 0) value))
176 (:unsigned-short (setf (cref (:unsigned-short *) ptr 0) value))
177 (:int (setf (cref (:long *) ptr 0) value))
178 (:unsigned-int (setf (cref (:unsigned-long *) ptr 0) value))
179 (:long (setf (cref (:long *) ptr 0) value))
180 (:unsigned-long (setf (cref (:unsigned-long *) ptr 0) value))
181 (:float (setf (cref (:single-float *) ptr 0) value))
182 (:double (setf (cref (:double-float *) ptr 0) value))
183 (:pointer (setf (cref (:handle *) ptr 0) value))))
185 ;;;# Calling Foreign Functions
187 (defun %foreign-type-size (type-keyword)
188 "Return the size in bytes of a foreign type."
189 (sizeof (convert-foreign-type type-keyword)))
191 ;;; Couldn't find anything in sys/ffi.lisp and the C declaration parser
192 ;;; doesn't seem to care about alignment so we'll assume that it's the
193 ;;; same as its size.
194 (defun %foreign-type-alignment (type-keyword)
195 (sizeof (convert-foreign-type type-keyword)))
197 (defun find-dll-containing-function (name)
198 "Searches for NAME in the loaded DLLs. If found, returns
199 the DLL's name (a string), else returns NIL."
200 (dolist (dll ct::*dlls-loaded*)
201 (when (ignore-errors
202 (ct::get-dll-proc-address name (ct::dll-record-handle dll)))
203 (return (ct::dll-record-name dll)))))
205 ;;; This won't work at all...
207 (defmacro %foreign-funcall (name &rest args)
208 (let ((sym (gensym)))
209 `(let (,sym)
210 (ct::install-dll-function ,(find-dll-containing-function name)
211 ,name ,sym)
212 (funcall ,sym ,@(loop for (type arg) on args by #'cddr
213 if arg collect arg)))))
216 ;;; It *might* be possible to implement by copying most of the code
217 ;;; from Corman's DEFUN-DLL. Alternatively, it could implemented the
218 ;;; same way as Lispworks' foreign-funcall. In practice, nobody uses
219 ;;; Corman with CFFI, apparently. :)
220 (defmacro %foreign-funcall (name &rest args)
221 "Call a foreign function NAME passing arguments ARGS."
222 `(format t "~&;; Calling ~A with args ~S.~%" ,name ',args))
224 (defun defcfun-helper-forms (name lisp-name rettype args types)
225 "Return 2 values for DEFCFUN. A prelude form and a caller form."
226 (let ((ff-name (intern (format nil "%cffi-foreign-function/~A" lisp-name)))
227 ;; XXX This will only work if the dll is already loaded, fix this.
228 (dll (find-dll-containing-function name)))
229 (values
230 `(defun-dll ,ff-name
231 ,(mapcar (lambda (type)
232 (list (gensym) (convert-foreign-type type)))
233 types)
234 :return-type ,(convert-foreign-type rettype)
235 :library-name ,dll
236 :entry-name ,name
237 ;; we want also :pascal linkage type to access
238 ;; the win32 api for instance..
239 :linkage-type :c)
240 `(,ff-name ,@args))))
242 ;;;# Callbacks
244 ;;; defun-c-callback vs. defun-direct-c-callback?
245 ;;; same issue as Allegro, no return type declaration, should we coerce?
246 (defmacro %defcallback (name rettype arg-names arg-types body-form)
247 (declare (ignore rettype))
248 (with-unique-names (cb-sym)
249 `(progn
250 (defun-c-callback ,cb-sym
251 ,(mapcar (lambda (sym type) (list sym (convert-foreign-type type)))
252 arg-names arg-types)
253 ,body-form)
254 (setf (get ',name 'callback-ptr)
255 (get-callback-procinst ',cb-sym)))))
257 ;;; Just continue to use the plist for now even though this really
258 ;;; should use a *CALLBACKS* hash table and not define the callbacks
259 ;;; as gensyms. Someone with access to Corman should update this.
260 (defun %callback (name)
261 (get name 'callback-ptr))
263 ;;;# Loading Foreign Libraries
265 (defun %load-foreign-library (name)
266 "Load the foreign library NAME."
267 (ct::get-dll-record name))
269 (defun %close-foreign-library (name)
270 "Close the foreign library NAME."
271 (error "Not implemented."))
273 (defun native-namestring (pathname)
274 (namestring pathname)) ; TODO: confirm
276 ;;;# Foreign Globals
278 ;;; FFI to GetProcAddress from the Win32 API.
279 ;;; "The GetProcAddress function retrieves the address of an exported
280 ;;; function or variable from the specified dynamic-link library (DLL)."
281 (defun-dll get-proc-address
282 ((module HMODULE)
283 (name LPCSTR))
284 :return-type FARPROC
285 :library-name "Kernel32.dll"
286 :entry-name "GetProcAddress"
287 :linkage-type :pascal)
289 (defun foreign-symbol-pointer (name)
290 "Returns a pointer to a foreign symbol NAME."
291 (let ((str (lisp-string-to-c-string name)))
292 (unwind-protect
293 (dolist (dll ct::*dlls-loaded*)
294 (let ((ptr (get-proc-address
295 (int-to-foreign-ptr (ct::dll-record-handle dll))
296 str)))
297 (when (not (cpointer-null ptr))
298 (return ptr))))
299 (free str))))