mycpp

Coverage Report

Created: 2025-05-12 20:29

/home/uke/oil/mycpp/bump_leak_heap.h
Line
Count
Source
1
// mycpp/bump_leak_heap.h: Leaky Bump Allocator
2
3
#ifndef MYCPP_BUMP_LEAK_HEAP_H
4
#define MYCPP_BUMP_LEAK_HEAP_H
5
6
#include <stdint.h>  // int64_t
7
8
#ifdef BUMP_ROOT
9
  #include <algorithm>  // max()
10
  #include <vector>
11
#endif
12
13
#include "mycpp/common.h"
14
#include "mycpp/gc_obj.h"
15
16
class BumpLeakHeap {
17
 public:
18
  // reserve 32 frames to start
19
3
  BumpLeakHeap() {
20
3
  }
21
22
2
  void Init() {
23
2
  }
24
1
  void Init(int gc_threshold) {
25
1
  }
26
27
  // the BumpLeakHeap doesn't need rooting, but provide the option to
28
  // approximate its costs.
29
1
  void PushRoot(RawObject** p) {
30
#ifdef BUMP_ROOT
31
    roots_.push_back(p);
32
#endif
33
1
  }
34
1
  void PopRoot() {
35
#ifdef BUMP_ROOT
36
    roots_.pop_back();
37
#endif
38
1
  }
39
40
1
  void RootGlobalVar(void* root) {
41
1
  }
42
43
  void* Allocate(size_t num_bytes);
44
  void* Reallocate(void* p, size_t num_bytes);
45
1
  int MaybeCollect() {
46
#ifdef BUMP_ROOT
47
    // Do some computation with the roots
48
    max_roots_ = std::max(max_roots_, static_cast<int>(roots_.size()));
49
#endif
50
1
    return -1;  // no collection attempted
51
1
  }
52
53
  void PrintStats(int fd);
54
  void PrintShortStats();
55
56
  void CleanProcessExit();
57
  void ProcessExit();
58
59
  bool is_initialized_ = true;  // mark/sweep doesn't need to be initialized
60
61
  // In number of live objects, since we aren't keeping track of total bytes
62
  int gc_threshold_;
63
  int mem_pos_ = 0;
64
65
  // Cumulative stats
66
  int num_allocated_ = 0;
67
  int64_t bytes_allocated_ = 0;  // avoid overflow
68
69
#ifdef BUMP_ROOT
70
  std::vector<RawObject**> roots_;
71
  int max_roots_ = 0;
72
#endif
73
};
74
75
#ifdef BUMP_LEAK
76
extern BumpLeakHeap gHeap;
77
#endif
78
79
#endif  // MYCPP_BUMP_LEAK_HEAP_H