9905632f37c30450590a3a1d2769c240a3949e2d
[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 /*#define QSHOW */ /* Enable low-level debugging output? */
19
20 #ifdef QSHOW
21 #define FSHOW(args) fprintf args
22 #define SHOW(string) FSHOW((stderr, "/%s\n", string))
23 #else
24 #define FSHOW(args)
25 #define SHOW(string)
26 #endif
27
28 /* Enable extra-verbose low-level debugging output for signals? (You
29  * probably don't want this unless you're trying to debug very early
30  * cold boot on a new machine, or one where you've just messed up
31  * signal handling.)
32  *
33  * Note: It may be that doing this is fundamentally unsound, since it
34  * causes output from signal handlers, and the i/o libraries aren't
35  * necessarily reentrant. But it can still be very convenient for
36  * figuring out what's going on when you have a signal handling
37  * problem.. */
38 #define QSHOW_SIGNALS 0
39
40 #if QSHOW_SIGNALS
41 #define FSHOW_SIGNAL FSHOW
42 #else
43 #define FSHOW_SIGNAL(args)
44 #endif
45
46 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
47  * in practice the "foo int" definitions work for all the machines
48  * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
49  * other non-32-bit machine we'll probably need real machine-dependent
50  * and OS-dependent definitions again. */
51 /* even on alpha, int happens to be 4 bytes.  long is longer. */
52 /* FIXME: these names really shouldn't reflect their length and this
53    is not quite right for some of the FFI stuff */
54 typedef unsigned long u64;
55 typedef signed long s64;
56 typedef unsigned int u32;
57 typedef signed int s32;
58
59 /* this is an integral type the same length as a machine pointer */
60 typedef unsigned long pointer_sized_uint_t ;
61
62 #include <sys/types.h>
63
64 #if defined(LISP_FEATURE_SB_THREAD)
65 #include <pthread.h>
66 typedef pthread_t os_thread_t;
67 #else
68 typedef pid_t os_thread_t;
69 #endif
70
71 /* FIXME: we do things this way because of the alpha32 port.  once
72    alpha64 has arrived, all this nastiness can go away */
73 #if 64 == N_WORD_BITS
74 #define LOW_WORD(c) ((pointer_sized_uint_t)c)
75 typedef unsigned long lispobj;
76 #else
77 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
78 /* fake it on alpha32 */
79 typedef unsigned int lispobj;
80 #endif
81
82 static inline int
83 lowtag_of(lispobj obj)
84 {
85     return obj & LOWTAG_MASK;
86 }
87
88 static inline int
89 widetag_of(lispobj obj)
90 {
91     return obj & WIDETAG_MASK;
92 }
93
94 static inline unsigned long
95 HeaderValue(lispobj obj)
96 {
97   return obj >> N_WIDETAG_BITS;
98 }
99
100 static inline struct cons *
101 CONS(lispobj obj)
102 {
103   return (struct cons *)(obj - LIST_POINTER_LOWTAG);
104 }
105
106 static inline struct symbol *
107 SYMBOL(lispobj obj)
108 {
109   return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
110 }
111
112 static inline struct fdefn *
113 FDEFN(lispobj obj)
114 {
115   return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
116 }
117
118 /* Is the Lisp object obj something with pointer nature (as opposed to
119  * e.g. a fixnum or character or unbound marker)? */
120 static inline int
121 is_lisp_pointer(lispobj obj)
122 {
123     return obj & 1;
124 }
125
126 #include "fixnump.h"
127
128 /* Is the Lisp object obj something with immediate nature (e.g. a
129  * fixnum or character or unbound marker)? */
130 static inline int
131 is_lisp_immediate(lispobj obj)
132 {
133     return (fixnump(obj)
134             || (widetag_of(obj) == CHARACTER_WIDETAG)
135 #if N_WORD_BITS == 64
136             || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
137 #endif
138             || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
139 }
140
141 /* Convert from a lispobj with type bits to a native (ordinary
142  * C/assembly) pointer to the beginning of the object. */
143 static inline lispobj *
144 native_pointer(lispobj obj)
145 {
146     return (lispobj *) ((pointer_sized_uint_t) (obj & ~LOWTAG_MASK));
147 }
148
149 /* inverse operation: create a suitably tagged lispobj from a native
150  * pointer or integer.*/
151 static inline lispobj
152 make_lispobj(void *o, int low_tag)
153 {
154     return LOW_WORD(o) | low_tag;
155 }
156
157 static inline lispobj
158 make_fixnum(long n)
159 {
160     return n << N_FIXNUM_TAG_BITS;
161 }
162
163 static inline long
164 fixnum_value(lispobj n)
165 {
166     return n >> N_FIXNUM_TAG_BITS;
167 }
168
169 #if defined(LISP_FEATURE_WIN32)
170 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
171  * shlobj.h.
172  *
173  * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
174  * or get rid of our own boolean type.  If the boolean type is only used in
175  * the runtime, and never passed to Lisp, then it doesn't matter which one
176  * we use.
177  */
178 #define boolean rpcndr_boolean
179 #include <shlobj.h>
180 #undef boolean
181 #endif
182 typedef int boolean;
183
184 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
185  * "this function never returns". This is the way that you do it
186  * in GCC later than version 2.5 or so. */
187 #if defined(__GNUC__)
188 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
189 #define never_returns __attribute__ ((noreturn))
190 #else
191 #define never_returns
192 #endif
193 #else
194 #define never_returns
195 #endif
196
197 extern void *successful_malloc (size_t size);
198 extern char *copied_string (char *string);
199
200 #endif /* _SBCL_RUNTIME_H_ */