cpp

Coverage Report

Created: 2024-07-21 16:42

/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
11.6k
  #define DCHECK(cond) assert(cond)
18
#endif
19
20
// TODO: all assert() should be DCHECK() or CHECK()
21
120
#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
321
inline void log(const char* fmt, ...) {
38
321
  va_list args;
39
321
  va_start(args, fmt);
40
321
  vfprintf(stderr, fmt, args);
41
321
  va_end(args);
42
321
  fputs("\n", stderr);
43
321
}
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
0
inline size_t aligned(size_t n) {
55
0
  // https://stackoverflow.com/questions/2022179/c-quick-calculation-of-next-multiple-of-4
56
0
  // return (n + 7) & ~7;
57
0
  return (n + kMask) & ~kMask;
58
0
}
59
60
18
#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
5
  if (begin < 0) {                    \
73
0
    begin += len;                     \
74
0
    if (begin < 0) {                  \
75
0
      begin = 0;                      \
76
0
    }                                 \
77
5
  } else if (begin > len) {           \
78
0
    begin = len;                      \
79
0
  }                                   \
80
5
  if (end < 0) {                      \
81
0
    end += len;                       \
82
0
    if (end < 0) {                    \
83
0
      end = 0;                        \
84
0
    }                                 \
85
5
  } else if (end > len) {             \
86
0
    end = len;                        \
87
0
  }                                   \
88
5
  if (begin > end) {                  \
89
0
    end = begin;                      \
90
0
  }
91
92
#endif  // COMMON_H