Initial revision
[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
15 (sb!int:file-comment
16   "$Header$")
17 \f
18 ;;;; extra types
19
20 (def-alien-type char (integer 8))
21 (def-alien-type short (integer 16))
22 (def-alien-type int (integer 32))
23 (def-alien-type long (integer #!-alpha 32 #!+alpha 64))
24
25 (def-alien-type unsigned-char (unsigned 8))
26 (def-alien-type unsigned-short (unsigned 16))
27 (def-alien-type unsigned-int (unsigned 32))
28 (def-alien-type unsigned-long (unsigned #!-alpha 32 #!+alpha 64))
29
30 (def-alien-type float single-float)
31 (def-alien-type double double-float)
32
33 (def-alien-type-translator void ()
34   (parse-alien-type '(values) (sb!kernel:make-null-lexenv)))
35 \f
36 (defun %naturalize-c-string (sap)
37   (declare (type system-area-pointer sap))
38   (with-alien ((ptr (* char) sap))
39     (locally
40      (declare (optimize (speed 3) (safety 0)))
41      (let ((length (loop
42                      for offset of-type fixnum upfrom 0
43                      until (zerop (deref ptr offset))
44                      finally (return offset))))
45        (let ((result (make-string length)))
46          (sb!kernel:copy-from-system-area (alien-sap ptr) 0
47                                           result (* sb!vm:vector-data-offset
48                                                     sb!vm:word-bits)
49                                           (* length sb!vm:byte-bits))
50          result)))))