X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcode%2Ftarget-c-call.lisp;h=e35489e1c139d8cb66b0ec119c5689537ed1e45f;hb=77d94d36bcfd3d5eea73ad51e6ee621a8938f995;hp=0d4e2cbedafac6474c8118f8fa2acd3ccb701840;hpb=545fa4548b327804cf78afe38a2ecd94ced86162;p=sbcl.git diff --git a/src/code/target-c-call.lisp b/src/code/target-c-call.lisp index 0d4e2cb..e35489e 100644 --- a/src/code/target-c-call.lisp +++ b/src/code/target-c-call.lisp @@ -1,5 +1,10 @@ -;;;; This file contains some extensions to the Alien facility to -;;;; simplify importing C interfaces. +;;;; FIXME: This file and host-c-call.lisp are separate from the +;;;; rest of the alien source code for historical reasons: CMU CL +;;;; made a distinction between the stuff in the C-CALL package and +;;;; stuff in the ALIEN package. There's no obvious boundary +;;;; there, though, and SBCL doesn't try to make this distinction, +;;;; so it might make sense to just merge these files in with the +;;;; rest of the SB-ALIEN code. ;;;; This software is part of the SBCL system. See the README file for ;;;; more information. @@ -10,38 +15,135 @@ ;;;; provided with absolutely no warranty. See the COPYING and CREDITS ;;;; files for more information. -(in-package "SB!C-CALL") +(in-package "SB!ALIEN") ;;;; extra types -(def-alien-type char (integer 8)) -(def-alien-type short (integer 16)) -(def-alien-type int (integer 32)) -(def-alien-type long (integer #!-alpha 32 #!+alpha 64)) +(define-alien-type char (integer 8)) +(define-alien-type short (integer 16)) +(define-alien-type int (integer 32)) +(define-alien-type long (integer #!-alpha 32 #!+alpha 64)) -(def-alien-type unsigned-char (unsigned 8)) -(def-alien-type unsigned-short (unsigned 16)) -(def-alien-type unsigned-int (unsigned 32)) -(def-alien-type unsigned-long (unsigned #!-alpha 32 #!+alpha 64)) +(define-alien-type unsigned-char (unsigned 8)) +(define-alien-type unsigned-short (unsigned 16)) +(define-alien-type unsigned-int (unsigned 32)) +(define-alien-type unsigned-long (unsigned #!-alpha 32 #!+alpha 64)) -(def-alien-type float single-float) -(def-alien-type double double-float) +(define-alien-type float single-float) +(define-alien-type double double-float) -(def-alien-type-translator void () +(define-alien-type-translator void () (parse-alien-type '(values) (sb!kernel:make-null-lexenv))) +;;; FIXME: %NATURALIZE-C-STRING (and the UTF8 siblings below) would +;;; appear to be vulnerable to the lisp string moving from underneath +;;; them if the world undergoes a GC, possibly triggered by another +;;; thread. Ugh. (defun %naturalize-c-string (sap) (declare (type system-area-pointer sap)) - (with-alien ((ptr (* char) sap)) - (locally - (declare (optimize (speed 3) (safety 0))) - (let ((length (loop - for offset of-type fixnum upfrom 0 - until (zerop (deref ptr offset)) - finally (return offset)))) - (let ((result (make-string length))) - (sb!kernel:copy-from-system-area (alien-sap ptr) 0 - result (* sb!vm:vector-data-offset - sb!vm:n-word-bits) - (* length sb!vm:byte-bits)) - result))))) + (locally + (declare (optimize (speed 3) (safety 0))) + (let ((length (loop for offset of-type fixnum upfrom 0 + until (zerop (sap-ref-8 sap offset)) + finally (return offset)))) + (let ((result (make-string length :element-type 'base-char))) + (sb!kernel:copy-from-system-area sap 0 + result (* sb!vm:vector-data-offset + sb!vm:n-word-bits) + (* length sb!vm:n-byte-bits)) + result)))) + +(defun %naturalize-utf8-string (sap) + (declare (type system-area-pointer sap)) + (locally + (declare (optimize (speed 3) (safety 0))) + (let ((length (do* ((offset 0) + (byte (sap-ref-8 sap offset) (sap-ref-8 sap offset)) + (index 0 (1+ index))) + ((zerop byte) index) + (declare (type fixnum offset index)) + (cond + ;; FIXME: Here, and below, we don't defend + ;; against malformed utf-8 with any degree of + ;; rigour. + ((< byte #x80) (incf offset)) + ((< byte #xe0) (incf offset 2)) + ((< byte #xf0) (incf offset 3)) + (t (incf offset 4)))))) + (let ((result (make-string length :element-type 'character))) + (do* ((offset 0) + (byte (sap-ref-8 sap offset) (sap-ref-8 sap offset)) + (index 0 (1+ index))) + ((>= index length) result) + (declare (type fixnum offset index)) + (setf (char result index) + (cond + ((< byte #x80) + (prog1 (code-char byte) (incf offset))) + ((< byte #xe0) + (prog1 (code-char (dpb byte (byte 5 6) + (sap-ref-8 sap (1+ offset)))) + (incf offset 2))) + ((< byte #xf0) + (prog1 (code-char + (dpb byte (byte 4 12) + (dpb (sap-ref-8 sap (1+ offset)) (byte 6 6) + (sap-ref-8 sap (+ 2 offset))))) + (incf offset 3))) + (t + (prog1 + (code-char + (dpb byte (byte 3 18) + (dpb (sap-ref-8 sap (1+ offset)) (byte 6 12) + (dpb (sap-ref-8 sap (+ 2 offset)) (byte 6 6) + (sap-ref-8 sap (+ 3 offset)))))) + (incf offset 4)))))))))) + +(defun %deport-utf8-string (string) + (declare (type simple-string string)) + (locally + (declare (optimize (speed 3) (safety 0))) + (let ((length (1+ (do* ((offset 0) + (length (length string)) + (index 0 (1+ index))) + ((= index length) offset) + (declare (type fixnum offset)) + (let ((bits (char-code (char string index)))) + (cond + ((< bits #x80) (incf offset 1)) + ((< bits #x800) (incf offset 2)) + ((< bits #x10000) (incf offset 3)) + (t (incf offset 4)))))))) + (let ((vector (make-array length :element-type '(unsigned-byte 8) + :initial-element 0))) + (do* ((offset 0) + (length (length string)) + (index 0 (1+ index))) + ((= index length) vector) + (declare (type fixnum offset)) + (let ((bits (char-code (char string index)))) + (cond + ((< bits #x80) + (setf (aref vector offset) bits) + (incf offset)) + ((< bits #x800) + (setf (aref vector offset) (logior #xc0 (ldb (byte 5 6) bits))) + (setf (aref vector (1+ offset)) + (logior #x80 (ldb (byte 6 0) bits))) + (incf offset 2)) + ((< bits #x10000) + (setf (aref vector offset) (logior #xe0 (ldb (byte 4 12) bits))) + (setf (aref vector (1+ offset)) + (logior #x80 (ldb (byte 6 6) bits))) + (setf (aref vector (+ offset 2)) + (logior #x80 (ldb (byte 6 0) bits))) + (incf offset 3)) + (t + (setf (aref vector offset) (logior #xf0 (ldb (byte 3 18) bits))) + (setf (aref vector (1+ offset)) + (logior #x80 (ldb (byte 6 12) bits))) + (setf (aref vector (+ offset 2)) + (logior #x80 (ldb (byte 6 6) bits))) + (setf (aref vector (+ offset 3)) + (logior #x80 (ldb (byte 6 0) bits))) + (incf offset 4)))))))))