0.9.16.17:
[sbcl.git] / src / code / host-c-call.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
9
10 (in-package "SB!ALIEN")
11
12 (/show0 "host-c-call.lisp 12")
13
14 (define-alien-type-class (c-string :include pointer :include-args (to))
15   (external-format :default :type keyword)
16   (element-type 'character :type (member character base-char)))
17
18 (define-alien-type-translator c-string
19     (&key (external-format :default)
20           (element-type 'character))
21   (make-alien-c-string-type
22    :to (parse-alien-type 'char (sb!kernel:make-null-lexenv))
23    :element-type element-type
24    :external-format external-format))
25
26 (defun c-string-external-format (type)
27   (let ((external-format (alien-c-string-type-external-format type)))
28     (if (eq external-format :default)
29         (default-c-string-external-format)
30         external-format)))
31
32 (define-alien-type-method (c-string :unparse) (type)
33   (list 'c-string
34         :external-format (alien-c-string-type-external-format type)
35         :element-type (alien-c-string-type-element-type type)))
36
37 (define-alien-type-method (c-string :lisp-rep) (type)
38   (declare (ignore type))
39   '(or simple-string null (alien (* char))))
40
41 (defun c-string-needs-conversion-p (type)
42   #+sb-xc-host
43   t
44   #-sb-xc-host
45   (let ((external-format (sb!impl::get-external-format
46                           ;; Can't use C-STRING-EXTERNAL-FORMAT here,
47                           ;; since the meaning of :DEFAULT can change
48                           ;; when *DEFAULT-C-STRING-EXTERNAL-FORMAT*
49                           ;; changes.
50                           (alien-c-string-type-external-format type))))
51     (not (and external-format
52               (or (eq (caar external-format) :ascii)
53                   ;; On non-SB-UNICODE all latin-1 codepoints will fit
54                   ;; into a base-char, on SB-UNICODE they won't.
55                   #!-sb-unicode
56                   (eq (caar external-format) :latin-1))))))
57
58 (define-alien-type-method (c-string :naturalize-gen) (type alien)
59   `(if (zerop (sap-int ,alien))
60        nil
61        ;; Check whether we need to do a full external-format
62        ;; conversion, or whether we can just do a cheap byte-by-byte
63        ;; copy of the c-string data.
64        ;;
65        ;; On SB-UNICODE we can never do the cheap copy, even if the
66        ;; external format and element-type are suitable, since
67        ;; simple-base-strings may not contain ISO-8859-1 characters.
68        ;; If we need to check for non-ascii data in the input, we
69        ;; might as well go through the usual external-format machinery
70        ;; instead of rewriting another version of it.
71        ,(if #!+sb-unicode t
72             #!-sb-unicode (c-string-needs-conversion-p type)
73             `(sb!alien::c-string-to-string ,alien
74                                            (c-string-external-format ,type)
75                                            (alien-c-string-type-element-type
76                                             ,type))
77             `(%naturalize-c-string ,alien))))
78
79 (define-alien-type-method (c-string :deport-gen) (type value)
80   `(etypecase ,value
81      (null (int-sap 0))
82      ((alien (* char)) (alien-sap ,value))
83      ;; FIXME: GC safety alert! These SAPs are not safe, since the
84      ;; Lisp string can move. This is not hard to arrange, for example
85      ;; the following will fail very quickly on a SB-UNICODE build:
86      ;;
87      ;;   (setf (bytes-consed-between-gcs) 4096)
88      ;;   (define-alien-routine "strcmp" int (s1 c-string) (s2 c-string))
89      ;;
90      ;;   (loop
91      ;;     (let ((string "hello, world"))
92      ;;       (assert (zerop (strcmp string string)))))
93      ;;
94      ;; (This will appear to work on post-0.9.8.19 GENCGC, since
95      ;;  the GC no longer zeroes memory immediately after releasing
96      ;;  it after a minor GC. Either enabling the READ_PROTECT_FREE_PAGES
97      ;;  #define in gencgc.c or modifying the example so that a major
98      ;;  GC will occasionally be triggered would unmask the bug).
99      ;;
100      ;; The pure VECTOR-SAP branch for the SIMPLE-BASE-STRING case
101      ;; will generally be very hard to trigger on GENCGC (even when
102      ;; threaded) thanks to GC conservativeness. It's mostly a problem
103      ;; on cheneygc.  -- JES, 2006-01-13
104      (simple-base-string
105       ,(if (c-string-needs-conversion-p type)
106            ;; If the alien type is not ascii-compatible (+SB-UNICODE)
107            ;; or latin-1-compatible (-SB-UNICODE), we need to do
108            ;; external format conversion.
109            `(vector-sap (string-to-c-string ,value
110                                             (c-string-external-format ,type)))
111            ;; Otherwise we can just pass it uncopied.
112            `(vector-sap ,value)))
113      ;; This case, on the other hand, will cause trouble on GENCGC, since
114      ;; we're taking the SAP of a immediately discarded temporary -> the
115      ;; conservativeness doesn't protect us.
116      ;; -- JES, 2006-01-13
117      (simple-string
118       (vector-sap (string-to-c-string ,value
119                                       (c-string-external-format ,type))))))
120
121 (/show0 "host-c-call.lisp end of file")