CookMem 1.6.0
A C++ memory context / allocator
Loading...
Searching...
No Matches
avltree.h
1/*
2 * Copyright (c) 2018-2026 Heng Yuan
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef COOK_PTR_AVLTREE_H
17#define COOK_PTR_AVLTREE_H
18
19#include <cstddef>
20#include <cstdint>
21#include <cstdio>
22
23#include "exception.h"
24
25namespace cookmem
26{
27
33{
34private:
38 struct Node
39 {
41 std::size_t reserved;
43 std::size_t size;
45 Node* left;
47 Node* right;
49 Node* next;
51 std::int16_t height;
52 };
53
59 struct RemoveStruct
60 {
62 std::size_t size;
64 Node* node;
65
66 RemoveStruct (std::size_t s)
67 : size(s), node(nullptr)
68 {
69 }
70 };
71
72public:
77 : m_root(nullptr)
78 {
79 }
80
85
96 void
97 add (void* ptr, std::size_t size)
98 {
99 Node* node = reinterpret_cast<Node*>(ptr);
100 node->size = size;
101
102 if (m_root == nullptr)
103 {
104 node->left = nullptr;
105 node->right = nullptr;
106 node->next = nullptr;
107 node->height = 1;
108 m_root = node;
109
110 return;
111 }
112
113 Node* root;
114 Node* stack[sizeof(Node*) * 8 + 1];
115 std::int16_t depth;
116
117 root = stack[0] = m_root;
118
119 for (depth = 0; ; ++depth)
120 {
121 if (root == nullptr)
122 {
123 setParent (node, stack[depth - 1]);
124 node->left = nullptr;
125 node->right = nullptr;
126 node->next = nullptr;
127 node->height = 1;
128
129 root = balance (stack, depth);
130 if (root != nullptr)
131 {
132 m_root = root;
133 }
134 return;
135 }
136
137 if (size < root->size)
138 {
139 root = stack[depth + 1] = root->left;
140 continue;
141 }
142 else if (size > root->size)
143 {
144 root = stack[depth + 1] = root->right;
145 continue;
146 }
147 else
148 {
149 // The current root matches the node's size. In this case we
150 // just add node to the next pointer of the root.
151 //
152 Node* next = root->next;
153 root->next = node;
154 node->next = next;
155 node->left = root;
156 node->height = -1; // indicating that this node is in a DLL
157 if (next)
158 {
159 next->left = node;
160 }
161
162 // We can simply return since the tree structure is not
163 // changed.
164 return;
165 }
166 }
167 }
168
182 void*
183 remove (std::size_t& size, std::size_t limit = ~static_cast<std::size_t> (0))
184 {
185 Node* stack[sizeof(Node*) * 8 + 1];
186 Node* root = m_root;
187 Node* candidate = nullptr;
188 std::int16_t depth = 0;
189 std::int16_t candidateDepth = 0;
190
191 while (root != nullptr)
192 {
193 stack[depth] = root;
194
195 if (size <= root->size)
196 {
197 candidate = root;
198 candidateDepth = depth;
199 root = root->left;
200 }
201 else
202 {
203 root = root->right;
204 }
205 ++depth;
206 }
207
208 if (candidate == nullptr || candidate->size >= limit)
209 {
210 return nullptr;
211 }
212
213 // The candidate is the smallest node whose size is at least the key.
214 if (candidate->next)
215 {
216 Node* next = candidate->next;
217 candidate->next = next->next;
218 if (candidate->next != nullptr)
219 {
220 candidate->next->left = candidate;
221 }
222
223 size = next->size;
224 return next;
225 }
226
227 size = candidate->size;
228 removeNode (candidate, stack, candidateDepth);
229 return candidate;
230 }
231
238 void
239 remove (void* ptr)
240 {
241 Node* node = reinterpret_cast<Node*>(ptr);
242
243 if (node->height == -1)
244 {
245 // this is a simple case that node is in a DLL
246 Node* prev = node->left;
247 Node* next = node->next;
248 prev->next = next;
249 if (next)
250 {
251 next->left = prev;
252 }
253 return;
254 }
255
256 // The node is not in a DLL. So this is a more complicate case that
257 // need to do the removal from the root (since there is a potential
258 // need to balance the tree from root.
259
260 Node* root;
261 Node* stack[sizeof(Node*) * 8 + 1];
262 std::int16_t depth;
263
264 std::size_t size = node->size;
265
266 COOKMEM_ASSERT(m_root != nullptr);
267
268 root = stack[0] = m_root;
269
270 for (depth = 0; ; ++depth)
271 {
272 if (root == nullptr)
273 {
274 throw Exception (MEM_ERROR_GENERAL, "pointer not found.", __FILE__, __LINE__);
275 }
276
277 if (size < root->size)
278 {
279 root = stack[depth + 1] = root->left;
280 continue;
281 }
282 else if (size > root->size)
283 {
284 root = stack[depth + 1] = root->right;
285 continue;
286 }
287
288 break;
289 }
290
291 COOKMEM_ASSERT(root == node);
292
293 if (root->next)
294 {
295 // Just use the next node to replace the current node.
296
297 Node* next = root->next;
298 next->left = root->left;
299 next->right = root->right;
300 next->height = root->height;
301
302 if (depth == 0)
303 {
304 m_root = next;
305 }
306 else
307 {
308 setParent (next, stack[depth - 1]);
309 }
310 return;
311 }
312
313 removeNode (root, stack, depth);
314 }
315
322 bool
323 contains (void* ptr)
324 {
325 if (m_root == nullptr)
326 {
327 return false;
328 }
329
330 Node* node = reinterpret_cast<Node*>(ptr);
331 const std::size_t size = node->size;
332
333 Node* root = m_root;
334 while (root != nullptr)
335 {
336 if (size < root->size)
337 {
338 root = root->left;
339 }
340 else if (size > root->size)
341 {
342 root = root->right;
343 }
344 else
345 {
346 do
347 {
348 if (root == node)
349 {
350 return true;
351 }
352 root = root->next;
353 }
354 while (root != nullptr);
355 }
356 }
357 return false;
358 }
359
371 bool
372 canGrowInPlace (void* ptr, std::size_t newSize)
373 {
374 Node* node = reinterpret_cast<Node*>(ptr);
375
376 // All nodes in a DLL must have the same size.
377 if (node->height == -1 || node->next != nullptr)
378 {
379 return false;
380 }
381
382 // Find the next larger node. There are no parent pointers, so
383 // search from the root and remember the last node where the search
384 // went left.
385 std::size_t size = node->size;
386 Node* nextLarger = nullptr;
387 Node* root = m_root;
388 while (root != node)
389 {
390 if (root == nullptr)
391 {
392 return false;
393 }
394 if (size < root->size)
395 {
396 nextLarger = root;
397 root = root->left;
398 }
399 else
400 {
401 root = root->right;
402 }
403 }
404
405 if (node->right != nullptr)
406 {
407 nextLarger = node->right;
408 while (nextLarger->left != nullptr)
409 {
410 nextLarger = nextLarger->left;
411 }
412 }
413
414 return nextLarger == nullptr || newSize < nextLarger->size;
415 }
416
422 bool
424 {
425 return m_root == nullptr;
426 }
427
431 void
433 {
434 printf ("graph G {\n");
435 printNode (m_root);
436 printf ("}\n");
437 }
438private:
439 inline static std::int16_t
440 getMax (std::int16_t a, std::int16_t b)
441 {
442 return (a < b) ? b : a;
443 }
444
445 inline static std::int16_t
446 getHeight (const Node* node)
447 {
448 return node ? node->height : 0;
449 }
450
451 inline static void
452 updateHeight (Node* node)
453 {
454 node->height = getMax (getHeight (node->left), getHeight (node->right)) + 1;
455 }
456
457 inline static void
458 setParent (Node* node, Node* parent)
459 {
460 if (node->size < parent->size)
461 {
462 parent->left = node;
463 }
464 else
465 {
466 parent->right = node;
467 }
468 }
469
470 inline static void
471 setLeftChild (Node* root, Node* left)
472 {
473 root->left = left;
474 }
475
476 inline static void
477 setRightChild (Node* root, Node* right)
478 {
479 root->right = right;
480 }
481
482 inline static Node*
483 balance (Node** stack, std::int16_t depth)
484 {
485 for (;;)
486 {
487 --depth;
488 Node* root = stack[depth];
489 Node* left = root->left;
490 Node* right = root->right;
491
492 std::int16_t oldHeight = root->height;
493 int16_t diff = getHeight (left) - getHeight (right);
494 if (diff > 1)
495 {
496 if (getHeight (left->left) >= getHeight (left->right))
497 {
498 root = rotateWithLeftChild (root);
499 }
500 else
501 {
502 root = doubleWithLeftChild (root);
503 }
504 // update the parent
505 if (depth == 0)
506 {
507 return root;
508 }
509 setParent (root, stack[depth - 1]);
510 continue;
511 }
512 else if (diff < -1)
513 {
514 if (getHeight (right->right) >= getHeight (right->left))
515 {
516 root = rotateWithRightChild (root);
517 }
518 else
519 {
520 root = doubleWithRightChild (root);
521 }
522 // update the parent
523 if (depth == 0)
524 {
525 return root;
526 }
527 setParent (root, stack[depth - 1]);
528 continue;
529 }
530 else
531 {
532 updateHeight (root);
533 }
534
535 if (depth == 0)
536 {
537 return root;
538 }
539
540 if (oldHeight == root->height)
541 break;
542 }
543 return nullptr;
544 }
545
546 inline static Node*
547 rotateWithLeftChild (Node* root)
548 {
549 Node* left = root->left;
550 setLeftChild (root, left->right);
551 setRightChild (left, root);
552 updateHeight (root);
553 updateHeight (left);
554 return left;
555 }
556
557 inline static Node*
558 rotateWithRightChild (Node* root)
559 {
560 Node* right = root->right;
561 setRightChild (root, right->left);
562 setLeftChild (right, root);
563 updateHeight (root);
564 updateHeight (right);
565 return right;
566 }
567
568 inline static Node*
569 doubleWithLeftChild (Node* root)
570 {
571 setLeftChild (root, rotateWithRightChild (root->left));
572 return rotateWithLeftChild (root);
573 }
574
575 inline static Node*
576 doubleWithRightChild (Node* root)
577 {
578 setRightChild (root, rotateWithLeftChild (root->right));
579 return rotateWithRightChild (root);
580 }
581
582 static void
583 printNode (Node* root)
584 {
585 if (!root)
586 return;
587 printf ("%016zx -- {", (std::size_t)root);
588 if (root->left)
589 {
590 printf (" %016zx", (std::size_t)root->left);
591 }
592 if (root->right)
593 {
594 printf (" %016zx", (std::size_t)root->right);
595 }
596 printf (" }\n");
597 printf ("%016zx [ label = %zd ]\n",
598 (std::size_t)root,
599 root->size);
600 printNode (root->left);
601 printNode (root->right);
602 }
603
604 void
605 removeNode (Node* root, Node** stack, std::int16_t depth)
606 {
607 Node* left = root->left;
608 Node* right = root->right;
609
610 Node* newRoot;
611 if (left == nullptr)
612 {
613 newRoot = right;
614 }
615 else if (right == nullptr)
616 {
617 newRoot = left;
618 }
619 else
620 {
621 // this is a more complicated case.
622 // the solution is the move the smallest of the right branch
623 // here.
624 std::int16_t newDepth;
625 Node* newStack[sizeof (Node*) * 8 + 1];
626 newStack[0] = right;
627 for (newDepth = 0; newStack[newDepth]->left != nullptr; ++newDepth)
628 {
629 newStack[newDepth + 1] = newStack[newDepth]->left;
630 }
631 Node* min = newStack[newDepth];
632 if (min == right)
633 {
634 newRoot = right;
635 setLeftChild (newRoot, left);
636 updateHeight (newRoot);
637 }
638 else
639 {
640 // first detach min from the parent
641 Node* parent = newStack[newDepth - 1];
642 parent->left = min->right;
643
644 // rebalance the right
645 Node* newRight = balance (newStack, newDepth);
646 if (newRight != nullptr)
647 {
648 right = newRight;
649 }
650
651 setLeftChild (min, left);
652 setRightChild (min, right);
653 updateHeight (min);
654
655 newRoot = min;
656 }
657 }
658
659 if (depth == 0)
660 {
661 m_root = newRoot;
662 if (newRoot != nullptr)
663 {
664 stack[0] = newRoot;
665 newRoot = balance (stack, 1);
666 if (newRoot != nullptr)
667 {
668 m_root = newRoot;
669 }
670 }
671 return;
672 }
673 if (newRoot == nullptr)
674 {
675 // so root is the leaf.
676 if (root->size < stack[depth - 1]->size)
677 {
678 stack[depth - 1]->left = nullptr;
679 }
680 else
681 {
682 stack[depth - 1]->right = nullptr;
683 }
684 newRoot = balance (stack, depth);
685 if (newRoot != nullptr)
686 {
687 m_root = newRoot;
688 }
689 return;
690 }
691
692 setParent (newRoot, stack[depth - 1]);
693
694 stack[depth] = newRoot;
695 newRoot = balance (stack, static_cast<std::int16_t> (depth + 1));
696
697 if (newRoot != nullptr)
698 {
699 m_root = newRoot;
700 }
701 }
702
703private:
704 // node entries
705 Node* m_root;
706};
707
708} // namespace cookmem
709
710#endif // COOK_PTR_AVLTREE_H
Definition exception.h:39
void printGraph()
Definition avltree.h:432
bool isEmpty()
Definition avltree.h:423
void add(void *ptr, std::size_t size)
Definition avltree.h:97
PtrAVLTree()
Definition avltree.h:76
bool canGrowInPlace(void *ptr, std::size_t newSize)
Definition avltree.h:372
void remove(void *ptr)
Definition avltree.h:239
bool contains(void *ptr)
Definition avltree.h:323
~PtrAVLTree()
Definition avltree.h:84
void * remove(std::size_t &size, std::size_t limit=~static_cast< std::size_t >(0))
Definition avltree.h:183
Definition allocator.h:29
@ MEM_ERROR_GENERAL
general error
Definition exception.h:29