Remove single use function, revise comment, fix inlining failure
[sbcl.git] / src / code / target-extensions.lisp
blob8c48807ae5dc1f0c76b5ee26b51ec89f7759bbe7
1 ;;;; This file contains things for the extensions packages (SB-EXT and
2 ;;;; also "internal extensions" SB-INT) which can't be built at
3 ;;;; cross-compile time, and perhaps also some things which might as
4 ;;;; well not be built at cross-compile time because they're not
5 ;;;; needed then. Things which can't be built at cross-compile time
6 ;;;; (e.g. because they need machinery which only exists inside SBCL's
7 ;;;; implementation of the LISP package) do not belong in this file.
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.
18 (in-package "SB!IMPL")
20 ;;;; variables initialization and shutdown sequences
22 ;;; (Most of the save-a-core functionality is defined later, in its
23 ;;; own file, but we'd like to have these symbols declared special and
24 ;;; initialized ASAP.)
26 (declaim (type list *save-hooks* *init-hooks* *exit-hooks*))
28 (defvar *save-hooks* nil
29 "A list of function designators which are called in an unspecified
30 order before creating a saved core image.
32 Unused by SBCL itself: reserved for user and applications.")
34 (defvar *init-hooks* nil
35 "A list of function designators which are called in an unspecified
36 order when a saved core image starts up, after the system itself has
37 been initialized.
39 Unused by SBCL itself: reserved for user and applications.")
41 (defvar *exit-hooks* nil
42 "A list of function designators which are called in an unspecified
43 order when SBCL process exits.
45 Unused by SBCL itself: reserved for user and applications.
47 Using (SB-EXT:EXIT :ABORT T), or calling exit(3) directly circumvents
48 these hooks.")
51 ;;; Binary search for simple vectors
52 (defun binary-search (value seq &key (key #'identity))
53 (declare (simple-vector seq))
54 (labels ((recurse (start end)
55 (when (< start end)
56 (let* ((i (+ start (truncate (- end start) 2)))
57 (elt (svref seq i))
58 (key-value (funcall key elt)))
59 (cond ((< value key-value)
60 (recurse start i))
61 ((> value key-value)
62 (recurse (1+ i) end))
64 elt))))))
65 (recurse 0 (length seq))))
67 (defun double-vector-binary-search (value vector)
68 (declare (simple-vector vector)
69 (optimize speed)
70 (integer value))
71 (labels ((recurse (start end)
72 (declare (type index start end))
73 (when (< start end)
74 (let* ((i (+ start (truncate (- end start) 2)))
75 (elt (svref vector (truly-the index (* 2 i)))))
76 (declare (type integer elt)
77 (type index i))
78 (cond ((< value elt)
79 (recurse start i))
80 ((> value elt)
81 (recurse (1+ i) end))
83 (svref vector (truly-the index (1+ (* 2 i))))))))))
84 (recurse 0 (truncate (length vector) 2))))
87 ;;;; helpers for C library calls
89 ;;; Signal a SIMPLE-CONDITION/ERROR condition associated with an ANSI C
90 ;;; errno problem, arranging for the condition's print representation
91 ;;; to be similar to the ANSI C perror(3) style.
92 (defun simple-perror (prefix-string
93 &key
94 (errno (get-errno))
95 (simple-error 'simple-error)
96 other-condition-args)
97 (declare (type symbol simple-error))
98 (aver (subtypep simple-error 'simple-condition))
99 (aver (subtypep simple-error 'error))
100 (apply #'error
101 simple-error
102 :format-control "~@<~A: ~2I~_~A~:>"
103 :format-arguments (list prefix-string (strerror errno))
104 other-condition-args))
106 ;;; Constructing shortish strings one character at a time. More efficient then
107 ;;; a string-stream, as can directly use simple-base-strings when applicable,
108 ;;; and if the maximum size is know doesn't need to copy the result at all --
109 ;;; but if the result is going to be HUGE, string-streams will win.
110 (defmacro with-push-char ((&key (element-type 'character) (initial-size 28)) &body body)
111 (with-unique-names (string size pointer)
112 `(let* ((,size ,initial-size)
113 (,string (make-array ,size :element-type ',element-type))
114 (,pointer 0))
115 (declare (type (integer 0 ,sb!xc:array-dimension-limit) ,size)
116 (type (integer 0 ,(1- sb!xc:array-dimension-limit)) ,pointer)
117 (type (simple-array ,element-type (*)) ,string))
118 (flet ((push-char (char)
119 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
120 (when (= ,pointer ,size)
121 (let ((old ,string))
122 (setf ,size (* 2 (+ ,size 2))
123 ,string (make-array ,size :element-type ',element-type))
124 (replace ,string old)))
125 (setf (char ,string ,pointer) char)
126 (incf ,pointer))
127 (get-pushed-string ()
128 (let ((string ,string)
129 (size ,pointer))
130 (setf ,size 0
131 ,pointer 0
132 ,string ,(coerce "" `(simple-array ,element-type (*))))
133 ;; This is really local, so we can be destructive!
134 (%shrink-vector string size)
135 string)))
136 ,@body))))