X-Git-Url: https://git.distorted.org.uk/~mdw/clg/blobdiff_plain/8e0e01e42b1c846d06a0a38f5cfb38cf897e2f96..953030a3519ecb9d66d9f55d46f8c8b6906094ed:/glib/glib.lisp diff --git a/glib/glib.lisp b/glib/glib.lisp index 6d01049..e8f6f47 100644 --- a/glib/glib.lisp +++ b/glib/glib.lisp @@ -20,13 +20,22 @@ ;; 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.37 2006-04-25 21:51:32 espen Exp $ +;; $Id: glib.lisp,v 1.42 2007-10-17 14:30:41 espen Exp $ (in-package "GLIB") (use-prefix "g") +#-sb-thread +(progn + (defun make-mutex () + nil) + + (defmacro with-mutex ((mutex) &body body) + (declare (ignore mutex)) + `(progn ,@body))) + ;;;; Memory management @@ -36,48 +45,87 @@ (defbinding (%deallocate-memory "g_free") () nil (address pointer)) -(setf - (symbol-function 'allocate-memory) #'%allocate-memory - (symbol-function 'deallocate-memory) #'%deallocate-memory) +;; (setf +;; (symbol-function 'allocate-memory) #'%allocate-memory +;; (symbol-function 'deallocate-memory) #'%deallocate-memory) + +(setf *memory-allocator* #'%allocate-memory) +(setf *memory-deallocator* #'%deallocate-memory) + +(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 mechanism +;;;; 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)) - (incf *user-data-count*) - (setf - (gethash *user-data-count* *user-data*) - (cons object destroy-function)) - *user-data-count*) + (with-mutex (*user-data-lock*) + (setf + (gethash *user-data-next-id* *user-data*) + (cons object destroy-function)) + (1- (incf *user-data-next-id*)))) (defun find-user-data (id) (check-type id fixnum) - (multiple-value-bind (user-data p) (gethash id *user-data*) - (values (car user-data) p))) + (with-mutex (*user-data-lock*) + (multiple-value-bind (user-data p) (gethash id *user-data*) + (values (car user-data) p)))) (defun user-data-exists-p (id) (nth-value 1 (find-user-data id))) (defun update-user-data (id object) (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))) - (setf (car user-data) object))))) + (with-mutex (*user-data-lock*) + (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))) + (setf (car user-data) object)))))) (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*)) + (with-mutex (*user-data-lock*) + (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 @@ -163,10 +211,19 @@ (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)) +(define-type-method type-alignment ((type glist) &key inlined) + (assert-not-inlined type inlined) + (type-alignment 'pointer)) (define-type-method alien-arg-wrapper ((type glist) var list style form &optional copy-in-p) (destructuring-bind (element-type) (rest (type-expand-to 'glist type)) @@ -313,10 +370,20 @@ (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)) +(define-type-method type-alignment ((type gslist) &key inlined) + (assert-not-inlined type inlined) + (type-alignment 'pointer)) + (define-type-method alien-arg-wrapper ((type gslist) var list style form &optional copy-in-p) (destructuring-bind (element-type) (rest (type-expand-to 'gslist type)) (cond