OILS / mycpp / bump_leak_heap.h View on Github | oils.pub

79 lines, 48 significant
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
16class BumpLeakHeap {
17 public:
18 // reserve 32 frames to start
19 BumpLeakHeap() {
20 }
21
22 void Init() {
23 }
24 void Init(int gc_threshold) {
25 }
26
27 // the BumpLeakHeap doesn't need rooting, but provide the option to
28 // approximate its costs.
29 void PushRoot(RawObject** p) {
30#ifdef BUMP_ROOT
31 roots_.push_back(p);
32#endif
33 }
34 void PopRoot() {
35#ifdef BUMP_ROOT
36 roots_.pop_back();
37#endif
38 }
39
40 void RootGlobalVar(void* root) {
41 }
42
43 void* Allocate(size_t num_bytes);
44 void* Reallocate(void* p, size_t num_bytes);
45 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 return -1; // no collection attempted
51 }
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
76extern BumpLeakHeap gHeap;
77#endif
78
79#endif // MYCPP_BUMP_LEAK_HEAP_H