chiark / gitweb /
Bug fixes
[clg] / glib / proxy.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 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.30 2006/02/08 21:43:33 espen Exp $
24
25 (in-package "GLIB")
26
27 ;;;; Superclass for all metaclasses implementing some sort of virtual slots
28
29 (eval-when (:compile-toplevel :load-toplevel :execute)
30   (defclass virtual-slots-class (standard-class) 
31     ())
32
33   (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
34     ((setter :reader slot-definition-setter :initarg :setter)
35      (getter :reader slot-definition-getter :initarg :getter)
36      (unbound :reader slot-definition-unbound :initarg :unbound)
37      (boundp :reader slot-definition-boundp :initarg :boundp)))
38   
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)
42      (unbound :reader slot-definition-unbound :initarg :unbound)
43      (boundp :reader slot-definition-boundp :initarg :boundp)))
44
45   (defclass direct-special-slot-definition (standard-direct-slot-definition)
46     ())
47   
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
67
68 (defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs)
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))))
73
74 (defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs)
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))))
79
80
81 (defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition))
82   (if (not (slot-boundp slotd 'getter))
83       (setf
84        (slot-value slotd 'reader-function)
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
103                               (setq reader
104                                (mkbinding getter 
105                                 (slot-definition-type slotd) 'pointer)))
106                             (funcall reader (foreign-location object))))))))))
107
108       (setf 
109        (slot-value slotd 'boundp-function)
110        (cond
111         ((slot-boundp slotd 'unbound)
112          (let ((unbound-value (slot-value slotd 'unbound)))
113            #'(lambda (object)
114                (not (eq (funcall getter-function object) unbound-value)))))
115         ((slot-boundp slotd 'boundp)
116          (let ((boundp (slot-value slotd 'boundp)))
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)))
127                            (funcall reader (foreign-location object))))))))
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))))
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)))
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)))))
146         ((slot-boundp slotd 'boundp)
147          (let ((boundp-function (slot-value slotd 'boundp-function)))
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)))))))
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
170        (etypecase setter
171          (function setter)
172          ((or symbol cons) 
173           #'(lambda (value object)
174               (funcall (fdefinition setter) value object)))
175          (string
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))))
184                  (funcall writer (foreign-location object) value)))))))))
185
186   #-sbcl>=0.9.8(initialize-internal-slot-gfs (slot-definition-name slotd)))
187
188
189
190 (defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf)
191   nil)
192
193 (defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds)
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)))
209     (call-next-method)))
210
211
212 (defmethod slot-value-using-class
213     ((class virtual-slots-class) (object standard-object)
214      (slotd effective-virtual-slot-definition))
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))))
218
219 (defmethod slot-boundp-using-class
220     ((class virtual-slots-class) (object standard-object)
221      (slotd effective-virtual-slot-definition))
222   (funcall (slot-value slotd 'boundp-function) object))
223   
224 (defmethod (setf slot-value-using-class) 
225     (value (class virtual-slots-class) (object standard-object)
226      (slotd effective-virtual-slot-definition))
227   (funcall (slot-value slotd 'writer-function) value object))
228
229   
230 (defmethod validate-superclass
231     ((class virtual-slots-class) (super standard-class))
232   t)
233
234
235 ;;;; Proxy cache
236
237 (defvar *instance-cache* (make-hash-table :test #'eql))
238
239 (defun cache-instance (instance &optional (weak-ref t))
240   (setf
241    (gethash (sap-int (foreign-location instance)) *instance-cache*)
242    (if weak-ref
243        (make-weak-pointer instance)
244      instance)))
245
246 (defun find-cached-instance (location)
247   (let ((ref (gethash (sap-int location) *instance-cache*)))
248     (when ref
249       (if (weak-pointer-p ref)
250           (weak-pointer-value ref)
251         ref))))
252
253 (defun instance-cached-p (location)
254   (gethash (sap-int location) *instance-cache*))
255
256 (defun remove-cached-instance (location)
257   (remhash (sap-int location) *instance-cache*))
258
259 ;; For debuging
260 (defun list-cached-instances ()
261   (let ((instances ()))
262     (maphash #'(lambda (location ref)
263                  (declare (ignore location))
264                  (push ref instances))
265              *instance-cache*)
266     instances))
267                         
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
289
290
291 ;;;; Proxy for alien instances
292
293 ;; TODO: add a ref-counted-proxy subclass
294 (defclass proxy ()
295   ((location :allocation :special :reader foreign-location :type pointer))
296   (:metaclass virtual-slots-class))
297
298 (defgeneric instance-finalizer (object))
299 (defgeneric reference-foreign (class location))
300 (defgeneric unreference-foreign (class location))
301 (defgeneric invalidate-instance (object))
302
303 (defmethod reference-foreign ((name symbol) location)
304   (reference-foreign (find-class name) location))
305
306 (defmethod unreference-foreign ((name symbol) location)
307   (unreference-foreign (find-class name) location))
308
309 (defmethod unreference-foreign :around ((class class) location)
310   (unless (null-pointer-p location)
311     (call-next-method)))
312
313 (defmethod print-object ((instance proxy) stream)
314   (print-unreadable-object (instance stream :type t :identity nil)
315     (if (slot-boundp instance 'location)
316         (format stream "at 0x~X" (sap-int (foreign-location instance)))
317       (write-string "at \"unbound\"" stream))))
318
319 (defmethod initialize-instance :around ((instance proxy) &rest initargs)
320   (declare (ignore initargs))
321   (prog1
322       (call-next-method)
323     (cache-instance instance)
324     (finalize instance (instance-finalizer instance))))
325
326 (defmethod instance-finalizer ((instance proxy))
327   (let ((location (foreign-location instance))
328         (class (class-of instance)))    
329 ;;     (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil)
330 ;;       (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class))
331     #'(lambda ()
332         (remove-cached-instance location)
333         (unreference-foreign class location))))
334
335 ;; Any reference to the foreign object the instance may have held
336 ;; should be released before this method is invoked
337 (defmethod invalidate-instance ((instance proxy))
338   (remove-cached-instance (foreign-location instance))
339   (slot-makunbound instance 'location)
340   (cancel-finalization instance)
341   (cache-invalidated-instance instance))
342
343
344 ;;;; Metaclass used for subclasses of proxy
345
346 (defgeneric most-specific-proxy-superclass (class))
347 (defgeneric direct-proxy-superclass (class))
348   
349
350 (eval-when (:compile-toplevel :load-toplevel :execute)
351   (defclass proxy-class (virtual-slots-class)
352     ((size :reader foreign-size)))
353
354   (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
355     ((allocation :initform :alien)
356      (offset :reader slot-definition-offset :initarg :offset)))
357   
358   (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
359     ((offset :reader slot-definition-offset :initarg :offset)))
360
361   (defmethod most-specific-proxy-superclass ((class proxy-class))
362     (find-if
363      #'(lambda (class)
364          (subtypep (class-name class) 'proxy))
365      (cdr (compute-class-precedence-list class))))
366
367   (defmethod direct-proxy-superclass ((class proxy-class))
368     (find-if
369      #'(lambda (class)
370          (subtypep (class-name class) 'proxy))
371      (class-direct-superclasses class)))
372   
373   (defmethod shared-initialize ((class proxy-class) names &key size)
374     (call-next-method)
375     (cond
376       (size (setf (slot-value class 'size) (first size)))
377       ((slot-boundp class 'size) (slot-makunbound class 'size))))
378
379   (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
380     (case (getf initargs :allocation)
381       (:alien (find-class 'direct-alien-slot-definition))
382       (t (call-next-method))))
383   
384   (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
385     (case (getf initargs :allocation)
386       (:alien (find-class 'effective-alien-slot-definition))
387       (t (call-next-method))))
388   
389   
390   (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
391     (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien)
392         (nconc 
393          (list :offset (most-specific-slot-value direct-slotds 'offset))
394          (call-next-method))
395       (call-next-method)))
396   
397
398   (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition))
399     (with-slots (offset) slotd
400       (let ((type (slot-definition-type slotd)))
401         (unless (slot-boundp slotd 'getter)
402           (let ((reader (reader-function type)))
403             (setf 
404              (slot-value slotd 'getter)
405              #'(lambda (object)
406                  (funcall reader (foreign-location object) offset)))))
407
408         (unless (slot-boundp slotd 'setter)
409           (let ((writer (writer-function type))
410                 (destroy (destroy-function type)))
411             (setf 
412              (slot-value slotd 'setter)
413              #'(lambda (value object)
414                  (let ((location (foreign-location object)))
415                    (funcall destroy location offset) ; destroy old value
416                    (funcall writer value location offset))))))))
417
418     (call-next-method))
419   
420   ;; TODO: call some C code to detect this a compile time
421   (defconstant +struct-alignmen+ 4)
422
423   (defun align-offset (size)
424     (if (zerop (mod size +struct-alignmen+))
425         size
426       (+ size (- +struct-alignmen+ (mod size +struct-alignmen+)))))
427
428   (defmethod compute-slots ((class proxy-class))
429     (let ((alien-slots 
430            (remove-if-not
431             #'(lambda (slotd)
432                 (eq (slot-definition-allocation slotd) :alien))
433             (class-direct-slots class))))      
434       (when alien-slots
435         (loop 
436          as offset = (align-offset (foreign-size 
437                                     (most-specific-proxy-superclass class)))
438                      then (align-offset 
439                            (+ 
440                             (slot-definition-offset slotd)
441                             (size-of (slot-definition-type slotd))))
442        for slotd in alien-slots
443        unless (slot-boundp slotd 'offset)
444        do (setf (slot-value slotd 'offset) offset))))
445     (call-next-method))
446
447   (defmethod validate-superclass ((class proxy-class) (super standard-class))
448     (subtypep (class-name super) 'proxy))
449   
450   (defmethod foreign-size ((class-name symbol))
451     (foreign-size (find-class class-name))))
452
453 (defmethod foreign-size ((object proxy))
454   (foreign-size (class-of object)))
455   
456
457 (defmethod alien-type ((class proxy-class) &rest args)
458   (declare (ignore class args))
459   (alien-type 'pointer))
460
461 (defmethod size-of ((class proxy-class) &rest args)
462   (declare (ignore class args))
463   (size-of 'pointer))
464
465 (defmethod from-alien-form (location (class proxy-class) &rest args)
466   (declare (ignore args))
467   `(ensure-proxy-instance ',(class-name class) ,location))
468
469 (defmethod from-alien-function ((class proxy-class) &rest args)
470   (declare (ignore args))  
471   #'(lambda (location)
472       (ensure-proxy-instance class location)))
473
474 (defmethod to-alien-form (instance (class proxy-class) &rest args)
475   (declare (ignore class args))
476   `(foreign-location ,instance))
477
478 (defmethod to-alien-function ((class proxy-class) &rest args)
479   (declare (ignore class args))
480   #'foreign-location)
481
482 (defmethod copy-from-alien-form (location (class proxy-class) &rest args)
483   (declare (ignore args))
484   (let ((class-name (class-name class)))
485     `(ensure-proxy-instance ',class-name
486       (reference-foreign ',class-name ,location))))
487
488 (defmethod copy-from-alien-function ((class proxy-class) &rest args)
489   (declare (ignore args))  
490   #'(lambda (location)
491       (ensure-proxy-instance class (reference-foreign class location))))
492
493 (defmethod copy-to-alien-form (instance (class proxy-class) &rest args)
494   (declare (ignore args))
495   `(reference-foreign ',(class-name class) (foreign-location ,instance)))
496
497 (defmethod copy-to-alien-function ((class proxy-class) &rest args)
498   (declare (ignore args))
499   #'(lambda (instance)
500       (reference-foreign class (foreign-location instance))))
501
502 (defmethod writer-function ((class proxy-class) &rest args)
503   (declare (ignore args))
504   #'(lambda (instance location &optional (offset 0))
505       (assert (null-pointer-p (sap-ref-sap location offset)))
506       (setf 
507        (sap-ref-sap location offset)
508        (reference-foreign class (foreign-location instance)))))
509
510 (defmethod reader-function ((class proxy-class) &rest args)
511   (declare (ignore args))
512   #'(lambda (location &optional (offset 0) weak-p)
513       (declare (ignore weak-p))
514       (let ((instance (sap-ref-sap location offset)))
515         (unless (null-pointer-p instance)
516           (ensure-proxy-instance class (reference-foreign class instance))))))
517
518 (defmethod destroy-function ((class proxy-class) &rest args)
519   (declare (ignore args))
520   #'(lambda (location &optional (offset 0))
521       (unreference-foreign class (sap-ref-sap location offset))))
522
523 (defmethod unbound-value ((class proxy-class) &rest args)
524   (declare (ignore args))
525   (values t nil))
526
527 (defun ensure-proxy-instance (class location &rest initargs)
528   "Returns a proxy object representing the foreign object at the give
529 location. If an existing object is not found in the cache
530 MAKE-PROXY-INSTANCE is called to create one."
531   (unless (null-pointer-p location)
532     (or 
533      #-debug-ref-counting(find-cached-instance location)
534      #+debug-ref-counting
535      (let ((instance (find-cached-instance location)))
536        (when instance
537          (format t "Object found in cache: ~A~%" instance)
538          instance))
539      (let ((instance (apply #'make-proxy-instance class location initargs)))
540        (cache-instance instance)
541        instance))))
542
543 (defgeneric make-proxy-instance (class location &key weak)
544   (:documentation "Creates a new proxy object representing the foreign
545 object at the give location. If WEAK is non NIL the foreign memory
546 will not be released when the proxy is garbage collected."))
547
548 (defmethod make-proxy-instance ((class symbol) location &rest initargs)
549   (apply #'make-proxy-instance (find-class class) location initargs))
550
551 (defmethod make-proxy-instance ((class proxy-class) location &key weak)
552   (let ((instance
553          (or
554           (find-invalidated-instance class)
555           (allocate-instance class))))
556     (setf (slot-value instance 'location) location)
557     (unless weak
558       (finalize instance (instance-finalizer instance)))
559     instance))
560
561
562 ;;;; Superclasses for wrapping of C structures
563
564 (defclass struct (proxy)
565   ()
566   (:metaclass proxy-class)
567   (:size 0))
568
569 (defmethod initialize-instance ((struct struct) &rest initargs)
570   (declare (ignore initargs))
571   (unless (slot-boundp struct 'location)
572     (let ((size (foreign-size (class-of struct))))
573       (if (zerop size)
574           (error "~A has zero size" (class-of struct))
575         (setf (slot-value struct 'location) (allocate-memory size)))))
576   (call-next-method))
577
578
579 ;;;; Metaclasses used for subclasses of struct
580
581 (defclass struct-class (proxy-class)
582   ())
583
584 (defmethod direct-slot-definition-class ((class struct-class) &rest initargs)
585   (if (not (getf initargs :allocation))
586       (find-class 'direct-alien-slot-definition)
587     (call-next-method)))
588
589 (defmethod reference-foreign ((class struct-class) location)
590   (copy-memory location (foreign-size class)))
591
592 (defmethod unreference-foreign ((class struct-class) location)
593   (deallocate-memory location))
594
595 (defmethod compute-slots :around ((class struct-class))
596     (let ((slots (call-next-method)))
597       (when (and 
598              #-sbcl>=0.9.8(class-finalized-p class) #+sbc098 t
599              (not (slot-boundp class 'size)))
600         (let ((size (loop
601                      for slotd in slots
602                      when (eq (slot-definition-allocation slotd) :alien)
603                      maximize (+ 
604                                (slot-definition-offset slotd)
605                                (size-of (slot-definition-type slotd))))))
606           (setf (slot-value class 'size) (+ size (mod size +struct-alignmen+)))))
607       slots))
608
609 (defmethod reader-function ((class struct-class) &rest args)
610   (declare (ignore args))
611   #'(lambda (location &optional (offset 0) weak-p)
612       (let ((instance (sap-ref-sap location offset)))
613         (unless (null-pointer-p instance)
614           (if weak-p
615               (ensure-proxy-instance class instance :weak t)
616             (ensure-proxy-instance class (reference-foreign class instance)))))))
617
618
619 (defclass static-struct-class (struct-class)
620   ())
621
622 (defmethod reference-foreign ((class static-struct-class) location)
623   (declare (ignore class))
624   location)
625
626 (defmethod unreference-foreign ((class static-struct-class) location)
627   (declare (ignore class location))
628   nil)
629
630 (defmethod reader-function ((class struct-class) &rest args)
631   (declare (ignore args))
632   #'(lambda (location &optional (offset 0) weak-p)
633       (declare (ignore weak-p))
634       (let ((instance (sap-ref-sap location offset)))
635         (unless (null-pointer-p instance)
636           (ensure-proxy-instance class instance :weak t)))))
637
638
639 ;;; Pseudo type for structs which are inlined in other objects
640
641 (defmethod size-of ((type (eql 'inlined)) &rest args)
642   (declare (ignore type))
643   (foreign-size (first args)))
644
645 (defmethod reader-function ((type (eql 'inlined)) &rest args)
646   (declare (ignore type))
647   (destructuring-bind (class) args
648     #'(lambda (location &optional (offset 0) weak-p)
649         (declare (ignore weak-p))
650         (ensure-proxy-instance class 
651          (reference-foreign class (sap+ location offset))))))
652
653 (defmethod writer-function ((type (eql 'inlined)) &rest args)
654   (declare (ignore type))
655   (destructuring-bind (class) args
656     #'(lambda (instance location &optional (offset 0))
657         (copy-memory (foreign-location instance) (foreign-size class) (sap+ location offset)))))
658
659 (defmethod destroy-function ((type (eql 'inlined)) &rest args)
660   (declare (ignore args))
661   #'(lambda (location &optional (offset 0))
662       (declare (ignore location offset))))
663
664 (export 'inlined)