| 1 | // -*- C++ -*- |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef HL_NESTEDHEAP_H_ |
| 19 | #define HL_NESTEDHEAP_H_ |
| 20 | |
| 21 | #include <assert.h> |
| 22 | |
| 23 | /** |
| 24 | * @class NestedHeap |
| 25 | * @brief Hierarchical heaps. |
| 26 | * @author Emery Berger <http://www.cs.umass.edu/~emery> |
| 27 | */ |
| 28 | |
| 29 | namespace HL { |
| 30 | |
| 31 | template <class SuperHeap> |
| 32 | class NestedHeap : public SuperHeap { |
| 33 | public: |
| 34 | |
| 35 | NestedHeap() |
| 36 | : parent (NULL), |
| 37 | child (NULL), |
| 38 | prev (NULL), |
| 39 | next (NULL) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | ~NestedHeap() |
| 44 | { |
| 45 | clear(); |
| 46 | if (parent != NULL) { |
| 47 | parent->removeChild (this); |
| 48 | } |
| 49 | removeSibling (this); |
| 50 | } |
| 51 | |
| 52 | inline void clear() { |
| 53 | |
| 54 | // Clear this heap. |
| 55 | SuperHeap::clear(); |
| 56 | |
| 57 | #if 0 |
| 58 | // |
| 59 | // Iterate through all children and delete them. |
| 60 | // |
| 61 | |
| 62 | if (child != NULL) { |
| 63 | NestedHeap<SuperHeap> * nextChild = child->next; |
| 64 | while (child != NULL) { |
| 65 | NestedHeap<SuperHeap> * prevChild = child->prev; |
| 66 | delete child; |
| 67 | child = prevChild; |
| 68 | } |
| 69 | child = nextChild; |
| 70 | while (child != NULL) { |
| 71 | nextChild = child->next; |
| 72 | delete child; |
| 73 | child = nextChild; |
| 74 | } |
| 75 | } |
| 76 | assert (child == NULL); |
| 77 | |
| 78 | #else // clear all the children. |
| 79 | |
| 80 | NestedHeap<SuperHeap> * ch = child; |
| 81 | while (ch != NULL) { |
| 82 | NestedHeap<SuperHeap> * nextChild = ch->next; |
| 83 | ch->clear(); |
| 84 | ch = nextChild; |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | } |
| 89 | |
| 90 | void addChild (NestedHeap<SuperHeap> * ch) |
| 91 | { |
| 92 | if (child == NULL) { |
| 93 | child = ch; |
| 94 | child->prev = NULL; |
| 95 | child->next = NULL; |
| 96 | } else { |
| 97 | assert (child->prev == NULL); |
| 98 | assert (ch->next == NULL); |
| 99 | ch->prev = NULL; |
| 100 | ch->next = child; |
| 101 | child->prev = ch; |
| 102 | child = ch; |
| 103 | } |
| 104 | child->parent = this; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | |
| 109 | void removeChild (NestedHeap<SuperHeap> * ch) |
| 110 | { |
| 111 | assert (ch != NULL); |
| 112 | if (child == ch) { |
| 113 | if (ch->prev) { |
| 114 | child = ch->prev; |
| 115 | } else if (ch->next) { |
| 116 | child = ch->next; |
| 117 | } else { |
| 118 | child = NULL; |
| 119 | } |
| 120 | } |
| 121 | removeSibling (ch); |
| 122 | } |
| 123 | |
| 124 | inline static void removeSibling (NestedHeap<SuperHeap> * sib) |
| 125 | { |
| 126 | if (sib->prev) { |
| 127 | sib->prev->next = sib->next; |
| 128 | } |
| 129 | if (sib->next) { |
| 130 | sib->next->prev = sib->prev; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | NestedHeap<SuperHeap> * parent; |
| 135 | NestedHeap<SuperHeap> * child; |
| 136 | NestedHeap<SuperHeap> * prev; |
| 137 | NestedHeap<SuperHeap> * next; |
| 138 | |
| 139 | }; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | #endif |