c2c3fbccec91a10150b076a7afc3e9858b1049eb
[clg] / gffi / proxy.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net>
3 ;;
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:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
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.
22
23 ;; $Id: proxy.lisp,v 1.3 2006-08-16 11:02:45 espen Exp $
24
25 (in-package "GFFI")
26
27
28 ;;;; Proxy cache
29
30 (defvar *instance-cache* (make-hash-table :test #'eql))
31
32 (defun cache-instance (instance &optional (weak-ref t))
33 (setf
34 (gethash (pointer-address (foreign-location instance)) *instance-cache*)
35 (if weak-ref
36 (make-weak-pointer instance)
37 instance)))
38
39 (defun find-cached-instance (location)
40 (let ((ref (gethash (pointer-address location) *instance-cache*)))
41 (when ref
42 (if (weak-pointer-p ref)
43 (weak-pointer-value ref)
44 ref))))
45
46 (defun instance-cached-p (location)
47 (gethash (pointer-address location) *instance-cache*))
48
49 (defun remove-cached-instance (location)
50 (remhash (pointer-address location) *instance-cache*))
51
52 ;; For debuging
53 (defun list-cached-instances ()
54 (let ((instances ()))
55 (maphash #'(lambda (location ref)
56 (declare (ignore location))
57 (push ref instances))
58 *instance-cache*)
59 instances))
60
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))
65
66 (defun cache-invalidated-instance (instance)
67 (push instance
68 (gethash (class-of instance) *invalidated-instance-cache*)))
69
70 (defun find-invalidated-instance (class)
71 (when (gethash class *invalidated-instance-cache*)
72 (pop (gethash class *invalidated-instance-cache*))))
73
74 (defun list-invalidated-instances ()
75 (let ((instances ()))
76 (maphash #'(lambda (location ref)
77 (declare (ignore location))
78 (push ref instances))
79 *invalidated-instance-cache*)
80 instances))
81
82
83
84 ;;;; Proxy for alien instances
85
86 #+clisp
87 (defvar *foreign-instance-locations* (make-hash-table :weak :key))
88
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)))
94
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))
100
101 (defun foreign-location (instance)
102 #-clisp(slot-value instance 'location)
103 #+clisp(gethash instance *foreign-instance-locations*))
104
105 (defun (setf foreign-location) (location instance)
106 #-clisp(setf (slot-value instance 'location) location)
107 #+clisp(setf (gethash instance *foreign-instance-locations*) location))
108
109 (defun proxy-valid-p (instance)
110 #-clisp(slot-boundp instance 'location)
111 #+clisp(and (gethash instance *foreign-instance-locations*) t))
112
113 (defmethod reference-function ((name symbol))
114 (reference-function (find-class name)))
115
116 (defmethod unreference-function ((name symbol))
117 (unreference-function (find-class name)))
118
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))))
124
125
126 (defmethod initialize-instance :around ((instance proxy) &rest initargs &key &allow-other-keys)
127 (setf
128 (foreign-location instance)
129 (apply #'allocate-foreign instance initargs))
130 (prog1
131 (call-next-method)
132 (cache-instance instance)
133 (finalize instance (instance-finalizer instance))))
134
135 (defmethod instance-finalizer :around ((instance proxy))
136 (let ((finalizer (call-next-method)))
137 (let ((location (foreign-location instance)))
138 #+(or cmu sbcl)
139 #'(lambda ()
140 (remove-cached-instance location)
141 (funcall finalizer))
142 #+clisp
143 #'(lambda (instance)
144 (declare (ignore instance))
145 (remove-cached-instance location)
146 (funcall finalizer)))))
147
148 (defmethod instance-finalizer ((instance proxy))
149 (let ((location (foreign-location instance))
150 (unref (unreference-function (class-of instance))))
151 #'(lambda ()
152 (funcall unref location))))
153
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))
159 #+(or sbcl cmu)
160 (progn
161 (when finalize-p
162 (funcall (instance-finalizer instance)))
163 (slot-makunbound instance 'location)
164 (cancel-finalization instance))
165 ;; We can't cache invalidated instances in CLISP beacuse it is
166 ;; not possible to cancel finalization
167 #-clisp(cache-invalidated-instance instance))
168
169
170 ;;;; Metaclass used for subclasses of proxy
171
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)))
178
179 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
180 ((offset :reader slot-definition-offset :initarg :offset))
181 (:default-initargs :allocation :alien))
182
183 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
184 ((offset :reader slot-definition-offset :initarg :offset)))
185
186 (defclass direct-virtual-alien-slot-definition (direct-virtual-slot-definition)
187 ())
188
189 (defclass effective-virtual-alien-slot-definition (effective-virtual-slot-definition)
190 ())
191
192 (defgeneric foreign-size-p (class))
193 (defgeneric most-specific-proxy-superclass (class))
194 (defgeneric direct-proxy-superclass (class))
195
196 (defmethod foreign-size-p ((class proxy-class))
197 (slot-boundp class 'size))
198
199 (defmethod most-specific-proxy-superclass ((class proxy-class))
200 (find-if
201 #'(lambda (class)
202 (subtypep (class-name class) 'proxy))
203 (cdr (compute-class-precedence-list class))))
204
205 (defmethod direct-proxy-superclass ((class proxy-class))
206 (find-if
207 #'(lambda (class)
208 (subtypep (class-name class) 'proxy))
209 (class-direct-superclasses class)))
210
211 (defmethod shared-initialize ((class proxy-class) names
212 &key size packed ref unref)
213 (declare (ignore names))
214 (cond
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))
218 (when ref
219 (setf (slot-value class 'ref) (first ref)))
220 (when unref
221 (setf (slot-value class 'unref) (first unref)))
222 (call-next-method))
223
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))))
229
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))))
235
236
237 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
238 (if (eq (slot-definition-allocation (first direct-slotds)) :alien)
239 (nconc
240 (list :offset (most-specific-slot-value direct-slotds 'offset))
241 (call-next-method))
242 (call-next-method)))
243
244
245 (defmethod compute-slot-reader-function ((slotd effective-alien-slot-definition) &optional signal-unbound-p)
246 (declare (ignore signal-unbound-p))
247 (let* ((type (slot-definition-type slotd))
248 (offset (slot-definition-offset slotd))
249 (reader (reader-function type)))
250 #'(lambda (object)
251 (funcall reader (foreign-location object) offset))))
252
253 (defmethod compute-slot-writer-function ((slotd effective-alien-slot-definition))
254 (let* ((type (slot-definition-type slotd))
255 (offset (slot-definition-offset slotd))
256 (writer (writer-function type))
257 (destroy (destroy-function type)))
258 #'(lambda (value object)
259 (let ((location (foreign-location object)))
260 (funcall destroy location offset) ; destroy old value
261 (funcall writer value location offset))
262 value)))
263
264 (defmethod compute-slot-reader-function ((slotd effective-virtual-alien-slot-definition) &optional signal-unbound-p)
265 (declare (ignore signal-unbound-p))
266 (if (and (slot-boundp slotd 'getter) (stringp (slot-definition-getter slotd)))
267 (let ((getter (slot-definition-getter slotd))
268 (type (slot-definition-type slotd))
269 (reader nil))
270 #'(lambda (object)
271 (unless reader
272 (setq reader (mkbinding getter type 'pointer)))
273 (funcall reader (foreign-location object))))
274 (call-next-method)))
275
276 (defmethod compute-slot-writer-function ((slotd effective-virtual-alien-slot-definition))
277 (if (and (slot-boundp slotd 'setter) (stringp (slot-definition-setter slotd)))
278 (let ((setter (slot-definition-setter slotd))
279 (type (slot-definition-type slotd))
280 (writer nil))
281 #'(lambda (value object)
282 (unless writer
283 (setq writer (mkbinding setter nil 'pointer type)))
284 (funcall writer (foreign-location object) value)))
285 (call-next-method)))
286
287 (defun adjust-offset (offset type &optional packed-p)
288 (let ((alignment (type-alignment type)))
289 (if (or packed-p (zerop (mod offset alignment)))
290 offset
291 (+ offset (- alignment (mod offset alignment))))))
292
293 (defmethod compute-slots ((class proxy-class))
294 (let ((alien-slots (remove-if-not
295 #'(lambda (allocation) (eq allocation :alien))
296 (class-direct-slots class)
297 :key #'slot-definition-allocation)))
298 (when alien-slots
299 (loop
300 with packed-p = (foreign-slots-packed-p class)
301 for slotd in alien-slots
302 as offset = (adjust-offset
303 (foreign-size (most-specific-proxy-superclass class))
304 (slot-definition-type slotd)
305 packed-p)
306 then (adjust-offset offset (slot-definition-type slotd) packed-p)
307 do (if (slot-boundp slotd 'offset)
308 (setf offset (slot-value slotd 'offset))
309 (setf (slot-value slotd 'offset) offset))
310 (incf offset (size-of (slot-definition-type slotd))))))
311 (call-next-method))
312
313 (defmethod validate-superclass ((class proxy-class) (super standard-class))
314 (subtypep (class-name super) 'proxy))
315
316 (defmethod foreign-size ((class-name symbol))
317 (foreign-size (find-class class-name))))
318
319 (defmethod foreign-size ((object proxy))
320 (foreign-size (class-of object)))
321
322 (define-type-method alien-type ((type proxy))
323 (declare (ignore type))
324 (alien-type 'pointer))
325
326 (define-type-method size-of ((type proxy) &key inlined)
327 (assert-not-inlined type inlined)
328 (size-of 'pointer))
329
330 (define-type-method type-alignment ((type proxy) &key inlined)
331 (assert-not-inlined type inlined)
332 (type-alignment 'pointer))
333
334 (define-type-method from-alien-form ((type proxy) form &key (ref :free))
335 (let ((class (type-expand type)))
336 (ecase ref
337 (:free `(ensure-proxy-instance ',class ,form :reference nil))
338 (:copy `(ensure-proxy-instance ',class ,form))
339 ((:static :temp) `(ensure-proxy-instance ',class ,form
340 :reference nil :finalize nil)))))
341
342 (define-type-method from-alien-function ((type proxy) &key (ref :free))
343 (let ((class (type-expand type)))
344 (ecase ref
345 (:free
346 #'(lambda (location)
347 (ensure-proxy-instance class location :reference nil)))
348 (:copy
349 #'(lambda (location)
350 (ensure-proxy-instance class location)))
351 ((:static :temp)
352 #'(lambda (location)
353 (ensure-proxy-instance class location :reference nil :finalize nil))))))
354
355 (define-type-method to-alien-form ((type proxy) instance &optional copy-p)
356 (if copy-p
357 (let* ((class (type-expand type))
358 (ref (reference-function class)))
359 (if (symbolp ref)
360 `(,ref (foreign-location ,instance))
361 `(funcall (reference-function ',class)
362 (foreign-location ,instance))))
363 `(foreign-location ,instance)))
364
365 (define-type-method to-alien-function ((type proxy) &optional copy-p)
366 (if copy-p
367 (let ((ref (reference-function (type-expand type))))
368 #'(lambda (instance)
369 (funcall ref (foreign-location instance))))
370 #'foreign-location))
371
372 (define-type-method writer-function ((type proxy) &key temp inlined)
373 (assert-not-inlined type inlined)
374 (if temp
375 #'(lambda (instance location &optional (offset 0))
376 (assert (null-pointer-p (ref-pointer location offset)))
377 (setf (ref-pointer location offset) (foreign-location instance)))
378 (let ((ref (reference-function (type-expand type))))
379 #'(lambda (instance location &optional (offset 0))
380 (assert (null-pointer-p (ref-pointer location offset)))
381 (setf
382 (ref-pointer location offset)
383 (funcall ref (foreign-location instance)))))))
384
385 (define-type-method reader-function ((type proxy) &key (ref :read) inlined)
386 (assert-not-inlined type inlined)
387 (let ((class (type-expand type)))
388 (ecase ref
389 (:read
390 #'(lambda (location &optional (offset 0))
391 (let ((instance (ref-pointer location offset)))
392 (unless (null-pointer-p instance)
393 (ensure-proxy-instance class instance)))))
394 (:peek
395 #'(lambda (location &optional (offset 0))
396 (let ((instance (ref-pointer location offset)))
397 (unless (null-pointer-p instance)
398 (ensure-proxy-instance class instance
399 :reference nil :finalize nil)))))
400 (:get
401 #'(lambda (location &optional (offset 0))
402 (let ((instance (ref-pointer location offset)))
403 (unless (null-pointer-p instance)
404 (prog1
405 (ensure-proxy-instance class instance :reference nil)
406 (setf (ref-pointer location offset) (make-pointer 0))))))))))
407
408 (define-type-method destroy-function ((type proxy) &key temp inlined)
409 (assert-not-inlined type inlined)
410 (if temp
411 #'(lambda (location &optional (offset 0))
412 (setf (ref-pointer location offset) (make-pointer 0)))
413 (let ((unref (unreference-function (type-expand type))))
414 #'(lambda (location &optional (offset 0))
415 (unless (null-pointer-p (ref-pointer location offset))
416 (funcall unref (ref-pointer location offset))
417 (setf (ref-pointer location offset) (make-pointer 0)))))))
418
419 (define-type-method copy-function ((type proxy) &key inlined)
420 (assert-not-inlined type inlined)
421 (let ((ref (reference-function (type-expand type))))
422 #'(lambda (from to &optional (offset 0))
423 (let ((instance (ref-pointer from offset)))
424 (unless (null-pointer-p instance)
425 (funcall ref instance))
426 (setf (ref-pointer to offset) instance)))))
427
428 (define-type-method unbound-value ((type proxy))
429 (declare (ignore type))
430 nil)
431
432 (defun ensure-proxy-instance (class location &rest initargs)
433 "Returns a proxy object representing the foreign object at the give
434 location. If an existing proxy object is not found,
435 MAKE-PROXY-INSTANCE is called to create a new one. A second return
436 value indicates whether a new proxy was created or not."
437 (unless (null-pointer-p location)
438 (or
439 #-debug-ref-counting(find-cached-instance location)
440 #+debug-ref-counting
441 (let ((instance (find-cached-instance location)))
442 (when instance
443 (format t "Object found in cache: ~A~%" instance)
444 instance))
445 (values
446 (apply #'make-proxy-instance class location initargs)
447 t))))
448
449 (defgeneric make-proxy-instance (class location &key reference finalize)
450 (:documentation "Creates a new proxy object representing the foreign
451 object at the give location."))
452
453 (defmethod make-proxy-instance ((class symbol) location &rest initargs)
454 (apply #'make-proxy-instance (find-class class) location initargs))
455
456 (defmethod make-proxy-instance ((class proxy-class) location
457 &key (reference t) (finalize t))
458 (let ((instance
459 (or
460 (find-invalidated-instance class)
461 (allocate-instance class))))
462 (setf (foreign-location instance)
463 (if reference
464 (funcall (reference-function class) location)
465 location))
466 (finalize instance
467 (if finalize
468 (instance-finalizer instance)
469 ;; We still need to remove the instance from the cache even if we
470 ;; don't do normal finalization
471 (let ((location (foreign-location instance)))
472 #+(or cmu sbcl)
473 #'(lambda ()
474 (remove-cached-instance location))
475 #+clisp
476 #'(lambda (instance)
477 (declare (ignore instance))
478 (remove-cached-instance location)))))
479 (cache-instance instance)
480 instance))
481
482
483 ;;;; Superclasses for wrapping of C structures
484
485 (defclass struct (proxy)
486 ()
487 (:metaclass proxy-class)
488 (:size 0))
489
490 (defmethod allocate-foreign ((struct struct) &rest initargs)
491 (declare (ignore initargs))
492 (let ((size (foreign-size (class-of struct))))
493 (if (zerop size)
494 (error "~A has zero size" (class-of struct))
495 (allocate-memory size))))
496
497
498 ;;;; Metaclasses used for subclasses of struct
499
500 (defclass struct-class (proxy-class)
501 ())
502
503 (defmethod shared-initialize ((class struct-class) names &rest initargs)
504 (declare (ignore names initargs))
505 (call-next-method)
506 (let ((offsets nil) (copy-functions nil) (destroy-functions nil))
507 (flet ((initialize-functions ()
508 (loop
509 for slotd in (class-slots class)
510 as type = (slot-definition-type slotd)
511 when (eq (slot-definition-allocation slotd) :alien)
512 do (push (slot-definition-offset slotd) offsets)
513 (push (copy-function type) copy-functions)
514 (push (destroy-function type) destroy-functions))))
515 (unless (slot-boundp class 'ref)
516 (setf
517 (slot-value class 'ref)
518 #'(lambda (from &optional (to (allocate-memory (foreign-size class))))
519 (assert (not (null-pointer-p from)))
520 (unless offsets
521 (initialize-functions))
522 (loop
523 for offset in offsets
524 for copy in copy-functions
525 do (funcall copy from to offset))
526 to)))
527 (unless (slot-boundp class 'unref)
528 (setf (slot-value class 'unref)
529 #'(lambda (location &optional inlined-p)
530 (assert (not (null-pointer-p location)))
531 (unless offsets
532 (initialize-functions))
533 (loop
534 for offset in offsets
535 for destroy in destroy-functions
536 do (funcall destroy location offset))
537 (unless inlined-p
538 (deallocate-memory location))))))))
539
540
541 (defmethod direct-slot-definition-class ((class struct-class) &rest initargs)
542 (if (not (getf initargs :allocation))
543 (find-class 'direct-alien-slot-definition)
544 (call-next-method)))
545
546
547 (defmethod compute-slots :around ((class struct-class))
548 (let ((slots (call-next-method)))
549 (when (and
550 #?-(or (sbcl>= 0 9 8) (featurep :clisp))(class-finalized-p class)
551 (not (slot-boundp class 'size)))
552 (setf (slot-value class 'size)
553 (or
554 (loop
555 for slotd in slots
556 when (eq (slot-definition-allocation slotd) :alien)
557 maximize (+
558 (slot-definition-offset slotd)
559 (size-of (slot-definition-type slotd))))
560 0)))
561 slots))
562
563 (define-type-method callback-wrapper ((type struct) var arg form)
564 (let ((class (type-expand type)))
565 `(let ((,var (ensure-proxy-instance ',class ,arg :finalize nil)))
566 (unwind-protect
567 ,form
568 (invalidate-instance ,var)))))
569
570 (define-type-method size-of ((type struct) &key inlined)
571 (if inlined
572 (foreign-size type)
573 (size-of 'pointer)))
574
575 (define-type-method type-alignment ((type struct) &key inlined)
576 (if inlined
577 (let ((slot1 (find-if
578 #'(lambda (slotd)
579 (eq (slot-definition-allocation slotd) :alien))
580 (class-slots (find-class type)))))
581 (type-alignment (slot-definition-type slot1)))
582 (type-alignment 'pointer)))
583
584 (define-type-method writer-function ((type struct) &key temp inlined)
585 (if inlined
586 (if temp
587 (let ((size (size-of type :inlined t)))
588 #'(lambda (instance location &optional (offset 0))
589 (copy-memory
590 (foreign-location instance) size
591 (pointer+ location offset))))
592 (let ((ref (reference-function (type-expand type))))
593 #'(lambda (instance location &optional (offset 0))
594 (funcall ref
595 (foreign-location instance)
596 (pointer+ location offset)))))
597 (call-next-method)))
598
599 (define-type-method reader-function ((type struct) &key (ref :read) inlined)
600 (if inlined
601 (let ((class (type-expand type))
602 (size (size-of type :inlined t)))
603 (ecase ref
604 (:read
605 #'(lambda (location &optional (offset 0))
606 (ensure-proxy-instance class (pointer+ location offset))))
607 (:peek
608 #'(lambda (location &optional (offset 0))
609 (ensure-proxy-instance class (pointer+ location offset)
610 :reference nil :finalize nil)))
611 (:get
612 #'(lambda (location &optional (offset 0))
613 (prog1
614 (ensure-proxy-instance class
615 (copy-memory (pointer+ location offset) size)
616 :reference nil)
617 (clear-memory (pointer+ location offset) size))))))
618 (call-next-method)))
619
620 (define-type-method destroy-function ((type struct) &key temp inlined)
621 (if inlined
622 (let ((size (size-of type :inlined t)))
623 (if temp
624 #'(lambda (location &optional (offset 0))
625 (clear-memory (pointer+ location offset) size))
626 (let ((unref (unreference-function (type-expand type))))
627 #'(lambda (location &optional (offset 0))
628 (funcall unref (pointer+ location offset) t)))))
629 (call-next-method)))
630
631 (define-type-method copy-function ((type struct) &key inlined)
632 (if inlined
633 (let ((ref (reference-function (type-expand type))))
634 #'(lambda (from to &optional (offset 0))
635 (funcall ref (pointer+ from offset) (pointer+ to offset))))
636 (call-next-method)))