0.pre7.90:
[sbcl.git] / src / code / target-c-call.lisp
1 ;;;; This file contains some extensions to the Alien facility to
2 ;;;; simplify importing C interfaces.
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!C-CALL")
14 \f
15 ;;;; extra types
16
17 (define-alien-type char (integer 8))
18 (define-alien-type short (integer 16))
19 (define-alien-type int (integer 32))
20 (define-alien-type long (integer #!-alpha 32 #!+alpha 64))
21
22 (define-alien-type unsigned-char (unsigned 8))
23 (define-alien-type unsigned-short (unsigned 16))
24 (define-alien-type unsigned-int (unsigned 32))
25 (define-alien-type unsigned-long (unsigned #!-alpha 32 #!+alpha 64))
26
27 (define-alien-type float single-float)
28 (define-alien-type double double-float)
29
30 (define-alien-type-translator void ()
31   (parse-alien-type '(values) (sb!kernel:make-null-lexenv)))
32 \f
33 (defun %naturalize-c-string (sap)
34   (declare (type system-area-pointer sap))
35   (with-alien ((ptr (* char) sap))
36     (locally
37      (declare (optimize (speed 3) (safety 0)))
38      (let ((length (loop
39                      for offset of-type fixnum upfrom 0
40                      until (zerop (deref ptr offset))
41                      finally (return offset))))
42        (let ((result (make-string length)))
43          (sb!kernel:copy-from-system-area (alien-sap ptr) 0
44                                           result (* sb!vm:vector-data-offset
45                                                     sb!vm:n-word-bits)
46                                           (* length sb!vm:n-byte-bits))
47          result)))))