/home/uke/oil/mycpp/common.h
Line | Count | Source (jump to first uncovered line) |
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 | 1.15M | #define DCHECK(cond) assert(cond) |
18 | | #endif |
19 | | |
20 | | // TODO: all assert() should be DCHECK() or CHECK() |
21 | 1.17k | #define CHECK(cond) assert(cond) |
22 | | |
23 | 0 | #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 | 991 | inline void log(const char* fmt, ...) { |
38 | 991 | va_list args; |
39 | 991 | va_start(args, fmt); |
40 | 991 | vfprintf(stderr, fmt, args); |
41 | 991 | va_end(args); |
42 | 991 | fputs("\n", stderr); |
43 | 991 | } |
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 | 4 | 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 | 4 | return (n + kMask) & ~kMask; |
58 | 4 | } |
59 | | |
60 | 150 | #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 | 1.24k | if (begin < 0) { \ |
73 | 287 | begin += len; \ |
74 | 287 | if (begin < 0) { \ |
75 | 72 | begin = 0; \ |
76 | 72 | } \ |
77 | 960 | } else if (begin > len) { \ |
78 | 76 | begin = len; \ |
79 | 76 | } \ |
80 | 1.24k | if (end < 0) { \ |
81 | 290 | end += len; \ |
82 | 290 | if (end < 0) { \ |
83 | 72 | end = 0; \ |
84 | 72 | } \ |
85 | 957 | } else if (end > len) { \ |
86 | 72 | end = len; \ |
87 | 72 | } \ |
88 | 1.24k | if (begin > end) { \ |
89 | 248 | end = begin; \ |
90 | 248 | } |
91 | | |
92 | | #endif // COMMON_H |