mycpp

Coverage Report

Created: 2025-05-19 04:04

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