aboutsummaryrefslogtreecommitdiffstats
path: root/mps/code/misc.h
diff options
context:
space:
mode:
authorNick Barnes2001-10-31 14:40:56 +0000
committerNick Barnes2001-10-31 14:40:56 +0000
commit7acfca905d76140f4cc0b09c9a12de237de364cd (patch)
tree3ed8babfa3a73d30f29e08ca5d5adcda4ca4e826 /mps/code/misc.h
parentb7ce4893f9902d57cd67ac9a92fa6c3d5a8fc833 (diff)
downloademacs-7acfca905d76140f4cc0b09c9a12de237de364cd.tar.gz
emacs-7acfca905d76140f4cc0b09c9a12de237de364cd.zip
Branch imports for masters.
Copied from Perforce Change: 23678 ServerID: perforce.ravenbrook.com
Diffstat (limited to 'mps/code/misc.h')
-rw-r--r--mps/code/misc.h190
1 files changed, 190 insertions, 0 deletions
diff --git a/mps/code/misc.h b/mps/code/misc.h
new file mode 100644
index 00000000000..333f2214389
--- /dev/null
+++ b/mps/code/misc.h
@@ -0,0 +1,190 @@
1/* impl.h.misc: MISCELLANEOUS DEFINITIONS
2 *
3 * $HopeName: MMsrc!misc.h(trunk.26) $
4 * Copyright (C) 1999 Harlequin Limited. All rights reserved.
5 *
6 * Small general things which are useful for C but aren't part of the
7 * memory manager itself. The only reason that this file exists is
8 * that these things are too small and trivial to be put in their own
9 * headers. If they ever become non-trivial they should be moved out.
10 */
11
12#ifndef misc_h
13#define misc_h
14
15#include <stddef.h>
16
17
18typedef int Bool; /* design.mps.type.bool */
19enum {
20 FALSE = 0,
21 TRUE = 1
22};
23
24
25/* offsetof -- offset of field within structure
26 *
27 * .hack.offsetof: On platform.sus8lc the offsetof macro is not defined
28 * (because LCC does not bother fixing up SunOS's broken header files).
29 * We define it here using normal C constructs. This hack is only
30 * required on platform.sus8lc and no other platforms. See
31 * change.mps.tracer2.170226
32 */
33
34#ifdef MPS_PF_SUS8LC
35#ifdef offsetof
36#error "offsetof was unexpectedly already defined on platform SUS8LC"
37#else
38#define offsetof(type, field) ((size_t)(((char *)&((type *)0)->field) \
39 - (char *)0))
40#endif /* offsetof */
41#endif /* MPS_PF_SUS8LC */
42
43
44/* SrcId -- source identification
45 *
46 * Every C source file should start with a SRCID declaration to
47 * create a local static source identification structure. This
48 * is used by other macros (particularly assertions) and can be
49 * used to reverse engineer binary deliverables.
50 */
51
52typedef const struct SrcIdStruct *SrcId;
53typedef const struct SrcIdStruct {
54 const char *file;
55 const char *hopename;
56 const char *build_date;
57 const char *build_time;
58} SrcIdStruct;
59
60#define SRCID(id, hopename) \
61 static SrcIdStruct FileSrcIdStruct = \
62 {__FILE__, (hopename), __DATE__, __TIME__}; \
63 SrcId id ## SrcId = &FileSrcIdStruct
64
65
66/* BEGIN and END -- statement brackets
67 *
68 * BEGIN and END can be used to bracket multi-statement blocks which
69 * will be followed by a semicolon, such as multi-statement macros.
70 * BEGIN and END should be used to bracket ALL multi-statement macros.
71 * The block, with its semicolon, still counts as a single statement.
72 * This ensures that such macros can be used in all statement contexts,
73 * including in the first branch of an if() statement which has an else
74 * clause.
75 */
76
77#define BEGIN do {
78#define END } while(0)
79
80
81
82/* RVALUE -- for method-style macros
83 *
84 * RVALUE is used to enclose the expansion of a macro that must not be
85 * used as an lvalue, e.g. a getter method.
86 */
87
88#define RVALUE(expr) ((void)0, (expr))
89
90/* NOOP -- null statement
91 *
92 * Do not be tempted to use NULL, or just semicolon as the null
93 * statement. These items are dangerously ambigous and could cause
94 * subtle bugs if misplaced. NOOP is a macro which is guaranteed to
95 * cause an error if it is not used in a statement context.
96 */
97
98#define NOOP do {} while(0)
99
100
101/* STR -- expands into a string of the expansion of the argument
102 *
103 * E.g., if we have:
104 * #define a b
105 * STR(a) will expand into "b".
106 */
107
108#define STR_(x) #x
109#define STR(x) STR_(x)
110
111
112/* DISCARD -- discards an expression, but checks syntax
113 *
114 * The argument is an expression; the expansion followed by a semicolon
115 * is syntactically a statement (to avoid it being used in computation).
116 *
117 * .discard: DISCARD uses sizeof so that the expression is not evaluated
118 * and yet the compiler will check that it is a valid expression. The
119 * conditional is compared with zero so it can designate a bitfield object.
120 */
121
122#define DISCARD(expr) \
123 BEGIN \
124 (void)sizeof((expr)!=0); \
125 END
126
127
128/* DISCARD_STAT -- discards a statement, but checks syntax
129 *
130 * The argument is a statement; the expansion followed by a semicolon
131 * is syntactically a statement.
132 */
133
134#define DISCARD_STAT(stat) \
135 BEGIN \
136 if (0) stat; \
137 END
138
139
140/* UNUSED -- declare parameter unused
141 *
142 * This macro supresses warnings about unused parameters. It should be
143 * applied to the parameter at the beginning of the body of the
144 * procedure.
145 *
146 * The cast to void appears to work for GCC, MSVC, and CodeWarrior.
147 * It's a shame there's no way to ensure that the parameter won't be
148 * used. We could scramble it, but that's undesirable in release
149 * versions.
150 */
151
152#define UNUSED(param) ((void)param)
153
154
155/* PARENT -- parent structure
156 *
157 * Given a pointer to a field of a structure this returns a pointer to
158 * the main structure. PARENT(foo_t, x, foo->x) == foo.
159 *
160 * This macro is thread-safe. design.mps.misc.parent.thread-safe
161 */
162
163#define PARENT(type, field, p) \
164 ((type *)((char *)(p) - offsetof(type, field)))
165
166
167/* Bit Sets -- sets of integers in [0,N-1].
168 *
169 * Can be used on any unsigned integral type, ty. These definitions
170 * are _syntactic_, hence macroid, hence upper case
171 * (guide.c.naming.macro.special).
172 */
173
174#define BS_EMPTY(ty) ((ty)0)
175#define BS_COMP(s) (~(s))
176#define BS_UNIV(ty) BS_COMP(BS_EMPTY(ty))
177#define BS_SINGLE(ty, i) ((ty)1 << (i))
178#define BS_IS_MEMBER(s, i) (((s) >> (i)) & 1)
179#define BS_UNION(s1, s2) ((s1) | (s2))
180#define BS_ADD(ty, s, i) BS_UNION((s), BS_SINGLE(ty, (i)))
181#define BS_INTER(s1, s2) ((s1) & (s2))
182#define BS_DIFF(s1, s2) BS_INTER((s1), BS_COMP(s2))
183#define BS_DEL(ty, s, i) BS_DIFF((s), BS_SINGLE(ty, (i)))
184#define BS_SUPER(s1, s2) (BS_INTER((s1), (s2)) == (s2))
185#define BS_SUB(s1, s2) BS_SUPER((s2), (s1))
186#define BS_IS_SINGLE(s) (((s) & ((s)-1)) == 0)
187#define BS_SYM_DIFF(s1, s2) ((s1) ^ (s2))
188
189
190#endif /* misc_h */