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
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
21 (defun size-of-foreign-type (type)
22 #+lispworks (fli:size-of type)
23 #+allegro (ff:sizeof-fobject type)
24 #+(or cmu scl) (ash (eval `(alien:alien-size ,type)) -3) ;; convert from bits to bytes
25 #+sbcl (ash (eval `(sb-alien:alien-size ,type)) -3) ;; convert from bits to bytes
26 #+clisp (values (ffi:size-of type))
27 #+(and mcl (not openmcl))
28 (let ((mcl-type (ccl:find-mactype type nil t)))
30 (ccl::mactype-record-size mcl-type)
31 (ccl::record-descriptor-length (ccl:find-record-descriptor type t t)))) ;error if not a record
32 #+openmcl (ccl::%foreign-type-or-record-size type :bytes)
36 (defmacro allocate-foreign-object (type &optional (size :unspecified))
37 "Allocates an instance of TYPE. If size is specified, then allocate
38 an array of TYPE with size SIZE. The TYPE parameter is evaluated."
39 (if (eq size :unspecified)
42 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
44 `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
46 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
48 `(ff:allocate-fobject ',(convert-from-uffi-type type :allocate) :c)
50 `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type type :allocation)))
56 (alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
57 (array ,(convert-from-uffi-type (eval type) :allocation) ,size))
59 (alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
60 (array ,(convert-from-uffi-type (eval type) :allocation) nil)))
64 (sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
65 (array ,(convert-from-uffi-type (eval type) :allocation) ,size))
67 (sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
68 (array ,(convert-from-uffi-type (eval type) :allocation) nil)))
70 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
72 `(ff:allocate-fobject (list :array (quote ,(convert-from-uffi-type type :allocate)) ,size) :c)
74 `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
77 (defmacro free-foreign-object (obj)
79 `(alien:free-alien ,obj)
81 `(sb-alien:free-alien ,obj)
83 `(fli:free-foreign-object ,obj)
85 `(ff:free-fobject ,obj)
90 (defmacro null-pointer-p (obj)
91 #+lispworks `(fli:null-pointer-p ,obj)
92 #+allegro `(zerop ,obj)
93 #+(or cmu scl) `(alien:null-alien ,obj)
94 #+sbcl `(sb-alien:null-alien ,obj)
95 #+mcl `(ccl:%null-ptr-p ,obj)
98 (defmacro make-null-pointer (type)
99 #+(or allegro mcl) (declare (ignore type))
100 #+(or cmu scl) `(alien:sap-alien (system:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
101 #+sbcl `(sb-alien:sap-alien (sb-sys:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
102 #+lispworks `(fli:make-pointer :address 0 :type (quote ,(convert-from-uffi-type (eval type) :type)))
104 #+mcl `(ccl:%null-ptr)
107 (defmacro make-pointer (addr type)
108 #+(or allegro mcl) (declare (ignore type))
109 #+(or cmu scl) `(alien:sap-alien (system:int-sap ,addr) (* ,(convert-from-uffi-type (eval type) :type)))
110 #+sbcl `(sb-alien:sap-alien (sb-sys:int-sap ,addr) (* ,(convert-from-uffi-type (eval type) :type)))
111 #+lispworks `(fli:make-pointer :address ,addr :type (quote ,(convert-from-uffi-type (eval type) :type)))
113 #+mcl `(ccl:%int-to-ptr ,addr)
117 (defmacro char-array-to-pointer (obj)
118 #+(or cmu scl) `(alien:cast ,obj (* (alien:unsigned 8)))
119 #+sbcl `(sb-alien:cast ,obj (* (sb-alien:unsigned 8)))
120 #+lispworks `(fli:make-pointer :type '(:unsigned :char)
121 :address (fli:pointer-address ,obj))
126 (defmacro deref-pointer (ptr type)
127 "Returns a object pointed"
128 #+(or cmu sbcl lispworks scl) (declare (ignore type))
129 #+(or cmu scl) `(alien:deref ,ptr)
130 #+sbcl `(sb-alien:deref ,ptr)
131 #+lispworks `(fli:dereference ,ptr)
132 #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref)) :c ,ptr)
133 #+mcl `(ccl:pref ,ptr ,(convert-from-uffi-type type :deref))
137 (defmacro deref-pointer-set (ptr type value)
138 `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
141 (defsetf deref-pointer deref-pointer-set)
143 (defmacro ensure-char-character (obj)
144 #+(or (and mcl (not openmcl))) obj
145 #+(or allegro cmu sbcl scl openmcl) `(code-char ,obj)
146 ;; lispworks varies whether deref'ing array vs. slot access of a char
148 `(if (characterp ,obj) ,obj (code-char ,obj)))
150 (defmacro ensure-char-integer (obj)
151 #+(or (and mcl (not openmcl))) `(char-code ,obj)
152 #+(or allegro cmu sbcl scl openmcl) obj
153 `(if (characterp ,obj) (char-code ,obj) ,obj))
155 (defmacro ensure-char-storable (obj)
156 #+(or lispworks (and mcl (not openmcl))) obj
157 #+(or allegro cmu sbcl scl openmcl) `(char-code ,obj))
159 (defmacro pointer-address (obj)
161 `(system:sap-int (alien:alien-sap ,obj))
163 `(sb-sys:sap-int (sb-alien:alien-sap ,obj))
165 `(fli:pointer-address ,obj)
169 `(ccl:%ptr-to-int ,obj)
172 ;; TYPE is evaluated.
174 (defmacro with-foreign-object ((var type) &rest body)
175 #-(or cmu sbcl lispworks scl) ; default version
176 `(let ((,var (allocate-foreign-object ,type)))
179 (free-foreign-object ,var)))
181 (let ((obj (gensym)))
182 `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
183 (let ((,var (alien:addr ,obj)))
187 (ctype (convert-from-uffi-type (eval type) :allocate)))
188 (if (and (consp ctype) (eq 'array (car ctype)))
189 `(sb-alien:with-alien ((,obj ,ctype))
192 `(sb-alien:with-alien ((,obj ,ctype))
193 (let* ((,var (sb-alien:addr ,obj)))
196 `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
197 (eval type) :allocate)))
202 (defmacro with-foreign-objects (bindings &rest body)
204 `(with-foreign-object ,(car bindings)
205 (with-foreign-objects ,(cdr bindings)
210 (defmacro with-foreign-objects (bindings &rest body)
211 (let ((params nil) type count)
212 (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
213 (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
215 (when (and (listp type) (eq (first type) :array))
216 (setf count (nth 2 type))
217 (unless (integerp count) (error "Invalid size for array: ~a" type))
218 (setf type (nth 1 type)))
219 (push (list (first spec) (* count (size-of-foreign-type type))) params))
220 `(ccl:%stack-block ,params ,@body)))
223 (defmacro with-foreign-object ((var type) &rest body)
224 `(with-foreign-objects ((,var ,type))
228 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
229 `(fli:with-coerced-pointer (,binding-name
230 :type ',(convert-from-uffi-type (eval type) :type))
235 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
236 `(let ((,binding-name
237 (#+(or cmu scl) alien:cast
239 ,pointer (* ,(convert-from-uffi-type (eval type) :type)))))
242 #+(or allegro openmcl)
243 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
244 (declare (ignore type))
245 `(let ((,binding-name ,pointer))
248 #-(or lispworks cmu scl sbcl allegro)
249 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
250 (declare (ignore binding-name pointer type))
251 '(error "WITH-CAST-POINTER not (yet) implemented for ~A"
252 (lisp-implementation-type)))
254 (defmacro def-foreign-var (names type module)
255 #-lispworks (declare (ignore module))
256 (let ((foreign-name (if (atom names) names (first names)))
257 (lisp-name (if (atom names) (make-lisp-name names) (second names)))
259 (var-type (convert-from-uffi-type type :type)))
261 `(alien:def-alien-variable (,foreign-name ,lisp-name) ,var-type)
263 `(sb-alien:define-alien-variable (,foreign-name ,lisp-name) ,var-type)
265 `(define-symbol-macro ,lisp-name
266 (ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref))
267 :c (ff:get-entry-point ,foreign-name)))
270 (fli:define-foreign-variable (,lisp-name ,foreign-name)
271 :accessor :address-of
274 (define-symbol-macro ,lisp-name (fli:dereference (,lisp-name)
275 :copy-foreign-object nil)))
276 #-(or allegro cmu scl sbcl lispworks)
277 `(define-symbol-macro ,lisp-name
278 '(error "DEF-FOREIGN-VAR not (yet) defined for ~A"
279 (lisp-implementation-type)))))