mycpp

Coverage Report

Created: 2024-06-09 04:56

/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
55
  void CleanProcessExit();
56
  void ProcessExit();
57
58
  bool is_initialized_ = true;  // mark/sweep doesn't need to be initialized
59
60
  // In number of live objects, since we aren't keeping track of total bytes
61
  int gc_threshold_;
62
  int mem_pos_ = 0;
63
64
  // Cumulative stats
65
  int num_allocated_ = 0;
66
  int64_t bytes_allocated_ = 0;  // avoid overflow
67
68
#ifdef BUMP_ROOT
69
  std::vector<RawObject**> roots_;
70
  int max_roots_ = 0;
71
#endif
72
};
73
74
#ifdef BUMP_LEAK
75
extern BumpLeakHeap gHeap;
76
#endif
77
78
#endif  // MYCPP_BUMP_LEAK_HEAP_H