/home/uke/oil/mycpp/mark_sweep_heap.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef MARKSWEEP_HEAP_H |
2 | | #define MARKSWEEP_HEAP_H |
3 | | |
4 | | #include <stdlib.h> |
5 | | |
6 | | #include <vector> |
7 | | |
8 | | #include "mycpp/common.h" |
9 | | #include "mycpp/gc_obj.h" |
10 | | |
11 | | class MarkSet { |
12 | | public: |
13 | 40 | MarkSet() : bits_() { |
14 | 40 | } |
15 | | |
16 | | // ReInit() must be called at the start of MarkObjects(). Allocate() should |
17 | | // keep track of the maximum object ID. |
18 | 119 | void ReInit(int max_obj_id) { |
19 | | // https://stackoverflow.com/questions/8848575/fastest-way-to-reset-every-value-of-stdvectorint-to-0 |
20 | 119 | std::fill(bits_.begin(), bits_.end(), 0); |
21 | 119 | int max_byte_index = (max_obj_id >> 3) + 1; // round up |
22 | | // log("ReInit max_byte_index %d", max_byte_index); |
23 | 119 | bits_.resize(max_byte_index); |
24 | 119 | } |
25 | | |
26 | | // Called by MarkObjects() |
27 | 59 | void Mark(int obj_id) { |
28 | 59 | DCHECK(obj_id >= 0); |
29 | | // log("obj id %d", obj_id); |
30 | 59 | DCHECK(!IsMarked(obj_id)); |
31 | 0 | int byte_index = obj_id >> 3; // 8 bits per byte |
32 | 59 | int bit_index = obj_id & 0b111; |
33 | | // log("byte_index %d %d", byte_index, bit_index); |
34 | 59 | bits_[byte_index] |= (1 << bit_index); |
35 | 59 | } |
36 | | |
37 | | // Called by Sweep() |
38 | 39.3k | bool IsMarked(int obj_id) { |
39 | 39.3k | DCHECK(obj_id >= 0); |
40 | 0 | int byte_index = obj_id >> 3; |
41 | 39.3k | int bit_index = obj_id & 0b111; |
42 | 39.3k | return bits_[byte_index] & (1 << bit_index); |
43 | 39.3k | } |
44 | | |
45 | 1 | void Debug() { |
46 | 1 | int n = bits_.size(); |
47 | 1 | dprintf(2, "[ "); |
48 | 4 | for (int i = 0; i < n; ++i) { |
49 | 3 | dprintf(2, "%02x ", bits_[i]); |
50 | 3 | } |
51 | 1 | dprintf(2, "] (%d bytes) \n", n); |
52 | 1 | dprintf(2, "[ "); |
53 | 1 | int num_bits = 0; |
54 | 4 | for (int i = 0; i < n; ++i) { |
55 | 27 | for (int j = 0; j < 8; ++j) { |
56 | 24 | int bit = (bits_[i] & (1 << j)) != 0; |
57 | 24 | dprintf(2, "%d", bit); |
58 | 24 | num_bits += bit; |
59 | 24 | } |
60 | 3 | } |
61 | 1 | dprintf(2, " ] (%d bits set)\n", num_bits); |
62 | 1 | } |
63 | | |
64 | | std::vector<uint8_t> bits_; // bit vector indexed by obj_id |
65 | | }; |
66 | | |
67 | | // A simple Pool allocator for allocating small objects. It maintains an ever |
68 | | // growing number of Blocks each consisting of a number of fixed size Cells. |
69 | | // Memory is handed out one Cell at a time. |
70 | | // Note: within the context of the Pool allocator we refer to object IDs as cell |
71 | | // IDs because in addition to identifying an object they're also used to index |
72 | | // into the Cell storage. |
73 | | template <int CellsPerBlock, size_t CellSize> |
74 | | class Pool { |
75 | | public: |
76 | | static constexpr size_t kMaxObjSize = CellSize; |
77 | | static constexpr int kBlockSize = CellSize * CellsPerBlock; |
78 | | |
79 | 27 | Pool() = default; _ZN4PoolILi682ELm24EEC2Ev Line | Count | Source | 79 | 12 | Pool() = default; |
_ZN4PoolILi341ELm48EEC2Ev Line | Count | Source | 79 | 12 | Pool() = default; |
Line | Count | Source | 79 | 2 | Pool() = default; |
Line | Count | Source | 79 | 1 | Pool() = default; |
|
80 | | |
81 | 2.78k | void* Allocate(int* obj_id) { |
82 | 2.78k | num_allocated_++; |
83 | | |
84 | 2.78k | if (!free_list_) { |
85 | | // Allocate a new Block and add every new Cell to the free list. |
86 | 27 | Block* block = static_cast<Block*>(malloc(sizeof(Block))); |
87 | 27 | blocks_.push_back(block); |
88 | 27 | bytes_allocated_ += kBlockSize; |
89 | 27 | num_free_ += CellsPerBlock; |
90 | | |
91 | | // The starting cell_id for Cells in this block. |
92 | 27 | int cell_id = (blocks_.size() - 1) * CellsPerBlock; |
93 | 11.2k | for (Cell& cell : block->cells) { |
94 | 11.2k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); |
95 | 11.2k | free_cell->id = cell_id++; |
96 | 11.2k | free_cell->next = free_list_; |
97 | 11.2k | free_list_ = free_cell; |
98 | 11.2k | } |
99 | 27 | } |
100 | | |
101 | 2.78k | FreeCell* cell = free_list_; |
102 | 2.78k | free_list_ = free_list_->next; |
103 | 2.78k | num_free_--; |
104 | 2.78k | *obj_id = cell->id; |
105 | 2.78k | return cell; |
106 | 2.78k | } _ZN4PoolILi682ELm24EE8AllocateEPi Line | Count | Source | 81 | 1.86k | void* Allocate(int* obj_id) { | 82 | 1.86k | num_allocated_++; | 83 | | | 84 | 1.86k | if (!free_list_) { | 85 | | // Allocate a new Block and add every new Cell to the free list. | 86 | 11 | Block* block = static_cast<Block*>(malloc(sizeof(Block))); | 87 | 11 | blocks_.push_back(block); | 88 | 11 | bytes_allocated_ += kBlockSize; | 89 | 11 | num_free_ += CellsPerBlock; | 90 | | | 91 | | // The starting cell_id for Cells in this block. | 92 | 11 | int cell_id = (blocks_.size() - 1) * CellsPerBlock; | 93 | 7.50k | for (Cell& cell : block->cells) { | 94 | 7.50k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 95 | 7.50k | free_cell->id = cell_id++; | 96 | 7.50k | free_cell->next = free_list_; | 97 | 7.50k | free_list_ = free_cell; | 98 | 7.50k | } | 99 | 11 | } | 100 | | | 101 | 1.86k | FreeCell* cell = free_list_; | 102 | 1.86k | free_list_ = free_list_->next; | 103 | 1.86k | num_free_--; | 104 | 1.86k | *obj_id = cell->id; | 105 | 1.86k | return cell; | 106 | 1.86k | } |
_ZN4PoolILi341ELm48EE8AllocateEPi Line | Count | Source | 81 | 914 | void* Allocate(int* obj_id) { | 82 | 914 | num_allocated_++; | 83 | | | 84 | 914 | if (!free_list_) { | 85 | | // Allocate a new Block and add every new Cell to the free list. | 86 | 11 | Block* block = static_cast<Block*>(malloc(sizeof(Block))); | 87 | 11 | blocks_.push_back(block); | 88 | 11 | bytes_allocated_ += kBlockSize; | 89 | 11 | num_free_ += CellsPerBlock; | 90 | | | 91 | | // The starting cell_id for Cells in this block. | 92 | 11 | int cell_id = (blocks_.size() - 1) * CellsPerBlock; | 93 | 3.75k | for (Cell& cell : block->cells) { | 94 | 3.75k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 95 | 3.75k | free_cell->id = cell_id++; | 96 | 3.75k | free_cell->next = free_list_; | 97 | 3.75k | free_list_ = free_cell; | 98 | 3.75k | } | 99 | 11 | } | 100 | | | 101 | 914 | FreeCell* cell = free_list_; | 102 | 914 | free_list_ = free_list_->next; | 103 | 914 | num_free_--; | 104 | 914 | *obj_id = cell->id; | 105 | 914 | return cell; | 106 | 914 | } |
_ZN4PoolILi2ELm32EE8AllocateEPi Line | Count | Source | 81 | 7 | void* Allocate(int* obj_id) { | 82 | 7 | num_allocated_++; | 83 | | | 84 | 7 | if (!free_list_) { | 85 | | // Allocate a new Block and add every new Cell to the free list. | 86 | 3 | Block* block = static_cast<Block*>(malloc(sizeof(Block))); | 87 | 3 | blocks_.push_back(block); | 88 | 3 | bytes_allocated_ += kBlockSize; | 89 | 3 | num_free_ += CellsPerBlock; | 90 | | | 91 | | // The starting cell_id for Cells in this block. | 92 | 3 | int cell_id = (blocks_.size() - 1) * CellsPerBlock; | 93 | 6 | for (Cell& cell : block->cells) { | 94 | 6 | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 95 | 6 | free_cell->id = cell_id++; | 96 | 6 | free_cell->next = free_list_; | 97 | 6 | free_list_ = free_cell; | 98 | 6 | } | 99 | 3 | } | 100 | | | 101 | 7 | FreeCell* cell = free_list_; | 102 | 7 | free_list_ = free_list_->next; | 103 | 7 | num_free_--; | 104 | 7 | *obj_id = cell->id; | 105 | 7 | return cell; | 106 | 7 | } |
_ZN4PoolILi1ELm32EE8AllocateEPi Line | Count | Source | 81 | 2 | void* Allocate(int* obj_id) { | 82 | 2 | num_allocated_++; | 83 | | | 84 | 2 | if (!free_list_) { | 85 | | // Allocate a new Block and add every new Cell to the free list. | 86 | 2 | Block* block = static_cast<Block*>(malloc(sizeof(Block))); | 87 | 2 | blocks_.push_back(block); | 88 | 2 | bytes_allocated_ += kBlockSize; | 89 | 2 | num_free_ += CellsPerBlock; | 90 | | | 91 | | // The starting cell_id for Cells in this block. | 92 | 2 | int cell_id = (blocks_.size() - 1) * CellsPerBlock; | 93 | 2 | for (Cell& cell : block->cells) { | 94 | 2 | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 95 | 2 | free_cell->id = cell_id++; | 96 | 2 | free_cell->next = free_list_; | 97 | 2 | free_list_ = free_cell; | 98 | 2 | } | 99 | 2 | } | 100 | | | 101 | 2 | FreeCell* cell = free_list_; | 102 | 2 | free_list_ = free_list_->next; | 103 | 2 | num_free_--; | 104 | 2 | *obj_id = cell->id; | 105 | 2 | return cell; | 106 | 2 | } |
|
107 | | |
108 | 79 | void PrepareForGc() { |
109 | 79 | DCHECK(!gc_underway_); |
110 | 0 | gc_underway_ = true; |
111 | 79 | mark_set_.ReInit(blocks_.size() * CellsPerBlock); |
112 | 79 | } _ZN4PoolILi682ELm24EE12PrepareForGcEv Line | Count | Source | 108 | 38 | void PrepareForGc() { | 109 | 38 | DCHECK(!gc_underway_); | 110 | 0 | gc_underway_ = true; | 111 | 38 | mark_set_.ReInit(blocks_.size() * CellsPerBlock); | 112 | 38 | } |
_ZN4PoolILi341ELm48EE12PrepareForGcEv Line | Count | Source | 108 | 38 | void PrepareForGc() { | 109 | 38 | DCHECK(!gc_underway_); | 110 | 0 | gc_underway_ = true; | 111 | 38 | mark_set_.ReInit(blocks_.size() * CellsPerBlock); | 112 | 38 | } |
_ZN4PoolILi2ELm32EE12PrepareForGcEv Line | Count | Source | 108 | 2 | void PrepareForGc() { | 109 | 2 | DCHECK(!gc_underway_); | 110 | 0 | gc_underway_ = true; | 111 | 2 | mark_set_.ReInit(blocks_.size() * CellsPerBlock); | 112 | 2 | } |
_ZN4PoolILi1ELm32EE12PrepareForGcEv Line | Count | Source | 108 | 1 | void PrepareForGc() { | 109 | 1 | DCHECK(!gc_underway_); | 110 | 0 | gc_underway_ = true; | 111 | 1 | mark_set_.ReInit(blocks_.size() * CellsPerBlock); | 112 | 1 | } |
|
113 | | |
114 | 61 | bool IsMarked(int cell_id) { |
115 | 61 | DCHECK(gc_underway_); |
116 | 0 | return mark_set_.IsMarked(cell_id); |
117 | 61 | } _ZN4PoolILi682ELm24EE8IsMarkedEi Line | Count | Source | 114 | 56 | bool IsMarked(int cell_id) { | 115 | 56 | DCHECK(gc_underway_); | 116 | 0 | return mark_set_.IsMarked(cell_id); | 117 | 56 | } |
_ZN4PoolILi341ELm48EE8IsMarkedEi Line | Count | Source | 114 | 5 | bool IsMarked(int cell_id) { | 115 | 5 | DCHECK(gc_underway_); | 116 | 0 | return mark_set_.IsMarked(cell_id); | 117 | 5 | } |
|
118 | | |
119 | 48 | void Mark(int cell_id) { |
120 | 48 | DCHECK(gc_underway_); |
121 | 0 | mark_set_.Mark(cell_id); |
122 | 48 | } _ZN4PoolILi682ELm24EE4MarkEi Line | Count | Source | 119 | 42 | void Mark(int cell_id) { | 120 | 42 | DCHECK(gc_underway_); | 121 | 0 | mark_set_.Mark(cell_id); | 122 | 42 | } |
_ZN4PoolILi341ELm48EE4MarkEi Line | Count | Source | 119 | 5 | void Mark(int cell_id) { | 120 | 5 | DCHECK(gc_underway_); | 121 | 0 | mark_set_.Mark(cell_id); | 122 | 5 | } |
_ZN4PoolILi1ELm32EE4MarkEi Line | Count | Source | 119 | 1 | void Mark(int cell_id) { | 120 | 1 | DCHECK(gc_underway_); | 121 | 0 | mark_set_.Mark(cell_id); | 122 | 1 | } |
|
123 | | |
124 | 79 | void Sweep() { |
125 | 79 | DCHECK(gc_underway_); |
126 | | // Iterate over every Cell linking the free ones into a new free list. |
127 | 0 | num_free_ = 0; |
128 | 79 | free_list_ = nullptr; |
129 | 79 | int cell_id = 0; |
130 | 79 | for (Block* block : blocks_) { |
131 | 38.5k | for (Cell& cell : block->cells) { |
132 | 38.5k | if (!mark_set_.IsMarked(cell_id)) { |
133 | 38.4k | num_free_++; |
134 | 38.4k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); |
135 | 38.4k | free_cell->id = cell_id; |
136 | 38.4k | free_cell->next = free_list_; |
137 | 38.4k | free_list_ = free_cell; |
138 | 38.4k | } |
139 | 38.5k | cell_id++; |
140 | 38.5k | } |
141 | 78 | } |
142 | 79 | gc_underway_ = false; |
143 | 79 | } _ZN4PoolILi682ELm24EE5SweepEv Line | Count | Source | 124 | 38 | void Sweep() { | 125 | 38 | DCHECK(gc_underway_); | 126 | | // Iterate over every Cell linking the free ones into a new free list. | 127 | 0 | num_free_ = 0; | 128 | 38 | free_list_ = nullptr; | 129 | 38 | int cell_id = 0; | 130 | 38 | for (Block* block : blocks_) { | 131 | 25.9k | for (Cell& cell : block->cells) { | 132 | 25.9k | if (!mark_set_.IsMarked(cell_id)) { | 133 | 25.8k | num_free_++; | 134 | 25.8k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 135 | 25.8k | free_cell->id = cell_id; | 136 | 25.8k | free_cell->next = free_list_; | 137 | 25.8k | free_list_ = free_cell; | 138 | 25.8k | } | 139 | 25.9k | cell_id++; | 140 | 25.9k | } | 141 | 38 | } | 142 | 38 | gc_underway_ = false; | 143 | 38 | } |
_ZN4PoolILi341ELm48EE5SweepEv Line | Count | Source | 124 | 38 | void Sweep() { | 125 | 38 | DCHECK(gc_underway_); | 126 | | // Iterate over every Cell linking the free ones into a new free list. | 127 | 0 | num_free_ = 0; | 128 | 38 | free_list_ = nullptr; | 129 | 38 | int cell_id = 0; | 130 | 38 | for (Block* block : blocks_) { | 131 | 12.6k | for (Cell& cell : block->cells) { | 132 | 12.6k | if (!mark_set_.IsMarked(cell_id)) { | 133 | 12.6k | num_free_++; | 134 | 12.6k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 135 | 12.6k | free_cell->id = cell_id; | 136 | 12.6k | free_cell->next = free_list_; | 137 | 12.6k | free_list_ = free_cell; | 138 | 12.6k | } | 139 | 12.6k | cell_id++; | 140 | 12.6k | } | 141 | 37 | } | 142 | 38 | gc_underway_ = false; | 143 | 38 | } |
_ZN4PoolILi2ELm32EE5SweepEv Line | Count | Source | 124 | 2 | void Sweep() { | 125 | 2 | DCHECK(gc_underway_); | 126 | | // Iterate over every Cell linking the free ones into a new free list. | 127 | 0 | num_free_ = 0; | 128 | 2 | free_list_ = nullptr; | 129 | 2 | int cell_id = 0; | 130 | 2 | for (Block* block : blocks_) { | 131 | 2 | for (Cell& cell : block->cells) { | 132 | 2 | if (!mark_set_.IsMarked(cell_id)) { | 133 | 2 | num_free_++; | 134 | 2 | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 135 | 2 | free_cell->id = cell_id; | 136 | 2 | free_cell->next = free_list_; | 137 | 2 | free_list_ = free_cell; | 138 | 2 | } | 139 | 2 | cell_id++; | 140 | 2 | } | 141 | 1 | } | 142 | 2 | gc_underway_ = false; | 143 | 2 | } |
_ZN4PoolILi1ELm32EE5SweepEv Line | Count | Source | 124 | 1 | void Sweep() { | 125 | 1 | DCHECK(gc_underway_); | 126 | | // Iterate over every Cell linking the free ones into a new free list. | 127 | 0 | num_free_ = 0; | 128 | 1 | free_list_ = nullptr; | 129 | 1 | int cell_id = 0; | 130 | 2 | for (Block* block : blocks_) { | 131 | 2 | for (Cell& cell : block->cells) { | 132 | 2 | if (!mark_set_.IsMarked(cell_id)) { | 133 | 1 | num_free_++; | 134 | 1 | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 135 | 1 | free_cell->id = cell_id; | 136 | 1 | free_cell->next = free_list_; | 137 | 1 | free_list_ = free_cell; | 138 | 1 | } | 139 | 2 | cell_id++; | 140 | 2 | } | 141 | 2 | } | 142 | 1 | gc_underway_ = false; | 143 | 1 | } |
|
144 | | |
145 | 25 | void Free() { |
146 | 27 | for (Block* block : blocks_) { |
147 | 27 | free(block); |
148 | 27 | } |
149 | 25 | blocks_.clear(); |
150 | 25 | } _ZN4PoolILi682ELm24EE4FreeEv Line | Count | Source | 145 | 11 | void Free() { | 146 | 11 | for (Block* block : blocks_) { | 147 | 11 | free(block); | 148 | 11 | } | 149 | 11 | blocks_.clear(); | 150 | 11 | } |
_ZN4PoolILi341ELm48EE4FreeEv Line | Count | Source | 145 | 11 | void Free() { | 146 | 11 | for (Block* block : blocks_) { | 147 | 11 | free(block); | 148 | 11 | } | 149 | 11 | blocks_.clear(); | 150 | 11 | } |
_ZN4PoolILi2ELm32EE4FreeEv Line | Count | Source | 145 | 2 | void Free() { | 146 | 3 | for (Block* block : blocks_) { | 147 | 3 | free(block); | 148 | 3 | } | 149 | 2 | blocks_.clear(); | 150 | 2 | } |
_ZN4PoolILi1ELm32EE4FreeEv Line | Count | Source | 145 | 1 | void Free() { | 146 | 2 | for (Block* block : blocks_) { | 147 | 2 | free(block); | 148 | 2 | } | 149 | 1 | blocks_.clear(); | 150 | 1 | } |
|
151 | | |
152 | 14 | int num_allocated() { |
153 | 14 | return num_allocated_; |
154 | 14 | } _ZN4PoolILi682ELm24EE13num_allocatedEv Line | Count | Source | 152 | 6 | int num_allocated() { | 153 | 6 | return num_allocated_; | 154 | 6 | } |
_ZN4PoolILi341ELm48EE13num_allocatedEv Line | Count | Source | 152 | 6 | int num_allocated() { | 153 | 6 | return num_allocated_; | 154 | 6 | } |
_ZN4PoolILi2ELm32EE13num_allocatedEv Line | Count | Source | 152 | 2 | int num_allocated() { | 153 | 2 | return num_allocated_; | 154 | 2 | } |
|
155 | | |
156 | 8 | int64_t bytes_allocated() { |
157 | 8 | return bytes_allocated_; |
158 | 8 | } _ZN4PoolILi682ELm24EE15bytes_allocatedEv Line | Count | Source | 156 | 3 | int64_t bytes_allocated() { | 157 | 3 | return bytes_allocated_; | 158 | 3 | } |
_ZN4PoolILi341ELm48EE15bytes_allocatedEv Line | Count | Source | 156 | 3 | int64_t bytes_allocated() { | 157 | 3 | return bytes_allocated_; | 158 | 3 | } |
_ZN4PoolILi2ELm32EE15bytes_allocatedEv Line | Count | Source | 156 | 2 | int64_t bytes_allocated() { | 157 | 2 | return bytes_allocated_; | 158 | 2 | } |
|
159 | | |
160 | 292 | int num_live() { |
161 | 292 | return blocks_.size() * CellsPerBlock - num_free_; |
162 | 292 | } _ZN4PoolILi682ELm24EE8num_liveEv Line | Count | Source | 160 | 144 | int num_live() { | 161 | 144 | return blocks_.size() * CellsPerBlock - num_free_; | 162 | 144 | } |
_ZN4PoolILi341ELm48EE8num_liveEv Line | Count | Source | 160 | 144 | int num_live() { | 161 | 144 | return blocks_.size() * CellsPerBlock - num_free_; | 162 | 144 | } |
_ZN4PoolILi2ELm32EE8num_liveEv Line | Count | Source | 160 | 3 | int num_live() { | 161 | 3 | return blocks_.size() * CellsPerBlock - num_free_; | 162 | 3 | } |
_ZN4PoolILi1ELm32EE8num_liveEv Line | Count | Source | 160 | 1 | int num_live() { | 161 | 1 | return blocks_.size() * CellsPerBlock - num_free_; | 162 | 1 | } |
|
163 | | |
164 | | private: |
165 | | using Cell = uint8_t[CellSize]; |
166 | | |
167 | | struct Block { |
168 | | Cell cells[CellsPerBlock]; |
169 | | }; |
170 | | |
171 | | // Unused/free cells are tracked via a linked list of FreeCells. The FreeCells |
172 | | // are stored in the unused Cells, so it takes no extra memory to track them. |
173 | | struct FreeCell { |
174 | | int id; |
175 | | FreeCell* next; |
176 | | }; |
177 | | static_assert(CellSize >= sizeof(FreeCell), "CellSize is too small"); |
178 | | |
179 | | // Whether a GC is underway, for asserting that calls are in order. |
180 | | bool gc_underway_ = false; |
181 | | |
182 | | FreeCell* free_list_ = nullptr; |
183 | | int num_free_ = 0; |
184 | | int num_allocated_ = 0; |
185 | | int64_t bytes_allocated_ = 0; |
186 | | std::vector<Block*> blocks_; |
187 | | MarkSet mark_set_; |
188 | | |
189 | | DISALLOW_COPY_AND_ASSIGN(Pool<CellsPerBlock COMMA CellSize>); |
190 | | }; |
191 | | |
192 | | class MarkSweepHeap { |
193 | | public: |
194 | | // reserve 32 frames to start |
195 | 12 | MarkSweepHeap() { |
196 | 12 | } |
197 | | |
198 | | void Init(); // use default threshold |
199 | | void Init(int gc_threshold); |
200 | | |
201 | 54.1k | void PushRoot(RawObject** p) { |
202 | 54.1k | roots_.push_back(p); |
203 | 54.1k | } |
204 | | |
205 | 54.1k | void PopRoot() { |
206 | 54.1k | roots_.pop_back(); |
207 | 54.1k | } |
208 | | |
209 | 2 | void RootGlobalVar(void* root) { |
210 | 2 | global_roots_.push_back(reinterpret_cast<RawObject*>(root)); |
211 | 2 | } |
212 | | |
213 | | void* Allocate(size_t num_bytes, int* obj_id, int* pool_id); |
214 | | |
215 | | #if 0 |
216 | | void* Reallocate(void* p, size_t num_bytes); |
217 | | #endif |
218 | | int MaybeCollect(); |
219 | | int Collect(); |
220 | | |
221 | | void MaybeMarkAndPush(RawObject* obj); |
222 | | void TraceChildren(); |
223 | | |
224 | | void Sweep(); |
225 | | |
226 | | void PrintStats(int fd); // public for testing |
227 | | |
228 | | void CleanProcessExit(); // do one last GC, used in unit tests |
229 | | void ProcessExit(); // main() lets OS clean up, except ASAN variant |
230 | | |
231 | 144 | int num_live() { |
232 | 144 | return num_live_ |
233 | 144 | #ifndef NO_POOL_ALLOC |
234 | 144 | + pool1_.num_live() + pool2_.num_live() |
235 | 144 | #endif |
236 | 144 | ; |
237 | 144 | } |
238 | | |
239 | | bool is_initialized_ = true; // mark/sweep doesn't need to be initialized |
240 | | |
241 | | // Runtime params |
242 | | |
243 | | // Threshold is a number of live objects, since we aren't keeping track of |
244 | | // total bytes |
245 | | int gc_threshold_; |
246 | | |
247 | | // Show debug logging |
248 | | bool gc_verbose_ = false; |
249 | | |
250 | | // Current stats |
251 | | int num_live_ = 0; |
252 | | // Should we keep track of sizes? |
253 | | // int64_t bytes_live_ = 0; |
254 | | |
255 | | // Cumulative stats |
256 | | int max_survived_ = 0; // max # live after a collection |
257 | | int num_allocated_ = 0; |
258 | | int64_t bytes_allocated_ = 0; // avoid overflow |
259 | | int num_gc_points_ = 0; // manual collection points |
260 | | int num_collections_ = 0; |
261 | | int num_growths_; |
262 | | double max_gc_millis_ = 0.0; |
263 | | double total_gc_millis_ = 0.0; |
264 | | |
265 | | #ifndef NO_POOL_ALLOC |
266 | | // 16,384 / 24 bytes = 682 cells (rounded), 16,368 bytes |
267 | | // 16,384 / 48 bytes = 341 cells (rounded), 16,368 bytes |
268 | | // Conveniently, the glibc malloc header is 16 bytes, giving exactly 16 Ki |
269 | | // differences |
270 | | Pool<682, 24> pool1_; |
271 | | Pool<341, 48> pool2_; |
272 | | #endif |
273 | | |
274 | | std::vector<RawObject**> roots_; |
275 | | std::vector<RawObject*> global_roots_; |
276 | | |
277 | | // Allocate() appends live objects, and Sweep() compacts it |
278 | | std::vector<ObjHeader*> live_objs_; |
279 | | // Allocate lazily frees these, and Sweep() replenishes it |
280 | | std::vector<ObjHeader*> to_free_; |
281 | | |
282 | | std::vector<ObjHeader*> gray_stack_; |
283 | | MarkSet mark_set_; |
284 | | |
285 | | int greatest_obj_id_ = 0; |
286 | | |
287 | | private: |
288 | | void FreeEverything(); |
289 | | void MaybePrintStats(); |
290 | | |
291 | | DISALLOW_COPY_AND_ASSIGN(MarkSweepHeap); |
292 | | }; |
293 | | |
294 | | #endif // MARKSWEEP_HEAP_H |