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