cpp

Coverage Report

Created: 2025-06-02 15:02

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