1 | // mycpp/common.h
|
2 | //
|
3 | // A grab bag of definitions needed in multiple places.
|
4 |
|
5 | #ifndef COMMON_H
|
6 | #define COMMON_H
|
7 |
|
8 | #include <assert.h> // assert()
|
9 | #include <stdarg.h> // va_list, etc.
|
10 | #include <stddef.h> // max_align_t
|
11 | #include <stdio.h> // vprintf
|
12 |
|
13 | // opt variants pass -D OPTIMIZED
|
14 | #ifdef OPTIMIZED
|
15 | #define DCHECK(cond)
|
16 | #else
|
17 | #define DCHECK(cond) assert(cond)
|
18 | #endif
|
19 |
|
20 | // TODO: all assert() should be DCHECK() or CHECK()
|
21 | #define CHECK(cond) assert(cond)
|
22 |
|
23 | #define FAIL(reason) assert(false)
|
24 |
|
25 | enum Reason { kShouldNotGetHere, kNotImplemented };
|
26 |
|
27 | // Workaround for macros that take templates
|
28 | #define COMMA ,
|
29 |
|
30 | // Prevent silent copies
|
31 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
32 | TypeName(TypeName&) = delete; \
|
33 | void operator=(TypeName) = delete;
|
34 |
|
35 | // log() is for hand-written code, not generated
|
36 |
|
37 | inline void log(const char* fmt, ...) {
|
38 | va_list args;
|
39 | va_start(args, fmt);
|
40 | vfprintf(stderr, fmt, args);
|
41 | va_end(args);
|
42 | fputs("\n", stderr);
|
43 | }
|
44 |
|
45 | // I'm not sure why this matters but we get crashes when aligning to 8 bytes.
|
46 | // That is annoying.
|
47 | // Example: we get a crash in cpp/frontend_flag_spec.cc
|
48 | // auto out = new flag_spec::_FlagSpecAndMore();
|
49 | //
|
50 | // https://stackoverflow.com/questions/52531695/int128-alignment-segment-fault-with-gcc-o-sse-optimize
|
51 | constexpr int kMask = alignof(max_align_t) - 1; // e.g. 15 or 7
|
52 |
|
53 | // Align returned pointers to the worst case of 8 bytes (64-bit pointers)
|
54 | inline size_t aligned(size_t n) {
|
55 | // https://stackoverflow.com/questions/2022179/c-quick-calculation-of-next-multiple-of-4
|
56 | // return (n + 7) & ~7;
|
57 | return (n + kMask) & ~kMask;
|
58 | }
|
59 |
|
60 | #define KiB(bytes) ((bytes)*1024)
|
61 | #define MiB(bytes) (KiB(bytes) * 1024)
|
62 | #define GiB(bytes) (MiB(bytes) * 1024)
|
63 |
|
64 | const int kMaxRoots = KiB(4);
|
65 |
|
66 | // Used by both Str and List slicing. 3 adjustments:
|
67 | // 1. begin: negative, or greater than len
|
68 | // 2. end: negative, or greater than len
|
69 | // 3. begin > end means empty slice
|
70 |
|
71 | #define SLICE_ADJUST(begin, end, len) \
|
72 | if (begin < 0) { \
|
73 | begin += len; \
|
74 | if (begin < 0) { \
|
75 | begin = 0; \
|
76 | } \
|
77 | } else if (begin > len) { \
|
78 | begin = len; \
|
79 | } \
|
80 | if (end < 0) { \
|
81 | end += len; \
|
82 | if (end < 0) { \
|
83 | end = 0; \
|
84 | } \
|
85 | } else if (end > len) { \
|
86 | end = len; \
|
87 | } \
|
88 | if (begin > end) { \
|
89 | end = begin; \
|
90 | }
|
91 |
|
92 | #endif // COMMON_H
|