chiark / gitweb /
FOREIGN
[clg] / glib / proxy.lisp
CommitLineData
112ac1d3 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
94f15c3c 3;;
112ac1d3 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:
94f15c3c 11;;
112ac1d3 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
94f15c3c 14;;
112ac1d3 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.
94f15c3c 22
c0e19882 23;; $Id: proxy.lisp,v 1.31 2006-02-08 22:10:47 espen Exp $
94f15c3c 24
25(in-package "GLIB")
26
94f15c3c 27;;;; Superclass for all metaclasses implementing some sort of virtual slots
28
29(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 30 (defclass virtual-slots-class (standard-class)
4d83a8a6 31 ())
94f15c3c 32
33 (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
12d0437e 34 ((setter :reader slot-definition-setter :initarg :setter)
4d83a8a6 35 (getter :reader slot-definition-getter :initarg :getter)
eeda1c2d 36 (unbound :reader slot-definition-unbound :initarg :unbound)
4d83a8a6 37 (boundp :reader slot-definition-boundp :initarg :boundp)))
94f15c3c 38
4d83a8a6 39 (defclass effective-virtual-slot-definition (standard-effective-slot-definition)
40 ((setter :reader slot-definition-setter :initarg :setter)
41 (getter :reader slot-definition-getter :initarg :getter)
eeda1c2d 42 (unbound :reader slot-definition-unbound :initarg :unbound)
e2ebafb1 43 (boundp :reader slot-definition-boundp :initarg :boundp)))
4d83a8a6 44
e2ebafb1 45 (defclass direct-special-slot-definition (standard-direct-slot-definition)
46 ())
94f15c3c 47
e2ebafb1 48 (defclass effective-special-slot-definition (standard-effective-slot-definition)
49 ()))
50
51(defvar *unbound-marker* (gensym "UNBOUND-MARKER-"))
52
53(defun most-specific-slot-value (instances slot &optional (default *unbound-marker*))
54 (let ((object (find-if
55 #'(lambda (ob)
56 (and (slot-exists-p ob slot) (slot-boundp ob slot)))
57 instances)))
58 (if object
59 (slot-value object slot)
60 default)))
61
62(defmethod initialize-instance ((slotd effective-special-slot-definition) &rest initargs)
63 (declare (ignore initargs))
64 (call-next-method)
65 (setf (slot-value slotd 'allocation) :instance))
66
94f15c3c 67
9adccb27 68(defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs)
e2ebafb1 69 (case (getf initargs :allocation)
70 (:virtual (find-class 'direct-virtual-slot-definition))
71 (:special (find-class 'direct-special-slot-definition))
72 (t (call-next-method))))
94f15c3c 73
9adccb27 74(defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs)
e2ebafb1 75 (case (getf initargs :allocation)
76 (:virtual (find-class 'effective-virtual-slot-definition))
77 (:special (find-class 'effective-special-slot-definition))
78 (t (call-next-method))))
94f15c3c 79
4d83a8a6 80
81(defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition))
eeda1c2d 82 (if (not (slot-boundp slotd 'getter))
83 (setf
4d83a8a6 84 (slot-value slotd 'reader-function)
eeda1c2d 85 #'(lambda (object)
86 (declare (ignore object))
87 (error "Can't read slot: ~A" (slot-definition-name slotd)))
88 (slot-value slotd 'boundp-function)
89 #'(lambda (object) (declare (ignore object)) nil))
90
91 (let ((getter-function
92 (let ((getter (slot-value slotd 'getter)))
93 (etypecase getter
94 (function getter)
95 (symbol
96 #'(lambda (object)
97 (funcall getter object)))
98 (string
99 (let ((reader nil))
100 (setf (slot-value slotd 'reader-function)
101 #'(lambda (object)
102 (unless reader
12b7df04 103 (setq reader
104 (mkbinding getter
105 (slot-definition-type slotd) 'pointer)))
09f6e237 106 (funcall reader (foreign-location object))))))))))
eeda1c2d 107
4d83a8a6 108 (setf
eeda1c2d 109 (slot-value slotd 'boundp-function)
110 (cond
eeda1c2d 111 ((slot-boundp slotd 'unbound)
112 (let ((unbound-value (slot-value slotd 'unbound)))
12b7df04 113 #'(lambda (object)
114 (not (eq (funcall getter-function object) unbound-value)))))
115 ((slot-boundp slotd 'boundp)
116 (let ((boundp (slot-value slotd 'boundp)))
eeda1c2d 117 (etypecase boundp
118 (function boundp)
119 (symbol #'(lambda (object)
120 (funcall boundp object)))
121 (string (let ((reader ()))
122 #'(lambda (object)
123 (unless reader
124 (setq reader
125 (mkbinding boundp
126 (slot-definition-type slotd) 'pointer)))
09f6e237 127 (funcall reader (foreign-location object))))))))
12b7df04 128 ((multiple-value-bind (unbound-p unbound-value)
129 (unbound-value (slot-definition-type slotd))
130 (when unbound-p
131 #'(lambda (object)
132 (not (eq (funcall getter-function object) unbound-value))))))
133 (#'(lambda (object) (declare (ignore object)) t))))
eeda1c2d 134
135 (setf
136 (slot-value slotd 'reader-function)
137 (cond
138 ((slot-boundp slotd 'unbound)
139 (let ((unbound (slot-value slotd 'unbound))
140 (slot-name (slot-definition-name slotd)))
12b7df04 141 #'(lambda (object)
142 (let ((value (funcall getter-function object)))
143 (if (eq value unbound)
144 (slot-unbound (class-of object) object slot-name)
145 value)))))
eeda1c2d 146 ((slot-boundp slotd 'boundp)
147 (let ((boundp-function (slot-value slotd 'boundp-function)))
12b7df04 148 #'(lambda (object)
149 (and
150 (funcall boundp-function object)
151 (funcall getter-function object)))))
152 ((multiple-value-bind (unbound-p unbound-value)
153 (unbound-value (slot-definition-type slotd))
154 (let ((slot-name (slot-definition-name slotd)))
155 (when unbound-p
156 #'(lambda (object)
157 (let ((value (funcall getter-function object)))
158 (if (eq value unbound-value)
159 (slot-unbound (class-of object) object slot-name)
160 value)))))))
eeda1c2d 161 (getter-function)))))
162
163 (setf
164 (slot-value slotd 'writer-function)
165 (if (not (slot-boundp slotd 'setter))
166 #'(lambda (object)
167 (declare (ignore object))
168 (error "Can't set slot: ~A" (slot-definition-name slotd)))
169 (with-slots (setter) slotd
4d83a8a6 170 (etypecase setter
171 (function setter)
eeda1c2d 172 ((or symbol cons)
173 #'(lambda (value object)
174 (funcall (fdefinition setter) value object)))
7d1ddc9e 175 (string
eeda1c2d 176 (let ((writer ()))
177 (setf
178 (slot-value slotd 'writer-function)
179 #'(lambda (value object)
180 (unless writer
181 (setq writer
182 (mkbinding setter 'nil 'pointer
183 (slot-definition-type slotd))))
09f6e237 184 (funcall writer (foreign-location object) value)))))))))
eeda1c2d 185
65466e9c 186 #-sbcl>=0.9.8(initialize-internal-slot-gfs (slot-definition-name slotd)))
4d83a8a6 187
188
189
eeda1c2d 190(defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf)
4d83a8a6 191 nil)
192
9adccb27 193(defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds)
eeda1c2d 194 (if (typep (first direct-slotds) 'direct-virtual-slot-definition)
195 (let ((initargs ()))
196 (let ((getter (most-specific-slot-value direct-slotds 'getter)))
197 (unless (eq getter *unbound-marker*)
198 (setf (getf initargs :getter) getter)))
199 (let ((setter (most-specific-slot-value direct-slotds 'setter)))
200 (unless (eq setter *unbound-marker*)
201 (setf (getf initargs :setter) setter)))
202 (let ((unbound (most-specific-slot-value direct-slotds 'unbound)))
203 (unless (eq unbound *unbound-marker*)
204 (setf (getf initargs :unbound) unbound)))
205 (let ((boundp (most-specific-slot-value direct-slotds 'boundp)))
206 (unless (eq boundp *unbound-marker*)
207 (setf (getf initargs :boundp) boundp)))
208 (nconc initargs (call-next-method)))
4d83a8a6 209 (call-next-method)))
210
94f15c3c 211
94f15c3c 212(defmethod slot-value-using-class
9adccb27 213 ((class virtual-slots-class) (object standard-object)
94f15c3c 214 (slotd effective-virtual-slot-definition))
4d83a8a6 215 (if (funcall (slot-value slotd 'boundp-function) object)
216 (funcall (slot-value slotd 'reader-function) object)
217 (slot-unbound class object (slot-definition-name slotd))))
94f15c3c 218
94f15c3c 219(defmethod slot-boundp-using-class
9adccb27 220 ((class virtual-slots-class) (object standard-object)
94f15c3c 221 (slotd effective-virtual-slot-definition))
4d83a8a6 222 (funcall (slot-value slotd 'boundp-function) object))
223
224(defmethod (setf slot-value-using-class)
9adccb27 225 (value (class virtual-slots-class) (object standard-object)
94f15c3c 226 (slotd effective-virtual-slot-definition))
4d83a8a6 227 (funcall (slot-value slotd 'writer-function) value object))
228
229
94f15c3c 230(defmethod validate-superclass
9adccb27 231 ((class virtual-slots-class) (super standard-class))
94f15c3c 232 t)
233
234
235;;;; Proxy cache
236
94f15c3c 237(defvar *instance-cache* (make-hash-table :test #'eql))
238
982a215a 239(defun cache-instance (instance &optional (weak-ref t))
94f15c3c 240 (setf
09f6e237 241 (gethash (sap-int (foreign-location instance)) *instance-cache*)
982a215a 242 (if weak-ref
243 (make-weak-pointer instance)
244 instance)))
94f15c3c 245
246(defun find-cached-instance (location)
73572c12 247 (let ((ref (gethash (sap-int location) *instance-cache*)))
94f15c3c 248 (when ref
982a215a 249 (if (weak-pointer-p ref)
250 (weak-pointer-value ref)
251 ref))))
94f15c3c 252
0f134a29 253(defun instance-cached-p (location)
73572c12 254 (gethash (sap-int location) *instance-cache*))
0f134a29 255
94f15c3c 256(defun remove-cached-instance (location)
73572c12 257 (remhash (sap-int location) *instance-cache*))
94f15c3c 258
9adccb27 259;; For debuging
982a215a 260(defun list-cached-instances ()
9adccb27 261 (let ((instances ()))
262 (maphash #'(lambda (location ref)
263 (declare (ignore location))
982a215a 264 (push ref instances))
9adccb27 265 *instance-cache*)
266 instances))
267
ca01de1b 268;; Instances that gets invalidated tend to be short lived, but created
269;; in large numbers. So we're keeping them in a hash table to be able
270;; to reuse them (and thus reduce consing)
271(defvar *invalidated-instance-cache* (make-hash-table :test #'eql))
272
273(defun cache-invalidated-instance (instance)
274 (push instance
275 (gethash (class-of instance) *invalidated-instance-cache*)))
276
277(defun find-invalidated-instance (class)
278 (when (gethash class *invalidated-instance-cache*)
279 (pop (gethash class *invalidated-instance-cache*))))
280
281(defun list-invalidated-instances ()
282 (let ((instances ()))
283 (maphash #'(lambda (location ref)
284 (declare (ignore location))
285 (push ref instances))
286 *invalidated-instance-cache*)
287 instances))
288
94f15c3c 289
290
291;;;; Proxy for alien instances
292
4a64c16d 293;; TODO: add a ref-counted-proxy subclass
9adccb27 294(defclass proxy ()
c0e19882 295 ((location :allocation :special :type pointer))
e2ebafb1 296 (:metaclass virtual-slots-class))
94f15c3c 297
9adccb27 298(defgeneric instance-finalizer (object))
299(defgeneric reference-foreign (class location))
300(defgeneric unreference-foreign (class location))
4a64c16d 301(defgeneric invalidate-instance (object))
9adccb27 302
c0e19882 303(defun foreign-location (instance)
304 (slot-value instance 'location))
305
306(defun (setf foreign-location) (location instance)
307 (setf (slot-value instance 'location) location))
308
309(defun proxy-valid-p (instance)
310 (slot-boundp instance 'location))
311
3b167652 312(defmethod reference-foreign ((name symbol) location)
313 (reference-foreign (find-class name) location))
314
315(defmethod unreference-foreign ((name symbol) location)
316 (unreference-foreign (find-class name) location))
317
9adccb27 318(defmethod unreference-foreign :around ((class class) location)
319 (unless (null-pointer-p location)
09f6e237 320 (call-next-method)))
94f15c3c 321
0f134a29 322(defmethod print-object ((instance proxy) stream)
323 (print-unreadable-object (instance stream :type t :identity nil)
09f6e237 324 (if (slot-boundp instance 'location)
325 (format stream "at 0x~X" (sap-int (foreign-location instance)))
326 (write-string "at \"unbound\"" stream))))
94f15c3c 327
8958fa4a 328(defmethod initialize-instance :around ((instance proxy) &rest initargs)
329 (declare (ignore initargs))
330 (prog1
331 (call-next-method)
332 (cache-instance instance)
333 (finalize instance (instance-finalizer instance))))
94f15c3c 334
335(defmethod instance-finalizer ((instance proxy))
09f6e237 336 (let ((location (foreign-location instance))
9adccb27 337 (class (class-of instance)))
338;; (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil)
339;; (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class))
340 #'(lambda ()
1dbf4216 341 (remove-cached-instance location)
9adccb27 342 (unreference-foreign class location))))
12d0437e 343
ca01de1b 344;; Any reference to the foreign object the instance may have held
345;; should be released before this method is invoked
4a64c16d 346(defmethod invalidate-instance ((instance proxy))
347 (remove-cached-instance (foreign-location instance))
ca01de1b 348 (slot-makunbound instance 'location)
349 (cancel-finalization instance)
350 (cache-invalidated-instance instance))
4a64c16d 351
94f15c3c 352
353;;;; Metaclass used for subclasses of proxy
354
73572c12 355(defgeneric most-specific-proxy-superclass (class))
356(defgeneric direct-proxy-superclass (class))
357
358
94f15c3c 359(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 360 (defclass proxy-class (virtual-slots-class)
09f6e237 361 ((size :reader foreign-size)))
94f15c3c 362
363 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
12d0437e 364 ((allocation :initform :alien)
365 (offset :reader slot-definition-offset :initarg :offset)))
94f15c3c 366
367 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
4d83a8a6 368 ((offset :reader slot-definition-offset :initarg :offset)))
12d0437e 369
94f15c3c 370 (defmethod most-specific-proxy-superclass ((class proxy-class))
371 (find-if
372 #'(lambda (class)
373 (subtypep (class-name class) 'proxy))
4d83a8a6 374 (cdr (compute-class-precedence-list class))))
73572c12 375
12d0437e 376 (defmethod direct-proxy-superclass ((class proxy-class))
377 (find-if
378 #'(lambda (class)
379 (subtypep (class-name class) 'proxy))
4d83a8a6 380 (class-direct-superclasses class)))
381
eeda1c2d 382 (defmethod shared-initialize ((class proxy-class) names &key size)
94f15c3c 383 (call-next-method)
12d0437e 384 (cond
4d83a8a6 385 (size (setf (slot-value class 'size) (first size)))
9adccb27 386 ((slot-boundp class 'size) (slot-makunbound class 'size))))
09f6e237 387
4d83a8a6 388 (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 389 (case (getf initargs :allocation)
e2ebafb1 390 (:alien (find-class 'direct-alien-slot-definition))
94f15c3c 391 (t (call-next-method))))
4d83a8a6 392
393 (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 394 (case (getf initargs :allocation)
395 (:alien (find-class 'effective-alien-slot-definition))
94f15c3c 396 (t (call-next-method))))
397
4d83a8a6 398
399 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
400 (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien)
401 (nconc
402 (list :offset (most-specific-slot-value direct-slotds 'offset))
403 (call-next-method))
404 (call-next-method)))
405
406
407 (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition))
408 (with-slots (offset) slotd
9adccb27 409 (let ((type (slot-definition-type slotd)))
eeda1c2d 410 (unless (slot-boundp slotd 'getter)
9adccb27 411 (let ((reader (reader-function type)))
412 (setf
eeda1c2d 413 (slot-value slotd 'getter)
9adccb27 414 #'(lambda (object)
09f6e237 415 (funcall reader (foreign-location object) offset)))))
4d83a8a6 416
eeda1c2d 417 (unless (slot-boundp slotd 'setter)
9adccb27 418 (let ((writer (writer-function type))
419 (destroy (destroy-function type)))
420 (setf
eeda1c2d 421 (slot-value slotd 'setter)
9adccb27 422 #'(lambda (value object)
09f6e237 423 (let ((location (foreign-location object)))
9adccb27 424 (funcall destroy location offset) ; destroy old value
eeda1c2d 425 (funcall writer value location offset))))))))
426
4d83a8a6 427 (call-next-method))
428
4d83a8a6 429 ;; TODO: call some C code to detect this a compile time
430 (defconstant +struct-alignmen+ 4)
94f15c3c 431
09f6e237 432 (defun align-offset (size)
433 (if (zerop (mod size +struct-alignmen+))
434 size
435 (+ size (- +struct-alignmen+ (mod size +struct-alignmen+)))))
436
94f15c3c 437 (defmethod compute-slots ((class proxy-class))
09f6e237 438 (let ((alien-slots
439 (remove-if-not
440 #'(lambda (slotd)
441 (eq (slot-definition-allocation slotd) :alien))
442 (class-direct-slots class))))
443 (when alien-slots
444 (loop
445 as offset = (align-offset (foreign-size
446 (most-specific-proxy-superclass class)))
447 then (align-offset
448 (+
449 (slot-definition-offset slotd)
450 (size-of (slot-definition-type slotd))))
451 for slotd in alien-slots
452 unless (slot-boundp slotd 'offset)
453 do (setf (slot-value slotd 'offset) offset))))
94f15c3c 454 (call-next-method))
3e15002d 455
4d83a8a6 456 (defmethod validate-superclass ((class proxy-class) (super standard-class))
457 (subtypep (class-name super) 'proxy))
458
09f6e237 459 (defmethod foreign-size ((class-name symbol))
460 (foreign-size (find-class class-name))))
47a11c16 461
09f6e237 462(defmethod foreign-size ((object proxy))
463 (foreign-size (class-of object)))
4d83a8a6 464
09f6e237 465
9adccb27 466(defmethod alien-type ((class proxy-class) &rest args)
467 (declare (ignore class args))
468 (alien-type 'pointer))
469
470(defmethod size-of ((class proxy-class) &rest args)
471 (declare (ignore class args))
472 (size-of 'pointer))
473
474(defmethod from-alien-form (location (class proxy-class) &rest args)
475 (declare (ignore args))
476 `(ensure-proxy-instance ',(class-name class) ,location))
477
478(defmethod from-alien-function ((class proxy-class) &rest args)
479 (declare (ignore args))
480 #'(lambda (location)
481 (ensure-proxy-instance class location)))
94f15c3c 482
9adccb27 483(defmethod to-alien-form (instance (class proxy-class) &rest args)
484 (declare (ignore class args))
09f6e237 485 `(foreign-location ,instance))
94f15c3c 486
9adccb27 487(defmethod to-alien-function ((class proxy-class) &rest args)
488 (declare (ignore class args))
09f6e237 489 #'foreign-location)
9adccb27 490
9ca5565a 491(defmethod copy-from-alien-form (location (class proxy-class) &rest args)
492 (declare (ignore args))
493 (let ((class-name (class-name class)))
494 `(ensure-proxy-instance ',class-name
495 (reference-foreign ',class-name ,location))))
496
497(defmethod copy-from-alien-function ((class proxy-class) &rest args)
498 (declare (ignore args))
499 #'(lambda (location)
500 (ensure-proxy-instance class (reference-foreign class location))))
501
502(defmethod copy-to-alien-form (instance (class proxy-class) &rest args)
503 (declare (ignore args))
09f6e237 504 `(reference-foreign ',(class-name class) (foreign-location ,instance)))
9ca5565a 505
506(defmethod copy-to-alien-function ((class proxy-class) &rest args)
73572c12 507 (declare (ignore args))
9ca5565a 508 #'(lambda (instance)
09f6e237 509 (reference-foreign class (foreign-location instance))))
9ca5565a 510
9adccb27 511(defmethod writer-function ((class proxy-class) &rest args)
512 (declare (ignore args))
513 #'(lambda (instance location &optional (offset 0))
514 (assert (null-pointer-p (sap-ref-sap location offset)))
515 (setf
516 (sap-ref-sap location offset)
09f6e237 517 (reference-foreign class (foreign-location instance)))))
94f15c3c 518
9adccb27 519(defmethod reader-function ((class proxy-class) &rest args)
520 (declare (ignore args))
3005806e 521 #'(lambda (location &optional (offset 0) weak-p)
522 (declare (ignore weak-p))
9ca5565a 523 (let ((instance (sap-ref-sap location offset)))
524 (unless (null-pointer-p instance)
525 (ensure-proxy-instance class (reference-foreign class instance))))))
94f15c3c 526
9adccb27 527(defmethod destroy-function ((class proxy-class) &rest args)
528 (declare (ignore args))
529 #'(lambda (location &optional (offset 0))
530 (unreference-foreign class (sap-ref-sap location offset))))
531
12b7df04 532(defmethod unbound-value ((class proxy-class) &rest args)
47a11c16 533 (declare (ignore args))
12b7df04 534 (values t nil))
9adccb27 535
8958fa4a 536(defun ensure-proxy-instance (class location &rest initargs)
537 "Returns a proxy object representing the foreign object at the give
538location. If an existing object is not found in the cache
539MAKE-PROXY-INSTANCE is called to create one."
9adccb27 540 (unless (null-pointer-p location)
541 (or
aaced14e 542 #-debug-ref-counting(find-cached-instance location)
543 #+debug-ref-counting
4a64c16d 544 (let ((instance (find-cached-instance location)))
545 (when instance
546 (format t "Object found in cache: ~A~%" instance)
547 instance))
8958fa4a 548 (let ((instance (apply #'make-proxy-instance class location initargs)))
549 (cache-instance instance)
550 instance))))
551
552(defgeneric make-proxy-instance (class location &key weak)
553 (:documentation "Creates a new proxy object representing the foreign
554object at the give location. If WEAK is non NIL the foreign memory
555will not be released when the proxy is garbage collected."))
556
4a64c16d 557(defmethod make-proxy-instance ((class symbol) location &rest initargs)
558 (apply #'make-proxy-instance (find-class class) location initargs))
8958fa4a 559
560(defmethod make-proxy-instance ((class proxy-class) location &key weak)
ca01de1b 561 (let ((instance
562 (or
563 (find-invalidated-instance class)
564 (allocate-instance class))))
c0e19882 565 (setf (foreign-location instance) location)
8958fa4a 566 (unless weak
567 (finalize instance (instance-finalizer instance)))
568 instance))
94f15c3c 569
12d0437e 570
571;;;; Superclasses for wrapping of C structures
94f15c3c 572
9adccb27 573(defclass struct (proxy)
574 ()
09f6e237 575 (:metaclass proxy-class)
576 (:size 0))
94f15c3c 577
9adccb27 578(defmethod initialize-instance ((struct struct) &rest initargs)
94f15c3c 579 (declare (ignore initargs))
ec1a4146 580 (unless (slot-boundp struct 'location)
09f6e237 581 (let ((size (foreign-size (class-of struct))))
ec1a4146 582 (if (zerop size)
583 (error "~A has zero size" (class-of struct))
47a11c16 584 (setf (slot-value struct 'location) (allocate-memory size)))))
94f15c3c 585 (call-next-method))
586
587
9adccb27 588;;;; Metaclasses used for subclasses of struct
589
590(defclass struct-class (proxy-class)
591 ())
94f15c3c 592
e2ebafb1 593(defmethod direct-slot-definition-class ((class struct-class) &rest initargs)
594 (if (not (getf initargs :allocation))
595 (find-class 'direct-alien-slot-definition)
596 (call-next-method)))
597
9adccb27 598(defmethod reference-foreign ((class struct-class) location)
09f6e237 599 (copy-memory location (foreign-size class)))
9adccb27 600
601(defmethod unreference-foreign ((class struct-class) location)
12d0437e 602 (deallocate-memory location))
94f15c3c 603
65466e9c 604(defmethod compute-slots :around ((class struct-class))
605 (let ((slots (call-next-method)))
606 (when (and
607 #-sbcl>=0.9.8(class-finalized-p class) #+sbc098 t
608 (not (slot-boundp class 'size)))
609 (let ((size (loop
610 for slotd in slots
611 when (eq (slot-definition-allocation slotd) :alien)
612 maximize (+
613 (slot-definition-offset slotd)
614 (size-of (slot-definition-type slotd))))))
615 (setf (slot-value class 'size) (+ size (mod size +struct-alignmen+)))))
616 slots))
09f6e237 617
3005806e 618(defmethod reader-function ((class struct-class) &rest args)
4a64c16d 619 (declare (ignore args))
3005806e 620 #'(lambda (location &optional (offset 0) weak-p)
4a64c16d 621 (let ((instance (sap-ref-sap location offset)))
622 (unless (null-pointer-p instance)
3005806e 623 (if weak-p
624 (ensure-proxy-instance class instance :weak t)
625 (ensure-proxy-instance class (reference-foreign class instance)))))))
4a64c16d 626
94f15c3c 627
9adccb27 628(defclass static-struct-class (struct-class)
629 ())
94f15c3c 630
9adccb27 631(defmethod reference-foreign ((class static-struct-class) location)
632 (declare (ignore class))
12d0437e 633 location)
94f15c3c 634
9adccb27 635(defmethod unreference-foreign ((class static-struct-class) location)
636 (declare (ignore class location))
12d0437e 637 nil)
bde0b906 638
3005806e 639(defmethod reader-function ((class struct-class) &rest args)
640 (declare (ignore args))
641 #'(lambda (location &optional (offset 0) weak-p)
642 (declare (ignore weak-p))
643 (let ((instance (sap-ref-sap location offset)))
644 (unless (null-pointer-p instance)
645 (ensure-proxy-instance class instance :weak t)))))
646
bde0b906 647
648;;; Pseudo type for structs which are inlined in other objects
649
650(defmethod size-of ((type (eql 'inlined)) &rest args)
651 (declare (ignore type))
09f6e237 652 (foreign-size (first args)))
bde0b906 653
654(defmethod reader-function ((type (eql 'inlined)) &rest args)
655 (declare (ignore type))
656 (destructuring-bind (class) args
3005806e 657 #'(lambda (location &optional (offset 0) weak-p)
658 (declare (ignore weak-p))
bde0b906 659 (ensure-proxy-instance class
660 (reference-foreign class (sap+ location offset))))))
661
4a64c16d 662(defmethod writer-function ((type (eql 'inlined)) &rest args)
663 (declare (ignore type))
664 (destructuring-bind (class) args
665 #'(lambda (instance location &optional (offset 0))
666 (copy-memory (foreign-location instance) (foreign-size class) (sap+ location offset)))))
667
bde0b906 668(defmethod destroy-function ((type (eql 'inlined)) &rest args)
669 (declare (ignore args))
670 #'(lambda (location &optional (offset 0))
671 (declare (ignore location offset))))
672
673(export 'inlined)