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)))
54 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
56 `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
58 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
60 `(ff:allocate-fobject (list :array (quote ,(convert-from-uffi-type type :allocate)) ,size) :c)
62 `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
65 (defmacro free-foreign-object (obj)
67 `(alien:free-alien ,obj)
69 `(sb-alien:free-alien ,obj)
71 `(fli:free-foreign-object ,obj)
73 `(ff:free-fobject ,obj)
78 (defmacro null-pointer-p (obj)
79 #+lispworks `(fli:null-pointer-p ,obj)
80 #+allegro `(zerop ,obj)
81 #+(or cmu scl) `(alien:null-alien ,obj)
82 #+sbcl `(sb-alien:null-alien ,obj)
83 #+mcl `(ccl:%null-ptr-p ,obj)
86 (defmacro make-null-pointer (type)
87 #+(or allegro mcl) (declare (ignore type))
88 #+(or cmu scl) `(alien:sap-alien (system:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
89 #+sbcl `(sb-alien:sap-alien (sb-sys:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
90 #+lispworks `(fli:make-pointer :address 0 :type (quote ,(convert-from-uffi-type (eval type) :type)))
92 #+mcl `(ccl:%null-ptr)
95 (defmacro make-pointer (addr type)
96 #+(or allegro mcl) (declare (ignore type))
97 #+(or cmu scl) `(alien:sap-alien (system:int-sap ,addr) (* ,(convert-from-uffi-type (eval type) :type)))
98 #+sbcl `(sb-alien:sap-alien (sb-sys:int-sap ,addr) (* ,(convert-from-uffi-type (eval type) :type)))
99 #+lispworks `(fli:make-pointer :address ,addr :type (quote ,(convert-from-uffi-type (eval type) :type)))
101 #+mcl `(ccl:%int-to-ptr ,addr)
105 (defmacro char-array-to-pointer (obj)
106 #+(or cmu scl) `(alien:cast ,obj (* (alien:unsigned 8)))
107 #+sbcl `(sb-alien:cast ,obj (* (sb-alien:unsigned 8)))
108 #+lispworks `(fli:make-pointer :type '(:unsigned :char)
109 :address (fli:pointer-address ,obj))
114 (defmacro deref-pointer (ptr type)
115 "Returns a object pointed"
116 #+(or cmu sbcl lispworks scl) (declare (ignore type))
117 #+(or cmu scl) `(alien:deref ,ptr)
118 #+sbcl `(sb-alien:deref ,ptr)
119 #+lispworks `(fli:dereference ,ptr)
120 #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref)) :c ,ptr)
121 #+mcl `(ccl:pref ,ptr ,(convert-from-uffi-type type :deref))
124 #+(and mcl (not openmcl))
125 (defmacro deref-pointer-set (ptr type value)
126 `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
128 #+(and mcl (not openmcl))
129 (defsetf deref-pointer deref-pointer-set)
131 (defmacro ensure-char-character (obj)
132 #+(or (and mcl (not openmcl))) obj
133 #+(or allegro cmu sbcl scl openmcl) `(code-char ,obj)
134 ;; lispworks varies whether deref'ing array vs. slot access of a char
136 `(if (characterp ,obj) ,obj (code-char ,obj)))
138 (defmacro ensure-char-integer (obj)
139 #+(or (and mcl (not openmcl))) `(char-code ,obj)
140 #+(or allegro cmu sbcl scl openmcl) obj
141 `(if (characterp ,obj) (char-code ,obj) ,obj))
143 (defmacro ensure-char-storable (obj)
144 #+(or lispworks (and mcl (not openmcl))) obj
145 #+(or allegro cmu sbcl scl openmcl) `(char-code ,obj))
147 (defmacro pointer-address (obj)
149 `(system:sap-int (alien:alien-sap ,obj))
151 `(sb-sys:sap-int (sb-alien:alien-sap ,obj))
153 `(fli:pointer-address ,obj)
157 `(ccl:%ptr-to-int ,obj)
160 ;; TYPE is evaluated.
162 (defmacro with-foreign-object ((var type) &rest body)
163 #-(or cmu sbcl lispworks scl) ; default version
164 `(let ((,var (allocate-foreign-object ,type)))
167 (free-foreign-object ,var)))
170 (ctype (convert-from-uffi-type (eval type) :allocate)))
171 (if (and (consp ctype) (eq 'array (car ctype)))
172 `(alien:with-alien ((,obj ,ctype))
175 `(alien:with-alien ((,obj ,ctype))
176 (let* ((,var (alien:addr ,obj)))
180 (ctype (convert-from-uffi-type (eval type) :allocate)))
181 (if (and (consp ctype) (eq 'array (car ctype)))
182 `(sb-alien:with-alien ((,obj ,ctype))
185 `(sb-alien:with-alien ((,obj ,ctype))
186 (let* ((,var (sb-alien:addr ,obj)))
189 `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
190 (eval type) :allocate)))
195 (defmacro with-foreign-objects (bindings &rest body)
197 `(with-foreign-object ,(car bindings)
198 (with-foreign-objects ,(cdr bindings)
203 (defmacro with-foreign-objects (bindings &rest body)
204 (let ((params nil) type count)
205 (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
206 (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
208 (when (and (listp type) (eq (first type) :array))
209 (setf count (nth 2 type))
210 (unless (integerp count) (error "Invalid size for array: ~a" type))
211 (setf type (nth 1 type)))
212 (push (list (first spec) (* count (size-of-foreign-type type))) params))
213 `(ccl:%stack-block ,params ,@body)))
216 (defmacro with-foreign-object ((var type) &rest body)
217 `(with-foreign-objects ((,var ,type))
221 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
222 `(fli:with-coerced-pointer (,binding-name
223 :type ',(convert-from-uffi-type (eval type) :type))
228 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
229 `(let ((,binding-name
230 (#+(or cmu scl) alien:cast
232 ,pointer (* ,(convert-from-uffi-type (eval type) :type)))))
235 #+(or allegro openmcl)
236 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
237 (declare (ignore type))
238 `(let ((,binding-name ,pointer))
241 #-(or lispworks cmu scl sbcl allegro)
242 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
243 (declare (ignore binding-name pointer type))
244 '(error "WITH-CAST-POINTER not (yet) implemented for ~A"
245 (lisp-implementation-type)))
247 (defmacro def-foreign-var (names type module)
248 #-lispworks (declare (ignore module))
249 (let ((foreign-name (if (atom names) names (first names)))
250 (lisp-name (if (atom names) (make-lisp-name names) (second names)))
252 (var-type (convert-from-uffi-type type :type)))
254 `(alien:def-alien-variable (,foreign-name ,lisp-name) ,var-type)
256 `(sb-alien:define-alien-variable (,foreign-name ,lisp-name) ,var-type)
258 `(define-symbol-macro ,lisp-name
259 (ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref))
260 :c (ff:get-entry-point ,foreign-name)))
263 (fli:define-foreign-variable (,lisp-name ,foreign-name)
264 :accessor :address-of
267 (define-symbol-macro ,lisp-name (fli:dereference (,lisp-name)
268 :copy-foreign-object nil)))
269 #-(or allegro cmu scl sbcl lispworks)
270 `(define-symbol-macro ,lisp-name
271 '(error "DEF-FOREIGN-VAR not (yet) defined for ~A"
272 (lisp-implementation-type)))))