1.0.25.18: it's only SHOW
[sbcl.git] / src / runtime / runtime.h
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
5  * This software is derived from the CMU CL system, which was
6  * written at Carnegie Mellon University and released into the
7  * public domain. The software is in the public domain and is
8  * provided with absolutely no warranty. See the COPYING and CREDITS
9  * files for more information.
10  */
11
12 /* FIXME: Aren't symbols with underscore prefixes supposed to be
13  * reserved for system libraries? Perhaps rename stuff like this
14  * to names like INCLUDED_SBCL_RUNTIME_H. */
15 #ifndef _SBCL_RUNTIME_H_
16 #define _SBCL_RUNTIME_H_
17
18 #if defined(LISP_FEATURE_SB_THREAD)
19 #define thread_self pthread_self
20 #define thread_kill pthread_kill
21 #define thread_sigmask pthread_sigmask
22 #define thread_mutex_lock(l) pthread_mutex_lock(l)
23 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
24 #else
25 #define thread_self getpid
26 #define thread_kill kill
27 #define thread_sigmask sigprocmask
28 #define thread_mutex_lock(l) 0
29 #define thread_mutex_unlock(l) 0
30 #endif
31
32 /* #define QSHOW */ /* Enable low-level debugging output? */
33 /* #define QSHOW_SAFE */ /* Enable blocking interrupts for each SHOW. */
34
35 #ifdef QSHOW
36
37 #ifdef QSHOW_SAFE
38
39 #include <signal.h>
40 extern sigset_t blockable_sigset;
41
42 #define QSHOW_BLOCK                                             \
43         sigset_t oldset;                                        \
44         thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset);
45 #define QSHOW_UNBLOCK thread_sigmask(SIG_SETMASK,&oldset,0);
46 #else
47 #define QSHOW_BLOCK
48 #define QSHOW_UNBLOCK
49 #endif
50
51 #ifdef LISP_FEATURE_SB_THREAD
52 #define QSHOW_PREFIX fprintf(stderr, "%lu ", pthread_self());
53 #else
54 #define QSHOW_PREFIX
55 #endif
56
57 #define FSHOW(args)                                             \
58     do {                                                        \
59         QSHOW_BLOCK                                             \
60         QSHOW_PREFIX                                            \
61         fprintf args;                                           \
62         QSHOW_UNBLOCK                                           \
63     } while (0)
64 #define SHOW(string) FSHOW((stderr, "/%s\n", string))
65
66 #else
67
68 #define FSHOW(args)
69 #define SHOW(string)
70
71 #endif
72
73 /* Enable extra-verbose low-level debugging output for signals? (You
74  * probably don't want this unless you're trying to debug very early
75  * cold boot on a new machine, or one where you've just messed up
76  * signal handling.)
77  *
78  * Note: It may be that doing this is fundamentally unsound, since it
79  * causes output from signal handlers, and the i/o libraries aren't
80  * necessarily reentrant. But it can still be very convenient for
81  * figuring out what's going on when you have a signal handling
82  * problem.. */
83 #define QSHOW_SIGNALS 0
84
85 #if QSHOW_SIGNALS
86 #define FSHOW_SIGNAL FSHOW
87 #else
88 #define FSHOW_SIGNAL(args)
89 #endif
90
91 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
92  * in practice the "foo int" definitions work for all the machines
93  * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
94  * other non-32-bit machine we'll probably need real machine-dependent
95  * and OS-dependent definitions again. */
96 /* even on alpha, int happens to be 4 bytes.  long is longer. */
97 /* FIXME: these names really shouldn't reflect their length and this
98    is not quite right for some of the FFI stuff */
99 typedef unsigned long u64;
100 typedef signed long s64;
101 typedef unsigned int u32;
102 typedef signed int s32;
103
104 /* this is an integral type the same length as a machine pointer */
105 typedef unsigned long pointer_sized_uint_t ;
106
107 #include <sys/types.h>
108
109 #if defined(LISP_FEATURE_SB_THREAD)
110 #include <pthread.h>
111 typedef pthread_t os_thread_t;
112 #else
113 typedef pid_t os_thread_t;
114 #endif
115
116 /* FIXME: we do things this way because of the alpha32 port.  once
117    alpha64 has arrived, all this nastiness can go away */
118 #if 64 == N_WORD_BITS
119 #define LOW_WORD(c) ((pointer_sized_uint_t)c)
120 typedef unsigned long lispobj;
121 #else
122 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
123 /* fake it on alpha32 */
124 typedef unsigned int lispobj;
125 #endif
126
127 static inline int
128 lowtag_of(lispobj obj)
129 {
130     return obj & LOWTAG_MASK;
131 }
132
133 static inline int
134 widetag_of(lispobj obj)
135 {
136     return obj & WIDETAG_MASK;
137 }
138
139 static inline unsigned long
140 HeaderValue(lispobj obj)
141 {
142   return obj >> N_WIDETAG_BITS;
143 }
144
145 static inline struct cons *
146 CONS(lispobj obj)
147 {
148   return (struct cons *)(obj - LIST_POINTER_LOWTAG);
149 }
150
151 static inline struct symbol *
152 SYMBOL(lispobj obj)
153 {
154   return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
155 }
156
157 static inline struct fdefn *
158 FDEFN(lispobj obj)
159 {
160   return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
161 }
162
163 /* Is the Lisp object obj something with pointer nature (as opposed to
164  * e.g. a fixnum or character or unbound marker)? */
165 static inline int
166 is_lisp_pointer(lispobj obj)
167 {
168     return obj & 1;
169 }
170
171 #include "fixnump.h"
172
173 /* Is the Lisp object obj something with immediate nature (e.g. a
174  * fixnum or character or unbound marker)? */
175 static inline int
176 is_lisp_immediate(lispobj obj)
177 {
178     return (fixnump(obj)
179             || (widetag_of(obj) == CHARACTER_WIDETAG)
180 #if N_WORD_BITS == 64
181             || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
182 #endif
183             || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
184 }
185
186 /* Convert from a lispobj with type bits to a native (ordinary
187  * C/assembly) pointer to the beginning of the object. */
188 static inline lispobj *
189 native_pointer(lispobj obj)
190 {
191     return (lispobj *) ((pointer_sized_uint_t) (obj & ~LOWTAG_MASK));
192 }
193
194 /* inverse operation: create a suitably tagged lispobj from a native
195  * pointer or integer.*/
196 static inline lispobj
197 make_lispobj(void *o, int low_tag)
198 {
199     return LOW_WORD(o) | low_tag;
200 }
201
202 static inline lispobj
203 make_fixnum(long n)
204 {
205     return n << N_FIXNUM_TAG_BITS;
206 }
207
208 static inline long
209 fixnum_value(lispobj n)
210 {
211     return n >> N_FIXNUM_TAG_BITS;
212 }
213
214 #if defined(LISP_FEATURE_WIN32)
215 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
216  * shlobj.h.
217  *
218  * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
219  * or get rid of our own boolean type.  If the boolean type is only used in
220  * the runtime, and never passed to Lisp, then it doesn't matter which one
221  * we use.
222  */
223 #define boolean rpcndr_boolean
224 #include <shlobj.h>
225 #undef boolean
226 #endif
227 typedef int boolean;
228
229 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
230  * "this function never returns". This is the way that you do it
231  * in GCC later than version 2.5 or so. */
232 #if defined(__GNUC__)
233 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
234 #define never_returns __attribute__ ((noreturn))
235 #else
236 #define never_returns
237 #endif
238 #else
239 #define never_returns
240 #endif
241
242 extern void *successful_malloc (size_t size);
243 extern char *copied_string (char *string);
244
245 #define RUNTIME_OPTIONS_MAGIC 0x31EBF355
246 /* 1 for magic, 1 for boolean, 2 for struct runtime_options fields */
247 #define RUNTIME_OPTIONS_WORDS (1 + 1 + 2)
248
249 struct runtime_options {
250     size_t dynamic_space_size;
251     size_t thread_control_stack_size;
252 };
253
254 #endif /* _SBCL_RUNTIME_H_ */