1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net>
4 ;; Permission is hereby granted, free of charge, to any person obtaining
5 ;; a copy of this software and associated documentation files (the
6 ;; "Software"), to deal in the Software without restriction, including
7 ;; without limitation the rights to use, copy, modify, merge, publish,
8 ;; distribute, sublicense, and/or sell copies of the Software, and to
9 ;; permit persons to whom the Software is furnished to do so, subject to
10 ;; the following conditions:
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
15 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 ;; $Id: proxy.lisp,v 1.2 2006-06-08 13:25:09 espen Exp $
30 (defvar *instance-cache* (make-hash-table :test #'eql))
32 (defun cache-instance (instance &optional (weak-ref t))
34 (gethash (pointer-address (foreign-location instance)) *instance-cache*)
36 (make-weak-pointer instance)
39 (defun find-cached-instance (location)
40 (let ((ref (gethash (pointer-address location) *instance-cache*)))
42 (if (weak-pointer-p ref)
43 (weak-pointer-value ref)
46 (defun instance-cached-p (location)
47 (gethash (pointer-address location) *instance-cache*))
49 (defun remove-cached-instance (location)
50 (remhash (pointer-address location) *instance-cache*))
53 (defun list-cached-instances ()
55 (maphash #'(lambda (location ref)
56 (declare (ignore location))
61 ;; Instances that gets invalidated tend to be short lived, but created
62 ;; in large numbers. So we're keeping them in a hash table to be able
63 ;; to reuse them (and thus reduce consing)
64 (defvar *invalidated-instance-cache* (make-hash-table :test #'eql))
66 (defun cache-invalidated-instance (instance)
68 (gethash (class-of instance) *invalidated-instance-cache*)))
70 (defun find-invalidated-instance (class)
71 (when (gethash class *invalidated-instance-cache*)
72 (pop (gethash class *invalidated-instance-cache*))))
74 (defun list-invalidated-instances ()
76 (maphash #'(lambda (location ref)
77 (declare (ignore location))
79 *invalidated-instance-cache*)
84 ;;;; Proxy for alien instances
87 (defvar *foreign-instance-locations* (make-hash-table :weak :key))
89 ;; TODO: add a ref-counted-proxy subclass
90 (eval-when (:compile-toplevel :load-toplevel :execute)
91 (defclass proxy (virtual-slots-object)
92 (#-clisp(location :special t :type pointer))
93 (:metaclass virtual-slots-class)))
95 (defgeneric instance-finalizer (instance))
96 (defgeneric reference-function (class))
97 (defgeneric unreference-function (class))
98 (defgeneric invalidate-instance (instance &optional finalize-p))
99 (defgeneric allocate-foreign (object &key &allow-other-keys))
101 (defun foreign-location (instance)
102 #-clisp(slot-value instance 'location)
103 #+clisp(gethash instance *foreign-instance-locations*))
105 (defun (setf foreign-location) (location instance)
106 #-clisp(setf (slot-value instance 'location) location)
107 #+clisp(setf (gethash instance *foreign-instance-locations*) location))
109 (defun proxy-valid-p (instance)
110 #-clisp(slot-boundp instance 'location)
111 #+clisp(and (gethash instance *foreign-instance-locations*) t))
113 (defmethod reference-function ((name symbol))
114 (reference-function (find-class name)))
116 (defmethod unreference-function ((name symbol))
117 (unreference-function (find-class name)))
119 (defmethod print-object ((instance proxy) stream)
120 (print-unreadable-object (instance stream :type t :identity nil)
121 (if (proxy-valid-p instance)
122 (format stream "at 0x~X" (pointer-address (foreign-location instance)))
123 (write-string "at \"unbound\"" stream))))
126 (defmethod initialize-instance :around ((instance proxy) &rest initargs &key &allow-other-keys)
128 (foreign-location instance)
129 (apply #'allocate-foreign instance initargs))
132 (cache-instance instance)
133 (finalize instance (instance-finalizer instance))))
135 (defmethod instance-finalizer :around ((instance proxy))
136 (let ((finalizer (call-next-method)))
137 (let ((location (foreign-location instance)))
140 (remove-cached-instance location)
144 (declare (ignore instance))
145 (remove-cached-instance location)
146 (funcall finalizer)))))
148 (defmethod instance-finalizer ((instance proxy))
149 (let ((location (foreign-location instance))
150 (unref (unreference-function (class-of instance))))
152 (funcall unref location))))
154 ;; FINALIZE-P should always be given the same value as the keyword
155 ;; argument :FINALZIE given to MAKE-PROXY-INSTANCE or non NIL if the
156 ;; proxy was created with MAKE-INSTANCE
157 (defmethod invalidate-instance ((instance proxy) &optional finalize-p)
158 (remove-cached-instance (foreign-location instance))
162 (funcall (instance-finalizer instance)))
163 (slot-makunbound instance 'location)
164 (cancel-finalization instance))
165 ;; We can't cached invalidated instances in CLISP beacuse it is
166 ;; not possible to cancel finalization
167 #-clisp(cache-invalidated-instance instance))
170 ;;;; Metaclass used for subclasses of proxy
172 (eval-when (:compile-toplevel :load-toplevel :execute)
173 (defclass proxy-class (virtual-slots-class)
174 ((size :accessor foreign-size)
175 (packed :reader foreign-slots-packed-p)
176 (ref :reader reference-function)
177 (unref :reader unreference-function)))
179 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
180 ((offset :reader slot-definition-offset :initarg :offset))
181 (:default-initargs :allocation :alien))
183 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
184 ((offset :reader slot-definition-offset :initarg :offset)))
186 (defclass direct-virtual-alien-slot-definition (direct-virtual-slot-definition)
189 (defclass effective-virtual-alien-slot-definition (effective-virtual-slot-definition)
192 (defgeneric foreign-size-p (class))
193 (defgeneric most-specific-proxy-superclass (class))
194 (defgeneric direct-proxy-superclass (class))
196 (defmethod foreign-size-p ((class proxy-class))
197 (slot-boundp class 'size))
199 (defmethod most-specific-proxy-superclass ((class proxy-class))
202 (subtypep (class-name class) 'proxy))
203 (cdr (compute-class-precedence-list class))))
205 (defmethod direct-proxy-superclass ((class proxy-class))
208 (subtypep (class-name class) 'proxy))
209 (class-direct-superclasses class)))
211 (defmethod shared-initialize ((class proxy-class) names
212 &key size packed ref unref)
213 (declare (ignore names))
215 (size (setf (slot-value class 'size) (first size)))
216 ((slot-boundp class 'size) (slot-makunbound class 'size)))
217 (setf (slot-value class 'packed) (first packed))
219 (setf (slot-value class 'ref) (first ref)))
221 (setf (slot-value class 'unref) (first unref)))
224 (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
225 (case (getf initargs :allocation)
226 (:alien (find-class 'direct-alien-slot-definition))
227 (:virtual (find-class 'direct-virtual-alien-slot-definition))
228 (t (call-next-method))))
230 (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
231 (case (getf initargs :allocation)
232 (:alien (find-class 'effective-alien-slot-definition))
233 (:virtual (find-class 'effective-virtual-alien-slot-definition))
234 (t (call-next-method))))
237 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
238 (if (eq (slot-definition-allocation (first direct-slotds)) :alien)
240 (list :offset (most-specific-slot-value direct-slotds 'offset))
245 (defmethod compute-slot-reader-function ((slotd effective-alien-slot-definition))
246 (let* ((type (slot-definition-type slotd))
247 (offset (slot-definition-offset slotd))
248 (reader (reader-function type)))
250 (funcall reader (foreign-location object) offset))))
252 (defmethod compute-slot-writer-function ((slotd effective-alien-slot-definition))
253 (let* ((type (slot-definition-type slotd))
254 (offset (slot-definition-offset slotd))
255 (writer (writer-function type))
256 (destroy (destroy-function type)))
257 #'(lambda (value object)
258 (let ((location (foreign-location object)))
259 (funcall destroy location offset) ; destroy old value
260 (funcall writer value location offset))
263 (defmethod compute-slot-reader-function ((slotd effective-virtual-alien-slot-definition))
264 (if (and (slot-boundp slotd 'getter) (stringp (slot-definition-getter slotd)))
265 (let ((getter (slot-definition-getter slotd))
266 (type (slot-definition-type slotd))
270 (setq reader (mkbinding getter type 'pointer)))
271 (funcall reader (foreign-location object))))
274 (defmethod compute-slot-writer-function ((slotd effective-virtual-alien-slot-definition))
275 (if (and (slot-boundp slotd 'setter) (stringp (slot-definition-setter slotd)))
276 (let ((setter (slot-definition-setter slotd))
277 (type (slot-definition-type slotd))
279 #'(lambda (value object)
281 (setq writer (mkbinding setter nil 'pointer type)))
282 (funcall writer (foreign-location object) value)))
285 (defun adjust-offset (offset type &optional packed-p)
286 (let ((alignment (type-alignment type)))
287 (if (or packed-p (zerop (mod offset alignment)))
289 (+ offset (- alignment (mod offset alignment))))))
291 (defmethod compute-slots ((class proxy-class))
292 (let ((alien-slots (remove-if-not
293 #'(lambda (allocation) (eq allocation :alien))
294 (class-direct-slots class)
295 :key #'slot-definition-allocation)))
298 with packed-p = (foreign-slots-packed-p class)
299 for slotd in alien-slots
300 as offset = (adjust-offset
301 (foreign-size (most-specific-proxy-superclass class))
302 (slot-definition-type slotd)
304 then (adjust-offset offset (slot-definition-type slotd) packed-p)
305 do (if (slot-boundp slotd 'offset)
306 (setf offset (slot-value slotd 'offset))
307 (setf (slot-value slotd 'offset) offset))
308 (incf offset (size-of (slot-definition-type slotd))))))
311 (defmethod validate-superclass ((class proxy-class) (super standard-class))
312 (subtypep (class-name super) 'proxy))
314 (defmethod foreign-size ((class-name symbol))
315 (foreign-size (find-class class-name))))
317 (defmethod foreign-size ((object proxy))
318 (foreign-size (class-of object)))
320 (define-type-method alien-type ((type proxy))
321 (declare (ignore type))
322 (alien-type 'pointer))
324 (define-type-method size-of ((type proxy) &key inlined)
325 (assert-not-inlined type inlined)
328 (define-type-method type-alignment ((type proxy) &key inlined)
329 (assert-not-inlined type inlined)
330 (type-alignment 'pointer))
332 (define-type-method from-alien-form ((type proxy) form &key (ref :free))
333 (let ((class (type-expand type)))
335 (:free `(ensure-proxy-instance ',class ,form :reference nil))
336 (:copy `(ensure-proxy-instance ',class ,form))
337 ((:static :temp) `(ensure-proxy-instance ',class ,form
338 :reference nil :finalize nil)))))
340 (define-type-method from-alien-function ((type proxy) &key (ref :free))
341 (let ((class (type-expand type)))
345 (ensure-proxy-instance class location :reference nil)))
348 (ensure-proxy-instance class location)))
351 (ensure-proxy-instance class location :reference nil :finalize nil))))))
353 (define-type-method to-alien-form ((type proxy) instance &optional copy-p)
355 (let* ((class (type-expand type))
356 (ref (reference-function class)))
358 `(,ref (foreign-location ,instance))
359 `(funcall (reference-function ',class)
360 (foreign-location ,instance))))
361 `(foreign-location ,instance)))
363 (define-type-method to-alien-function ((type proxy) &optional copy-p)
365 (let ((ref (reference-function (type-expand type))))
367 (funcall ref (foreign-location instance))))
370 (define-type-method writer-function ((type proxy) &key temp inlined)
371 (assert-not-inlined type inlined)
373 #'(lambda (instance location &optional (offset 0))
374 (assert (null-pointer-p (ref-pointer location offset)))
375 (setf (ref-pointer location offset) (foreign-location instance)))
376 (let ((ref (reference-function (type-expand type))))
377 #'(lambda (instance location &optional (offset 0))
378 (assert (null-pointer-p (ref-pointer location offset)))
380 (ref-pointer location offset)
381 (funcall ref (foreign-location instance)))))))
383 (define-type-method reader-function ((type proxy) &key (ref :read) inlined)
384 (assert-not-inlined type inlined)
385 (let ((class (type-expand type)))
388 #'(lambda (location &optional (offset 0))
389 (let ((instance (ref-pointer location offset)))
390 (unless (null-pointer-p instance)
391 (ensure-proxy-instance class instance)))))
393 #'(lambda (location &optional (offset 0))
394 (let ((instance (ref-pointer location offset)))
395 (unless (null-pointer-p instance)
396 (ensure-proxy-instance class instance
397 :reference nil :finalize nil)))))
399 #'(lambda (location &optional (offset 0))
400 (let ((instance (ref-pointer location offset)))
401 (unless (null-pointer-p instance)
403 (ensure-proxy-instance class instance :reference nil)
404 (setf (ref-pointer location offset) (make-pointer 0))))))))))
406 (define-type-method destroy-function ((type proxy) &key temp inlined)
407 (assert-not-inlined type inlined)
409 #'(lambda (location &optional (offset 0))
410 (setf (ref-pointer location offset) (make-pointer 0)))
411 (let ((unref (unreference-function (type-expand type))))
412 #'(lambda (location &optional (offset 0))
413 (unless (null-pointer-p (ref-pointer location offset))
414 (funcall unref (ref-pointer location offset))
415 (setf (ref-pointer location offset) (make-pointer 0)))))))
417 (define-type-method copy-function ((type proxy) &key inlined)
418 (assert-not-inlined type inlined)
419 (let ((ref (reference-function (type-expand type))))
420 #'(lambda (from to &optional (offset 0))
421 (let ((instance (ref-pointer from offset)))
422 (unless (null-pointer-p instance)
423 (funcall ref instance))
424 (setf (ref-pointer to offset) instance)))))
426 (define-type-method unbound-value ((type proxy))
427 (declare (ignore type))
430 (defun ensure-proxy-instance (class location &rest initargs)
431 "Returns a proxy object representing the foreign object at the give
432 location. If an existing proxy object is not found,
433 MAKE-PROXY-INSTANCE is called to create a new one. A second return
434 value indicates whether a new proxy was created or not."
435 (unless (null-pointer-p location)
437 #-debug-ref-counting(find-cached-instance location)
439 (let ((instance (find-cached-instance location)))
441 (format t "Object found in cache: ~A~%" instance)
444 (apply #'make-proxy-instance class location initargs)
447 (defgeneric make-proxy-instance (class location &key reference finalize)
448 (:documentation "Creates a new proxy object representing the foreign
449 object at the give location."))
451 (defmethod make-proxy-instance ((class symbol) location &rest initargs)
452 (apply #'make-proxy-instance (find-class class) location initargs))
454 (defmethod make-proxy-instance ((class proxy-class) location
455 &key (reference t) (finalize t))
458 (find-invalidated-instance class)
459 (allocate-instance class))))
460 (setf (foreign-location instance)
462 (funcall (reference-function class) location)
466 (instance-finalizer instance)
467 ;; We still need to remove the instance from the cache even if we
468 ;; don't do normal finalization
469 (let ((location (foreign-location instance)))
472 (remove-cached-instance location))
475 (declare (ignore instance))
476 (remove-cached-instance location)))))
477 (cache-instance instance)
481 ;;;; Superclasses for wrapping of C structures
483 (defclass struct (proxy)
485 (:metaclass proxy-class)
488 (defmethod allocate-foreign ((struct struct) &rest initargs)
489 (declare (ignore initargs))
490 (let ((size (foreign-size (class-of struct))))
492 (error "~A has zero size" (class-of struct))
493 (allocate-memory size))))
496 ;;;; Metaclasses used for subclasses of struct
498 (defclass struct-class (proxy-class)
501 (defmethod shared-initialize ((class struct-class) names &rest initargs)
502 (declare (ignore names initargs))
504 (let ((offsets nil) (copy-functions nil) (destroy-functions nil))
505 (flet ((initialize-functions ()
507 for slotd in (class-slots class)
508 as type = (slot-definition-type slotd)
509 when (eq (slot-definition-allocation slotd) :alien)
510 do (push (slot-definition-offset slotd) offsets)
511 (push (copy-function type) copy-functions)
512 (push (destroy-function type) destroy-functions))))
513 (unless (slot-boundp class 'ref)
515 (slot-value class 'ref)
516 #'(lambda (from &optional (to (allocate-memory (foreign-size class))))
517 (assert (not (null-pointer-p from)))
519 (initialize-functions))
521 for offset in offsets
522 for copy in copy-functions
523 do (funcall copy from to offset))
525 (unless (slot-boundp class 'unref)
526 (setf (slot-value class 'unref)
527 #'(lambda (location &optional inlined-p)
528 (assert (not (null-pointer-p location)))
530 (initialize-functions))
532 for offset in offsets
533 for destroy in destroy-functions
534 do (funcall destroy location offset))
536 (deallocate-memory location))))))))
539 (defmethod direct-slot-definition-class ((class struct-class) &rest initargs)
540 (if (not (getf initargs :allocation))
541 (find-class 'direct-alien-slot-definition)
545 (defmethod compute-slots :around ((class struct-class))
546 (let ((slots (call-next-method)))
548 #?-(or (sbcl>= 0 9 8) (featurep :clisp))(class-finalized-p class)
549 (not (slot-boundp class 'size)))
550 (setf (slot-value class 'size)
554 when (eq (slot-definition-allocation slotd) :alien)
556 (slot-definition-offset slotd)
557 (size-of (slot-definition-type slotd))))
561 (define-type-method callback-wrapper ((type struct) var arg form)
562 (let ((class (type-expand type)))
563 `(let ((,var (ensure-proxy-instance ',class ,arg :finalize nil)))
566 (invalidate-instance ,var)))))
568 (define-type-method size-of ((type struct) &key inlined)
573 (define-type-method type-alignment ((type struct) &key inlined)
575 (let ((slot1 (find-if
577 (eq (slot-definition-allocation slotd) :alien))
578 (class-slots (find-class type)))))
579 (type-alignment (slot-definition-type slot1)))
580 (type-alignment 'pointer)))
582 (define-type-method writer-function ((type struct) &key temp inlined)
585 (let ((size (size-of type :inlined t)))
586 #'(lambda (instance location &optional (offset 0))
588 (foreign-location instance) size
589 (pointer+ location offset))))
590 (let ((ref (reference-function (type-expand type))))
591 #'(lambda (instance location &optional (offset 0))
593 (foreign-location instance)
594 (pointer+ location offset)))))
597 (define-type-method reader-function ((type struct) &key (ref :read) inlined)
599 (let ((class (type-expand type))
600 (size (size-of type :inlined t)))
603 #'(lambda (location &optional (offset 0))
604 (ensure-proxy-instance class (pointer+ location offset))))
606 #'(lambda (location &optional (offset 0))
607 (ensure-proxy-instance class (pointer+ location offset)
608 :reference nil :finalize nil)))
610 #'(lambda (location &optional (offset 0))
612 (ensure-proxy-instance class
613 (copy-memory (pointer+ location offset) size)
615 (clear-memory (pointer+ location offset) size))))))
618 (define-type-method destroy-function ((type struct) &key temp inlined)
620 (let ((size (size-of type :inlined t)))
622 #'(lambda (location &optional (offset 0))
623 (clear-memory (pointer+ location offset) size))
624 (let ((unref (unreference-function (type-expand type))))
625 #'(lambda (location &optional (offset 0))
626 (funcall unref (pointer+ location offset) t)))))
629 (define-type-method copy-function ((type struct) &key inlined)
631 (let ((ref (reference-function (type-expand type))))
632 #'(lambda (from to &optional (offset 0))
633 (funcall ref (pointer+ from offset) (pointer+ to offset))))