chiark / gitweb /
New class REF-COUNTED-OBJECT
[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.6 2007-01-12 10:26:44 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 #?(or (sbcl>= 0 9 17) (featurep :clisp))
87 (defvar *foreign-instance-locations* 
88   (make-hash-table #+clisp :weak #+sbcl :weakness :key))
89
90 ;; TODO: add a ref-counted-proxy subclass
91 (eval-when (:compile-toplevel :load-toplevel :execute)
92   (defclass proxy (virtual-slots-object)
93     (#?-(or (sbcl>= 0 9 17) (featurep :clisp))(%location :special t :type pointer))
94     (:metaclass virtual-slots-class)))
95
96 (defgeneric instance-finalizer (instance))
97 (defgeneric reference-function (class))
98 (defgeneric unreference-function (class))
99 (defgeneric invalidate-instance (instance &optional finalize-p))
100 (defgeneric allocate-foreign (object &key &allow-other-keys))
101
102 #?-(or (sbcl>= 0 9 17) (featurep :clisp))
103 (progn
104   (defun foreign-location (instance)
105     (slot-value instance '%location))
106
107   (defun (setf foreign-location) (location instance)
108     (setf (slot-value instance '%location) location))
109   
110   (defun proxy-valid-p (instance)
111     (slot-boundp instance '%location)))
112   
113 #?(or (sbcl>= 0 9 17) (featurep :clisp))
114 (progn
115   (defun foreign-location (instance)
116     (gethash instance *foreign-instance-locations*))
117
118   (defun (setf foreign-location) (location instance)
119     (setf (gethash instance *foreign-instance-locations*) location))
120
121   (defun proxy-valid-p (instance)
122     (and (gethash instance *foreign-instance-locations*) t)))
123
124
125 (defmethod reference-function ((name symbol))
126   (reference-function (find-class name)))
127
128 (defmethod unreference-function ((name symbol))
129   (unreference-function (find-class name)))
130
131 (defmethod print-object ((instance proxy) stream)
132   (print-unreadable-object (instance stream :type t :identity nil)
133     (if (proxy-valid-p instance)
134         (format stream "at 0x~X" (pointer-address (foreign-location instance)))
135       (write-string "at \"unbound\"" stream))))
136
137
138 (defmethod initialize-instance :around ((instance proxy) &rest initargs &key &allow-other-keys) 
139   (setf  
140    (foreign-location instance)
141    (apply #'allocate-foreign instance initargs))
142   (prog1
143       (call-next-method)
144     (cache-instance instance)
145     (finalize instance (instance-finalizer instance))))
146
147 (defmethod instance-finalizer :around ((instance proxy))
148   (let ((finalizer (call-next-method)))
149     (let ((location (foreign-location instance)))
150       #+(or cmu sbcl)
151       #'(lambda ()
152           (remove-cached-instance location)
153           (funcall finalizer))
154       #+clisp
155       #'(lambda (instance)
156           (declare (ignore instance))
157           (remove-cached-instance location)
158           (funcall finalizer)))))
159
160 (defmethod instance-finalizer ((instance proxy))
161   (let ((location (foreign-location instance))
162         (unref (unreference-function (class-of instance))))
163     #'(lambda ()
164         (funcall unref location))))
165
166 ;; FINALIZE-P should always be the same as the keyword argument
167 ;; :FINALZIE given to MAKE-PROXY-INSTANCE or non NIL if the proxy was
168 ;; created with MAKE-INSTANCE
169 (defmethod invalidate-instance ((instance proxy) &optional finalize-p)
170   (remove-cached-instance (foreign-location instance))
171   #+(or sbcl cmu)
172   (progn
173     (when finalize-p
174       (funcall (instance-finalizer instance)))
175     #?-(sbcl>= 0 9 17)(slot-makunbound instance '%location)
176     #?(sbcl>= 0 9 17)(remhash instance *foreign-instance-locations*)
177     (cancel-finalization instance))
178   ;; We can't cache invalidated instances in CLISP beacuse it is
179   ;; not possible to cancel finalization
180   #-clisp(cache-invalidated-instance instance))
181
182
183 ;;;; Metaclass used for subclasses of proxy
184
185 (eval-when (:compile-toplevel :load-toplevel :execute)
186   (defclass proxy-class (virtual-slots-class)
187     ((size :accessor foreign-size)
188      (packed :reader foreign-slots-packed-p)
189      (ref :reader reference-function)
190      (unref :reader unreference-function)))
191
192   (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
193     ((offset :reader slot-definition-offset :initarg :offset))
194     (:default-initargs :allocation :alien))
195   
196   (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
197     ((offset :reader slot-definition-offset :initarg :offset)))
198
199   (defclass direct-virtual-alien-slot-definition (direct-virtual-slot-definition)
200     ())
201   
202   (defclass effective-virtual-alien-slot-definition (effective-virtual-slot-definition)
203     ())
204
205   (defgeneric foreign-size-p (class))
206   (defgeneric most-specific-proxy-superclass (class))
207   (defgeneric direct-proxy-superclass (class))
208   
209   (defmethod foreign-size-p ((class proxy-class))
210     (slot-boundp class 'size))
211
212   (defmethod most-specific-proxy-superclass ((class proxy-class))
213     (find-if
214      #'(lambda (class)
215          (subtypep (class-name class) 'proxy))
216      (cdr (compute-class-precedence-list class))))
217
218   (defmethod direct-proxy-superclass ((class proxy-class))
219     (find-if
220      #'(lambda (class)
221          (subtypep (class-name class) 'proxy))
222      (class-direct-superclasses class)))
223   
224   (defmethod shared-initialize ((class proxy-class) names 
225                                 &key size packed ref unref)
226     (declare (ignore names))
227     (cond
228      (size (setf (slot-value class 'size) (first size)))
229      ((slot-boundp class 'size) (slot-makunbound class 'size)))
230     (setf (slot-value class 'packed) (first packed))
231     (when ref
232       (setf (slot-value class 'ref) (first ref)))
233     (when unref
234       (setf (slot-value class 'unref) (first unref)))
235     (call-next-method))
236
237   (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
238     (case (getf initargs :allocation)
239       (:alien (find-class 'direct-alien-slot-definition))
240       (:virtual (find-class 'direct-virtual-alien-slot-definition))
241       (t (call-next-method))))
242   
243   (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
244     (case (getf initargs :allocation)
245       (:alien (find-class 'effective-alien-slot-definition))
246       (:virtual (find-class 'effective-virtual-alien-slot-definition))
247       (t (call-next-method))))
248
249   
250   (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
251     (if (eq (slot-definition-allocation (first direct-slotds)) :alien)
252         (nconc 
253          (list :offset (most-specific-slot-value direct-slotds 'offset))
254          (call-next-method))
255       (call-next-method)))
256   
257   (defmethod slot-readable-p ((slotd effective-alien-slot-definition))
258     (declare (ignore slotd))
259     t)
260
261   (defmethod compute-slot-reader-function ((slotd effective-alien-slot-definition) &optional signal-unbound-p)
262     (declare (ignore signal-unbound-p))
263     (let* ((type (slot-definition-type slotd))
264            (offset (slot-definition-offset slotd))
265            (reader (reader-function type)))
266       #'(lambda (object)
267           (funcall reader (foreign-location object) offset))))
268
269   (defmethod slot-writable-p ((slotd effective-alien-slot-definition))
270     (declare (ignore slotd))
271     t)
272
273   (defmethod compute-slot-writer-function ((slotd effective-alien-slot-definition))
274     (let* ((type (slot-definition-type slotd))
275            (offset (slot-definition-offset slotd))
276            (writer (writer-function type))
277            (destroy (destroy-function type)))
278       #'(lambda (value object)
279           (let ((location (foreign-location object)))
280             (funcall destroy location offset) ; destroy old value
281             (funcall writer value location offset))
282           value)))
283   
284   (defmethod compute-slot-reader-function ((slotd effective-virtual-alien-slot-definition) &optional signal-unbound-p)
285     (declare (ignore signal-unbound-p))
286     (if (and (slot-boundp slotd 'getter) (stringp (slot-definition-getter slotd)))
287         (let ((getter (slot-definition-getter slotd))
288               (type (slot-definition-type slotd))
289               (reader nil))
290           #'(lambda (object)
291               (unless reader
292                 (setq reader (mkbinding getter type 'pointer)))
293               (funcall reader (foreign-location object))))
294       (call-next-method)))
295
296   (defmethod compute-slot-writer-function ((slotd effective-virtual-alien-slot-definition))
297     (if (and (slot-boundp slotd 'setter) (stringp (slot-definition-setter slotd)))
298         (let ((setter (slot-definition-setter slotd))
299               (type (slot-definition-type slotd))
300               (writer nil))
301           #'(lambda (value object)
302               (unless writer
303                 (setq writer (mkbinding setter nil 'pointer type)))
304               (funcall writer (foreign-location object) value)))
305       (call-next-method)))
306   
307   (defun adjust-offset (offset type &optional packed-p)
308     (let ((alignment (type-alignment type)))
309       (if (or packed-p (zerop (mod offset alignment)))
310           offset
311         (+ offset (- alignment (mod offset alignment))))))
312
313   (defmethod compute-slots ((class proxy-class))
314     (let ((alien-slots (remove-if-not 
315                         #'(lambda (allocation) (eq allocation :alien))
316                         (class-direct-slots class)
317                         :key #'slot-definition-allocation)))
318       (when alien-slots
319         (loop 
320          with packed-p = (foreign-slots-packed-p class)
321          for slotd in alien-slots
322          as offset = (adjust-offset 
323                       (foreign-size (most-specific-proxy-superclass class))
324                       (slot-definition-type slotd)
325                       packed-p)
326                      then (adjust-offset offset (slot-definition-type slotd) packed-p)
327          do (if (slot-boundp slotd 'offset)
328                 (setf offset (slot-value slotd 'offset))
329               (setf (slot-value slotd 'offset) offset))
330             (incf offset (size-of (slot-definition-type slotd))))))
331     (call-next-method))
332
333   (defmethod validate-superclass ((class proxy-class) (super standard-class))
334     (subtypep (class-name super) 'proxy))
335   
336   (defmethod foreign-size ((class-name symbol))
337     (foreign-size (find-class class-name))))
338
339 (defmethod foreign-size ((object proxy))
340   (foreign-size (class-of object)))
341
342 (define-type-method alien-type ((type proxy))
343   (declare (ignore type))
344   (alien-type 'pointer))
345
346 (define-type-method size-of ((type proxy) &key inlined)
347   (assert-not-inlined type inlined)
348   (size-of 'pointer))
349
350 (define-type-method type-alignment ((type proxy) &key inlined)
351   (assert-not-inlined type inlined)
352   (type-alignment 'pointer))
353
354 (define-type-method from-alien-form ((type proxy) form &key (ref :free))
355   (let ((class (type-expand type)))
356     (ecase ref
357       (:free `(ensure-proxy-instance ',class ,form :reference nil))
358       (:copy `(ensure-proxy-instance ',class ,form))
359       ((:static :temp) `(ensure-proxy-instance ',class ,form 
360                          :reference nil :finalize nil)))))
361
362 (define-type-method from-alien-function ((type proxy) &key (ref :free))
363   (let ((class (type-expand type)))
364     (ecase ref
365       (:free 
366        #'(lambda (location)
367            (ensure-proxy-instance class location :reference nil)))
368       (:copy 
369        #'(lambda (location)
370            (ensure-proxy-instance class location)))
371       ((:static :temp)
372        #'(lambda (location)
373            (ensure-proxy-instance class location :reference nil :finalize nil))))))
374
375 (define-type-method to-alien-form ((type proxy) instance &optional copy-p)
376   (if copy-p
377       (let* ((class (type-expand type))
378              (ref (reference-function class)))
379         (if (symbolp ref)
380             `(,ref (foreign-location ,instance))
381           `(funcall (reference-function ',class) 
382             (foreign-location ,instance))))
383     `(foreign-location ,instance)))
384
385 (define-type-method to-alien-function ((type proxy) &optional copy-p)
386   (if copy-p
387       (let ((ref (reference-function (type-expand type))))
388         #'(lambda (instance)
389             (funcall ref (foreign-location instance))))
390     #'foreign-location))
391
392 (define-type-method writer-function ((type proxy) &key temp inlined)
393   (assert-not-inlined type inlined)
394   (if temp
395       #'(lambda (instance location &optional (offset 0))
396           (assert (null-pointer-p (ref-pointer location offset)))
397           (setf (ref-pointer location offset) (foreign-location instance)))
398     (let ((ref (reference-function (type-expand type))))
399       #'(lambda (instance location &optional (offset 0))
400           (assert (null-pointer-p (ref-pointer location offset)))
401           (setf 
402            (ref-pointer location offset)
403            (funcall ref (foreign-location instance)))))))
404
405 (define-type-method reader-function ((type proxy) &key (ref :read) inlined)
406   (assert-not-inlined type inlined)
407   (let ((class (type-expand type)))
408     (ecase ref
409       (:read
410        #'(lambda (location &optional (offset 0))
411            (let ((instance (ref-pointer location offset)))
412              (unless (null-pointer-p instance)
413                (ensure-proxy-instance class instance)))))
414       (:peek
415        #'(lambda (location &optional (offset 0))
416            (let ((instance (ref-pointer location offset)))
417              (unless (null-pointer-p instance)
418                (ensure-proxy-instance class instance 
419                 :reference nil :finalize nil)))))
420       (:get
421        #'(lambda (location &optional (offset 0))
422            (let ((instance (ref-pointer location offset)))
423              (unless (null-pointer-p instance)
424                (prog1
425                    (ensure-proxy-instance class instance :reference nil)
426                  (setf (ref-pointer location offset) (make-pointer 0))))))))))
427
428 (define-type-method destroy-function ((type proxy) &key temp inlined)
429   (assert-not-inlined type inlined)
430   (if temp
431       #'(lambda (location &optional (offset 0))
432           (setf (ref-pointer location offset) (make-pointer 0)))
433     (let ((unref (unreference-function (type-expand type))))
434       #'(lambda (location &optional (offset 0))
435           (unless (null-pointer-p (ref-pointer location offset))
436             (funcall unref (ref-pointer location offset))
437             (setf (ref-pointer location offset) (make-pointer 0)))))))
438
439 (define-type-method copy-function ((type proxy) &key inlined)
440   (assert-not-inlined type inlined)
441   (let ((ref (reference-function (type-expand type))))
442     #'(lambda (from to &optional (offset 0))
443         (let ((instance (ref-pointer from offset)))
444           (unless (null-pointer-p instance)
445             (funcall ref instance))
446           (setf (ref-pointer to offset) instance)))))
447
448 (define-type-method unbound-value ((type proxy))
449   (declare (ignore type))
450   nil)
451
452 (defun ensure-proxy-instance (class location &rest initargs)
453   "Returns a proxy object representing the foreign object at the give
454 location. If an existing proxy object is not found,
455 MAKE-PROXY-INSTANCE is called to create a new one. A second return
456 value indicates whether a new proxy was created or not."
457   (unless (null-pointer-p location)
458     (or 
459      #-debug-ref-counting(find-cached-instance location)
460      #+debug-ref-counting
461      (let ((instance (find-cached-instance location)))
462        (when instance
463          (format t "Object found in cache: ~A~%" instance)
464          instance))
465      (values
466       (apply #'make-proxy-instance class location initargs)
467       t))))
468
469 (defgeneric make-proxy-instance (class location &key reference finalize)
470   (:documentation "Creates a new proxy object representing the foreign
471 object at the give location."))
472
473 (defmethod make-proxy-instance ((class symbol) location &rest initargs)
474   (apply #'make-proxy-instance (find-class class) location initargs))
475
476 (defmethod make-proxy-instance ((class proxy-class) location 
477                                 &key (reference t) (finalize t))
478   (let ((instance
479          (or
480           (find-invalidated-instance class)
481           (allocate-instance class))))    
482     (setf (foreign-location instance) 
483      (if reference
484          (funcall (reference-function class) location)
485        location))
486     (finalize instance 
487      (if finalize
488          (instance-finalizer instance)
489        ;; We still need to remove the instance from the cache even if we 
490        ;; don't do normal finalization
491        (let ((location (foreign-location instance)))
492          #+(or cmu sbcl)
493          #'(lambda ()
494              (remove-cached-instance location))
495          #+clisp
496          #'(lambda (instance)
497              (declare (ignore instance))
498              (remove-cached-instance location)))))
499     (cache-instance instance)
500     instance))
501
502 ;;;; Superclass for ref-counted objects
503
504 (defclass ref-counted-object (proxy)
505   ()
506   (:metaclass proxy-class))
507
508 (define-type-method from-alien-form ((type ref-counted-object) form 
509                                      &key (ref :copy))
510   (call-next-method type form :ref ref))
511
512 (define-type-method from-alien-function ((type ref-counted-object) 
513                                          &key (ref :copy))
514   (call-next-method type :ref ref))
515
516
517 ;;;; Superclasses for wrapping of C structures
518
519 (defclass struct (proxy)
520   ()
521   (:metaclass proxy-class)
522   (:size 0))
523
524 (defmethod allocate-foreign ((struct struct) &rest initargs)
525   (declare (ignore initargs))
526   (let ((size (foreign-size (class-of struct))))
527     (if (zerop size)
528         (error "~A has zero size" (class-of struct))
529       (allocate-memory size))))
530
531
532 ;;;; Metaclasses used for subclasses of struct
533
534 (defclass struct-class (proxy-class)
535   ())
536
537 (defmethod shared-initialize ((class struct-class) names &rest initargs)
538   (declare (ignore names initargs))
539   (call-next-method)
540   (let ((offsets nil) (copy-functions nil) (destroy-functions nil))
541     (flet ((initialize-functions ()
542              (loop
543               for slotd in (class-slots class)
544               as type = (slot-definition-type slotd)
545               when (eq (slot-definition-allocation slotd) :alien)
546               do (push (slot-definition-offset slotd) offsets)
547                  (push (copy-function type) copy-functions)
548                  (push (destroy-function type) destroy-functions))))
549       (unless (slot-boundp class 'ref)
550         (setf 
551          (slot-value class 'ref)
552          #'(lambda (from &optional (to (allocate-memory (foreign-size class))))
553              (assert (not (null-pointer-p from)))
554              (unless offsets 
555                (initialize-functions))
556              (loop
557               for offset in offsets
558               for copy in copy-functions
559               do (funcall copy from to offset))
560              to)))
561       (unless (slot-boundp class 'unref)
562         (setf (slot-value class 'unref) 
563          #'(lambda (location &optional inlined-p)
564              (assert (not (null-pointer-p location)))
565              (unless offsets 
566                (initialize-functions))
567              (loop
568               for offset in offsets
569               for destroy in destroy-functions
570               do (funcall destroy location offset))
571              (unless inlined-p
572                (deallocate-memory location))))))))
573
574
575 (defmethod direct-slot-definition-class ((class struct-class) &rest initargs)
576   (if (not (getf initargs :allocation))
577       (find-class 'direct-alien-slot-definition)
578     (call-next-method)))
579
580
581 (defmethod compute-slots :around ((class struct-class))  
582   (let ((slots (call-next-method)))
583     (when (and
584            #?-(or (sbcl>= 0 9 8) (featurep :clisp))(class-finalized-p class)
585            (not (slot-boundp class 'size)))
586       (setf (slot-value class 'size)
587        (or
588         (loop
589          for slotd in slots
590          when (eq (slot-definition-allocation slotd) :alien)
591          maximize (+ 
592                    (slot-definition-offset slotd)
593                    (size-of (slot-definition-type slotd))))
594         0)))
595     slots))
596
597 (define-type-method callback-wrapper ((type struct) var arg form)
598   (let ((class (type-expand type)))
599     `(let ((,var (ensure-proxy-instance ',class ,arg :finalize nil)))
600        (unwind-protect
601            ,form
602          (invalidate-instance ,var)))))
603
604 (define-type-method size-of ((type struct) &key inlined)
605   (if inlined
606       (foreign-size type)
607     (size-of 'pointer)))
608
609 (define-type-method type-alignment ((type struct) &key inlined)
610   (if inlined
611       (let ((slot1 (find-if
612                     #'(lambda (slotd)
613                         (eq (slot-definition-allocation slotd) :alien))
614                     (class-slots (find-class type)))))
615         (type-alignment (slot-definition-type slot1)))
616     (type-alignment 'pointer)))
617
618 (define-type-method writer-function ((type struct) &key temp inlined)
619   (if inlined
620       (if temp
621           (let ((size (size-of type :inlined t)))
622             #'(lambda (instance location &optional (offset 0))
623                 (copy-memory 
624                  (foreign-location instance) size
625                  (pointer+ location offset))))
626         (let ((ref (reference-function  (type-expand type))))
627           #'(lambda (instance location &optional (offset 0))
628               (funcall ref 
629                (foreign-location instance) 
630                (pointer+ location offset)))))
631     (call-next-method)))
632
633 (define-type-method reader-function ((type struct) &key (ref :read) inlined)
634   (if inlined
635       (let ((class (type-expand type))
636             (size (size-of type :inlined t)))
637         (ecase ref
638           (:read
639            #'(lambda (location &optional (offset 0))
640                (ensure-proxy-instance class (pointer+ location offset))))
641           (:peek
642            #'(lambda (location &optional (offset 0))           
643                (ensure-proxy-instance class (pointer+ location offset) 
644                 :reference nil :finalize nil)))
645           (:get
646            #'(lambda (location &optional (offset 0))
647                (prog1
648                    (ensure-proxy-instance class
649                     (copy-memory (pointer+ location offset) size)
650                     :reference nil)
651                  (clear-memory (pointer+ location offset) size))))))
652     (call-next-method)))
653
654 (define-type-method destroy-function ((type struct) &key temp inlined)
655   (if inlined
656       (let ((size (size-of type :inlined t)))
657         (if temp
658             #'(lambda (location &optional (offset 0))
659                 (clear-memory (pointer+ location offset) size))
660           (let ((unref (unreference-function  (type-expand type))))
661             #'(lambda (location &optional (offset 0))
662                 (funcall unref (pointer+ location offset) t)))))
663     (call-next-method)))
664
665 (define-type-method copy-function ((type struct) &key inlined)
666   (if inlined
667         (let ((ref (reference-function  (type-expand type))))
668           #'(lambda (from to &optional (offset 0))
669               (funcall ref (pointer+ from offset) (pointer+ to offset))))
670     (call-next-method)))