Eneboo - Documentación para desarrolladores
|
00001 // -*- C++ -*- 00002 00003 /* 00004 00005 Heap Layers: An Extensible Memory Allocation Infrastructure 00006 00007 Copyright (C) 2000-2003 by Emery Berger 00008 http://www.cs.umass.edu/~emery 00009 emery@cs.umass.edu 00010 00011 This program is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or 00014 (at your option) any later version. 00015 00016 This program is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 GNU General Public License for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with this program; if not, write to the Free Software 00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00024 00025 */ 00026 00027 #ifndef _NESTEDHEAP_H_ 00028 #define _NESTEDHEAP_H_ 00029 00030 #include <assert.h> 00031 00038 namespace HL { 00039 00040 template <class SuperHeap> 00041 class NestedHeap : public SuperHeap { 00042 public: 00043 00044 NestedHeap (void) 00045 : parent (NULL), 00046 child (NULL), 00047 prev (NULL), 00048 next (NULL) 00049 { 00050 } 00051 00052 ~NestedHeap (void) 00053 { 00054 clear(); 00055 if (parent != NULL) { 00056 parent->removeChild (this); 00057 } 00058 removeSibling (this); 00059 } 00060 00061 inline void clear (void) { 00062 00063 // Clear this heap. 00064 SuperHeap::clear(); 00065 00066 #if 0 00067 // 00068 // Iterate through all children and delete them. 00069 // 00070 00071 if (child != NULL) { 00072 NestedHeap<SuperHeap> * nextChild = child->next; 00073 while (child != NULL) { 00074 NestedHeap<SuperHeap> * prevChild = child->prev; 00075 delete child; 00076 child = prevChild; 00077 } 00078 child = nextChild; 00079 while (child != NULL) { 00080 nextChild = child->next; 00081 delete child; 00082 child = nextChild; 00083 } 00084 } 00085 assert (child == NULL); 00086 00087 #else // clear all the children. 00088 00089 NestedHeap<SuperHeap> * ch = child; 00090 while (ch != NULL) { 00091 NestedHeap<SuperHeap> * nextChild = ch->next; 00092 ch->clear(); 00093 ch = nextChild; 00094 } 00095 #endif 00096 00097 } 00098 00099 void addChild (NestedHeap<SuperHeap> * ch) 00100 { 00101 if (child == NULL) { 00102 child = ch; 00103 child->prev = NULL; 00104 child->next = NULL; 00105 } else { 00106 assert (child->prev == NULL); 00107 assert (ch->next == NULL); 00108 ch->prev = NULL; 00109 ch->next = child; 00110 child->prev = ch; 00111 child = ch; 00112 } 00113 child->parent = this; 00114 } 00115 00116 private: 00117 00118 void removeChild (NestedHeap<SuperHeap> * ch) 00119 { 00120 assert (ch != NULL); 00121 if (child == ch) { 00122 if (ch->prev) { 00123 child = ch->prev; 00124 } else if (ch->next) { 00125 child = ch->next; 00126 } else { 00127 child = NULL; 00128 } 00129 } 00130 removeSibling (ch); 00131 } 00132 00133 inline static void removeSibling (NestedHeap<SuperHeap> * sib) 00134 { 00135 if (sib->prev) { 00136 sib->prev->next = sib->next; 00137 } 00138 if (sib->next) { 00139 sib->next->prev = sib->prev; 00140 } 00141 } 00142 00143 NestedHeap<SuperHeap> * parent; 00144 NestedHeap<SuperHeap> * child; 00145 NestedHeap<SuperHeap> * prev; 00146 NestedHeap<SuperHeap> * next; 00147 00148 }; 00149 00150 } 00151 00152 #endif