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 | |
441c7382 |
23 | ;; $Id: proxy.lisp,v 1.5 2006/09/29 13:14:19 espen Exp $ |
bd691e21 |
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 | |
441c7382 |
86 | #?(or (sbcl>= 0 9 17) (featurep :clisp)) |
87 | (defvar *foreign-instance-locations* |
88 | (make-hash-table #+clisp :weak #+sbcl :weakness :key)) |
bd691e21 |
89 | |
90 | ;; TODO: add a ref-counted-proxy subclass |
91 | (eval-when (:compile-toplevel :load-toplevel :execute) |
92 | (defclass proxy (virtual-slots-object) |
441c7382 |
93 | (#?-(or (sbcl>= 0 9 17) (featurep :clisp))(%location :special t :type pointer)) |
bd691e21 |
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 | |
441c7382 |
102 | #?-(or (sbcl>= 0 9 17) (featurep :clisp)) |
103 | (progn |
104 | (defun foreign-location (instance) |
105 | (slot-value instance '%location)) |
bd691e21 |
106 | |
441c7382 |
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))) |
bd691e21 |
123 | |
bd691e21 |
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 | |
441c7382 |
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 |
bd691e21 |
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))) |
441c7382 |
175 | #?-(sbcl>= 0 9 17)(slot-makunbound instance '%location) |
176 | #?(sbcl>= 0 9 17)(remhash instance *foreign-instance-locations*) |
bd691e21 |
177 | (cancel-finalization instance)) |
6b716036 |
178 | ;; We can't cache invalidated instances in CLISP beacuse it is |
bd691e21 |
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 | |
376809b0 |
257 | (defmethod slot-readable-p ((slotd effective-alien-slot-definition)) |
258 | (declare (ignore slotd)) |
259 | t) |
bd691e21 |
260 | |
6b716036 |
261 | (defmethod compute-slot-reader-function ((slotd effective-alien-slot-definition) &optional signal-unbound-p) |
262 | (declare (ignore signal-unbound-p)) |
bd691e21 |
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 | |
376809b0 |
269 | (defmethod slot-writable-p ((slotd effective-alien-slot-definition)) |
270 | (declare (ignore slotd)) |
271 | t) |
272 | |
bd691e21 |
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 | |
6b716036 |
284 | (defmethod compute-slot-reader-function ((slotd effective-virtual-alien-slot-definition) &optional signal-unbound-p) |
285 | (declare (ignore signal-unbound-p)) |
bd691e21 |
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 | |
83f60e84 |
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)))))) |
bd691e21 |
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) |
83f60e84 |
321 | for slotd in alien-slots |
322 | as offset = (adjust-offset |
bd691e21 |
323 | (foreign-size (most-specific-proxy-superclass class)) |
83f60e84 |
324 | (slot-definition-type slotd) |
bd691e21 |
325 | packed-p) |
83f60e84 |
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)))))) |
bd691e21 |
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 | |
83f60e84 |
350 | (define-type-method type-alignment ((type proxy) &key inlined) |
351 | (assert-not-inlined type inlined) |
352 | (type-alignment 'pointer)) |
353 | |
bd691e21 |
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 | |
bd691e21 |
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 | |
503 | ;;;; Superclasses for wrapping of C structures |
504 | |
505 | (defclass struct (proxy) |
506 | () |
507 | (:metaclass proxy-class) |
508 | (:size 0)) |
509 | |
510 | (defmethod allocate-foreign ((struct struct) &rest initargs) |
511 | (declare (ignore initargs)) |
512 | (let ((size (foreign-size (class-of struct)))) |
513 | (if (zerop size) |
514 | (error "~A has zero size" (class-of struct)) |
515 | (allocate-memory size)))) |
516 | |
517 | |
518 | ;;;; Metaclasses used for subclasses of struct |
519 | |
520 | (defclass struct-class (proxy-class) |
521 | ()) |
522 | |
523 | (defmethod shared-initialize ((class struct-class) names &rest initargs) |
524 | (declare (ignore names initargs)) |
525 | (call-next-method) |
526 | (let ((offsets nil) (copy-functions nil) (destroy-functions nil)) |
527 | (flet ((initialize-functions () |
528 | (loop |
529 | for slotd in (class-slots class) |
530 | as type = (slot-definition-type slotd) |
531 | when (eq (slot-definition-allocation slotd) :alien) |
532 | do (push (slot-definition-offset slotd) offsets) |
533 | (push (copy-function type) copy-functions) |
534 | (push (destroy-function type) destroy-functions)))) |
535 | (unless (slot-boundp class 'ref) |
536 | (setf |
537 | (slot-value class 'ref) |
538 | #'(lambda (from &optional (to (allocate-memory (foreign-size class)))) |
539 | (assert (not (null-pointer-p from))) |
540 | (unless offsets |
541 | (initialize-functions)) |
542 | (loop |
543 | for offset in offsets |
544 | for copy in copy-functions |
545 | do (funcall copy from to offset)) |
546 | to))) |
547 | (unless (slot-boundp class 'unref) |
548 | (setf (slot-value class 'unref) |
549 | #'(lambda (location &optional inlined-p) |
550 | (assert (not (null-pointer-p location))) |
551 | (unless offsets |
552 | (initialize-functions)) |
553 | (loop |
554 | for offset in offsets |
555 | for destroy in destroy-functions |
556 | do (funcall destroy location offset)) |
557 | (unless inlined-p |
558 | (deallocate-memory location)))))))) |
559 | |
560 | |
561 | (defmethod direct-slot-definition-class ((class struct-class) &rest initargs) |
562 | (if (not (getf initargs :allocation)) |
563 | (find-class 'direct-alien-slot-definition) |
564 | (call-next-method))) |
565 | |
566 | |
567 | (defmethod compute-slots :around ((class struct-class)) |
568 | (let ((slots (call-next-method))) |
569 | (when (and |
570 | #?-(or (sbcl>= 0 9 8) (featurep :clisp))(class-finalized-p class) |
571 | (not (slot-boundp class 'size))) |
83f60e84 |
572 | (setf (slot-value class 'size) |
573 | (or |
574 | (loop |
575 | for slotd in slots |
576 | when (eq (slot-definition-allocation slotd) :alien) |
577 | maximize (+ |
578 | (slot-definition-offset slotd) |
579 | (size-of (slot-definition-type slotd)))) |
580 | 0))) |
bd691e21 |
581 | slots)) |
582 | |
583 | (define-type-method callback-wrapper ((type struct) var arg form) |
584 | (let ((class (type-expand type))) |
585 | `(let ((,var (ensure-proxy-instance ',class ,arg :finalize nil))) |
586 | (unwind-protect |
587 | ,form |
588 | (invalidate-instance ,var))))) |
589 | |
590 | (define-type-method size-of ((type struct) &key inlined) |
591 | (if inlined |
592 | (foreign-size type) |
593 | (size-of 'pointer))) |
594 | |
83f60e84 |
595 | (define-type-method type-alignment ((type struct) &key inlined) |
596 | (if inlined |
597 | (let ((slot1 (find-if |
598 | #'(lambda (slotd) |
599 | (eq (slot-definition-allocation slotd) :alien)) |
600 | (class-slots (find-class type))))) |
601 | (type-alignment (slot-definition-type slot1))) |
602 | (type-alignment 'pointer))) |
603 | |
bd691e21 |
604 | (define-type-method writer-function ((type struct) &key temp inlined) |
605 | (if inlined |
606 | (if temp |
607 | (let ((size (size-of type :inlined t))) |
608 | #'(lambda (instance location &optional (offset 0)) |
609 | (copy-memory |
610 | (foreign-location instance) size |
611 | (pointer+ location offset)))) |
612 | (let ((ref (reference-function (type-expand type)))) |
613 | #'(lambda (instance location &optional (offset 0)) |
614 | (funcall ref |
615 | (foreign-location instance) |
616 | (pointer+ location offset))))) |
617 | (call-next-method))) |
618 | |
619 | (define-type-method reader-function ((type struct) &key (ref :read) inlined) |
620 | (if inlined |
621 | (let ((class (type-expand type)) |
622 | (size (size-of type :inlined t))) |
623 | (ecase ref |
624 | (:read |
625 | #'(lambda (location &optional (offset 0)) |
626 | (ensure-proxy-instance class (pointer+ location offset)))) |
627 | (:peek |
628 | #'(lambda (location &optional (offset 0)) |
629 | (ensure-proxy-instance class (pointer+ location offset) |
630 | :reference nil :finalize nil))) |
631 | (:get |
632 | #'(lambda (location &optional (offset 0)) |
633 | (prog1 |
634 | (ensure-proxy-instance class |
635 | (copy-memory (pointer+ location offset) size) |
636 | :reference nil) |
637 | (clear-memory (pointer+ location offset) size)))))) |
638 | (call-next-method))) |
639 | |
640 | (define-type-method destroy-function ((type struct) &key temp inlined) |
641 | (if inlined |
642 | (let ((size (size-of type :inlined t))) |
643 | (if temp |
644 | #'(lambda (location &optional (offset 0)) |
645 | (clear-memory (pointer+ location offset) size)) |
646 | (let ((unref (unreference-function (type-expand type)))) |
647 | #'(lambda (location &optional (offset 0)) |
648 | (funcall unref (pointer+ location offset) t))))) |
649 | (call-next-method))) |
650 | |
651 | (define-type-method copy-function ((type struct) &key inlined) |
652 | (if inlined |
653 | (let ((ref (reference-function (type-expand type)))) |
654 | #'(lambda (from to &optional (offset 0)) |
655 | (funcall ref (pointer+ from offset) (pointer+ to offset)))) |
656 | (call-next-method))) |