+;;;; User data mechanism
+
+(declaim (fixnum *user-data-count*))
+
+(defvar *user-data* (make-hash-table))
+(defvar *user-data-count* 0)
+
+;; Until the callback mechanism is moved to glib, the value of
+;; *destroy-marshal* is set in gtkobject.lisp
+(defvar *destroy-marshal* nil)
+
+(defun register-user-data (object &optional destroy-function)
+ (check-type destroy-function (or null symbol function))
+; (incf *user-data-count*)
+ (setq *user-data-count* (the fixnum (1+ *user-data-count*)))
+ (setf
+ (gethash *user-data-count* *user-data*)
+ (cons object destroy-function))
+ *user-data-count*)
+
+(defun find-user-data (id)
+ (check-type id fixnum)
+ (multiple-value-bind (user-data p) (gethash id *user-data*)
+ (values (car user-data) p)))
+
+(defun destroy-user-data (id)
+ (check-type id fixnum)
+ (let ((user-data (gethash id *user-data*)))
+ (when (cdr user-data)
+ (funcall (cdr user-data) (car user-data))))
+ (remhash id *user-data*))
+
+(define-foreign %object-set-qdata-full () nil
+ (object gobject)
+ (id quark)
+ (data unsigned-long)
+ (destroy-marshal pointer))
+
+(defun (setf object-data) (data object key &key (test #'eq))
+ (%object-set-qdata-full
+ object (quark-from-object key :test test)
+ (register-user-data data) *destroy-marshal*)
+ data)
+
+(define-foreign %object-get-qdata () unsigned-long
+ (object gobject)
+ (id quark))
+
+(defun object-data (object key &key (test #'eq))
+ (find-user-data
+ (%object-get-qdata object (quark-from-object key :test test))))
+
+