OILS / mycpp / bump_leak_heap.h View on Github | oilshell.org

78 lines, 47 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
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
75extern BumpLeakHeap gHeap;
76#endif
77
78#endif // MYCPP_BUMP_LEAK_HEAP_H