-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBTreeAllocator.cpp
More file actions
647 lines (531 loc) · 17.4 KB
/
RBTreeAllocator.cpp
File metadata and controls
647 lines (531 loc) · 17.4 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#include "RBTreeAllocator.h"
RBTreeAllocator::Node* RBTreeAllocator::Node::sibling() {
if (this->parent) {
if (this->parent->left == this)
return this->parent->right;
else
return this->parent->left;
}
return NULL;
}
RBTreeAllocator::RBTreeAllocator(size_t sz): m_size(sz), m_initialized(false), m_root(NULL), m_start(NULL), m_end(NULL)
{
// only STATIC mode for now,
// once the data structure is working, it's quite easy to port PREALLOC & VMDYNAMIC modes
m_start = (uint8_t*)malloc(m_size * sizeof(uint8_t));
m_end = (void*)((uintptr_t)m_start + m_size * sizeof(uint8_t));
m_initialized = true;
build();
}
RBTreeAllocator::~RBTreeAllocator()
{
}
void RBTreeAllocator::build() {
m_root = new(m_start) Node{NULL, NULL, NULL, COLOR::BLACK, m_size, NULL, NULL};
}
size_t RBTreeAllocator::_splitBlock(void* block, void* ptr, size_t sz) {
Node* blockHeader = (Node*)block;
ptrdiff_t remainingSpace = (uintptr_t)block + blockHeader->sz - (uintptr_t)ptr - sz;
_deleteNode((Node*)block);
if (remainingSpace >= freeHeaderSize) {
// create a new free block
void* newBlock = (void*)((uintptr_t)ptr + sz);
Node* newNode = new(newBlock) Node;
size_t freeBlockSize = blockHeader->sz - ((uintptr_t)ptr - (uintptr_t)block + sz);
newNode->sz = freeBlockSize;
_rbtreeInsert(m_root, newNode, freeBlockSize);
return sz;
}
else {
// cant create a new free block because remaining space is not large enough for header
return (uintptr_t)blockHeader->sz - (uintptr_t)ptr + (uintptr_t)block;
}
return 0;
}
void RBTreeAllocator::_fitPadding(ptrdiff_t& padding, size_t alignment) {
// if can't fit header into the padding
if (padding < allocHeaderSize) {
if ((allocHeaderSize - padding) % alignment == 0) {
padding = allocHeaderSize;
}
else {
padding += alignment * (1 + (allocHeaderSize - padding) / alignment);
}
}
}
void* RBTreeAllocator::_fitToBlock(
void* block, size_t sz, size_t alignment, ptrdiff_t& leftover, ptrdiff_t& padding)
{
void* ptr = (void*)(((uintptr_t)block + alignment - 1) & ~(alignment - 1));
padding = (uintptr_t)ptr - (uintptr_t)block;
_fitPadding(padding, alignment);
ptr = (void*)((uintptr_t)block + padding);
leftover = (ptrdiff_t)((Node*)block)->sz - (padding + sz);
return ptr;
}
void * RBTreeAllocator::Alloc(size_t sz, size_t alignment)
{
assert((alignment & (alignment - 1)) == 0);
Node* block = NULL;
ptrdiff_t padding;
void* ptr = _rbtreeStrictBestFit(m_root, sz, alignment, block, padding);
AllocatedBlockHeader* allocHeader = new((void*)((uintptr_t)ptr - allocHeaderSize)) AllocatedBlockHeader;
allocHeader->sz = _splitBlock(block, ptr, sz);
allocHeader->padding = padding;
return ptr;
}
/*
* Currently, we don't coalesce the free blocks
*/
void RBTreeAllocator::Free(void*& ptr)
{
if (!ptr)
return;
AllocatedBlockHeader* allocHeader = (AllocatedBlockHeader*)((uintptr_t)ptr - allocHeaderSize);
size_t allocSize = allocHeader->sz;
size_t allocPadding = allocHeader->padding;
void* freeHeaderPtr = (void*)((uintptr_t)ptr - allocPadding);
Node* newNode = new(freeHeaderPtr) Node;
size_t freeBlockSize = allocSize + allocPadding;
newNode->sz = freeBlockSize;
_rbtreeInsert(m_root, newNode, freeBlockSize);
}
inline void RBTreeAllocator::Release()
{
}
inline void RBTreeAllocator::Reset()
{
}
inline void RBTreeAllocator::ZeroMem()
{
}
void RBTreeAllocator::Layout()
{
int i = 0, l;
GPCL::pair<Node*, int> stack[64];
stack[0] = GPCL::make_pair(m_root, 0);
Node* cur;
Node* temp;
while (i >= 0) {
cur = stack[i].first;
l = stack[i--].second;
if (!cur)
continue;
for (int j = 0; j < l; ++j)
std::cout << " ";
std::cout << cur->sz << " | " << (void*)cur << " | ";
//printf("%d | %p | ", cur->key, (void*)cur);
temp = cur->llNext;
while (temp) {
std::cout << cur->sz << " | " << (void*)cur << " | ";
//printf("%d | %p | ", cur->key, (void*)cur);
temp = temp->llNext;
}
std::cout << "\n";
stack[++i] = GPCL::make_pair(cur->left, l + 1);
stack[++i] = GPCL::make_pair(cur->right, l + 1);
}
std::cout << std::endl;
}
RBTreeAllocator::Node* RBTreeAllocator::_rbtreeInsert(Node*& root, Node* node, size_t key) {
node->sz = key;
Node* cur = root;
Node* parent = NULL;
while (cur) {
parent = cur;
if (key > cur->sz) {
cur = cur->right;
}
else if (key < cur->sz) {
cur = cur->left;
}
else {
node->parent = cur->parent;
node->left = cur->left;
node->right = cur->right;
node->color = cur->color;
node->llPrev = NULL;
node->llNext = cur;
cur->llPrev = node;
if (cur->parent) {
if (cur == cur->parent->left)
cur->parent->left = node;
else
cur->parent->right = node;
}
if (cur->left)
cur->left->parent = node;
if (cur->right)
cur->right->parent = node;
return node;
}
}
node->parent = parent;
node->left = NULL;
node->right = NULL;
node->llPrev = NULL;
node->llNext = NULL;
if (!parent) {
root = node;
node->color = COLOR::BLACK;
return node;
}
node->color = COLOR::RED;
if (key > parent->sz) {
parent->right = node;
}
else {
parent->left = node;
}
if (parent->parent)
_rbtreeFixup(root, node);
return node;
}
void RBTreeAllocator::_rbtreeFixup(Node *&root, Node *&pt)
{
Node *parent_pt = NULL;
Node *grand_parent_pt = NULL;
while ((pt != root) && (pt->color != COLOR::BLACK) &&
(pt->parent->color == COLOR::RED))
{
parent_pt = pt->parent;
grand_parent_pt = pt->parent->parent;
if (parent_pt == grand_parent_pt->left)
{
Node *uncle_pt = grand_parent_pt->right;
if (uncle_pt != NULL && uncle_pt->color == COLOR::RED)
{
grand_parent_pt->color = COLOR::RED;
parent_pt->color = COLOR::BLACK;
uncle_pt->color = COLOR::BLACK;
pt = grand_parent_pt;
}
else
{
if (pt == parent_pt->right)
{
_rbtreeRotateLeft(root, parent_pt);
pt = parent_pt;
parent_pt = pt->parent;
}
_rbtreeRotateRight(root, grand_parent_pt);
COLOR t = parent_pt->color;
parent_pt->color = grand_parent_pt->color;
grand_parent_pt->color = t;
pt = parent_pt;
}
}
else
{
Node *uncle_pt = grand_parent_pt->left;
if ((uncle_pt != NULL) && (uncle_pt->color == COLOR::RED))
{
grand_parent_pt->color = COLOR::RED;
parent_pt->color = COLOR::BLACK;
uncle_pt->color = COLOR::BLACK;
pt = grand_parent_pt;
}
else
{
if (pt == parent_pt->left)
{
_rbtreeRotateRight(root, parent_pt);
pt = parent_pt;
parent_pt = pt->parent;
}
_rbtreeRotateLeft(root, grand_parent_pt);
COLOR t = parent_pt->color;
parent_pt->color = grand_parent_pt->color;
grand_parent_pt->color = t;
pt = parent_pt;
}
}
}
root->color = COLOR::BLACK;
}
void RBTreeAllocator::_rbtreeRotateLeft(Node *&root, Node *&pt)
{
Node *pt_right = pt->right;
pt->right = pt_right->left;
if (pt->right != NULL)
pt->right->parent = pt;
pt_right->parent = pt->parent;
if (pt->parent == NULL)
root = pt_right;
else if (pt == pt->parent->left)
pt->parent->left = pt_right;
else
pt->parent->right = pt_right;
pt_right->left = pt;
pt->parent = pt_right;
}
void RBTreeAllocator::_rbtreeRotateRight(Node *&root, Node *&pt)
{
Node *pt_left = pt->left;
pt->left = pt_left->right;
if (pt->left != NULL)
pt->left->parent = pt;
pt_left->parent = pt->parent;
if (pt->parent == NULL)
root = pt_left;
else if (pt == pt->parent->left)
pt->parent->left = pt_left;
else
pt->parent->right = pt_left;
pt_left->right = pt;
pt->parent = pt_left;
}
void RBTreeAllocator::_deleteNode(Node* node)
{
// node is the head of LL, as all links from adjacent nodes are to the top of the LL
// if multiple nodes exist in the LL, just remove the head
Node* nextNode = node->llNext;
if (nextNode) {
// NOTE that in any tree operation, only the head of the LL changes,
// thus we'll need to propogate changes to the next node
nextNode->llPrev = NULL;
nextNode->color = node->color;
nextNode->left = node->left;
nextNode->right = node->right;
nextNode->parent = node->parent;
// update the parent s.t. it holds a link to the new head
if (node->parent) {
if (node == node->parent->left)
node->parent->left = nextNode;
else
node->parent->right = nextNode;
}
// update the children
if (node->left)
node->left->parent = nextNode;
if (node->right)
node->right->parent = nextNode;
return;
}
// if cur is the only node with the given key, do a tree deletion
_rbtreeDelete(node);
return;
}
RBTreeAllocator::Node* RBTreeAllocator::_smallestInSubtree(Node* node) {
while (node->left)
node = node->left;
return node;
}
RBTreeAllocator::Node* RBTreeAllocator::_BSTSubst(Node* node) {
if (node->left && node->right)
return _smallestInSubtree(node->right);
if (!node->left && !node->right)
return NULL;
if (node->left)
return node->left;
else
return node->right;
}
void RBTreeAllocator::_rbtreeFixDoubleBlack(Node* node) {
if (node == m_root)
return;
Node* parent = node->parent;
Node* sibling = node->sibling();
if (!sibling) {
_rbtreeFixDoubleBlack(parent);
}
else {
bool siblingOnLeft = sibling->parent->left == sibling;
if (sibling->color == COLOR::RED) {
parent->color = COLOR::RED;
sibling->color = COLOR::BLACK;
if (siblingOnLeft) {
_rbtreeRotateRight(m_root, parent);
}
else {
_rbtreeRotateLeft(m_root, parent);
}
_rbtreeFixDoubleBlack(node);
}
else {
if ((sibling->left && sibling->left->color == COLOR::RED) ||
(sibling->right && sibling->right->color == COLOR::RED)) {
if (!sibling->left && sibling->left->color == COLOR::RED) {
if (siblingOnLeft) {
sibling->left->color = sibling->color;
sibling->color = parent->color;
_rbtreeRotateRight(m_root, parent);
}
else {
sibling->left->color = parent->color;
_rbtreeRotateRight(m_root, sibling);
_rbtreeRotateLeft(m_root, parent);
}
}
else {
if (siblingOnLeft) {
sibling->right->color = parent->color;
_rbtreeRotateLeft(m_root, sibling);
_rbtreeRotateRight(m_root, parent);
}
else {
sibling->right->color = sibling->color;
sibling->color = parent->color;
_rbtreeRotateLeft(m_root, parent);
}
}
parent->color = COLOR::BLACK;
}
else {
sibling->color = COLOR::RED;
if (parent->color == COLOR::BLACK)
_rbtreeFixDoubleBlack(parent);
else
parent->color = COLOR::BLACK;
}
}
}
}
void RBTreeAllocator::_rbtreeDelete(Node* node) {
Node* subst = _BSTSubst(node);
bool bothBlack = ((!subst || subst->color == COLOR::BLACK) && node->color == COLOR::BLACK);
Node* parent = node->parent;
if (!subst) {
// subst NULL => node must be leaf
if (node == m_root) {
m_root = NULL;
}
else {
if (bothBlack) {
_rbtreeFixDoubleBlack(node);
}
else {
Node* sibling = node->sibling();
if (sibling)
sibling->color = COLOR::RED;
}
if (parent->left == node)
parent->left = NULL;
else
parent->right = NULL;
}
return;
}
if (!node->left || !node->right) {
// node has 1 child
if (node == m_root) {
m_root = subst;
}
else {
// Detach v from tree and move u up
if (parent->left == node)
parent->left = subst;
else
parent->right = subst;
subst->parent = parent;
if (bothBlack)
_rbtreeFixDoubleBlack(subst);
else
subst->color = COLOR::BLACK;
}
return;
}
// address consistency for memory models
_rbtreeSwapNodes(node, subst);
_rbtreeDelete(node);
}
// swap the positions of the nodes in the graph while keeping their addresses consistent
void RBTreeAllocator::_rbtreeSwapNodes(Node* node, Node* subst) {
Node* parent = node->parent;
Node temp;
memcpy(&temp, node, sizeof(Node));
if (parent->left == node)
parent->left = subst;
else
parent->right = subst;
if (subst->left)
subst->left->parent = node;
if (subst->right)
subst->right->parent = node;
node->parent = subst;
node->left = subst->left;
node->right = subst->right;
subst->parent = temp.parent;
if (temp.left == subst) {
subst->left = node;
subst->right = temp.right;
temp.right->parent = subst;
}
else {
subst->right = node;
subst->left = temp.left;
temp.left->parent = subst;
}
}
RBTreeAllocator::Node* RBTreeAllocator::_rbtreeFindKey(size_t key) {
Node* cur = m_root;
Node* parent = NULL;
while (cur) {
parent = cur;
if (key > cur->sz) {
cur = cur->right;
}
else if (key < cur->sz) {
cur = cur->left;
}
else {
return cur;
}
}
return NULL;
}
/*
* NOTE: THIS METHOD ASSUMES ARBITRARY ALIGNMENT
* Consequently, it can (however VERY unlikely) fall back to O(N)
* if all free blocks
* have greater size than the allocation size,
* but can't allow required alignment and padding.
* This problem happens regardless of the data structure used, we need to define the problem better.
*
* Fortunetely, in a real world application, we know the maximum possible alignment that can be requested.
* Unless you want to waste memory for no reason, the maximum alignment can be set to 8 bytes.
* So, we can GUARANTEE O(log(N)) operation, with minimal to no amount of wasted space
*/
void* RBTreeAllocator::_rbtreeStrictBestFit(Node* node, size_t sz, size_t alignment, Node*& block, ptrdiff_t& padding) {
printf("Current node at %p > %d\n", node, node->sz);
if (!node || (!node->left && !node->right && node->sz < sz))
return NULL;
void* ptr;
ptrdiff_t leftover;
if (node->sz >= sz && (!node->left || node->left->sz < sz)) {
// node has key gt sz, and left child has key lt sz, eliminate the left branch
ptr = _fitToBlock(node, sz, alignment, leftover, padding);
if (leftover > 0) {
// node can accomodate for the padding and the header
block = node;
return ptr;
}
else {
// if not, search right branch
return _rbtreeStrictBestFit(node->right, sz, alignment, block, padding);
}
}
if (node->sz <= sz) {
return _rbtreeStrictBestFit(node->right, sz, alignment, block, padding);
}
else {
// both node and the left child have keys greater than sz
ptr = _rbtreeStrictBestFit(node->left, sz, alignment, block, padding);
if (!ptr) {
// despite having a key greater than sz, left child can't account for the padding & header
ptr = _fitToBlock(node, sz, alignment, leftover, padding);
if (leftover > 0) {
// we fall back to this node again,
block = node;
return ptr;
}
else {
// the worst case, we continue searching the right branch
return _rbtreeStrictBestFit(node->right, sz, alignment, block, padding);
}
}
else {
return ptr;
}
}
}