-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpz_gc.cpp
273 lines (224 loc) · 5.97 KB
/
pz_gc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Plasma garbage collector
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*/
#include "pz_common.h"
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include "pz_util.h"
#include "pz_gc.h"
#include "pz_gc_util.h"
#include "pz_gc.impl.h"
#include "pz_gc_layout.h"
#include "pz_gc_layout.impl.h"
/*
* Plasma GC
* ---------
*
* We want a GC that provides enough features to meet some MVP-ish goals. It
* only needs to be good enough to ensure we recover memory. It is
* currently a little bit better than that.
*
* * Mark/Sweep
* * Non-moving
* * Conservative
* * Interior pointers (up to 7 byte offset)
* * Block based, each block contains cells of a particular size, a marking
* bitmap and free list pointer (the free list is made of unused cell
* contents.
* * Blocks are allocated from Chunks. We allocate chunks from the OS.
*
* This GC is fairly simple. There are a few changes we could make to
* improve it in the medium term:
*
* https://github.com/PlasmaLang/plasma/labels/component%3A%20gc
*
* In the slightly longer term we should:
*
* * Use accurate pointer information and test it by adding compaction.
*
* In the long term, and with much tweaking, this GC will become the
* tenured and maybe the tenured/mutable part of a larger GC with more
* features and improvements.
*/
namespace pz {
/***************************************************************************
*
* These procedures will likely move somewhere else, but maybe after some
* refactoring.
*/
size_t heap_get_usage(const Heap * heap)
{
return heap->usage();
}
unsigned heap_get_collections(const Heap * heap)
{
return heap->collections();
}
void heap_set_meta_info(Heap * heap, void * obj, void * meta)
{
heap->set_meta_info(obj, meta);
}
void * heap_interior_ptr_to_ptr(const Heap * heap, void * ptr)
{
return heap->interior_ptr_to_ptr(ptr);
}
void * Heap::interior_ptr_to_ptr(void * iptr) const
{
CellPtrBOP cellb = ptr_to_bop_cell_interior(iptr);
if (cellb.is_valid()) {
return cellb.pointer();
} else {
CellPtrFit cellf = ptr_to_fit_cell_interior(iptr);
if (cellf.is_valid()) {
return cellf.pointer();
}
}
return nullptr;
}
void * heap_meta_info(const Heap * heap, void * obj)
{
return heap->meta_info(obj);
}
bool ChunkBOP::is_empty() const
{
for (unsigned i = 0; i < m_wilderness; i++) {
if (m_blocks[i].is_in_use()) return false;
}
return true;
}
bool ChunkFit::is_empty()
{
CellPtrFit cell = first_cell();
return !cell.is_allocated() &&
cell.size() ==
((Payload_Bytes - CellPtrFit::CellInfoOffset) / WORDSIZE_BYTES);
}
/***************************************************************************/
Heap::Heap(const Options & options)
: m_options(options)
, m_chunk_bop("GC BOP")
, m_chunk_fit("GC fit")
, m_threshold(GC_Initial_Threshold)
{}
Heap::~Heap()
{
assert(!m_chunk_bop.is_mapped());
assert(!m_chunk_fit.is_mapped());
}
bool Heap::init()
{
assert(!m_chunk_bop.is_mapped());
if (m_chunk_bop.allocate(GC_Chunk_Size)) {
new (m_chunk_bop.ptr()) ChunkBOP(this);
} else {
return false;
}
assert(!m_chunk_fit.is_mapped());
if (m_chunk_fit.allocate(GC_Chunk_Size)) {
new (m_chunk_fit.ptr()) ChunkFit(this);
} else {
return false;
}
return true;
}
bool Heap::finalise(bool fast)
{
if (fast) {
m_chunk_bop.forget();
m_chunk_fit.forget();
return true;
}
bool result = true;
if (m_chunk_bop.is_mapped()) {
if (!m_chunk_bop.release()) {
result = false;
}
}
if (m_chunk_fit.is_mapped()) {
// sweeping first ensures we run finalisers.
m_chunk_fit->sweep(m_options);
if (!m_chunk_fit.release()) {
result = false;
}
}
return result;
}
/***************************************************************************/
Block::Block(const Options & options, size_t cell_size_) : m_header(cell_size_)
{
assert(cell_size_ >= GC_Min_Cell_Size);
memset(m_header.bitmap, 0, GC_Cells_Per_Block * sizeof(uint8_t));
#if PZ_DEV
if (options.gc_poison()) {
memset(m_bytes, Poison_Byte, Payload_Bytes);
}
#endif
sweep(options);
}
/***************************************************************************/
size_t Block::usage()
{
return num_allocated() * size() * WORDSIZE_BYTES;
}
unsigned Block::num_allocated()
{
unsigned count = 0;
for (unsigned i = 0; i < num_cells(); i++) {
CellPtrBOP cell(this, i);
if (cell.is_allocated()) {
count++;
}
}
return count;
}
size_t ChunkBOP::usage()
{
size_t usage = 0;
for (unsigned i = 0; i < m_wilderness; i++) {
if (m_blocks[i].is_in_use()) {
usage += m_blocks[i].usage();
}
}
return usage;
}
size_t ChunkFit::usage()
{
size_t size = 0;
CellPtrFit cell = first_cell();
while (cell.is_valid()) {
if (cell.is_allocated()) {
size += cell.size() * WORDSIZE_BYTES + CellPtrFit::CellInfoOffset;
}
cell = cell.next_in_chunk();
}
return size;
}
/***************************************************************************/
void Heap::set_meta_info(void * obj, void * meta)
{
CellPtrFit cell = ptr_to_fit_cell(obj);
assert(cell.is_valid());
*cell.meta() = meta;
}
void * Heap::meta_info(void * obj) const
{
CellPtrFit cell = ptr_to_fit_cell(obj);
assert(cell.is_valid());
return *cell.meta();
}
} // namespace pz
/***************************************************************************
*
* Check arhitecture assumptions
*/
// 8 bits per byte
static_assert(WORDSIZE_BYTES * 8 == WORDSIZE_BITS, "8 bits in a byte");
// 32 or 64 bit.
static_assert(WORDSIZE_BITS == 64 || WORDSIZE_BITS == 32,
"Either 32 or 64bit wordsize");