Slight speedup up string reading.
[sbcl.git] / src / code / target-extensions.lisp
blob4aae3746ff2c437828cd8e3a1c3c39e8983c2b88
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
24 ;; and initialized ASAP.)
25 (defvar *save-hooks* nil
26 #!+sb-doc
27 "This is a list of functions which are called in an unspecified
28 order before creating a saved core image. Unused by SBCL itself:
29 reserved for user and applications.")
31 (defvar *init-hooks* nil
32 #!+sb-doc
33 "This is a list of functions which are called in an unspecified
34 order when a saved core image starts up, after the system itself has
35 been initialized. Unused by SBCL itself: reserved for user and
36 applications.")
38 (defvar *exit-hooks* nil
39 #!+sb-doc
40 "This is a list of functions which are called in an unspecified
41 order when SBCL process exits. Unused by SBCL itself: reserved for
42 user and applications. Using (SB-EXT:EXIT :ABORT T), or calling
43 exit(3) directly will circumvent these hooks.")
46 ;;; Binary search for simple vectors
47 (defun binary-search (value seq &key (key #'identity))
48 (declare (simple-vector seq))
49 (labels ((recurse (start end)
50 (when (< start end)
51 (let* ((i (+ start (truncate (- end start) 2)))
52 (elt (svref seq i))
53 (key-value (funcall key elt)))
54 (cond ((< value key-value)
55 (recurse start i))
56 ((> value key-value)
57 (recurse (1+ i) end))
59 elt))))))
60 (recurse 0 (length seq))))
62 (defun double-vector-binary-search (value vector)
63 (declare (simple-vector vector)
64 (optimize speed)
65 (integer value))
66 (labels ((recurse (start end)
67 (declare (type index start end))
68 (when (< start end)
69 (let* ((i (+ start (truncate (- end start) 2)))
70 (elt (svref vector (truly-the index (* 2 i)))))
71 (declare (type integer elt)
72 (type index i))
73 (cond ((< value elt)
74 (recurse start i))
75 ((> value elt)
76 (recurse (1+ i) end))
78 (svref vector (truly-the index (1+ (* 2 i))))))))))
79 (recurse 0 (truncate (length vector) 2))))
82 ;;;; helpers for C library calls
84 ;;; Signal a SIMPLE-CONDITION/ERROR condition associated with an ANSI C
85 ;;; errno problem, arranging for the condition's print representation
86 ;;; to be similar to the ANSI C perror(3) style.
87 (defun simple-perror (prefix-string
88 &key
89 (errno (get-errno))
90 (simple-error 'simple-error)
91 other-condition-args)
92 (declare (type symbol simple-error))
93 (aver (subtypep simple-error 'simple-condition))
94 (aver (subtypep simple-error 'error))
95 (apply #'error
96 simple-error
97 :format-control "~@<~A: ~2I~_~A~:>"
98 :format-arguments (list prefix-string (strerror errno))
99 other-condition-args))
101 ;;; Constructing shortish strings one character at a time. More efficient then
102 ;;; a string-stream, as can directly use simple-base-strings when applicable,
103 ;;; and if the maximum size is know doesn't need to copy the result at all --
104 ;;; but if the result is going to be HUGE, string-streams will win.
105 (defmacro with-push-char ((&key (element-type 'character) (initial-size 28)) &body body)
106 (with-unique-names (string size pointer)
107 `(let* ((,size ,initial-size)
108 (,string (make-array ,size :element-type ',element-type))
109 (,pointer 0))
110 (declare (type (integer 0 ,sb!xc:array-dimension-limit) ,size)
111 (type (integer 0 ,(1- sb!xc:array-dimension-limit)) ,pointer)
112 (type (simple-array ,element-type (*)) ,string))
113 (flet ((push-char (char)
114 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
115 (when (= ,pointer ,size)
116 (let ((old ,string))
117 (setf ,size (* 2 (+ ,size 2))
118 ,string (make-array ,size :element-type ',element-type))
119 (replace ,string old)))
120 (setf (char ,string ,pointer) char)
121 (incf ,pointer))
122 (get-pushed-string ()
123 (let ((string ,string)
124 (size ,pointer))
125 (setf ,size 0
126 ,pointer 0
127 ,string ,(coerce "" `(simple-array ,element-type (*))))
128 ;; This is really local, so we can be destructive!
129 (%shrink-vector string size)
130 string)))
131 ,@body))))
133 ;;; The smallest power of two that is equal to or greater than X.
134 (defun power-of-two-ceiling (x)
135 (declare (index x))
136 (ash 1 (integer-length (1- x))))