mycpp

Coverage Report

Created: 2025-04-27 02:05

/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
43
  MarkSet() : bits_() {
14
43
  }
15
16
  // ReInit() must be called at the start of MarkObjects().  Allocate() should
17
  // keep track of the maximum object ID.
18
122
  void ReInit(int max_obj_id) {
19
    // https://stackoverflow.com/questions/8848575/fastest-way-to-reset-every-value-of-stdvectorint-to-0
20
122
    std::fill(bits_.begin(), bits_.end(), 0);
21
122
    int max_byte_index = (max_obj_id >> 3) + 1;  // round up
22
    // log("ReInit max_byte_index %d", max_byte_index);
23
122
    bits_.resize(max_byte_index);
24
122
  }
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
41.4k
  bool IsMarked(int obj_id) {
39
41.4k
    DCHECK(obj_id >= 0);
40
0
    int byte_index = obj_id >> 3;
41
41.4k
    int bit_index = obj_id & 0b111;
42
41.4k
    return bits_[byte_index] & (1 << bit_index);
43
41.4k
  }
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
29
  Pool() = default;
_ZN4PoolILi682ELm24EEC2Ev
Line
Count
Source
79
13
  Pool() = default;
_ZN4PoolILi341ELm48EEC2Ev
Line
Count
Source
79
13
  Pool() = default;
_ZN4PoolILi2ELm32EEC2Ev
Line
Count
Source
79
2
  Pool() = default;
_ZN4PoolILi1ELm32EEC2Ev
Line
Count
Source
79
1
  Pool() = default;
80
81
2.76k
  void* Allocate(int* obj_id) {
82
2.76k
    num_allocated_++;
83
84
2.76k
    if (!free_list_) {
85
      // Allocate a new Block and add every new Cell to the free list.
86
31
      Block* block = static_cast<Block*>(malloc(sizeof(Block)));
87
31
      blocks_.push_back(block);
88
31
      bytes_allocated_ += kBlockSize;
89
31
      num_free_ += CellsPerBlock;
90
91
      // The starting cell_id for Cells in this block.
92
31
      int cell_id = (blocks_.size() - 1) * CellsPerBlock;
93
13.3k
      for (Cell& cell : block->cells) {
94
13.3k
        FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell);
95
13.3k
        free_cell->id = cell_id++;
96
13.3k
        free_cell->next = free_list_;
97
13.3k
        free_list_ = free_cell;
98
13.3k
      }
99
31
    }
100
101
2.76k
    FreeCell* cell = free_list_;
102
2.76k
    free_list_ = free_list_->next;
103
2.76k
    num_free_--;
104
2.76k
    *obj_id = cell->id;
105
2.76k
    return cell;
106
2.76k
  }
_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
13
      Block* block = static_cast<Block*>(malloc(sizeof(Block)));
87
13
      blocks_.push_back(block);
88
13
      bytes_allocated_ += kBlockSize;
89
13
      num_free_ += CellsPerBlock;
90
91
      // The starting cell_id for Cells in this block.
92
13
      int cell_id = (blocks_.size() - 1) * CellsPerBlock;
93
8.86k
      for (Cell& cell : block->cells) {
94
8.86k
        FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell);
95
8.86k
        free_cell->id = cell_id++;
96
8.86k
        free_cell->next = free_list_;
97
8.86k
        free_list_ = free_cell;
98
8.86k
      }
99
13
    }
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
892
  void* Allocate(int* obj_id) {
82
892
    num_allocated_++;
83
84
892
    if (!free_list_) {
85
      // Allocate a new Block and add every new Cell to the free list.
86
13
      Block* block = static_cast<Block*>(malloc(sizeof(Block)));
87
13
      blocks_.push_back(block);
88
13
      bytes_allocated_ += kBlockSize;
89
13
      num_free_ += CellsPerBlock;
90
91
      // The starting cell_id for Cells in this block.
92
13
      int cell_id = (blocks_.size() - 1) * CellsPerBlock;
93
4.43k
      for (Cell& cell : block->cells) {
94
4.43k
        FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell);
95
4.43k
        free_cell->id = cell_id++;
96
4.43k
        free_cell->next = free_list_;
97
4.43k
        free_list_ = free_cell;
98
4.43k
      }
99
13
    }
100
101
892
    FreeCell* cell = free_list_;
102
892
    free_list_ = free_list_->next;
103
892
    num_free_--;
104
892
    *obj_id = cell->id;
105
892
    return cell;
106
892
  }
_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
81
  void PrepareForGc() {
109
81
    DCHECK(!gc_underway_);
110
0
    gc_underway_ = true;
111
81
    mark_set_.ReInit(blocks_.size() * CellsPerBlock);
112
81
  }
_ZN4PoolILi682ELm24EE12PrepareForGcEv
Line
Count
Source
108
39
  void PrepareForGc() {
109
39
    DCHECK(!gc_underway_);
110
0
    gc_underway_ = true;
111
39
    mark_set_.ReInit(blocks_.size() * CellsPerBlock);
112
39
  }
_ZN4PoolILi341ELm48EE12PrepareForGcEv
Line
Count
Source
108
39
  void PrepareForGc() {
109
39
    DCHECK(!gc_underway_);
110
0
    gc_underway_ = true;
111
39
    mark_set_.ReInit(blocks_.size() * CellsPerBlock);
112
39
  }
_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
81
  void Sweep() {
125
81
    DCHECK(gc_underway_);
126
    // Iterate over every Cell linking the free ones into a new free list.
127
0
    num_free_ = 0;
128
81
    free_list_ = nullptr;
129
81
    int cell_id = 0;
130
82
    for (Block* block : blocks_) {
131
40.5k
      for (Cell& cell : block->cells) {
132
40.5k
        if (!mark_set_.IsMarked(cell_id)) {
133
40.5k
          num_free_++;
134
40.5k
          FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell);
135
40.5k
          free_cell->id = cell_id;
136
40.5k
          free_cell->next = free_list_;
137
40.5k
          free_list_ = free_cell;
138
40.5k
        }
139
40.5k
        cell_id++;
140
40.5k
      }
141
82
    }
142
81
    gc_underway_ = false;
143
81
  }
_ZN4PoolILi682ELm24EE5SweepEv
Line
Count
Source
124
39
  void Sweep() {
125
39
    DCHECK(gc_underway_);
126
    // Iterate over every Cell linking the free ones into a new free list.
127
0
    num_free_ = 0;
128
39
    free_list_ = nullptr;
129
39
    int cell_id = 0;
130
40
    for (Block* block : blocks_) {
131
27.2k
      for (Cell& cell : block->cells) {
132
27.2k
        if (!mark_set_.IsMarked(cell_id)) {
133
27.2k
          num_free_++;
134
27.2k
          FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell);
135
27.2k
          free_cell->id = cell_id;
136
27.2k
          free_cell->next = free_list_;
137
27.2k
          free_list_ = free_cell;
138
27.2k
        }
139
27.2k
        cell_id++;
140
27.2k
      }
141
40
    }
142
39
    gc_underway_ = false;
143
39
  }
_ZN4PoolILi341ELm48EE5SweepEv
Line
Count
Source
124
39
  void Sweep() {
125
39
    DCHECK(gc_underway_);
126
    // Iterate over every Cell linking the free ones into a new free list.
127
0
    num_free_ = 0;
128
39
    free_list_ = nullptr;
129
39
    int cell_id = 0;
130
39
    for (Block* block : blocks_) {
131
13.2k
      for (Cell& cell : block->cells) {
132
13.2k
        if (!mark_set_.IsMarked(cell_id)) {
133
13.2k
          num_free_++;
134
13.2k
          FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell);
135
13.2k
          free_cell->id = cell_id;
136
13.2k
          free_cell->next = free_list_;
137
13.2k
          free_list_ = free_cell;
138
13.2k
        }
139
13.2k
        cell_id++;
140
13.2k
      }
141
39
    }
142
39
    gc_underway_ = false;
143
39
  }
_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
27
  void Free() {
146
31
    for (Block* block : blocks_) {
147
31
      free(block);
148
31
    }
149
27
    blocks_.clear();
150
27
    num_free_ = 0;
151
27
  }
_ZN4PoolILi682ELm24EE4FreeEv
Line
Count
Source
145
12
  void Free() {
146
13
    for (Block* block : blocks_) {
147
13
      free(block);
148
13
    }
149
12
    blocks_.clear();
150
12
    num_free_ = 0;
151
12
  }
_ZN4PoolILi341ELm48EE4FreeEv
Line
Count
Source
145
12
  void Free() {
146
13
    for (Block* block : blocks_) {
147
13
      free(block);
148
13
    }
149
12
    blocks_.clear();
150
12
    num_free_ = 0;
151
12
  }
_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
    num_free_ = 0;
151
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
    num_free_ = 0;
151
1
  }
152
153
14
  int num_allocated() {
154
14
    return num_allocated_;
155
14
  }
_ZN4PoolILi682ELm24EE13num_allocatedEv
Line
Count
Source
153
6
  int num_allocated() {
154
6
    return num_allocated_;
155
6
  }
_ZN4PoolILi341ELm48EE13num_allocatedEv
Line
Count
Source
153
6
  int num_allocated() {
154
6
    return num_allocated_;
155
6
  }
_ZN4PoolILi2ELm32EE13num_allocatedEv
Line
Count
Source
153
2
  int num_allocated() {
154
2
    return num_allocated_;
155
2
  }
156
157
8
  int64_t bytes_allocated() {
158
8
    return bytes_allocated_;
159
8
  }
_ZN4PoolILi682ELm24EE15bytes_allocatedEv
Line
Count
Source
157
3
  int64_t bytes_allocated() {
158
3
    return bytes_allocated_;
159
3
  }
_ZN4PoolILi341ELm48EE15bytes_allocatedEv
Line
Count
Source
157
3
  int64_t bytes_allocated() {
158
3
    return bytes_allocated_;
159
3
  }
_ZN4PoolILi2ELm32EE15bytes_allocatedEv
Line
Count
Source
157
2
  int64_t bytes_allocated() {
158
2
    return bytes_allocated_;
159
2
  }
160
161
298
  int num_live() {
162
298
#ifndef OPTIMIZED
163
298
    int capacity = blocks_.size() * CellsPerBlock;
164
    // log("Pool capacity = %d", capacity);
165
    // log("Pool num_free_ = %d", num_free_);
166
298
    DCHECK(num_free_ <= capacity);
167
0
#endif
168
0
    return blocks_.size() * CellsPerBlock - num_free_;
169
298
  }
_ZN4PoolILi682ELm24EE8num_liveEv
Line
Count
Source
161
147
  int num_live() {
162
147
#ifndef OPTIMIZED
163
147
    int capacity = blocks_.size() * CellsPerBlock;
164
    // log("Pool capacity = %d", capacity);
165
    // log("Pool num_free_ = %d", num_free_);
166
147
    DCHECK(num_free_ <= capacity);
167
0
#endif
168
0
    return blocks_.size() * CellsPerBlock - num_free_;
169
147
  }
_ZN4PoolILi341ELm48EE8num_liveEv
Line
Count
Source
161
147
  int num_live() {
162
147
#ifndef OPTIMIZED
163
147
    int capacity = blocks_.size() * CellsPerBlock;
164
    // log("Pool capacity = %d", capacity);
165
    // log("Pool num_free_ = %d", num_free_);
166
147
    DCHECK(num_free_ <= capacity);
167
0
#endif
168
0
    return blocks_.size() * CellsPerBlock - num_free_;
169
147
  }
_ZN4PoolILi2ELm32EE8num_liveEv
Line
Count
Source
161
3
  int num_live() {
162
3
#ifndef OPTIMIZED
163
3
    int capacity = blocks_.size() * CellsPerBlock;
164
    // log("Pool capacity = %d", capacity);
165
    // log("Pool num_free_ = %d", num_free_);
166
3
    DCHECK(num_free_ <= capacity);
167
0
#endif
168
0
    return blocks_.size() * CellsPerBlock - num_free_;
169
3
  }
_ZN4PoolILi1ELm32EE8num_liveEv
Line
Count
Source
161
1
  int num_live() {
162
1
#ifndef OPTIMIZED
163
1
    int capacity = blocks_.size() * CellsPerBlock;
164
    // log("Pool capacity = %d", capacity);
165
    // log("Pool num_free_ = %d", num_free_);
166
1
    DCHECK(num_free_ <= capacity);
167
0
#endif
168
0
    return blocks_.size() * CellsPerBlock - num_free_;
169
1
  }
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
13
  MarkSweepHeap() {
203
13
  }
204
205
  void Init();  // use default threshold
206
  void Init(int gc_threshold);
207
208
54.1k
  void PushRoot(RawObject** p) {
209
54.1k
    roots_.push_back(p);
210
54.1k
  }
211
212
54.1k
  void PopRoot() {
213
54.1k
    roots_.pop_back();
214
54.1k
  }
215
216
3
  void RootGlobalVar(void* root) {
217
3
    global_roots_.push_back(reinterpret_cast<RawObject*>(root));
218
3
  }
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
147
  int num_live() {
240
147
    return num_live_
241
147
#ifndef NO_POOL_ALLOC
242
147
           + pool1_.num_live() + pool2_.num_live()
243
147
#endif
244
147
        ;
245
147
  }
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