1 ;;;; FIXME: This file and host-c-call.lisp are separate from the
2 ;;;; rest of the alien source code for historical reasons: CMU CL
3 ;;;; made a distinction between the stuff in the C-CALL package and
4 ;;;; stuff in the ALIEN package. There's no obvious boundary
5 ;;;; there, though, and SBCL doesn't try to make this distinction,
6 ;;;; so it might make sense to just merge these files in with the
7 ;;;; rest of the SB-ALIEN code.
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
18 (in-package "SB-ALIEN")
22 (define-alien-type char
(integer 8))
23 (define-alien-type short
(integer 16))
24 (define-alien-type int
(integer 32))
26 (define-alien-type long
(integer #.sb-vm
:n-machine-word-bits
))
28 (define-alien-type long
(integer 32))
30 (define-alien-type long-long
(integer 64))
32 (define-alien-type unsigned-char
(unsigned 8))
33 (define-alien-type unsigned-short
(unsigned 16))
34 (define-alien-type unsigned-int
(unsigned 32))
36 (define-alien-type unsigned-long
(unsigned #.sb-vm
:n-machine-word-bits
))
38 (define-alien-type unsigned-long
(unsigned 32))
39 (define-alien-type unsigned-long-long
(unsigned 64))
41 (define-alien-type float single-float
)
42 (define-alien-type double double-float
)
44 (define-alien-type utf8-string
(c-string :external-format
:utf8
))
46 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
47 (define-alien-type-translator void
()
48 (parse-alien-type '(values) (sb-kernel:make-null-lexenv
))))
51 (defun default-c-string-external-format ()
52 (or *default-c-string-external-format
*
53 (setf *default-c-string-external-format
*
54 (sb-impl::default-external-format
))))
56 (defun %naturalize-c-string
(sap)
57 (declare (type system-area-pointer sap
))
58 ;; It can be assumed that any modern implementation of strlen() reads 4, 8, 16,
59 ;; or possibly even 32 bytes at a time when searching for the '\0' terminator.
60 ;; As such, we expect it to be on average much faster than a loop over SAP-REF-8.
61 ;; And much to my surprise, the foreign call overhead on x86-64 is so small that
62 ;; there is not a minimum threshold length below which the foreign call costs too much.
63 ;; With as few as 5 characters in the string, I saw 2x speedup.
64 ;; Below that, it's about the same to do a foreign call versus staying in lisp.
65 ;; The limiting case of a 0 length string would be faster without the foreign call,
66 ;; but pre-checking would slow down every other case.
67 (let* ((length (alien-funcall
68 (extern-alien "strlen" (function size-t system-area-pointer
))
70 (result (make-string length
:element-type
'base-char
)))
71 ;; COPY-UB8 pins the lisp string, no need to do it here
72 (sb-kernel:copy-ub8-from-system-area sap
0 result
0 length
)