1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: objects.lisp
6 ;;;; Purpose: UFFI source to handle objects and pointers
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
12 ;;;; *************************************************************************
16 (eval-when (:compile-toplevel :load-toplevel :execute)
17 (defun size-of-foreign-type (type)
18 #+lispworks (fli:size-of type)
19 #+allegro (ff:sizeof-fobject type)
20 #+(or cmu scl) (ash (eval `(alien:alien-size ,type)) -3) ;; convert from bits to bytes
21 #+sbcl (ash (eval `(sb-alien:alien-size ,type)) -3) ;; convert from bits to bytes
22 #+clisp (values (ffi:size-of type))
24 (let ((mcl-type (ccl:find-mactype type nil t)))
26 (ccl::mactype-record-size mcl-type)
27 (ccl::record-descriptor-length (ccl:find-record-descriptor type t t)))) ;error if not a record
28 #+openmcl (ccl::%foreign-type-or-record-size type :bytes)
31 (defmacro allocate-foreign-object (type &optional (size :unspecified))
32 "Allocates an instance of TYPE. If size is specified, then allocate
33 an array of TYPE with size SIZE. The TYPE parameter is evaluated."
34 (if (eq size :unspecified)
37 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
39 `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
41 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
43 `(ff:allocate-fobject ',(convert-from-uffi-type type :allocate) :c)
44 #+(or openmcl digitool)
45 `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type type :allocation)))
49 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
51 `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
53 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
55 `(ff:allocate-fobject (list :array (quote ,(convert-from-uffi-type type :allocate)) ,size) :c)
56 #+(or openmcl digitool)
57 `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
60 (defmacro free-foreign-object (obj)
62 `(alien:free-alien ,obj)
64 `(sb-alien:free-alien ,obj)
66 `(fli:free-foreign-object ,obj)
68 `(ff:free-fobject ,obj)
69 #+(or openmcl digitool)
73 (defmacro null-pointer-p (obj)
74 #+lispworks `(fli:null-pointer-p ,obj)
75 #+allegro `(zerop ,obj)
76 #+(or cmu scl) `(alien:null-alien ,obj)
77 #+sbcl `(sb-alien:null-alien ,obj)
78 #+(or openmcl digitool) `(ccl:%null-ptr-p ,obj)
81 (defmacro make-null-pointer (type)
82 #+(or allegro openmcl digitool) (declare (ignore type))
83 #+(or cmu scl) `(alien:sap-alien (system:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
84 #+sbcl `(sb-alien:sap-alien (sb-sys:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
85 #+lispworks `(fli:make-pointer :address 0 :type (quote ,(convert-from-uffi-type (eval type) :type)))
87 #+(or openmcl digitool) `(ccl:%null-ptr)
90 (defmacro make-pointer (addr type)
91 #+(or allegro openmcl digitool) (declare (ignore type))
92 #+(or cmu scl) `(alien:sap-alien (system:int-sap ,addr) (* ,(convert-from-uffi-type (eval type) :type)))
93 #+sbcl `(sb-alien:sap-alien (sb-sys:int-sap ,addr) (* ,(convert-from-uffi-type (eval type) :type)))
94 #+lispworks `(fli:make-pointer :address ,addr :type (quote ,(convert-from-uffi-type (eval type) :type)))
96 #+(or openmcl digitool) `(ccl:%int-to-ptr ,addr)
100 (defmacro char-array-to-pointer (obj)
101 #+(or cmu scl) `(alien:cast ,obj (* (alien:unsigned 8)))
102 #+sbcl `(sb-alien:cast ,obj (* (sb-alien:unsigned 8)))
103 #+lispworks `(fli:make-pointer :type '(:unsigned :char)
104 :address (fli:pointer-address ,obj))
106 #+(or openmcl digitool) obj
109 (defmacro deref-pointer (ptr type)
110 "Returns a object pointed"
111 #+(or cmu sbcl lispworks scl) (declare (ignore type))
112 #+(or cmu scl) `(alien:deref ,ptr)
113 #+sbcl `(sb-alien:deref ,ptr)
114 #+lispworks `(fli:dereference ,ptr)
115 #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref)) :c ,ptr)
116 #+(or openmcl digitool) `(ccl:pref ,ptr ,(convert-from-uffi-type type :deref))
120 (defmacro deref-pointer-set (ptr type value)
121 `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
124 (defsetf deref-pointer deref-pointer-set)
126 (defmacro ensure-char-character (obj)
128 #+(or allegro cmu sbcl scl openmcl) `(code-char ,obj)
129 ;; lispworks varies whether deref'ing array vs. slot access of a char
130 #+lispworks `(if (characterp ,obj) ,obj (code-char ,obj)))
132 (defmacro ensure-char-integer (obj)
133 #+(or digitool) `(char-code ,obj)
134 #+(or allegro cmu sbcl scl openmcl) obj
135 ;; lispworks varies whether deref'ing array vs. slot access of a char
137 `(if (integerp ,obj) ,obj (char-code ,obj)))
139 (defmacro ensure-char-storable (obj)
140 #+(or digitool (and lispworks (not lispworks5) (not lispworks6))) obj
141 #+(or allegro cmu lispworks5 lispworks6 openmcl sbcl scl)
144 (defmacro pointer-address (obj)
146 `(system:sap-int (alien:alien-sap ,obj))
148 `(sb-sys:sap-int (sb-alien:alien-sap ,obj))
150 `(fli:pointer-address ,obj)
153 #+(or openmcl digitool)
154 `(ccl:%ptr-to-int ,obj)
157 ;; TYPE is evaluated.
158 #-(or openmcl digitool)
159 (defmacro with-foreign-object ((var type) &rest body)
160 #-(or cmu sbcl lispworks scl) ; default version
161 `(let ((,var (allocate-foreign-object ,type)))
164 (free-foreign-object ,var)))
167 (ctype (convert-from-uffi-type (eval type) :allocate)))
168 (if (and (consp ctype) (eq 'array (car ctype)))
169 `(alien:with-alien ((,obj ,ctype))
172 `(alien:with-alien ((,obj ,ctype))
173 (let* ((,var (alien:addr ,obj)))
177 (ctype (convert-from-uffi-type (eval type) :allocate)))
178 (if (and (consp ctype) (eq 'array (car ctype)))
179 `(sb-alien:with-alien ((,obj ,ctype))
182 `(sb-alien:with-alien ((,obj ,ctype))
183 (let* ((,var (sb-alien:addr ,obj)))
186 `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
187 (eval type) :allocate)))
191 #-(or openmcl digitool)
192 (defmacro with-foreign-objects (bindings &rest body)
194 `(with-foreign-object ,(car bindings)
195 (with-foreign-objects ,(cdr bindings)
199 #+(or openmcl digitool)
200 (defmacro with-foreign-objects (bindings &rest body)
201 (let ((params nil) type count)
202 (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
203 (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
205 (when (and (listp type) (eq (first type) :array))
206 (setf count (nth 2 type))
207 (unless (integerp count) (error "Invalid size for array: ~a" type))
208 (setf type (nth 1 type)))
209 (push (list (first spec) (* count (size-of-foreign-type type))) params))
210 `(ccl:%stack-block ,params ,@body)))
212 #+(or openmcl digitool)
213 (defmacro with-foreign-object ((var type) &rest body)
214 `(with-foreign-objects ((,var ,type))
218 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
219 `(fli:with-coerced-pointer (,binding-name
220 :type ',(convert-from-uffi-type (eval type) :type))
225 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
226 `(let ((,binding-name
227 (#+(or cmu scl) alien:cast
229 ,pointer (* ,(convert-from-uffi-type (eval type) :type)))))
232 #+(or allegro openmcl)
233 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
234 (declare (ignore type))
235 `(let ((,binding-name ,pointer))
238 #-(or lispworks cmu scl sbcl allegro openmcl)
239 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
240 (declare (ignore binding-name pointer type body))
241 '(error "WITH-CAST-POINTER not (yet) implemented for ~A"
242 (lisp-implementation-type)))
244 #+(or allegro openmcl)
245 (defun convert-external-name (name)
246 "Add an underscore to NAME if necessary for the ABI."
247 #+(or macosx darwinppc-target) (concatenate 'string "_" name)
248 #-(or macosx darwinppc-target) name)
250 (defmacro def-foreign-var (names type module)
251 #-lispworks (declare (ignore module))
252 (let ((foreign-name (if (atom names) names (first names)))
253 (lisp-name (if (atom names) (make-lisp-name names) (second names)))
255 (var-type (convert-from-uffi-type type :type)))
257 `(alien:def-alien-variable (,foreign-name ,lisp-name) ,var-type)
259 `(sb-alien:define-alien-variable (,foreign-name ,lisp-name) ,var-type)
261 `(define-symbol-macro ,lisp-name
262 (ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref))
263 :c (ff:get-entry-point ,(convert-external-name foreign-name))))
266 (fli:define-foreign-variable (,lisp-name ,foreign-name)
267 :accessor :address-of
270 (define-symbol-macro ,lisp-name (fli:dereference (,lisp-name)
271 :copy-foreign-object nil)))
273 `(define-symbol-macro ,lisp-name
274 (deref-pointer (ccl:foreign-symbol-address
275 ,(convert-external-name foreign-name)) ,var-type))
276 #-(or allegro cmu scl sbcl lispworks openmcl)
277 `(define-symbol-macro ,lisp-name
278 '(error "DEF-FOREIGN-VAR not (yet) defined for ~A"
279 (lisp-implementation-type)))))
282 ;;; Define a special variable, like DEFVAR, that will be initialized
283 ;;; to a pointer which may need to be reset when a saved image is
284 ;;; loaded. This is needed for OpenMCL, which sets pointers to "dead
285 ;;; macptrs" when a saved image is loaded.
286 ;; This may possibly be needed for sbcl's SAVE-LISP-AND-DIE
287 (defmacro def-pointer-var (name value &optional doc)
288 #-openmcl `(defvar ,name ,value ,@(if doc (list doc)))
289 #+openmcl `(ccl::defloadvar ,name ,value ,doc))