chiark / gitweb /
Removed comment about setting up logical pathname translation
[clg] / glib / glib.lisp
index 2b3e256fc83154932ebf961ea3da60a05a6120a2..e8f6f47c5f0579120f53ee351d2240b2aa9a9c54 100644 (file)
@@ -20,7 +20,7 @@
 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-;; $Id: glib.lisp,v 1.40 2007-06-15 12:25:16 espen Exp $
+;; $Id: glib.lisp,v 1.42 2007-10-17 14:30:41 espen Exp $
 
 
 (in-package "GLIB")
@@ -52,20 +52,29 @@ (defbinding (%deallocate-memory "g_free") () nil
 (setf *memory-allocator* #'%allocate-memory)
 (setf *memory-deallocator* #'%deallocate-memory)
 
-;;;; User data mechanism
+(defbinding (reallocate-memory "g_realloc") () pointer
+  (address pointer)
+  (size unsigned-long))
+
+(deftype gsize () 'unsigned-int)
+
+(defbinding (slice-alloc "g_slice_alloc0") () pointer
+  (block-size gsize))
+
+;;;; User data is a mechanism to store references to lisp objects in
+;;;; foreign code
 
 (defvar *user-data-lock* (make-mutex))
 (defvar *user-data* (make-hash-table))
-(defvar *user-data-count* 0)
+(defvar *user-data-next-id* 1)
 
 (defun register-user-data (object &optional destroy-function)
   (check-type destroy-function (or null symbol function))
   (with-mutex (*user-data-lock*)
-    (incf *user-data-count*)
     (setf
-     (gethash *user-data-count* *user-data*)
+     (gethash *user-data-next-id* *user-data*)
      (cons object destroy-function))
-    *user-data-count*))
+    (1- (incf *user-data-next-id*))))
 
 (defun find-user-data (id)
   (check-type id fixnum)
@@ -90,10 +99,33 @@ (defun update-user-data (id object)
 (defun destroy-user-data (id)
   (check-type id fixnum)
   (with-mutex (*user-data-lock*)
-    (let ((user-data (gethash id *user-data*)))
-      (when (cdr user-data)
-       (funcall (cdr user-data) (car user-data))))
-    (remhash id *user-data*)))
+    (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
+      (cond
+;       ((not exists-p) (error "User data id ~A does not exist" id))
+       (t
+       (when (cdr user-data)
+         (funcall (cdr user-data) (car user-data)))
+       (remhash id *user-data*))))))
+
+(defun take-user-data (id)
+  (check-type id fixnum)
+  (multiple-value-bind (user-data exists-p) (gethash id *user-data*)
+    (cond
+      ((not exists-p) (error "User data id ~A does not exist" id))
+      (t 
+       (when (cdr user-data)
+         (funcall (cdr user-data) (car user-data)))
+       (remhash id *user-data*)
+       (car user-data)))))
+
+(defmacro with-user-data ((var object) &body body)
+  `(let ((,var (register-user-data ,object)))
+     (unwind-protect
+         ,@body
+       (destroy-user-data ,var))))
+
+
+(deftype user-data-id () 'pointer-data)
 
 
 ;;;; Quarks
@@ -179,6 +211,12 @@ (define-type-method alien-type ((type glist))
   (declare (ignore type))
   (alien-type 'pointer))
 
+(define-type-method argument-type ((type glist))
+  'list)
+
+(define-type-method return-type ((type glist))
+  'list)
+
 (define-type-method size-of ((type glist) &key inlined)
   (assert-not-inlined type inlined)
   (size-of 'pointer))
@@ -332,6 +370,12 @@ (define-type-method alien-type ((type gslist))
   (declare (ignore type))
   (alien-type 'pointer))
 
+(define-type-method argument-type ((type gslist))
+  'list)
+
+(define-type-method return-type ((type gslist))
+  'list)
+
 (define-type-method size-of ((type gslist) &key inlined)
   (assert-not-inlined type inlined)
   (size-of 'pointer))