7a83d1021a80879036a93fc2ee5765291ff7c383
[sbcl.git] / src / code / target-c-call.lisp
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.
8
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
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.
17
18 (in-package "SB!ALIEN")
19 \f
20 ;;;; extra types
21
22 (define-alien-type char (integer 8))
23 (define-alien-type short (integer 16))
24 (define-alien-type int (integer 32))
25 (define-alien-type long (integer #.sb!vm::n-machine-word-bits))
26 (define-alien-type long-long (integer 64))
27
28 (define-alien-type unsigned-char (unsigned 8))
29 (define-alien-type unsigned-short (unsigned 16))
30 (define-alien-type unsigned-int (unsigned 32))
31 (define-alien-type unsigned-long (unsigned #.sb!vm::n-machine-word-bits))
32 (define-alien-type unsigned-long-long (unsigned 64))
33
34 (define-alien-type float single-float)
35 (define-alien-type double double-float)
36
37 (define-alien-type utf8-string (c-string :external-format :utf8))
38
39 (define-alien-type-translator void ()
40   (parse-alien-type '(values) (sb!kernel:make-null-lexenv)))
41 \f
42
43 (defun default-c-string-external-format ()
44   #!+sb-xc
45   :latin-1
46   #!-sb-xc
47   (or *default-c-string-external-format*
48       (setf *default-c-string-external-format*
49             (sb!impl::default-external-format))))
50
51 ;;; FIXME: %NATURALIZE-C-STRING (and the UTF8 siblings below) would
52 ;;; appear to be vulnerable to the lisp string moving from underneath
53 ;;; them if the world undergoes a GC, possibly triggered by another
54 ;;; thread.  Ugh.
55 ;;;
56 ;;; Actually the above shouldn't happen; x86 and x86-64 use GENCGC,
57 ;;; so the string can't move by virtue of pointers to it from
58 ;;; outside the heap. Other platforms will access the lisp string
59 ;;; through the GC-safe interior pointer. -- JES, 2006-01-13
60 (defun %naturalize-c-string (sap)
61   (declare (type system-area-pointer sap))
62   (locally
63       (declare (optimize (speed 3) (safety 0)))
64     (let ((length (loop for offset of-type fixnum upfrom 0
65                         until (zerop (sap-ref-8 sap offset))
66                         finally (return offset))))
67       (let ((result (make-string length :element-type 'base-char)))
68         (sb!kernel:copy-ub8-from-system-area sap 0 result 0 length)
69         result))))
70
71 (defun string-to-c-string (string external-format)
72   (declare (type simple-string string))
73   (locally
74       (declare (optimize (speed 3) (safety 0)))
75     (let ((external-format (sb!impl::get-external-format-or-lose external-format)))
76       (funcall (sb!impl::ef-write-c-string-fun external-format) string))))
77
78 (defun c-string-to-string (sap external-format element-type)
79   (declare (type system-area-pointer sap))
80   (locally
81       (declare (optimize (speed 3) (safety 0)))
82     (let ((external-format (sb!impl::get-external-format-or-lose external-format)))
83       (funcall (sb!impl::ef-read-c-string-fun external-format) sap element-type))))