cpp

Coverage Report

Created: 2023-11-29 23:45

/home/andy/git/oilshell/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
234k
  #define DCHECK(cond) assert(cond)
18
#endif
19
20
// TODO: all assert() should be DCHECK() or CHECK()
21
1.61k
#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
773
inline void log(const char* fmt, ...) {
38
773
  va_list args;
39
773
  va_start(args, fmt);
40
773
  vfprintf(stderr, fmt, args);
41
773
  va_end(args);
42
773
  fputs("\n", stderr);
43
773
}
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
144
#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
#endif  // COMMON_H