chiark / gitweb /
Initial checkin of CLISP port, code from glib/proxy.lisp
[clg] / glib / ffi.lisp
index 2f09bbfe64756f6fc2750111787e57c64e030218..4a47c7e1b39cc41751c25af686f3f5a6a5c3c782 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: ffi.lisp,v 1.26 2006/02/26 15:30:00 espen Exp $
+;; $Id: ffi.lisp,v 1.30 2006/03/03 20:31:24 espen Exp $
 
 (in-package "GLIB")
 
@@ -31,7 +31,7 @@ (defvar *package-prefix* nil)
 
 (defun set-package-prefix (prefix &optional (package *package*))
   (let ((package (find-package package)))
-    (delete-if #'(lambda (assoc) (eq (car assoc) package)) *package-prefix*)
+    (setq *package-prefix* (delete package *package-prefix* :key #'car))
     (push (cons package prefix) *package-prefix*))
   prefix)
 
@@ -348,8 +348,9 @@ (defmacro define-type-generic (name lambda-list &optional documentation)
        (find (first lambda-list) '(&optional &key &rest &allow-other-keys)))
       (error "A type generic needs at least one required argument")
     `(progn 
-       (setf (get ',name 'type-methods) (make-hash-table))
-       (setf (get ',name 'built-in-type-hierarchy) ())
+       (unless (get ',name 'type-methods)
+        (setf (get ',name 'type-methods) (make-hash-table))
+        (setf (get ',name 'built-in-type-hierarchy) ()))
        (defun ,name ,lambda-list
         ,documentation
         (funcall 
@@ -393,19 +394,48 @@ (define-type-generic unbound-value (type-spec)
   "Returns a value which should be intepreted as unbound for slots with virtual allocation")
 
 
+#+sbcl
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defun sb-sizeof-bits (type)
+    (sb-alien-internals:alien-type-bits
+     (sb-alien-internals:parse-alien-type type nil)))
+
+  (defun sb-sizeof (type)
+    (/ (sb-sizeof-bits type) 8)))
+
+
 ;; Sizes of fundamental C types in bytes (8 bits)
-(defconstant +size-of-short+ 2)
-(defconstant +size-of-int+ 4)
-(defconstant +size-of-long+ 4)
-(defconstant +size-of-pointer+ 4)
-(defconstant +size-of-float+ 4)
-(defconstant +size-of-double+ 8)
+(defconstant +size-of-short+
+  #+sbcl (sb-sizeof 'sb-alien:short)
+  #-sbcl 2)
+(defconstant +size-of-int+
+  #+sbcl (sb-sizeof 'sb-alien:int)
+  #-sbcl 4)
+(defconstant +size-of-long+
+  #+sbcl (sb-sizeof 'sb-alien:long)
+  #-sbcl 4)
+(defconstant +size-of-pointer+
+  #+sbcl (sb-sizeof 'sb-alien:system-area-pointer)
+  #-sbcl 4)
+(defconstant +size-of-float+
+  #+sbcl (sb-sizeof 'sb-alien:float)
+  #-sbcl 4)
+(defconstant +size-of-double+
+  #+sbcl (sb-sizeof 'sb-alien:double)
+  #-sbcl 8)
+
 
 ;; Sizes of fundamental C types in bits
 (defconstant +bits-of-byte+ 8)
-(defconstant +bits-of-short+ 16)
-(defconstant +bits-of-int+ 32)
-(defconstant +bits-of-long+ 32)
+(defconstant +bits-of-short+
+  #+sbcl (sb-sizeof-bits 'sb-alien:short)
+  #-sbcl 16)
+(defconstant +bits-of-int+
+  #+sbcl (sb-sizeof-bits 'sb-alien:int)
+  #-sbcl 32)
+(defconstant +bits-of-long+
+  #+sbcl (sb-sizeof-bits 'sb-alien:long)
+  #-sbcl 32)
 
 
 (deftype int () '(signed-byte #.+bits-of-int+))
@@ -1026,6 +1056,15 @@ (define-type-method from-alien-form ((type copy-of) form)
 (define-type-method from-alien-function ((type copy-of))
   (copy-from-alien-function (second (type-expand-to 'copy-of type))))
 
+(define-type-method cleanup-function ((type copy-of))
+  (declare (ignore type))
+  #'identity)
+
+(define-type-method destroy-function ((type copy-of))
+  (declare (ignore type))
+  #'(lambda (location &optional offset)
+      (declare (ignore location offset))))
+
 
 (define-type-method alien-type ((type callback))
   (declare (ignore type))