chiark / gitweb /
Improved alignment of struct slots
[clg] / gffi / proxy.lisp
CommitLineData
4e968638 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
90e8bbf6 23;; $Id: proxy.lisp,v 1.2 2006-06-08 13:25:09 espen Exp $
4e968638 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 cached 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))
246 (let* ((type (slot-definition-type slotd))
247 (offset (slot-definition-offset slotd))
248 (reader (reader-function type)))
249 #'(lambda (object)
250 (funcall reader (foreign-location object) offset))))
251
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))
261 value)))
262
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))
267 (reader nil))
268 #'(lambda (object)
269 (unless reader
270 (setq reader (mkbinding getter type 'pointer)))
271 (funcall reader (foreign-location object))))
272 (call-next-method)))
273
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))
278 (writer nil))
279 #'(lambda (value object)
280 (unless writer
281 (setq writer (mkbinding setter nil 'pointer type)))
282 (funcall writer (foreign-location object) value)))
283 (call-next-method)))
284
90e8bbf6 285 (defun adjust-offset (offset type &optional packed-p)
286 (let ((alignment (type-alignment type)))
287 (if (or packed-p (zerop (mod offset alignment)))
288 offset
289 (+ offset (- alignment (mod offset alignment))))))
4e968638 290
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)))
296 (when alien-slots
297 (loop
298 with packed-p = (foreign-slots-packed-p class)
90e8bbf6 299 for slotd in alien-slots
300 as offset = (adjust-offset
4e968638 301 (foreign-size (most-specific-proxy-superclass class))
90e8bbf6 302 (slot-definition-type slotd)
4e968638 303 packed-p)
90e8bbf6 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))))))
4e968638 309 (call-next-method))
310
311 (defmethod validate-superclass ((class proxy-class) (super standard-class))
312 (subtypep (class-name super) 'proxy))
313
314 (defmethod foreign-size ((class-name symbol))
315 (foreign-size (find-class class-name))))
316
317(defmethod foreign-size ((object proxy))
318 (foreign-size (class-of object)))
319
320(define-type-method alien-type ((type proxy))
321 (declare (ignore type))
322 (alien-type 'pointer))
323
324(define-type-method size-of ((type proxy) &key inlined)
325 (assert-not-inlined type inlined)
326 (size-of 'pointer))
327
90e8bbf6 328(define-type-method type-alignment ((type proxy) &key inlined)
329 (assert-not-inlined type inlined)
330 (type-alignment 'pointer))
331
4e968638 332(define-type-method from-alien-form ((type proxy) form &key (ref :free))
333 (let ((class (type-expand type)))
334 (ecase ref
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)))))
339
340(define-type-method from-alien-function ((type proxy) &key (ref :free))
341 (let ((class (type-expand type)))
342 (ecase ref
343 (:free
344 #'(lambda (location)
345 (ensure-proxy-instance class location :reference nil)))
346 (:copy
347 #'(lambda (location)
348 (ensure-proxy-instance class location)))
349 ((:static :temp)
350 #'(lambda (location)
351 (ensure-proxy-instance class location :reference nil :finalize nil))))))
352
353(define-type-method to-alien-form ((type proxy) instance &optional copy-p)
354 (if copy-p
355 (let* ((class (type-expand type))
356 (ref (reference-function class)))
357 (if (symbolp ref)
358 `(,ref (foreign-location ,instance))
359 `(funcall (reference-function ',class)
360 (foreign-location ,instance))))
361 `(foreign-location ,instance)))
362
363(define-type-method to-alien-function ((type proxy) &optional copy-p)
364 (if copy-p
365 (let ((ref (reference-function (type-expand type))))
366 #'(lambda (instance)
367 (funcall ref (foreign-location instance))))
368 #'foreign-location))
369
4e968638 370(define-type-method writer-function ((type proxy) &key temp inlined)
371 (assert-not-inlined type inlined)
372 (if temp
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)))
379 (setf
380 (ref-pointer location offset)
381 (funcall ref (foreign-location instance)))))))
382
383(define-type-method reader-function ((type proxy) &key (ref :read) inlined)
384 (assert-not-inlined type inlined)
385 (let ((class (type-expand type)))
386 (ecase ref
387 (:read
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)))))
392 (:peek
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)))))
398 (:get
399 #'(lambda (location &optional (offset 0))
400 (let ((instance (ref-pointer location offset)))
401 (unless (null-pointer-p instance)
402 (prog1
403 (ensure-proxy-instance class instance :reference nil)
404 (setf (ref-pointer location offset) (make-pointer 0))))))))))
405
406(define-type-method destroy-function ((type proxy) &key temp inlined)
407 (assert-not-inlined type inlined)
408 (if temp
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)))))))
416
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)))))
425
426(define-type-method unbound-value ((type proxy))
427 (declare (ignore type))
428 nil)
429
430(defun ensure-proxy-instance (class location &rest initargs)
431 "Returns a proxy object representing the foreign object at the give
432location. If an existing proxy object is not found,
433MAKE-PROXY-INSTANCE is called to create a new one. A second return
434value indicates whether a new proxy was created or not."
435 (unless (null-pointer-p location)
436 (or
437 #-debug-ref-counting(find-cached-instance location)
438 #+debug-ref-counting
439 (let ((instance (find-cached-instance location)))
440 (when instance
441 (format t "Object found in cache: ~A~%" instance)
442 instance))
443 (values
444 (apply #'make-proxy-instance class location initargs)
445 t))))
446
447(defgeneric make-proxy-instance (class location &key reference finalize)
448 (:documentation "Creates a new proxy object representing the foreign
449object at the give location."))
450
451(defmethod make-proxy-instance ((class symbol) location &rest initargs)
452 (apply #'make-proxy-instance (find-class class) location initargs))
453
454(defmethod make-proxy-instance ((class proxy-class) location
455 &key (reference t) (finalize t))
456 (let ((instance
457 (or
458 (find-invalidated-instance class)
459 (allocate-instance class))))
460 (setf (foreign-location instance)
461 (if reference
462 (funcall (reference-function class) location)
463 location))
464 (finalize instance
465 (if finalize
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)))
470 #+(or cmu sbcl)
471 #'(lambda ()
472 (remove-cached-instance location))
473 #+clisp
474 #'(lambda (instance)
475 (declare (ignore instance))
476 (remove-cached-instance location)))))
477 (cache-instance instance)
478 instance))
479
480
481;;;; Superclasses for wrapping of C structures
482
483(defclass struct (proxy)
484 ()
485 (:metaclass proxy-class)
486 (:size 0))
487
488(defmethod allocate-foreign ((struct struct) &rest initargs)
489 (declare (ignore initargs))
490 (let ((size (foreign-size (class-of struct))))
491 (if (zerop size)
492 (error "~A has zero size" (class-of struct))
493 (allocate-memory size))))
494
495
496;;;; Metaclasses used for subclasses of struct
497
498(defclass struct-class (proxy-class)
499 ())
500
501(defmethod shared-initialize ((class struct-class) names &rest initargs)
502 (declare (ignore names initargs))
503 (call-next-method)
504 (let ((offsets nil) (copy-functions nil) (destroy-functions nil))
505 (flet ((initialize-functions ()
506 (loop
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)
514 (setf
515 (slot-value class 'ref)
516 #'(lambda (from &optional (to (allocate-memory (foreign-size class))))
517 (assert (not (null-pointer-p from)))
518 (unless offsets
519 (initialize-functions))
520 (loop
521 for offset in offsets
522 for copy in copy-functions
523 do (funcall copy from to offset))
524 to)))
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)))
529 (unless offsets
530 (initialize-functions))
531 (loop
532 for offset in offsets
533 for destroy in destroy-functions
534 do (funcall destroy location offset))
535 (unless inlined-p
536 (deallocate-memory location))))))))
537
538
539(defmethod direct-slot-definition-class ((class struct-class) &rest initargs)
540 (if (not (getf initargs :allocation))
541 (find-class 'direct-alien-slot-definition)
542 (call-next-method)))
543
544
545(defmethod compute-slots :around ((class struct-class))
546 (let ((slots (call-next-method)))
547 (when (and
548 #?-(or (sbcl>= 0 9 8) (featurep :clisp))(class-finalized-p class)
549 (not (slot-boundp class 'size)))
90e8bbf6 550 (setf (slot-value class 'size)
551 (or
552 (loop
553 for slotd in slots
554 when (eq (slot-definition-allocation slotd) :alien)
555 maximize (+
556 (slot-definition-offset slotd)
557 (size-of (slot-definition-type slotd))))
558 0)))
4e968638 559 slots))
560
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)))
564 (unwind-protect
565 ,form
566 (invalidate-instance ,var)))))
567
568(define-type-method size-of ((type struct) &key inlined)
569 (if inlined
570 (foreign-size type)
571 (size-of 'pointer)))
572
90e8bbf6 573(define-type-method type-alignment ((type struct) &key inlined)
574 (if inlined
575 (let ((slot1 (find-if
576 #'(lambda (slotd)
577 (eq (slot-definition-allocation slotd) :alien))
578 (class-slots (find-class type)))))
579 (type-alignment (slot-definition-type slot1)))
580 (type-alignment 'pointer)))
581
4e968638 582(define-type-method writer-function ((type struct) &key temp inlined)
583 (if inlined
584 (if temp
585 (let ((size (size-of type :inlined t)))
586 #'(lambda (instance location &optional (offset 0))
587 (copy-memory
588 (foreign-location instance) size
589 (pointer+ location offset))))
590 (let ((ref (reference-function (type-expand type))))
591 #'(lambda (instance location &optional (offset 0))
592 (funcall ref
593 (foreign-location instance)
594 (pointer+ location offset)))))
595 (call-next-method)))
596
597(define-type-method reader-function ((type struct) &key (ref :read) inlined)
598 (if inlined
599 (let ((class (type-expand type))
600 (size (size-of type :inlined t)))
601 (ecase ref
602 (:read
603 #'(lambda (location &optional (offset 0))
604 (ensure-proxy-instance class (pointer+ location offset))))
605 (:peek
606 #'(lambda (location &optional (offset 0))
607 (ensure-proxy-instance class (pointer+ location offset)
608 :reference nil :finalize nil)))
609 (:get
610 #'(lambda (location &optional (offset 0))
611 (prog1
612 (ensure-proxy-instance class
613 (copy-memory (pointer+ location offset) size)
614 :reference nil)
615 (clear-memory (pointer+ location offset) size))))))
616 (call-next-method)))
617
618(define-type-method destroy-function ((type struct) &key temp inlined)
619 (if inlined
620 (let ((size (size-of type :inlined t)))
621 (if temp
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)))))
627 (call-next-method)))
628
629(define-type-method copy-function ((type struct) &key inlined)
630 (if 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))))
634 (call-next-method)))