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.
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
30 "A list of function designators which are called in an unspecified
31 order before creating a saved core image.
33 Unused by SBCL itself: reserved for user and applications.")
35 (defvar *init-hooks
* nil
37 "A list of function designators which are called in an unspecified
38 order when a saved core image starts up, after the system itself has
41 Unused by SBCL itself: reserved for user and applications.")
43 (defvar *exit-hooks
* nil
45 "A list of function designators which are called in an unspecified
46 order when SBCL process exits.
48 Unused by SBCL itself: reserved for user and applications.
50 Using (SB-EXT:EXIT :ABORT T), or calling exit(3) directly circumvents
54 ;;; Binary search for simple vectors
55 (defun binary-search (value seq
&key
(key #'identity
))
56 (declare (simple-vector seq
))
57 (labels ((recurse (start end
)
59 (let* ((i (+ start
(truncate (- end start
) 2)))
61 (key-value (funcall key elt
)))
62 (cond ((< value key-value
)
68 (recurse 0 (length seq
))))
70 (defun double-vector-binary-search (value vector
)
71 (declare (simple-vector vector
)
74 (labels ((recurse (start end
)
75 (declare (type index start end
))
77 (let* ((i (+ start
(truncate (- end start
) 2)))
78 (elt (svref vector
(truly-the index
(* 2 i
)))))
79 (declare (type integer elt
)
86 (svref vector
(truly-the index
(1+ (* 2 i
))))))))))
87 (recurse 0 (truncate (length vector
) 2))))
90 ;;;; helpers for C library calls
92 ;;; Signal a SIMPLE-CONDITION/ERROR condition associated with an ANSI C
93 ;;; errno problem, arranging for the condition's print representation
94 ;;; to be similar to the ANSI C perror(3) style.
95 (defun simple-perror (prefix-string
98 (simple-error 'simple-error
)
100 (declare (type symbol simple-error
))
101 (aver (subtypep simple-error
'simple-condition
))
102 (aver (subtypep simple-error
'error
))
105 :format-control
"~@<~A: ~2I~_~A~:>"
106 :format-arguments
(list prefix-string
(strerror errno
))
107 other-condition-args
))
109 ;;; Constructing shortish strings one character at a time. More efficient then
110 ;;; a string-stream, as can directly use simple-base-strings when applicable,
111 ;;; and if the maximum size is know doesn't need to copy the result at all --
112 ;;; but if the result is going to be HUGE, string-streams will win.
113 (defmacro with-push-char
((&key
(element-type 'character
) (initial-size 28)) &body body
)
114 (with-unique-names (string size pointer
)
115 `(let* ((,size
,initial-size
)
116 (,string
(make-array ,size
:element-type
',element-type
))
118 (declare (type (integer 0 ,sb
!xc
:array-dimension-limit
) ,size
)
119 (type (integer 0 ,(1- sb
!xc
:array-dimension-limit
)) ,pointer
)
120 (type (simple-array ,element-type
(*)) ,string
))
121 (flet ((push-char (char)
122 (declare (optimize (sb!c
::insert-array-bounds-checks
0)))
123 (when (= ,pointer
,size
)
125 (setf ,size
(* 2 (+ ,size
2))
126 ,string
(make-array ,size
:element-type
',element-type
))
127 (replace ,string old
)))
128 (setf (char ,string
,pointer
) char
)
130 (get-pushed-string ()
131 (let ((string ,string
)
135 ,string
,(coerce "" `(simple-array ,element-type
(*))))
136 ;; This is really local, so we can be destructive!
137 (%shrink-vector string size
)
141 ;;; The smallest power of two that is equal to or greater than X.
142 (defun power-of-two-ceiling (x)
144 (ash 1 (integer-length (1- x
))))