b44caf77 |
1 | ;; Common Lisp bindings for GTK+ v2.0 |
2 | ;; Copyright (C) 2000 Espen S. Johnsen <esj@stud.cs.uit.no> |
3 | ;; |
4 | ;; This library is free software; you can redistribute it and/or |
5 | ;; modify it under the terms of the GNU Lesser General Public |
6 | ;; License as published by the Free Software Foundation; either |
7 | ;; version 2 of the License, or (at your option) any later version. |
8 | ;; |
9 | ;; This library is distributed in the hope that it will be useful, |
10 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | ;; Lesser General Public License for more details. |
13 | ;; |
14 | ;; You should have received a copy of the GNU Lesser General Public |
15 | ;; License along with this library; if not, write to the Free Software |
16 | ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | |
b6bf802c |
18 | ;; $Id: proxy.lisp,v 1.17 2004/12/26 11:40:14 espen Exp $ |
b44caf77 |
19 | |
20 | (in-package "GLIB") |
21 | |
b44caf77 |
22 | ;;;; Superclass for all metaclasses implementing some sort of virtual slots |
23 | |
24 | (eval-when (:compile-toplevel :load-toplevel :execute) |
6baf860c |
25 | (defclass virtual-slots-class (standard-class) |
935a783c |
26 | ()) |
b44caf77 |
27 | |
28 | (defclass direct-virtual-slot-definition (standard-direct-slot-definition) |
ba25fa44 |
29 | ((setter :reader slot-definition-setter :initarg :setter) |
935a783c |
30 | (getter :reader slot-definition-getter :initarg :getter) |
64bce834 |
31 | (unbound :reader slot-definition-unbound :initarg :unbound) |
935a783c |
32 | (boundp :reader slot-definition-boundp :initarg :boundp))) |
b44caf77 |
33 | |
935a783c |
34 | (defclass effective-virtual-slot-definition (standard-effective-slot-definition) |
35 | ((setter :reader slot-definition-setter :initarg :setter) |
36 | (getter :reader slot-definition-getter :initarg :getter) |
64bce834 |
37 | (unbound :reader slot-definition-unbound :initarg :unbound) |
935a783c |
38 | (boundp :reader slot-definition-boundp :initarg :boundp))) |
64bce834 |
39 | |
40 | (defvar *unbound-marker* (gensym "UNBOUND-MARKER-")) |
935a783c |
41 | |
64bce834 |
42 | (defun most-specific-slot-value (instances slot &optional |
43 | (default *unbound-marker*)) |
935a783c |
44 | (let ((object (find-if |
45 | #'(lambda (ob) |
46 | (and (slot-exists-p ob slot) (slot-boundp ob slot))) |
47 | instances))) |
48 | (if object |
49 | (slot-value object slot) |
6baf860c |
50 | default)))) |
935a783c |
51 | |
b44caf77 |
52 | |
53 | |
6baf860c |
54 | (defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs) |
b44caf77 |
55 | (if (eq (getf initargs :allocation) :virtual) |
56 | (find-class 'direct-virtual-slot-definition) |
57 | (call-next-method))) |
58 | |
6baf860c |
59 | (defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs) |
b44caf77 |
60 | (if (eq (getf initargs :allocation) :virtual) |
61 | (find-class 'effective-virtual-slot-definition) |
62 | (call-next-method))) |
63 | |
935a783c |
64 | |
65 | (defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition)) |
64bce834 |
66 | (if (not (slot-boundp slotd 'getter)) |
67 | (setf |
935a783c |
68 | (slot-value slotd 'reader-function) |
64bce834 |
69 | #'(lambda (object) |
70 | (declare (ignore object)) |
71 | (error "Can't read slot: ~A" (slot-definition-name slotd))) |
72 | (slot-value slotd 'boundp-function) |
73 | #'(lambda (object) (declare (ignore object)) nil)) |
74 | |
75 | (let ((getter-function |
76 | (let ((getter (slot-value slotd 'getter))) |
77 | (etypecase getter |
78 | (function getter) |
79 | (symbol |
80 | #'(lambda (object) |
81 | (funcall getter object))) |
82 | (string |
83 | (let ((reader nil)) |
84 | (setf (slot-value slotd 'reader-function) |
85 | #'(lambda (object) |
86 | (unless reader |
b6bf802c |
87 | (setq reader |
88 | (mkbinding getter |
89 | (slot-definition-type slotd) 'pointer))) |
64bce834 |
90 | (funcall reader (proxy-location object)))))))))) |
91 | |
935a783c |
92 | (setf |
64bce834 |
93 | (slot-value slotd 'boundp-function) |
94 | (cond |
64bce834 |
95 | ((slot-boundp slotd 'unbound) |
96 | (let ((unbound-value (slot-value slotd 'unbound))) |
b6bf802c |
97 | #'(lambda (object) |
98 | (not (eq (funcall getter-function object) unbound-value))))) |
99 | ((slot-boundp slotd 'boundp) |
100 | (let ((boundp (slot-value slotd 'boundp))) |
64bce834 |
101 | (etypecase boundp |
102 | (function boundp) |
103 | (symbol #'(lambda (object) |
104 | (funcall boundp object))) |
105 | (string (let ((reader ())) |
106 | #'(lambda (object) |
107 | (unless reader |
108 | (setq reader |
109 | (mkbinding boundp |
110 | (slot-definition-type slotd) 'pointer))) |
b6bf802c |
111 | (funcall reader (proxy-location object)))))))) |
112 | ((multiple-value-bind (unbound-p unbound-value) |
113 | (unbound-value (slot-definition-type slotd)) |
114 | (when unbound-p |
115 | #'(lambda (object) |
116 | (not (eq (funcall getter-function object) unbound-value)))))) |
117 | (#'(lambda (object) (declare (ignore object)) t)))) |
64bce834 |
118 | |
119 | (setf |
120 | (slot-value slotd 'reader-function) |
121 | (cond |
122 | ((slot-boundp slotd 'unbound) |
123 | (let ((unbound (slot-value slotd 'unbound)) |
124 | (slot-name (slot-definition-name slotd))) |
b6bf802c |
125 | #'(lambda (object) |
126 | (let ((value (funcall getter-function object))) |
127 | (if (eq value unbound) |
128 | (slot-unbound (class-of object) object slot-name) |
129 | value))))) |
64bce834 |
130 | ((slot-boundp slotd 'boundp) |
131 | (let ((boundp-function (slot-value slotd 'boundp-function))) |
b6bf802c |
132 | #'(lambda (object) |
133 | (and |
134 | (funcall boundp-function object) |
135 | (funcall getter-function object))))) |
136 | ((multiple-value-bind (unbound-p unbound-value) |
137 | (unbound-value (slot-definition-type slotd)) |
138 | (let ((slot-name (slot-definition-name slotd))) |
139 | (when unbound-p |
140 | #'(lambda (object) |
141 | (let ((value (funcall getter-function object))) |
142 | (if (eq value unbound-value) |
143 | (slot-unbound (class-of object) object slot-name) |
144 | value))))))) |
64bce834 |
145 | (getter-function))))) |
146 | |
147 | (setf |
148 | (slot-value slotd 'writer-function) |
149 | (if (not (slot-boundp slotd 'setter)) |
150 | #'(lambda (object) |
151 | (declare (ignore object)) |
152 | (error "Can't set slot: ~A" (slot-definition-name slotd))) |
153 | (with-slots (setter) slotd |
935a783c |
154 | (etypecase setter |
155 | (function setter) |
64bce834 |
156 | ((or symbol cons) |
157 | #'(lambda (value object) |
158 | (funcall (fdefinition setter) value object))) |
0466f75e |
159 | (string |
64bce834 |
160 | (let ((writer ())) |
161 | (setf |
162 | (slot-value slotd 'writer-function) |
163 | #'(lambda (value object) |
164 | (unless writer |
165 | (setq writer |
166 | (mkbinding setter 'nil 'pointer |
167 | (slot-definition-type slotd)))) |
168 | (funcall writer (proxy-location object) value))))))))) |
169 | |
935a783c |
170 | (initialize-internal-slot-gfs (slot-definition-name slotd))) |
171 | |
172 | |
173 | |
64bce834 |
174 | (defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf) |
935a783c |
175 | nil) |
176 | |
6baf860c |
177 | (defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds) |
64bce834 |
178 | (if (typep (first direct-slotds) 'direct-virtual-slot-definition) |
179 | (let ((initargs ())) |
180 | (let ((getter (most-specific-slot-value direct-slotds 'getter))) |
181 | (unless (eq getter *unbound-marker*) |
182 | (setf (getf initargs :getter) getter))) |
183 | (let ((setter (most-specific-slot-value direct-slotds 'setter))) |
184 | (unless (eq setter *unbound-marker*) |
185 | (setf (getf initargs :setter) setter))) |
186 | (let ((unbound (most-specific-slot-value direct-slotds 'unbound))) |
187 | (unless (eq unbound *unbound-marker*) |
188 | (setf (getf initargs :unbound) unbound))) |
189 | (let ((boundp (most-specific-slot-value direct-slotds 'boundp))) |
190 | (unless (eq boundp *unbound-marker*) |
191 | (setf (getf initargs :boundp) boundp))) |
192 | (nconc initargs (call-next-method))) |
935a783c |
193 | (call-next-method))) |
194 | |
b44caf77 |
195 | |
b44caf77 |
196 | (defmethod slot-value-using-class |
6baf860c |
197 | ((class virtual-slots-class) (object standard-object) |
b44caf77 |
198 | (slotd effective-virtual-slot-definition)) |
935a783c |
199 | (if (funcall (slot-value slotd 'boundp-function) object) |
200 | (funcall (slot-value slotd 'reader-function) object) |
201 | (slot-unbound class object (slot-definition-name slotd)))) |
b44caf77 |
202 | |
b44caf77 |
203 | (defmethod slot-boundp-using-class |
6baf860c |
204 | ((class virtual-slots-class) (object standard-object) |
b44caf77 |
205 | (slotd effective-virtual-slot-definition)) |
935a783c |
206 | (funcall (slot-value slotd 'boundp-function) object)) |
207 | |
208 | (defmethod (setf slot-value-using-class) |
6baf860c |
209 | (value (class virtual-slots-class) (object standard-object) |
b44caf77 |
210 | (slotd effective-virtual-slot-definition)) |
935a783c |
211 | (funcall (slot-value slotd 'writer-function) value object)) |
212 | |
213 | |
b44caf77 |
214 | (defmethod validate-superclass |
6baf860c |
215 | ((class virtual-slots-class) (super standard-class)) |
b44caf77 |
216 | t) |
217 | |
218 | |
219 | ;;;; Proxy cache |
220 | |
221 | (internal *instance-cache*) |
222 | (defvar *instance-cache* (make-hash-table :test #'eql)) |
223 | |
224 | (defun cache-instance (instance) |
225 | (setf |
226 | (gethash (system:sap-int (proxy-location instance)) *instance-cache*) |
227 | (ext:make-weak-pointer instance))) |
228 | |
229 | (defun find-cached-instance (location) |
230 | (let ((ref (gethash (system:sap-int location) *instance-cache*))) |
231 | (when ref |
232 | (ext:weak-pointer-value ref)))) |
233 | |
a5c3a597 |
234 | (defun instance-cached-p (location) |
235 | (gethash (system:sap-int location) *instance-cache*)) |
236 | |
b44caf77 |
237 | (defun remove-cached-instance (location) |
238 | (remhash (system:sap-int location) *instance-cache*)) |
239 | |
6baf860c |
240 | ;; For debuging |
241 | (defun cached-instances () |
242 | (let ((instances ())) |
243 | (maphash #'(lambda (location ref) |
244 | (declare (ignore location)) |
245 | (push (ext:weak-pointer-value ref) instances)) |
246 | *instance-cache*) |
247 | instances)) |
248 | |
b44caf77 |
249 | |
250 | |
251 | ;;;; Proxy for alien instances |
252 | |
6baf860c |
253 | (defclass proxy () |
254 | ((location :reader proxy-location :type system-area-pointer))) |
b44caf77 |
255 | |
6baf860c |
256 | (defgeneric initialize-proxy (object &rest initargs)) |
257 | (defgeneric instance-finalizer (object)) |
258 | (defgeneric reference-foreign (class location)) |
259 | (defgeneric unreference-foreign (class location)) |
260 | |
c55abd76 |
261 | (defmethod reference-foreign ((name symbol) location) |
262 | (reference-foreign (find-class name) location)) |
263 | |
264 | (defmethod unreference-foreign ((name symbol) location) |
265 | (unreference-foreign (find-class name) location)) |
266 | |
6baf860c |
267 | (defmethod unreference-foreign :around ((class class) location) |
268 | (unless (null-pointer-p location) |
269 | ;; (format t "Unreferencing ~A at ~A" (class-name class) location) |
270 | ;; (finish-output *standard-output*) |
271 | (call-next-method) |
272 | ;; (write-line " done") |
273 | ;; (finish-output *standard-output*) |
274 | )) |
b44caf77 |
275 | |
a5c3a597 |
276 | (defmethod print-object ((instance proxy) stream) |
277 | (print-unreadable-object (instance stream :type t :identity nil) |
64bce834 |
278 | (when (slot-boundp instance 'location) |
279 | (format stream "at 0x~X" (sap-int (proxy-location instance)))))) |
b44caf77 |
280 | |
6baf860c |
281 | (defmethod initialize-instance :around ((instance proxy) &key location) |
282 | (if location |
283 | (setf (slot-value instance 'location) location) |
284 | (call-next-method)) |
ba25fa44 |
285 | (cache-instance instance) |
6baf860c |
286 | (ext:finalize instance (instance-finalizer instance)) |
287 | instance) |
b44caf77 |
288 | |
289 | (defmethod instance-finalizer ((instance proxy)) |
6baf860c |
290 | (let ((location (proxy-location instance)) |
291 | (class (class-of instance))) |
292 | ;; (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil) |
293 | ;; (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class)) |
294 | #'(lambda () |
29933e83 |
295 | (remove-cached-instance location) |
6baf860c |
296 | (unreference-foreign class location)))) |
ba25fa44 |
297 | |
b44caf77 |
298 | |
299 | ;;;; Metaclass used for subclasses of proxy |
300 | |
301 | (eval-when (:compile-toplevel :load-toplevel :execute) |
6baf860c |
302 | (defclass proxy-class (virtual-slots-class) |
303 | ((size :reader proxy-instance-size))) |
b44caf77 |
304 | |
305 | (defclass direct-alien-slot-definition (direct-virtual-slot-definition) |
ba25fa44 |
306 | ((allocation :initform :alien) |
307 | (offset :reader slot-definition-offset :initarg :offset))) |
b44caf77 |
308 | |
309 | (defclass effective-alien-slot-definition (effective-virtual-slot-definition) |
935a783c |
310 | ((offset :reader slot-definition-offset :initarg :offset))) |
ba25fa44 |
311 | |
b44caf77 |
312 | |
313 | (defmethod most-specific-proxy-superclass ((class proxy-class)) |
314 | (find-if |
315 | #'(lambda (class) |
316 | (subtypep (class-name class) 'proxy)) |
935a783c |
317 | (cdr (compute-class-precedence-list class)))) |
318 | |
ba25fa44 |
319 | (defmethod direct-proxy-superclass ((class proxy-class)) |
320 | (find-if |
321 | #'(lambda (class) |
322 | (subtypep (class-name class) 'proxy)) |
935a783c |
323 | (class-direct-superclasses class))) |
324 | |
64bce834 |
325 | (defmethod shared-initialize ((class proxy-class) names &key size) |
b44caf77 |
326 | (call-next-method) |
ba25fa44 |
327 | (cond |
935a783c |
328 | (size (setf (slot-value class 'size) (first size))) |
6baf860c |
329 | ((slot-boundp class 'size) (slot-makunbound class 'size)))) |
935a783c |
330 | |
331 | (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs) |
b44caf77 |
332 | (case (getf initargs :allocation) |
333 | ((nil :alien) (find-class 'direct-alien-slot-definition)) |
b44caf77 |
334 | (t (call-next-method)))) |
935a783c |
335 | |
336 | (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs) |
b44caf77 |
337 | (case (getf initargs :allocation) |
338 | (:alien (find-class 'effective-alien-slot-definition)) |
b44caf77 |
339 | (t (call-next-method)))) |
340 | |
935a783c |
341 | |
342 | (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds) |
343 | (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien) |
344 | (nconc |
345 | (list :offset (most-specific-slot-value direct-slotds 'offset)) |
346 | (call-next-method)) |
347 | (call-next-method))) |
348 | |
349 | |
350 | (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition)) |
351 | (with-slots (offset) slotd |
6baf860c |
352 | (let ((type (slot-definition-type slotd))) |
64bce834 |
353 | (unless (slot-boundp slotd 'getter) |
6baf860c |
354 | (let ((reader (reader-function type))) |
355 | (setf |
64bce834 |
356 | (slot-value slotd 'getter) |
6baf860c |
357 | #'(lambda (object) |
358 | (funcall reader (proxy-location object) offset))))) |
935a783c |
359 | |
64bce834 |
360 | (unless (slot-boundp slotd 'setter) |
6baf860c |
361 | (let ((writer (writer-function type)) |
362 | (destroy (destroy-function type))) |
363 | (setf |
64bce834 |
364 | (slot-value slotd 'setter) |
6baf860c |
365 | #'(lambda (value object) |
366 | (let ((location (proxy-location object))) |
367 | (funcall destroy location offset) ; destroy old value |
64bce834 |
368 | (funcall writer value location offset)))))))) |
369 | |
935a783c |
370 | (call-next-method)) |
371 | |
372 | |
935a783c |
373 | ;; TODO: call some C code to detect this a compile time |
374 | (defconstant +struct-alignmen+ 4) |
b44caf77 |
375 | |
376 | (defmethod compute-slots ((class proxy-class)) |
935a783c |
377 | (loop |
c55abd76 |
378 | with offset = (let ((size-of-super-classes |
379 | (proxy-instance-size |
380 | (most-specific-proxy-superclass class)))) |
381 | (+ size-of-super-classes |
382 | (mod size-of-super-classes +struct-alignmen+))) |
935a783c |
383 | with size = offset |
384 | for slotd in (class-direct-slots class) |
385 | when (eq (slot-definition-allocation slotd) :alien) |
386 | do (if (not (slot-boundp slotd 'offset)) |
387 | (setf (slot-value slotd 'offset) offset) |
388 | (setq offset (slot-value slotd 'offset))) |
389 | |
390 | (incf offset (size-of (slot-definition-type slotd))) |
391 | (incf offset (mod offset +struct-alignmen+)) |
392 | (setq size (max size offset)) |
393 | |
394 | finally (unless (slot-boundp class 'size) |
395 | (setf (slot-value class 'size) size))) |
b44caf77 |
396 | (call-next-method)) |
8ae7ddc2 |
397 | |
935a783c |
398 | |
399 | (defmethod validate-superclass ((class proxy-class) (super standard-class)) |
400 | (subtypep (class-name super) 'proxy)) |
401 | |
6baf860c |
402 | (defmethod proxy-instance-size (class) |
ba25fa44 |
403 | (declare (ignore class)) |
404 | 0) |
935a783c |
405 | ) |
406 | |
6baf860c |
407 | (defmethod alien-type ((class proxy-class) &rest args) |
408 | (declare (ignore class args)) |
409 | (alien-type 'pointer)) |
410 | |
411 | (defmethod size-of ((class proxy-class) &rest args) |
412 | (declare (ignore class args)) |
413 | (size-of 'pointer)) |
414 | |
415 | (defmethod from-alien-form (location (class proxy-class) &rest args) |
416 | (declare (ignore args)) |
417 | `(ensure-proxy-instance ',(class-name class) ,location)) |
418 | |
419 | (defmethod from-alien-function ((class proxy-class) &rest args) |
420 | (declare (ignore args)) |
421 | #'(lambda (location) |
422 | (ensure-proxy-instance class location))) |
b44caf77 |
423 | |
6baf860c |
424 | (defmethod to-alien-form (instance (class proxy-class) &rest args) |
425 | (declare (ignore class args)) |
426 | `(proxy-location ,instance)) |
b44caf77 |
427 | |
6baf860c |
428 | (defmethod to-alien-function ((class proxy-class) &rest args) |
429 | (declare (ignore class args)) |
430 | #'proxy-location) |
431 | |
508d13a7 |
432 | (defmethod copy-from-alien-form (location (class proxy-class) &rest args) |
433 | (declare (ignore args)) |
434 | (let ((class-name (class-name class))) |
435 | `(ensure-proxy-instance ',class-name |
436 | (reference-foreign ',class-name ,location)))) |
437 | |
438 | (defmethod copy-from-alien-function ((class proxy-class) &rest args) |
439 | (declare (ignore args)) |
440 | #'(lambda (location) |
441 | (ensure-proxy-instance class (reference-foreign class location)))) |
442 | |
443 | (defmethod copy-to-alien-form (instance (class proxy-class) &rest args) |
444 | (declare (ignore args)) |
445 | `(reference-foreign ',(class-name class) (proxy-location ,instance))) |
446 | |
447 | (defmethod copy-to-alien-function ((class proxy-class) &rest args) |
448 | (declare (ignore class args)) |
449 | #'(lambda (instance) |
450 | (reference-foreign class (proxy-location instance)))) |
451 | |
6baf860c |
452 | (defmethod writer-function ((class proxy-class) &rest args) |
453 | (declare (ignore args)) |
454 | #'(lambda (instance location &optional (offset 0)) |
455 | (assert (null-pointer-p (sap-ref-sap location offset))) |
456 | (setf |
457 | (sap-ref-sap location offset) |
458 | (reference-foreign class (proxy-location instance))))) |
b44caf77 |
459 | |
6baf860c |
460 | (defmethod reader-function ((class proxy-class) &rest args) |
461 | (declare (ignore args)) |
462 | #'(lambda (location &optional (offset 0)) |
508d13a7 |
463 | (let ((instance (sap-ref-sap location offset))) |
464 | (unless (null-pointer-p instance) |
465 | (ensure-proxy-instance class (reference-foreign class instance)))))) |
b44caf77 |
466 | |
6baf860c |
467 | (defmethod destroy-function ((class proxy-class) &rest args) |
468 | (declare (ignore args)) |
469 | #'(lambda (location &optional (offset 0)) |
470 | (unreference-foreign class (sap-ref-sap location offset)))) |
471 | |
b6bf802c |
472 | (defmethod unbound-value ((class proxy-class) &rest args) |
473 | (declare (ignore type args)) |
474 | (values t nil)) |
6baf860c |
475 | |
476 | (defgeneric ensure-proxy-instance (class location) |
477 | (:documentation "Returns a proxy object representing the foreign object at the give location.")) |
478 | |
479 | (defmethod ensure-proxy-instance :around (class location) |
480 | (unless (null-pointer-p location) |
481 | (or |
482 | (find-cached-instance location) |
483 | (call-next-method)))) |
484 | |
485 | (defmethod ensure-proxy-instance ((class symbol) location) |
486 | (ensure-proxy-instance (find-class class) location)) |
487 | |
488 | (defmethod ensure-proxy-instance ((class proxy-class) location) |
489 | (make-instance class :location location)) |
b44caf77 |
490 | |
ba25fa44 |
491 | |
492 | ;;;; Superclasses for wrapping of C structures |
b44caf77 |
493 | |
6baf860c |
494 | (defclass struct (proxy) |
495 | () |
496 | (:metaclass proxy-class)) |
b44caf77 |
497 | |
6baf860c |
498 | (defmethod initialize-instance ((struct struct) &rest initargs) |
b44caf77 |
499 | (declare (ignore initargs)) |
9da5a342 |
500 | (unless (slot-boundp struct 'location) |
501 | (let ((size (proxy-instance-size (class-of struct)))) |
502 | (if (zerop size) |
503 | (error "~A has zero size" (class-of struct)) |
504 | (setf (slot-value struct 'location) (allocate-memory size))))) |
b44caf77 |
505 | (call-next-method)) |
506 | |
507 | |
6baf860c |
508 | ;;;; Metaclasses used for subclasses of struct |
509 | |
510 | (defclass struct-class (proxy-class) |
511 | ()) |
b44caf77 |
512 | |
6baf860c |
513 | (defmethod reference-foreign ((class struct-class) location) |
514 | (copy-memory location (proxy-instance-size class))) |
515 | |
516 | (defmethod unreference-foreign ((class struct-class) location) |
ba25fa44 |
517 | (deallocate-memory location)) |
b44caf77 |
518 | |
519 | |
6baf860c |
520 | (defclass static-struct-class (struct-class) |
521 | ()) |
b44caf77 |
522 | |
6baf860c |
523 | (defmethod reference-foreign ((class static-struct-class) location) |
524 | (declare (ignore class)) |
ba25fa44 |
525 | location) |
b44caf77 |
526 | |
6baf860c |
527 | (defmethod unreference-foreign ((class static-struct-class) location) |
528 | (declare (ignore class location)) |
ba25fa44 |
529 | nil) |