X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/clg/blobdiff_plain/4be970ba51e2527f3c2da82ef699b9104bf179f3..ae462d0ee5172af35c06224439521b34b0f09246:/gffi/basic-types.lisp diff --git a/gffi/basic-types.lisp b/gffi/basic-types.lisp index b2b0a89..e5582db 100644 --- a/gffi/basic-types.lisp +++ b/gffi/basic-types.lisp @@ -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: basic-types.lisp,v 1.3 2006-09-06 09:45:26 espen Exp $ +;; $Id: basic-types.lisp,v 1.9 2007-06-20 09:49:06 espen Exp $ (in-package "GFFI") @@ -55,6 +55,11 @@ (deftype char () 'base-char) (deftype pointer () #+(or cmu sbcl) 'system-area-pointer #+clisp 'ffi:foreign-address) +(deftype pointer-data () + '(unsigned-byte #+sbcl #.(sb-sizeof-bits 'sb-alien:system-area-pointer) + #+clisp #.(ffi:bitsizeof 'ffi:c-pointer) + #-(or sbcl clisp) 32)) + (deftype bool (&optional (size '*)) (declare (ignore size)) 'boolean) (deftype copy-of (type) type) (deftype static (type) type) @@ -454,18 +459,13 @@ (define-type-method to-alien-function ((type single-float) &optional copy-p) (define-type-method writer-function ((type single-float) &key temp (inlined t)) (declare (ignore temp)) (assert-inlined type inlined) - #'(lambda (value location &optional (offset 0)) - (setf - #+(or cmu sbcl)(sap-ref-single location offset) - #+clisp(ffi:memory-as location 'single-float offset) - (coerce value 'single-float)))) + #'(lambda (number location &optional (offset 0)) + (setf (ref-single-float location offset) (coerce number 'single-float)))) (define-type-method reader-function ((type single-float) &key ref (inlined t)) (declare (ignore ref)) (assert-inlined type inlined) - #'(lambda (location &optional (offset 0)) - #+(or cmu sbcl)(sap-ref-single location offset) - #+clisp(ffi:memory-as location 'single-float offset))) + #'ref-single-float) @@ -502,17 +502,18 @@ (define-type-method writer-function ((type double-float) &key temp (inlined t)) (declare (ignore temp)) (assert-inlined type inlined) #'(lambda (value location &optional (offset 0)) - (setf - #+(or cmu sbcl)(sap-ref-double location offset) - #+clisp(ffi:memory-as location 'double-float offset) - (coerce value 'double-float)))) + (setf (ref-double-float location offset) (coerce value 'double-float)))) (define-type-method reader-function ((type double-float) &key ref (inlined t)) (declare (ignore ref)) (assert-inlined type inlined) - #'(lambda (location &optional (offset 0)) - #+(or cmu sbcl)(sap-ref-double location offset) - #+clisp(ffi:memory-as location 'double-float offset))) + #'ref-double-float) + +(deftype optimized-double-float () 'double-float) + +(define-type-method to-alien-form ((type optimized-double-float) form &optional copy-p) + (declare (ignore type copy-p)) + form) @@ -570,6 +571,7 @@ (define-type-method reader-function ((type base-char) &key ref (inlined t)) ;;; String (defun utf8-length (string) + "Returns the length including the trailing zero, of STRING encoded as UTF8" (1+ (loop for char across string as char-code = (char-code char) @@ -601,7 +603,7 @@ (defun encode-utf8-string (string &optional location) ((< char-code #x800) (encode 11)) ((< char-code #x10000) (encode 16)) ((< char-code #x200000) (encode 21))))) - (setf (ref-byte location len) 0) + (setf (ref-byte location (1- len)) 0) location)) (defun decode-utf8-string (c-string) @@ -1086,7 +1088,7 @@ (define-type-method from-alien-form ((type copy-of) form &key (ref :copy)) (define-type-method from-alien-function ((type copy-of) &key (ref :copy)) (if (eq ref :copy) (from-alien-function (second (type-expand-to 'copy-of type)) :ref ref) - (error "Keyword arg :REF to FROM-ALIEN-FORM should be :COPY for type ~A. It was give ~A" type ref))) + (error "Keyword arg :REF to FROM-ALIEN-FUNCTION should be :COPY for type ~A. It was give ~A" type ref))) (define-type-method to-alien-form ((type copy-of) form &optional (copy-p t)) (if copy-p @@ -1129,17 +1131,17 @@ (define-type-method from-alien-form ((type static) form &key (ref :static)) (define-type-method from-alien-function ((type static) &key (ref :static)) (if (eq ref :static) (from-alien-function (second (type-expand-to 'static type)) :ref ref) - (error "Keyword arg :REF to FROM-ALIEN-FORM should be :STATIC for type ~A. It was give ~A" type ref))) + (error "Keyword arg :REF to FROM-ALIEN-FUNCTION should be :STATIC for type ~A. It was give ~A" type ref))) (define-type-method to-alien-function ((type static) &optional copy-p) (if (not copy-p) (to-alien-function (second (type-expand-to 'static type)) t) - (error "COPY-P argument to TO-ALIEN-FUNCTION should always be NIL for type ~A" type))) + (error "COPY-P argument to TO-ALIEN-FUNCTION should always be NIL for type ~A" type))) (define-type-method to-alien-form ((type static) &optional copy-p) (if (not copy-p) - (to-alien-function (second (type-expand-to 'static type)) t) - (error "COPY-P argument to TO-ALIEN-FUNCTION should always be NIL for type ~A" type))) + (to-alien-form (second (type-expand-to 'static type)) t) + (error "COPY-P argument to TO-ALIEN-FORM should always be NIL for type ~A" type))) (define-type-method reader-function ((type static) &key (ref :read) (inlined nil inlined-p)) (if inlined-p