cpp

Coverage Report

Created: 2022-11-10 11:34

/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 <ctype.h>   // isalpha(), isdigit()
10
#include <limits.h>  // CHAR_BIT
11
#include <stdarg.h>  // va_list, etc.
12
#include <stddef.h>  // max_align_t
13
#include <stdint.h>  // uint8_t
14
#include <stdio.h>   // vprintf
15
#include <stdlib.h>
16
#include <string.h>  // strlen
17
18
#include <initializer_list>
19
20
0
#define NotImplemented() assert(!"Not Implemented")
21
0
#define InvalidCodePath() assert(!"Invalid Code Path")
22
23
// Workaround for macros that take templates
24
#define COMMA ,
25
26
// Prevent silent copies
27
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
28
  TypeName(TypeName&) = delete;            \
29
  void operator=(TypeName) = delete;
30
31
// log() is for hand-written code, not generated
32
33
277
inline void log(const char* fmt, ...) {
34
277
  va_list args;
35
277
  va_start(args, fmt);
36
277
  vfprintf(stderr, fmt, args);
37
277
  va_end(args);
38
277
  fputs("\n", stderr);
39
277
}
40
41
// I'm not sure why this matters but we get crashes when aligning to 8 bytes.
42
// That is annoying.
43
// Example: we get a crash in cpp/frontend_flag_spec.cc
44
// auto out = new flag_spec::_FlagSpecAndMore();
45
//
46
// https://stackoverflow.com/questions/52531695/int128-alignment-segment-fault-with-gcc-o-sse-optimize
47
constexpr int kMask = alignof(max_align_t) - 1;  // e.g. 15 or 7
48
49
// Align returned pointers to the worst case of 8 bytes (64-bit pointers)
50
1
inline size_t aligned(size_t n) {
51
  // https://stackoverflow.com/questions/2022179/c-quick-calculation-of-next-multiple-of-4
52
  // return (n + 7) & ~7;
53
1
  return (n + kMask) & ~kMask;
54
1
}
55
56
78
#define KiB(bytes) ((bytes) * 1024)
57
#define MiB(bytes) (KiB(bytes) * 1024)
58
#define GiB(bytes) (MiB(bytes) * 1024)
59
60
const int kMaxRoots = KiB(4);
61
62
#endif  // COMMON_H