bd691e21 |
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.1 2006/04/25 20:49:16 espen Exp $ |
24 | |
25 | (in-package "GFFI") |
26 | |
27 | |
28 | ;;;; Proxy cache |
29 | |
30 | (defvar *instance-cache* (make-hash-table :test #'eql)) |
31 | |
32 | (defun cache-instance (instance &optional (weak-ref t)) |
33 | (setf |
34 | (gethash (pointer-address (foreign-location instance)) *instance-cache*) |
35 | (if weak-ref |
36 | (make-weak-pointer instance) |
37 | instance))) |
38 | |
39 | (defun find-cached-instance (location) |
40 | (let ((ref (gethash (pointer-address location) *instance-cache*))) |
41 | (when ref |
42 | (if (weak-pointer-p ref) |
43 | (weak-pointer-value ref) |
44 | ref)))) |
45 | |
46 | (defun instance-cached-p (location) |
47 | (gethash (pointer-address location) *instance-cache*)) |
48 | |
49 | (defun remove-cached-instance (location) |
50 | (remhash (pointer-address location) *instance-cache*)) |
51 | |
52 | ;; For debuging |
53 | (defun list-cached-instances () |
54 | (let ((instances ())) |
55 | (maphash #'(lambda (location ref) |
56 | (declare (ignore location)) |
57 | (push ref instances)) |
58 | *instance-cache*) |
59 | instances)) |
60 | |
61 | ;; Instances that gets invalidated tend to be short lived, but created |
62 | ;; in large numbers. So we're keeping them in a hash table to be able |
63 | ;; to reuse them (and thus reduce consing) |
64 | (defvar *invalidated-instance-cache* (make-hash-table :test #'eql)) |
65 | |
66 | (defun cache-invalidated-instance (instance) |
67 | (push instance |
68 | (gethash (class-of instance) *invalidated-instance-cache*))) |
69 | |
70 | (defun find-invalidated-instance (class) |
71 | (when (gethash class *invalidated-instance-cache*) |
72 | (pop (gethash class *invalidated-instance-cache*)))) |
73 | |
74 | (defun list-invalidated-instances () |
75 | (let ((instances ())) |
76 | (maphash #'(lambda (location ref) |
77 | (declare (ignore location)) |
78 | (push ref instances)) |
79 | *invalidated-instance-cache*) |
80 | instances)) |
81 | |
82 | |
83 | |
84 | ;;;; Proxy for alien instances |
85 | |
86 | #+clisp |
87 | (defvar *foreign-instance-locations* (make-hash-table :weak :key)) |
88 | |
89 | ;; TODO: add a ref-counted-proxy subclass |
90 | (eval-when (:compile-toplevel :load-toplevel :execute) |
91 | (defclass proxy (virtual-slots-object) |
92 | (#-clisp(location :special t :type pointer)) |
93 | (:metaclass virtual-slots-class))) |
94 | |
95 | (defgeneric instance-finalizer (instance)) |
96 | (defgeneric reference-function (class)) |
97 | (defgeneric unreference-function (class)) |
98 | (defgeneric invalidate-instance (instance &optional finalize-p)) |
99 | (defgeneric allocate-foreign (object &key &allow-other-keys)) |
100 | |
101 | (defun foreign-location (instance) |
102 | #-clisp(slot-value instance 'location) |
103 | #+clisp(gethash instance *foreign-instance-locations*)) |
104 | |
105 | (defun (setf foreign-location) (location instance) |
106 | #-clisp(setf (slot-value instance 'location) location) |
107 | #+clisp(setf (gethash instance *foreign-instance-locations*) location)) |
108 | |
109 | (defun proxy-valid-p (instance) |
110 | #-clisp(slot-boundp instance 'location) |
111 | #+clisp(and (gethash instance *foreign-instance-locations*) t)) |
112 | |
113 | (defmethod reference-function ((name symbol)) |
114 | (reference-function (find-class name))) |
115 | |
116 | (defmethod unreference-function ((name symbol)) |
117 | (unreference-function (find-class name))) |
118 | |
119 | (defmethod print-object ((instance proxy) stream) |
120 | (print-unreadable-object (instance stream :type t :identity nil) |
121 | (if (proxy-valid-p instance) |
122 | (format stream "at 0x~X" (pointer-address (foreign-location instance))) |
123 | (write-string "at \"unbound\"" stream)))) |
124 | |
125 | |
126 | (defmethod initialize-instance :around ((instance proxy) &rest initargs &key &allow-other-keys) |
127 | (setf |
128 | (foreign-location instance) |
129 | (apply #'allocate-foreign instance initargs)) |
130 | (prog1 |
131 | (call-next-method) |
132 | (cache-instance instance) |
133 | (finalize instance (instance-finalizer instance)))) |
134 | |
135 | (defmethod instance-finalizer :around ((instance proxy)) |
136 | (let ((finalizer (call-next-method))) |
137 | (let ((location (foreign-location instance))) |
138 | #+(or cmu sbcl) |
139 | #'(lambda () |
140 | (remove-cached-instance location) |
141 | (funcall finalizer)) |
142 | #+clisp |
143 | #'(lambda (instance) |
144 | (declare (ignore instance)) |
145 | (remove-cached-instance location) |
146 | (funcall finalizer))))) |
147 | |
148 | (defmethod instance-finalizer ((instance proxy)) |
149 | (let ((location (foreign-location instance)) |
150 | (unref (unreference-function (class-of instance)))) |
151 | #'(lambda () |
152 | (funcall unref location)))) |
153 | |
154 | ;; FINALIZE-P should always be given the same value as the keyword |
155 | ;; argument :FINALZIE given to MAKE-PROXY-INSTANCE or non NIL if the |
156 | ;; proxy was created with MAKE-INSTANCE |
157 | (defmethod invalidate-instance ((instance proxy) &optional finalize-p) |
158 | (remove-cached-instance (foreign-location instance)) |
159 | #+(or sbcl cmu) |
160 | (progn |
161 | (when finalize-p |
162 | (funcall (instance-finalizer instance))) |
163 | (slot-makunbound instance 'location) |
164 | (cancel-finalization instance)) |
165 | ;; We can't 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 | |
285 | (defconstant +struct-alignmen+ (size-of 'pointer)) |
286 | |
287 | (defun align-offset (size &optional packed-p) |
288 | (if (or packed-p (zerop (mod size +struct-alignmen+))) |
289 | size |
290 | (+ size (- +struct-alignmen+ (mod size +struct-alignmen+))))) |
291 | |
292 | (defmethod compute-slots ((class proxy-class)) |
293 | (let ((alien-slots (remove-if-not |
294 | #'(lambda (allocation) (eq allocation :alien)) |
295 | (class-direct-slots class) |
296 | :key #'slot-definition-allocation))) |
297 | (when alien-slots |
298 | (loop |
299 | with packed-p = (foreign-slots-packed-p class) |
300 | as offset = (align-offset |
301 | (foreign-size (most-specific-proxy-superclass class)) |
302 | packed-p) |
303 | then (align-offset |
304 | (+ |
305 | (slot-definition-offset slotd) |
306 | (size-of (slot-definition-type slotd))) |
307 | packed-p) |
308 | for slotd in alien-slots |
309 | unless (slot-boundp slotd 'offset) |
310 | do (setf (slot-value slotd 'offset) offset)))) |
311 | (call-next-method)) |
312 | |
313 | (defmethod validate-superclass ((class proxy-class) (super standard-class)) |
314 | (subtypep (class-name super) 'proxy)) |
315 | |
316 | (defmethod foreign-size ((class-name symbol)) |
317 | (foreign-size (find-class class-name)))) |
318 | |
319 | (defmethod foreign-size ((object proxy)) |
320 | (foreign-size (class-of object))) |
321 | |
322 | (define-type-method alien-type ((type proxy)) |
323 | (declare (ignore type)) |
324 | (alien-type 'pointer)) |
325 | |
326 | (define-type-method size-of ((type proxy) &key inlined) |
327 | (assert-not-inlined type inlined) |
328 | (size-of 'pointer)) |
329 | |
330 | (define-type-method from-alien-form ((type proxy) form &key (ref :free)) |
331 | (let ((class (type-expand type))) |
332 | (ecase ref |
333 | (:free `(ensure-proxy-instance ',class ,form :reference nil)) |
334 | (:copy `(ensure-proxy-instance ',class ,form)) |
335 | ((:static :temp) `(ensure-proxy-instance ',class ,form |
336 | :reference nil :finalize nil))))) |
337 | |
338 | (define-type-method from-alien-function ((type proxy) &key (ref :free)) |
339 | (let ((class (type-expand type))) |
340 | (ecase ref |
341 | (:free |
342 | #'(lambda (location) |
343 | (ensure-proxy-instance class location :reference nil))) |
344 | (:copy |
345 | #'(lambda (location) |
346 | (ensure-proxy-instance class location))) |
347 | ((:static :temp) |
348 | #'(lambda (location) |
349 | (ensure-proxy-instance class location :reference nil :finalize nil)))))) |
350 | |
351 | (define-type-method to-alien-form ((type proxy) instance &optional copy-p) |
352 | (if copy-p |
353 | (let* ((class (type-expand type)) |
354 | (ref (reference-function class))) |
355 | (if (symbolp ref) |
356 | `(,ref (foreign-location ,instance)) |
357 | `(funcall (reference-function ',class) |
358 | (foreign-location ,instance)))) |
359 | `(foreign-location ,instance))) |
360 | |
361 | (define-type-method to-alien-function ((type proxy) &optional copy-p) |
362 | (if copy-p |
363 | (let ((ref (reference-function (type-expand type)))) |
364 | #'(lambda (instance) |
365 | (funcall ref (foreign-location instance)))) |
366 | #'foreign-location)) |
367 | |
368 | (define-type-method size-of ((type proxy) &key inlined) |
369 | (assert-not-inlined type inlined) |
370 | (size-of 'pointer)) |
371 | |
372 | (define-type-method writer-function ((type proxy) &key temp inlined) |
373 | (assert-not-inlined type inlined) |
374 | (if temp |
375 | #'(lambda (instance location &optional (offset 0)) |
376 | (assert (null-pointer-p (ref-pointer location offset))) |
377 | (setf (ref-pointer location offset) (foreign-location instance))) |
378 | (let ((ref (reference-function (type-expand type)))) |
379 | #'(lambda (instance location &optional (offset 0)) |
380 | (assert (null-pointer-p (ref-pointer location offset))) |
381 | (setf |
382 | (ref-pointer location offset) |
383 | (funcall ref (foreign-location instance))))))) |
384 | |
385 | (define-type-method reader-function ((type proxy) &key (ref :read) inlined) |
386 | (assert-not-inlined type inlined) |
387 | (let ((class (type-expand type))) |
388 | (ecase ref |
389 | (:read |
390 | #'(lambda (location &optional (offset 0)) |
391 | (let ((instance (ref-pointer location offset))) |
392 | (unless (null-pointer-p instance) |
393 | (ensure-proxy-instance class instance))))) |
394 | (:peek |
395 | #'(lambda (location &optional (offset 0)) |
396 | (let ((instance (ref-pointer location offset))) |
397 | (unless (null-pointer-p instance) |
398 | (ensure-proxy-instance class instance |
399 | :reference nil :finalize nil))))) |
400 | (:get |
401 | #'(lambda (location &optional (offset 0)) |
402 | (let ((instance (ref-pointer location offset))) |
403 | (unless (null-pointer-p instance) |
404 | (prog1 |
405 | (ensure-proxy-instance class instance :reference nil) |
406 | (setf (ref-pointer location offset) (make-pointer 0)))))))))) |
407 | |
408 | (define-type-method destroy-function ((type proxy) &key temp inlined) |
409 | (assert-not-inlined type inlined) |
410 | (if temp |
411 | #'(lambda (location &optional (offset 0)) |
412 | (setf (ref-pointer location offset) (make-pointer 0))) |
413 | (let ((unref (unreference-function (type-expand type)))) |
414 | #'(lambda (location &optional (offset 0)) |
415 | (unless (null-pointer-p (ref-pointer location offset)) |
416 | (funcall unref (ref-pointer location offset)) |
417 | (setf (ref-pointer location offset) (make-pointer 0))))))) |
418 | |
419 | (define-type-method copy-function ((type proxy) &key inlined) |
420 | (assert-not-inlined type inlined) |
421 | (let ((ref (reference-function (type-expand type)))) |
422 | #'(lambda (from to &optional (offset 0)) |
423 | (let ((instance (ref-pointer from offset))) |
424 | (unless (null-pointer-p instance) |
425 | (funcall ref instance)) |
426 | (setf (ref-pointer to offset) instance))))) |
427 | |
428 | (define-type-method unbound-value ((type proxy)) |
429 | (declare (ignore type)) |
430 | nil) |
431 | |
432 | (defun ensure-proxy-instance (class location &rest initargs) |
433 | "Returns a proxy object representing the foreign object at the give |
434 | location. If an existing proxy object is not found, |
435 | MAKE-PROXY-INSTANCE is called to create a new one. A second return |
436 | value indicates whether a new proxy was created or not." |
437 | (unless (null-pointer-p location) |
438 | (or |
439 | #-debug-ref-counting(find-cached-instance location) |
440 | #+debug-ref-counting |
441 | (let ((instance (find-cached-instance location))) |
442 | (when instance |
443 | (format t "Object found in cache: ~A~%" instance) |
444 | instance)) |
445 | (values |
446 | (apply #'make-proxy-instance class location initargs) |
447 | t)))) |
448 | |
449 | (defgeneric make-proxy-instance (class location &key reference finalize) |
450 | (:documentation "Creates a new proxy object representing the foreign |
451 | object at the give location.")) |
452 | |
453 | (defmethod make-proxy-instance ((class symbol) location &rest initargs) |
454 | (apply #'make-proxy-instance (find-class class) location initargs)) |
455 | |
456 | (defmethod make-proxy-instance ((class proxy-class) location |
457 | &key (reference t) (finalize t)) |
458 | (let ((instance |
459 | (or |
460 | (find-invalidated-instance class) |
461 | (allocate-instance class)))) |
462 | (setf (foreign-location instance) |
463 | (if reference |
464 | (funcall (reference-function class) location) |
465 | location)) |
466 | (finalize instance |
467 | (if finalize |
468 | (instance-finalizer instance) |
469 | ;; We still need to remove the instance from the cache even if we |
470 | ;; don't do normal finalization |
471 | (let ((location (foreign-location instance))) |
472 | #+(or cmu sbcl) |
473 | #'(lambda () |
474 | (remove-cached-instance location)) |
475 | #+clisp |
476 | #'(lambda (instance) |
477 | (declare (ignore instance)) |
478 | (remove-cached-instance location))))) |
479 | (cache-instance instance) |
480 | instance)) |
481 | |
482 | |
483 | ;;;; Superclasses for wrapping of C structures |
484 | |
485 | (defclass struct (proxy) |
486 | () |
487 | (:metaclass proxy-class) |
488 | (:size 0)) |
489 | |
490 | (defmethod allocate-foreign ((struct struct) &rest initargs) |
491 | (declare (ignore initargs)) |
492 | (let ((size (foreign-size (class-of struct)))) |
493 | (if (zerop size) |
494 | (error "~A has zero size" (class-of struct)) |
495 | (allocate-memory size)))) |
496 | |
497 | |
498 | ;;;; Metaclasses used for subclasses of struct |
499 | |
500 | (defclass struct-class (proxy-class) |
501 | ()) |
502 | |
503 | (defmethod shared-initialize ((class struct-class) names &rest initargs) |
504 | (declare (ignore names initargs)) |
505 | (call-next-method) |
506 | (let ((offsets nil) (copy-functions nil) (destroy-functions nil)) |
507 | (flet ((initialize-functions () |
508 | (loop |
509 | for slotd in (class-slots class) |
510 | as type = (slot-definition-type slotd) |
511 | when (eq (slot-definition-allocation slotd) :alien) |
512 | do (push (slot-definition-offset slotd) offsets) |
513 | (push (copy-function type) copy-functions) |
514 | (push (destroy-function type) destroy-functions)))) |
515 | (unless (slot-boundp class 'ref) |
516 | (setf |
517 | (slot-value class 'ref) |
518 | #'(lambda (from &optional (to (allocate-memory (foreign-size class)))) |
519 | (assert (not (null-pointer-p from))) |
520 | (unless offsets |
521 | (initialize-functions)) |
522 | (loop |
523 | for offset in offsets |
524 | for copy in copy-functions |
525 | do (funcall copy from to offset)) |
526 | to))) |
527 | (unless (slot-boundp class 'unref) |
528 | (setf (slot-value class 'unref) |
529 | #'(lambda (location &optional inlined-p) |
530 | (assert (not (null-pointer-p location))) |
531 | (unless offsets |
532 | (initialize-functions)) |
533 | (loop |
534 | for offset in offsets |
535 | for destroy in destroy-functions |
536 | do (funcall destroy location offset)) |
537 | (unless inlined-p |
538 | (deallocate-memory location)))))))) |
539 | |
540 | |
541 | (defmethod direct-slot-definition-class ((class struct-class) &rest initargs) |
542 | (if (not (getf initargs :allocation)) |
543 | (find-class 'direct-alien-slot-definition) |
544 | (call-next-method))) |
545 | |
546 | |
547 | (defmethod compute-slots :around ((class struct-class)) |
548 | (let ((slots (call-next-method))) |
549 | (when (and |
550 | #?-(or (sbcl>= 0 9 8) (featurep :clisp))(class-finalized-p class) |
551 | (not (slot-boundp class 'size))) |
552 | (let ((size (or |
553 | (loop |
554 | for slotd in slots |
555 | when (eq (slot-definition-allocation slotd) :alien) |
556 | maximize (+ |
557 | (slot-definition-offset slotd) |
558 | (size-of (slot-definition-type slotd)))) |
559 | 0))) |
560 | (setf (slot-value class 'size) (+ size (mod size +struct-alignmen+))))) |
561 | slots)) |
562 | |
563 | (define-type-method callback-wrapper ((type struct) var arg form) |
564 | (let ((class (type-expand type))) |
565 | `(let ((,var (ensure-proxy-instance ',class ,arg :finalize nil))) |
566 | (unwind-protect |
567 | ,form |
568 | (invalidate-instance ,var))))) |
569 | |
570 | (define-type-method size-of ((type struct) &key inlined) |
571 | (if inlined |
572 | (foreign-size type) |
573 | (size-of 'pointer))) |
574 | |
575 | (define-type-method writer-function ((type struct) &key temp inlined) |
576 | (if inlined |
577 | (if temp |
578 | (let ((size (size-of type :inlined t))) |
579 | #'(lambda (instance location &optional (offset 0)) |
580 | (copy-memory |
581 | (foreign-location instance) size |
582 | (pointer+ location offset)))) |
583 | (let ((ref (reference-function (type-expand type)))) |
584 | #'(lambda (instance location &optional (offset 0)) |
585 | (funcall ref |
586 | (foreign-location instance) |
587 | (pointer+ location offset))))) |
588 | (call-next-method))) |
589 | |
590 | (define-type-method reader-function ((type struct) &key (ref :read) inlined) |
591 | (if inlined |
592 | (let ((class (type-expand type)) |
593 | (size (size-of type :inlined t))) |
594 | (ecase ref |
595 | (:read |
596 | #'(lambda (location &optional (offset 0)) |
597 | (ensure-proxy-instance class (pointer+ location offset)))) |
598 | (:peek |
599 | #'(lambda (location &optional (offset 0)) |
600 | (ensure-proxy-instance class (pointer+ location offset) |
601 | :reference nil :finalize nil))) |
602 | (:get |
603 | #'(lambda (location &optional (offset 0)) |
604 | (prog1 |
605 | (ensure-proxy-instance class |
606 | (copy-memory (pointer+ location offset) size) |
607 | :reference nil) |
608 | (clear-memory (pointer+ location offset) size)))))) |
609 | (call-next-method))) |
610 | |
611 | (define-type-method destroy-function ((type struct) &key temp inlined) |
612 | (if inlined |
613 | (let ((size (size-of type :inlined t))) |
614 | (if temp |
615 | #'(lambda (location &optional (offset 0)) |
616 | (clear-memory (pointer+ location offset) size)) |
617 | (let ((unref (unreference-function (type-expand type)))) |
618 | #'(lambda (location &optional (offset 0)) |
619 | (funcall unref (pointer+ location offset) t))))) |
620 | (call-next-method))) |
621 | |
622 | (define-type-method copy-function ((type struct) &key inlined) |
623 | (if inlined |
624 | (let ((ref (reference-function (type-expand type)))) |
625 | #'(lambda (from to &optional (offset 0)) |
626 | (funcall ref (pointer+ from offset) (pointer+ to offset)))) |
627 | (call-next-method))) |