chiark / gitweb /
Initial checkin
authorespen <espen>
Sun, 29 Apr 2001 20:19:25 +0000 (20:19 +0000)
committerespen <espen>
Sun, 29 Apr 2001 20:19:25 +0000 (20:19 +0000)
glib/gboxed.lisp [new file with mode: 0644]
glib/genums.lisp [new file with mode: 0644]
glib/proxy.lisp [new file with mode: 0644]

diff --git a/glib/gboxed.lisp b/glib/gboxed.lisp
new file mode 100644 (file)
index 0000000..1bf2916
--- /dev/null
@@ -0,0 +1,108 @@
+;; Common Lisp bindings for GTK+ v2.0
+;; Copyright (C) 2001 Espen S. Johnsen <esj@stud.cs.uit.no>
+;;
+;; This library is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU Lesser General Public
+;; License as published by the Free Software Foundation; either
+;; version 2 of the License, or (at your option) any later version.
+;;
+;; This library is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; Lesser General Public License for more details.
+;;
+;; You should have received a copy of the GNU Lesser General Public
+;; License along with this library; if not, write to the Free Software
+;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+;; $Id: gboxed.lisp,v 1.1 2001-04-29 20:19:25 espen Exp $
+
+(in-package "GLIB")
+
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defclass boxed (alien-structure)
+    ()
+    (:metaclass proxy-class)))
+
+(defmethod initialize-proxy ((boxed boxed) &rest initargs
+                            &key location weak-ref)
+  (declare (ignore initargs))
+  (setf
+   (slot-value boxed 'location)
+   (if weak-ref
+       (%boxed-copy (find-type-number (class-of boxed)) location)
+     location))
+  (call-next-method))
+
+(defmethod instance-finalizer ((boxed boxed))
+  (let ((location (proxy-location boxed))
+       (type-number (find-type-number (class-of boxed))))
+    (declare (type system-area-pointer location))
+    #'(lambda ()
+       (%boxed-free type-number location)
+       (remove-cached-instance location))))
+
+
+(deftype-method translate-to-alien boxed (type-spec boxed &optional weak-ref)
+  (if weak-ref
+      `(proxy-location ,boxed)
+    `(let ((boxed ,boxed))
+       (%boxed-copy
+       (find-type-number type-spec)
+       (proxy-location boxed)))))
+
+(deftype-method unreference-alien boxed (type-spec c-struct)
+  `(%boxed-free ,(find-type-number type-spec) ,c-struct))
+
+
+(defbinding %boxed-copy () pointer
+  (type type-number)
+  (location pointer))
+
+(defbinding %boxed-free () nil
+  (type type-number)
+  (location pointer))
+
+
+;;;; Metaclass for boxed classes
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defclass boxed-class (proxy-class)))
+
+
+(defmethod shared-initialize ((class boxed-class) names
+                             &rest initargs
+                             &key name alien-name type-init)
+  (declare (ignore initargs names))
+  (call-next-method)
+
+  (let* ((class-name (or name (class-name class)))
+        (type-number
+         (cond
+          ((and alien-name type-init)
+           (error
+            "Specify either :type-init or :alien-name for class ~A"
+            class-name))
+          (alien-name (type-number-from-alien-name (first alien-name)))
+          (type-init
+            (funcall (alien-function (first type-init) '(unsigned 32))))
+          (t
+           (or
+            (type-number-from-alien-name
+             (default-alien-type-name class-name) nil)
+            (funcall
+             (alien-function 
+              (default-alien-fname (format nil "~A_get_type" class-name))
+              '(unsigned 32))))))))
+    (setf (find-type-number class) type-number)))
+
+
+(defmethod validate-superclass
+    ((class boxed-class) (super pcl::standard-class))
+  (subtypep (class-name super) 'boxed))
+
+
+;;;; Initializing type numbers
+
+(setf (alien-type-name 'boxed) "GBoxed")
diff --git a/glib/genums.lisp b/glib/genums.lisp
new file mode 100644 (file)
index 0000000..d2cbe4a
--- /dev/null
@@ -0,0 +1,190 @@
+;; Common Lisp bindings for GTK+ v2.0
+;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
+;;
+;; This library is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU Lesser General Public
+;; License as published by the Free Software Foundation; either
+;; version 2 of the License, or (at your option) any later version.
+;;
+;; This library is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; Lesser General Public License for more details.
+;;
+;; You should have received a copy of the GNU Lesser General Public
+;; License along with this library; if not, write to the Free Software
+;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+;; $Id: genums.lisp,v 1.1 2001-04-29 20:19:25 espen Exp $
+
+(in-package "GLIB")
+
+
+(defun %map-mappings (args op)
+  (let ((current-value 0))
+    (map
+     'list 
+     #'(lambda (mapping)
+        (destructuring-bind (symbol &optional (value current-value))
+            (mklist mapping)
+          (setf current-value (1+ value))
+          (case op
+            (:enum-int (list symbol value))
+            (:flags-int (list symbol value #|(ash 1 value)|#))
+            (:int-enum (list value symbol))
+            (:int-flags (list value #|(ash 1 value)|# symbol))
+            (:symbols symbol))))
+     (if (integerp (first args))
+        (rest args)
+       args))))
+
+
+;;;; Enum type
+
+(deftype enum (&rest args)
+  `(member ,@(%map-mappings args :symbols)))
+
+(deftype-method translate-type-spec enum (type-spec)
+  (let ((args (cdr (type-expand-to 'enum type-spec))))
+    (if (integerp (first args))
+       (translate-type-spec `(signed ,(first args)))
+      (translate-type-spec 'signed))))
+
+(deftype-method size-of enum (type-spec)
+  (let ((args (cdr (type-expand-to 'enum type-spec))))
+    (if (integerp (first args))
+       (size-of `(signed ,(first args)))
+      (size-of 'signed))))
+
+(deftype-method translate-to-alien enum (type-spec expr &optional weak-ref)
+  (declare (ignore weak-ref))
+  (let ((args (cdr (type-expand-to 'enum type-spec))))
+    `(ecase ,expr
+       ,@(%map-mappings args :enum-int))))
+
+(deftype-method translate-from-alien enum (type-spec expr &optional weak-ref)
+  (declare (ignore weak-ref))
+  (destructuring-bind (name &rest args) (type-expand-to 'enum type-spec)
+    (declare (ignore name))
+    `(ecase ,expr
+       ,@(%map-mappings args :int-enum))))
+
+(setf (alien-type-name 'enum) "GEnum")
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defclass %enum-value (alien-structure)
+    ((value :allocation :alien :type int)
+     (name :allocation :alien :type string)
+     (nickname :allocation :alien :type string))
+    (:metaclass proxy-class)))
+
+(defbinding %enum-class-values () (glist %enum-value)
+  (class pointer))
+
+(defun %query-enum-values (type-number)
+  (mapcar
+   #'(lambda (enum-value)
+       (list
+       (intern
+        (substitute
+         #\- #\_ (string-upcase (slot-value enum-value 'nickname))) "KEYWORD")
+       (slot-value enum-value 'value)))
+   (%enum-class-values (type-class-peek type-number))))
+
+(defun define-enum-by-query (init-fname &optional name)
+  (let ((type-number (type-init name init-fname)))
+    (unless (= (type-parent type-number) (find-type-number 'enum))
+      (error "~A is not an enum type" (alien-type-name type-number)))
+    
+    (type-class-ref type-number)
+    (setf (find-type-number name) type-number)
+    (let ((expanded (cons 'enum (%query-enum-values type-number)))
+         (name (or name (default-type-name (alien-type-name type-number)))))
+      (lisp::%deftype
+       name
+       #'(lambda (whole)
+          (unless (zerop (length (cdr whole)))
+            (lisp::do-arg-count-error 'deftype name (cdr whole) nil 0 0))
+          expanded)))))
+
+
+;;;;  Flags type
+
+(deftype flags (&rest args)
+  `(or
+    null
+    (cons
+     (member ,@(%map-mappings args :symbols))
+     list)))
+
+(deftype-method translate-type-spec flags (type-spec)
+  (let ((args (cdr (type-expand-to 'flags type-spec))))
+    (if (integerp (first args))
+       (translate-type-spec `(unsigned ,(first args)))
+      (translate-type-spec 'unsigned))))
+
+(deftype-method size-of flags (type-spec)
+  (let ((args (cdr (type-expand-to 'flags type-spec))))
+    (if (integerp (first args))
+       (size-of `(unsigned ,(first args)))
+      (size-of 'unsigned))))
+
+(deftype-method translate-to-alien flags (type-spec expr &optional weak-ref)
+  (declare (ignore weak-ref))
+  (destructuring-bind (name &rest args) (type-expand-to 'flags type-spec)
+    (declare (ignore name))
+    (let ((mappings (%map-mappings args :flags-int))
+         (value (make-symbol "VALUE")))
+      `(let ((,value 0))
+        (dolist (flag ,expr ,value)
+          (setq ,value (logior ,value (second (assoc flag ',mappings)))))))))
+
+(deftype-method translate-from-alien flags (type-spec expr &optional weak-ref)
+  (declare (ignore weak-ref))
+  (destructuring-bind (name &rest args) (type-expand-to 'flags type-spec)
+    (declare (ignore name))
+    (let ((mappings (%map-mappings args :int-flags))
+         (result (make-symbol "RESULT")))
+      `(let ((,result nil))
+        (dolist (mapping ',mappings ,result)
+          (unless (zerop (logand ,expr (first mapping)))
+            (push (second mapping) ,result)))))))
+
+(setf (alien-type-name 'flags) "GFlags")
+
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defclass %flags-value (alien-structure)
+    ((value :allocation :alien :type unsigned-int)
+     (name :allocation :alien :type string)
+     (nickname :allocation :alien :type string))
+    (:metaclass proxy-class)))
+
+(defbinding %flags-class-values () (glist %flags-value)
+  (class pointer))
+
+(defun %query-flags-values (type-number)
+  (mapcar
+   #'(lambda (flags-value)
+       (list
+       (intern
+        (substitute
+         #\- #\_ (string-upcase (slot-value flags-value 'nickname))) "KEYWORD")
+       (slot-value flags-value 'value)))
+   (%flags-class-values (type-class-peek type-number))))
+
+(defun define-flags-by-query (init-fname &optional name)
+  (let ((type-number (type-init nil init-fname)))
+    (unless (= (type-parent type-number) (find-type-number 'flags))
+      (error "~A is not a flags type" (alien-type-name type-number)))
+    
+    (type-class-ref type-number)
+    (setf (find-type-number name) type-number)
+    (let ((expanded (cons 'flags (%query-flags-values type-number)))
+         (name (or name (default-type-name (alien-type-name type-number)))))
+      (lisp::%deftype
+       name
+       #'(lambda (whole)
+          (unless (zerop (length (cdr whole)))
+            (lisp::do-arg-count-error 'deftype name (cdr whole) nil 0 0))
+          expanded)))))
diff --git a/glib/proxy.lisp b/glib/proxy.lisp
new file mode 100644 (file)
index 0000000..f4c5a59
--- /dev/null
@@ -0,0 +1,422 @@
+;; Common Lisp bindings for GTK+ v2.0
+;; Copyright (C) 2000 Espen S. Johnsen <esj@stud.cs.uit.no>
+;;
+;; This library is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU Lesser General Public
+;; License as published by the Free Software Foundation; either
+;; version 2 of the License, or (at your option) any later version.
+;;
+;; This library is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; Lesser General Public License for more details.
+;;
+;; You should have received a copy of the GNU Lesser General Public
+;; License along with this library; if not, write to the Free Software
+;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+;; $Id: proxy.lisp,v 1.1 2001-04-29 20:19:25 espen Exp $
+
+(in-package "GLIB")
+
+
+;;;; Superclass for all metaclasses implementing some sort of virtual slots
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defclass virtual-class (pcl::standard-class))
+
+  (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
+    ((location
+      :reader slot-definition-location
+      :initarg :location)))
+  
+  (defclass effective-virtual-slot-definition
+    (standard-effective-slot-definition)))
+  
+
+(defmethod direct-slot-definition-class ((class virtual-class) initargs)
+  (if (eq (getf initargs :allocation) :virtual)
+      (find-class 'direct-virtual-slot-definition)
+    (call-next-method)))
+
+
+(defmethod effective-slot-definition-class ((class virtual-class) initargs)
+  (if (eq (getf initargs :allocation) :virtual)
+      (find-class 'effective-virtual-slot-definition)
+    (call-next-method)))
+
+
+(defun %direct-slot-definitions-slot-value (slotds slot &optional default)
+  (let ((slotd
+        (find-if
+         #'(lambda (slotd)
+             (and
+              (slot-exists-p slotd slot)
+              (slot-boundp slotd slot)))
+         slotds)))
+    (if slotd
+       (slot-value slotd slot)
+      default)))
+  
+
+(defgeneric compute-virtual-slot-location (class slotd direct-slotds))
+
+(defmethod compute-virtual-slot-location
+    ((class virtual-class)
+     (slotd effective-virtual-slot-definition)
+     direct-slotds)
+    (let ((location
+          (%direct-slot-definitions-slot-value direct-slotds 'location)))
+      (if (and location (symbolp location))
+         (list location `(setf ,location))
+       location)))
+
+
+(defmethod compute-effective-slot-definition
+    ((class virtual-class) direct-slotds)
+  (let ((slotd (call-next-method)))
+    (when (typep slotd 'effective-virtual-slot-definition)
+      (setf
+       (slot-value slotd 'pcl::location)
+       (compute-virtual-slot-location class slotd direct-slotds)))
+    slotd))
+
+
+(defmethod slot-value-using-class
+    ((class virtual-class) (object standard-object)
+     (slotd effective-virtual-slot-definition))
+  (let ((reader (first (slot-definition-location slotd))))
+    (if reader
+       (funcall reader object)
+      (slot-unbound class object (slot-definition-name slotd)))))
+
+
+(defmethod slot-boundp-using-class
+    ((class virtual-class) (object standard-object)
+     (slotd effective-virtual-slot-definition))
+   (and (first (slot-definition-location slotd)) t))
+    
+
+
+(defmethod (setf slot-value-using-class)
+    (value (class virtual-class) (object standard-object)
+     (slotd effective-virtual-slot-definition))
+  (let ((writer (second (slot-definition-location slotd))))
+    (cond
+     ((null writer)
+      (error
+       "Can't set read-only slot ~A in ~A"
+       (slot-definition-name slotd)
+       object))
+     ((or (functionp writer) (symbolp writer))
+      (funcall writer value object)
+      value)
+     (t
+      (funcall (fdefinition writer) value object)
+      value))))
+       
+
+(defmethod validate-superclass
+    ((class virtual-class) (super pcl::standard-class))
+  t)
+
+
+;;;; Proxy cache
+
+(internal *instance-cache*)
+(defvar *instance-cache* (make-hash-table :test #'eql))
+
+(defun cache-instance (instance)
+  (setf
+   (gethash (system:sap-int (proxy-location instance)) *instance-cache*)
+   (ext:make-weak-pointer instance)))
+
+(defun find-cached-instance (location)
+  (let ((ref (gethash (system:sap-int location) *instance-cache*)))
+    (when ref
+      (ext:weak-pointer-value ref))))
+
+(defun remove-cached-instance (location)
+  (remhash (system:sap-int location) *instance-cache*))
+
+
+
+;;;; Proxy for alien instances
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defclass proxy ()
+    ((location
+      :reader proxy-location
+      :type system-area-pointer)))
+
+  (defgeneric initialize-proxy (object &rest initargs))
+  (defgeneric instance-finalizer (object)))
+
+
+(defmethod initialize-instance :after ((instance proxy)
+                                      &rest initargs &key)
+  (declare (ignore initargs))
+  (cache-instance instance)
+  (ext:finalize instance (instance-finalizer instance)))
+
+
+(defmethod initialize-proxy ((instance proxy)
+                            &rest initargs)
+  (declare (ignore initargs))
+  (cache-instance instance))
+
+
+(defmethod instance-finalizer ((instance proxy))
+  (let ((location (proxy-location instance)))
+    #'(lambda ()
+       (remove-cached-instance location))))
+
+
+(deftype-method translate-type-spec proxy (type-spec)
+  (declare (ignore type-spec))
+  (translate-type-spec 'pointer))
+
+(deftype-method size-of proxy (type-spec)
+  (declare (ignore type-spec))
+  (size-of 'pointer))
+
+(deftype-method translate-from-alien
+    proxy (type-spec location &optional weak-ref)
+  `(let ((location ,location))
+     (unless (null-pointer-p location)
+       (ensure-proxy-instance ',type-spec location ,weak-ref))))
+
+
+
+;;;; Metaclass used for subclasses of proxy
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defclass proxy-class (virtual-class)
+    ((size :reader proxy-class-instance-size)))
+
+  (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
+    ((allocation
+      :initform :alien)
+     (offset
+      :reader slot-definition-offset
+      :initarg :offset
+      :initform 0)))
+  
+  (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
+    ((offset :reader slot-definition-offset)))
+  
+  (defclass effective-virtual-alien-slot-definition
+    (effective-virtual-slot-definition))
+
+  (defmethod most-specific-proxy-superclass ((class proxy-class))
+    (find-if
+     #'(lambda (class)
+        (subtypep (class-name class) 'proxy))
+     (cdr (pcl::compute-class-precedence-list class))))
+
+
+  (defmethod shared-initialize ((class proxy-class) names
+                               &rest initargs &key size name)
+    (declare (ignore initargs))
+    (call-next-method)
+    (when size
+      (setf (slot-value class 'size) (first size))))
+    
+
+  (defmethod shared-initialize :after ((class proxy-class) names
+                                      &rest initargs &key)
+    (declare (ignore initargs names))
+    (let* ((super (most-specific-proxy-superclass class))
+          (actual-size
+           (if (eq (class-name super) 'proxy)
+               0
+             (proxy-class-instance-size super))))
+      (dolist (slotd (class-slots class))
+       (when (eq (slot-definition-allocation slotd) :alien)
+         (with-slots (offset type) slotd
+           (setq actual-size (max actual-size (+ offset (size-of type)))))))
+      (cond
+       ((not (slot-boundp class 'size))
+       (setf (slot-value class 'size) actual-size))
+       ((> actual-size (slot-value class 'size))
+       (warn "The actual size of class ~A is lager than specified" class)))))
+
+
+  (defmethod direct-slot-definition-class ((class proxy-class) initargs)
+    (case (getf initargs :allocation)
+      ((nil :alien) (find-class 'direct-alien-slot-definition))
+;      (:instance (error "Allocation :instance not allowed in class ~A" class))
+      (t (call-next-method))))
+
+
+  (defmethod effective-slot-definition-class ((class proxy-class) initargs)
+    (case (getf initargs :allocation)
+      (:alien (find-class 'effective-alien-slot-definition))
+      (:virtual (find-class 'effective-virtual-alien-slot-definition))
+      (t (call-next-method))))
+  
+  
+  (defmethod compute-virtual-slot-location
+      ((class proxy-class) (slotd effective-alien-slot-definition)
+       direct-slotds)
+    (with-slots (offset type) slotd
+      (setf offset (%direct-slot-definitions-slot-value direct-slotds 'offset))
+      (let ((reader (intern-reader-function type))
+           (writer (intern-writer-function type))
+           (destroy (intern-destroy-function type)))
+       (list
+        #'(lambda (object)
+            (funcall reader (proxy-location object) offset))
+        #'(lambda (value object)
+            (let ((location (proxy-location object)))
+              (funcall destroy location offset)
+              (funcall writer value location offset)))))))
+            
+  
+  (defmethod compute-virtual-slot-location
+      ((class proxy-class)
+       (slotd effective-virtual-alien-slot-definition)
+       direct-slotds)
+    (let ((location (call-next-method)))
+      (if (or (stringp location) (consp location))
+         (destructuring-bind (reader &optional writer) (mklist location)
+           (with-slots (type) slotd
+              (list
+              (if (stringp reader)
+                  (let* ((alien-type (translate-type-spec type))
+                         (alien
+                          (alien::%heap-alien
+                           (alien::make-heap-alien-info
+                            :type (alien::parse-alien-type
+                                   `(function ,alien-type system-area-pointer))
+                            :sap-form (system:foreign-symbol-address reader))))
+                         (translate-return-value
+                          (intern-return-value-translator type)))
+                    #'(lambda (object)
+                        (funcall
+                         translate-return-value
+                         (alien-funcall
+                          alien (proxy-location object)))))
+                reader)
+              (if (stringp writer)
+                  (let* ((alien-type (translate-type-spec type))
+                         (alien
+                          (alien::%heap-alien
+                           (alien::make-heap-alien-info
+                            :type (alien::parse-alien-type
+                                   `(function
+                                     void system-area-pointer ,alien-type))
+                            :sap-form (system:foreign-symbol-address writer))))
+                         (translate-argument (intern-argument-translator type))
+                         (cleanup (intern-cleanup-function type)))
+                    #'(lambda (value object)
+                        (let ((tmp (funcall translate-argument value))
+                              (location (proxy-location object)))
+                          (alien-funcall alien location tmp)
+                          (funcall cleanup tmp))))
+                writer))))
+       location)))
+
+
+  (defmethod compute-slots ((class proxy-class))
+    ;; Translating the user supplied relative (to previous slot) offsets
+    ;; to absolute offsets.
+    ;; This code is broken and have to be fixed.
+    (with-slots (direct-slots) class
+      (let* ((super (most-specific-proxy-superclass class))
+            (slot-offset
+             (if (eq (class-name super) 'proxy)
+                 0
+               (proxy-class-instance-size super))))
+       (dolist (slotd direct-slots)
+         (when (eq (slot-definition-allocation slotd) :alien)
+           (with-slots (offset type) slotd
+             (setf
+              offset (+ slot-offset offset)
+              slot-offset (+ offset (size-of type)))))))
+    
+      ;; Reverse the direct slot definitions so the effective slots
+      ;; will be in correct order.
+      (setf direct-slots (reverse direct-slots))
+      ;; This nreverse caused me so much frustration that I leave it
+      ;; here just as a reminder of what not to do.
+;      (setf direct-slots (nreverse direct-slots))
+      )
+    (call-next-method))
+
+
+  (defmethod validate-superclass ((class proxy-class)
+                                 (super pcl::standard-class))
+     (subtypep (class-name super) 'proxy))
+
+  (defgeneric make-proxy-instance (class location weak-ref &rest initargs &key)))
+
+
+(defmethod make-proxy-instance ((class symbol) location weak-ref
+                               &rest initargs &key)
+  (apply #'make-proxy-instance (find-class class) location weak-ref initargs))
+
+(defmethod make-proxy-instance ((class proxy-class) location weak-ref
+                               &rest initargs &key)
+  (let ((instance (allocate-instance class)))
+    (apply
+     #'initialize-proxy
+     instance :location location :weak-ref weak-ref initargs)
+    instance))
+
+(defun ensure-proxy-instance (class location weak-ref &rest initargs)
+  (or
+   (find-cached-instance location)
+   (apply #'make-proxy-instance class location weak-ref initargs)))
+
+
+;;;; Superclass for wrapping of C structures
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defclass alien-structure (proxy)
+    ()
+    (:metaclass proxy-class)
+    (:size 0)))
+
+
+(defmethod initialize-instance ((structure alien-structure)
+                               &rest initargs)
+  (declare (ignore initargs))
+  (setf 
+   (slot-value structure 'location)
+   (allocate-memory (proxy-class-instance-size (class-of structure))))
+  (call-next-method))
+
+
+(defmethod initialize-proxy ((structure alien-structure)
+                            &rest initargs &key location weak-ref)
+  (declare (ignore initargs))
+  (setf
+   (slot-value structure 'location)
+   (if weak-ref
+       (copy-memory location (proxy-class-instance-size (class-of structure)))
+     location))
+  (call-next-method))
+
+
+(defmethod instance-finalizer ((structure alien-structure))
+  (let ((location (proxy-location structure)))
+    (declare (type system-area-pointer location))
+    #'(lambda ()
+       (deallocate-memory location)
+       (remove-cached-instance location))))
+
+
+(deftype-method translate-to-alien
+     alien-structure (type-spec object &optional weak-ref)
+  (if weak-ref
+      `(proxy-location ,object)
+    `(copy-memory
+      (proxy-location ,object)
+      ,(proxy-class-instance-size (find-class type-spec)))))
+
+
+(deftype-method unreference-alien alien-structure (type-spec c-struct)
+  (declare (ignore type-spec))
+  `(deallocate-memory ,c-struct))