X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcode%2Fhost-c-call.lisp;h=eb12c398d7fb5b0b59baef3d5d2a3185617a3497;hb=17532463fa19f2fc2aba53b65c32e200a27ccd6a;hp=b7730f14e5dfa98db76996781fdcadfc094694d9;hpb=6139c89c89f45c03509e4f3156293ff656716a8c;p=sbcl.git diff --git a/src/code/host-c-call.lisp b/src/code/host-c-call.lisp index b7730f1..eb12c39 100644 --- a/src/code/host-c-call.lisp +++ b/src/code/host-c-call.lisp @@ -11,32 +11,97 @@ (/show0 "host-c-call.lisp 12") -(define-alien-type-class (c-string :include pointer :include-args (to))) +(define-alien-type-class (c-string :include pointer :include-args (to)) + (external-format :default :type keyword) + (element-type 'character :type (member character base-char))) -(define-alien-type-translator c-string () - (make-alien-c-string-type :to - (parse-alien-type 'char - (sb!kernel::make-null-lexenv)))) +(define-alien-type-translator c-string + (&key (external-format :default) + (element-type 'character)) + (make-alien-c-string-type + :to (parse-alien-type 'char (sb!kernel:make-null-lexenv)) + :element-type element-type + :external-format external-format)) + +(defun c-string-external-format (type) + (let ((external-format (alien-c-string-type-external-format type))) + (if (eq external-format :default) + (default-c-string-external-format) + external-format))) (define-alien-type-method (c-string :unparse) (type) - (declare (ignore type)) - 'c-string) + (list 'c-string + :external-format (alien-c-string-type-external-format type) + :element-type (alien-c-string-type-element-type type))) (define-alien-type-method (c-string :lisp-rep) (type) (declare (ignore type)) - '(or simple-base-string null (alien (* char)))) + '(or simple-string null (alien (* char)) (simple-array (unsigned-byte 8)))) -(define-alien-type-method (c-string :naturalize-gen) (type alien) +(define-alien-type-method (c-string :deport-pin-p) (type) (declare (ignore type)) + t) + +(defun c-string-needs-conversion-p (type) + #+sb-xc-host + t + #-sb-xc-host + (let ((external-format (sb!impl::get-external-format + ;; Can't use C-STRING-EXTERNAL-FORMAT here, + ;; since the meaning of :DEFAULT can change + ;; when *DEFAULT-C-STRING-EXTERNAL-FORMAT* + ;; changes. + (alien-c-string-type-external-format type)))) + (not (and external-format + (or (eq (caar external-format) :ascii) + ;; On non-SB-UNICODE all latin-1 codepoints will fit + ;; into a base-char, on SB-UNICODE they won't. + #!-sb-unicode + (eq (caar external-format) :latin-1)))))) + +(define-alien-type-method (c-string :naturalize-gen) (type alien) `(if (zerop (sap-int ,alien)) nil - (%naturalize-c-string ,alien))) + ;; Check whether we need to do a full external-format + ;; conversion, or whether we can just do a cheap byte-by-byte + ;; copy of the c-string data. + ;; + ;; On SB-UNICODE we can never do the cheap copy, even if the + ;; external format and element-type are suitable, since + ;; simple-base-strings may not contain ISO-8859-1 characters. + ;; If we need to check for non-ascii data in the input, we + ;; might as well go through the usual external-format machinery + ;; instead of rewriting another version of it. + ,(if #!+sb-unicode t + #!-sb-unicode (c-string-needs-conversion-p type) + `(sb!alien::c-string-to-string ,alien + (c-string-external-format ,type) + (alien-c-string-type-element-type + ,type)) + `(%naturalize-c-string ,alien)))) (define-alien-type-method (c-string :deport-gen) (type value) (declare (ignore type)) `(etypecase ,value (null (int-sap 0)) ((alien (* char)) (alien-sap ,value)) - (simple-base-string (vector-sap ,value)))) + (vector (vector-sap ,value)))) + +(define-alien-type-method (c-string :deport-alloc-gen) (type value) + `(etypecase ,value + (null nil) + ((alien (* char)) ,value) + (simple-base-string + ,(if (c-string-needs-conversion-p type) + ;; If the alien type is not ascii-compatible (+SB-UNICODE) + ;; or latin-1-compatible (-SB-UNICODE), we need to do + ;; external format conversion. + `(string-to-c-string ,value + (c-string-external-format ,type)) + ;; Otherwise we can just pass it uncopied. + value)) + (simple-string + (string-to-c-string ,value + (c-string-external-format ,type))))) (/show0 "host-c-call.lisp end of file")