/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 | 549k | #define DCHECK(cond) assert(cond) |
18 | | #endif |
19 | | |
20 | | // TODO: all assert() should be DCHECK() or CHECK() |
21 | 634 | #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 | 337 | inline void log(const char* fmt, ...) { |
38 | 337 | va_list args; |
39 | 337 | va_start(args, fmt); |
40 | 337 | vfprintf(stderr, fmt, args); |
41 | 337 | va_end(args); |
42 | 337 | fputs("\n", stderr); |
43 | 337 | } |
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 | 36 | #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 | 615 | if (begin < 0) { \ |
73 | 143 | begin += len; \ |
74 | 143 | if (begin < 0) { \ |
75 | 36 | begin = 0; \ |
76 | 36 | } \ |
77 | 472 | } else if (begin > len) { \ |
78 | 38 | begin = len; \ |
79 | 38 | } \ |
80 | 615 | if (end < 0) { \ |
81 | 144 | end += len; \ |
82 | 144 | if (end < 0) { \ |
83 | 36 | end = 0; \ |
84 | 36 | } \ |
85 | 471 | } else if (end > len) { \ |
86 | 36 | end = len; \ |
87 | 36 | } \ |
88 | 615 | if (begin > end) { \ |
89 | 124 | end = begin; \ |
90 | 124 | } |
91 | | |
92 | | #endif // COMMON_H |