5768f8a49f6556666faf29da2aac5ab891e2c727
[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 #ifdef 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     return obj & LOWTAG_MASK;
85 }
86
87 static inline int
88 widetag_of(lispobj obj) {
89     return obj & WIDETAG_MASK;
90 }
91
92 static inline unsigned long
93 HeaderValue(lispobj obj)
94 {
95   return obj >> N_WIDETAG_BITS;
96 }
97
98 static inline struct cons *
99 CONS(lispobj obj)
100 {
101   return (struct cons *)(obj - LIST_POINTER_LOWTAG);
102 }
103
104 static inline struct symbol *
105 SYMBOL(lispobj obj)
106 {
107   return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
108 }
109
110 static inline struct fdefn *
111 FDEFN(lispobj obj)
112 {
113   return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
114 }
115
116 /* Is the Lisp object obj something with pointer nature (as opposed to
117  * e.g. a fixnum or character or unbound marker)? */
118 static inline int
119 is_lisp_pointer(lispobj obj)
120 {
121     return obj & 1;
122 }
123
124 /* Convert from a lispobj with type bits to a native (ordinary
125  * C/assembly) pointer to the beginning of the object. */
126 static inline lispobj *
127 native_pointer(lispobj obj)
128 {
129     return (lispobj *) ((pointer_sized_uint_t) (obj & ~LOWTAG_MASK));
130 }
131 /* inverse operation: create a suitably tagged lispobj from a native
132  * pointer or integer.  Needs to be a macro due to the tedious C type
133  * system */
134 #define make_lispobj(o,low_tag) ((lispobj)(LOW_WORD(o)|low_tag))
135
136 /* FIXME: There seems to be no reason that make_fixnum and fixnum_value
137  * can't be implemented as (possibly inline) functions. */
138 #define make_fixnum(n) ((lispobj)((n)<<N_FIXNUM_TAG_BITS))
139 #define fixnum_value(n) (((long)n)>>N_FIXNUM_TAG_BITS)
140
141 /* Too bad ANSI C doesn't define "bool" as C++ does.. */
142 typedef int boolean;
143
144 /* This only works for static symbols. */
145 /* FIXME: should be called StaticSymbolFunction, right? */
146 #define SymbolFunction(sym) \
147     (((struct fdefn *)(native_pointer(SymbolValue(sym,0))))->fun)
148
149 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
150  * "this function never returns". This is the way that you do it
151  * in GCC later than version 2.5 or so. */
152 #if defined(__GNUC__)
153 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
154 #define never_returns __attribute__ ((noreturn))
155 #else
156 #define never_returns
157 #endif
158 #else
159 #define never_returns
160 #endif
161
162 extern void *successful_malloc (size_t size);
163 extern char *copied_string (char *string);
164
165 #endif /* _SBCL_RUNTIME_H_ */