X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcode%2Fhost-c-call.lisp;h=805023197fcc863f4d1aabb1e98c172e25f8d8db;hb=b0a7abdf2bd6f2d66fcce97196024cdb0e1a1886;hp=440b8facc76ed56236dcf94dd8648c902908412f;hpb=ce02ab2ecd9c6ae2e570abd8c93ebf3be55bbdad;p=sbcl.git diff --git a/src/code/host-c-call.lisp b/src/code/host-c-call.lisp index 440b8fa..8050231 100644 --- a/src/code/host-c-call.lisp +++ b/src/code/host-c-call.lisp @@ -7,36 +7,92 @@ ;;;; provided with absolutely no warranty. See the COPYING and CREDITS ;;;; files for more information. -(in-package "SB!C-CALL") +(in-package "SB!ALIEN") (/show0 "host-c-call.lisp 12") -(def-alien-type-class (c-string :include pointer :include-args (to))) +(define-alien-type-class (c-string :include pointer :include-args (to))) -(def-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 () + (make-alien-c-string-type + :to (parse-alien-type 'char (sb!kernel:make-null-lexenv)))) -(def-alien-type-method (c-string :unparse) (type) +(define-alien-type-method (c-string :unparse) (type) (declare (ignore type)) 'c-string) -(def-alien-type-method (c-string :lisp-rep) (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)))) -(def-alien-type-method (c-string :naturalize-gen) (type alien) +(define-alien-type-method (c-string :naturalize-gen) (type alien) (declare (ignore type)) `(if (zerop (sap-int ,alien)) nil (%naturalize-c-string ,alien))) -(def-alien-type-method (c-string :deport-gen) (type value) +(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)))) + ;; FIXME: GC safety alert! These SAPs are not safe, since the + ;; Lisp string can move. This is not hard to arrange, for example + ;; the following will fail very quickly on a SB-UNICODE build: + ;; + ;; (setf (bytes-consed-between-gcs) 4096) + ;; (define-alien-routine "strcmp" int (s1 c-string) (s2 c-string)) + ;; + ;; (loop + ;; (let ((string "hello, world")) + ;; (assert (zerop (strcmp string string))))) + ;; + ;; (This will appear to work on post-0.9.8.19 GENCGC, since + ;; the GC no longer zeroes memory immediately after releasing + ;; it after a minor GC. Either enabling the READ_PROTECT_FREE_PAGES + ;; #define in gencgc.c or modifying the example so that a major + ;; GC will occasionally be triggered would unmask the bug). + ;; + ;; The SIMPLE-BASE-STRING case will generally be very hard to + ;; trigger on GENCGC (even when threaded) thanks to GC + ;; conservativeness. It's mostly a problem on cheneygc. + ;; -- JES, 2006-01-13 + (simple-base-string (vector-sap ,value)) + ;; This case, on the other hand, will cause trouble on GENCGC, since + ;; we're taking the SAP of a immediately discarded temporary -> the + ;; conservativeness doesn't protect us. + ;; -- JES, 2006-01-13 + (simple-string (vector-sap (coerce ,value 'simple-base-string))))) + +(/show0 "host-c-call.lisp 42") + +(define-alien-type-class (utf8-string :include pointer :include-args (to))) + +(define-alien-type-translator utf8-string () + (make-alien-utf8-string-type + :to (parse-alien-type 'char (sb!kernel:make-null-lexenv)))) + +(define-alien-type-method (utf8-string :unparse) (type) + (declare (ignore type)) + 'utf8-string) + +(define-alien-type-method (utf8-string :lisp-rep) (type) + (declare (ignore type)) + '(or simple-string null (alien (* char)))) + +(define-alien-type-method (utf8-string :naturalize-gen) (type alien) + (declare (ignore type)) + `(if (zerop (sap-int ,alien)) + nil + (%naturalize-utf8-string ,alien))) + +(define-alien-type-method (utf8-string :deport-gen) (type value) + (declare (ignore type)) + `(etypecase ,value + (null (int-sap 0)) + ((alien (* char)) (alien-sap ,value)) + ;; See the C-STRING :DEPORT-GEN comments for GC safety issues. + (simple-base-string (vector-sap ,value)) + (simple-string (vector-sap (%deport-utf8-string ,value))))) (/show0 "host-c-call.lisp end of file")