src/share/vm/memory/binaryTreeDictionary.cpp

Mon, 26 Nov 2012 12:31:03 -0500

author
coleenp
date
Mon, 26 Nov 2012 12:31:03 -0500
changeset 4297
19c1bd641922
parent 4265
0400886d2613
child 4382
e51c9860cf66
permissions
-rw-r--r--

8003722: More gcc 4.7 compilation errors
Summary: Add a few more this->qualifications.
Reviewed-by: coleenp, dholmes
Contributed-by: duboscq@ssw.jku.at

duke@435 1 /*
jmasa@3730 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "gc_implementation/shared/allocationStats.hpp"
jmasa@3730 27 #include "memory/binaryTreeDictionary.hpp"
jmasa@4196 28 #include "memory/freeList.hpp"
jmasa@4196 29 #include "memory/freeBlockDictionary.hpp"
jmasa@4196 30 #include "memory/metablock.hpp"
jmasa@4196 31 #include "memory/metachunk.hpp"
stefank@2314 32 #include "runtime/globals.hpp"
stefank@2314 33 #include "utilities/ostream.hpp"
jmasa@3730 34 #ifndef SERIALGC
jmasa@4196 35 #include "gc_implementation/concurrentMarkSweep/adaptiveFreeList.hpp"
jmasa@4196 36 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
jmasa@3730 37 #include "gc_implementation/shared/spaceDecorator.hpp"
jmasa@3730 38 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
jmasa@3730 39 #endif // SERIALGC
duke@435 40
duke@435 41 ////////////////////////////////////////////////////////////////////////////////
duke@435 42 // A binary tree based search structure for free blocks.
duke@435 43 // This is currently used in the Concurrent Mark&Sweep implementation.
duke@435 44 ////////////////////////////////////////////////////////////////////////////////
duke@435 45
jmasa@4196 46 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 47 size_t TreeChunk<Chunk_t, FreeList_t>::_min_tree_chunk_size = sizeof(TreeChunk<Chunk_t, FreeList_t>)/HeapWordSize;
jmasa@4196 48
jmasa@4196 49 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 50 TreeChunk<Chunk_t, FreeList_t>* TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(Chunk_t* fc) {
duke@435 51 // Do some assertion checking here.
jmasa@4196 52 return (TreeChunk<Chunk_t, FreeList_t>*) fc;
duke@435 53 }
duke@435 54
jmasa@4196 55 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 56 void TreeChunk<Chunk_t, FreeList_t>::verify_tree_chunk_list() const {
jmasa@4196 57 TreeChunk<Chunk_t, FreeList_t>* nextTC = (TreeChunk<Chunk_t, FreeList_t>*)next();
duke@435 58 if (prev() != NULL) { // interior list node shouldn'r have tree fields
duke@435 59 guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL &&
duke@435 60 embedded_list()->right() == NULL, "should be clear");
duke@435 61 }
duke@435 62 if (nextTC != NULL) {
duke@435 63 guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain");
duke@435 64 guarantee(nextTC->size() == size(), "wrong size");
jmasa@3732 65 nextTC->verify_tree_chunk_list();
duke@435 66 }
duke@435 67 }
duke@435 68
jmasa@4196 69 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 70 TreeList<Chunk_t, FreeList_t>::TreeList() {}
duke@435 71
jmasa@4196 72 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 73 TreeList<Chunk_t, FreeList_t>*
jmasa@4196 74 TreeList<Chunk_t, FreeList_t>::as_TreeList(TreeChunk<Chunk_t,FreeList_t>* tc) {
duke@435 75 // This first free chunk in the list will be the tree list.
jmasa@4196 76 assert((tc->size() >= (TreeChunk<Chunk_t, FreeList_t>::min_size())),
jmasa@4196 77 "Chunk is too small for a TreeChunk");
jmasa@4196 78 TreeList<Chunk_t, FreeList_t>* tl = tc->embedded_list();
jmasa@4196 79 tl->initialize();
duke@435 80 tc->set_list(tl);
duke@435 81 tl->set_size(tc->size());
duke@435 82 tl->link_head(tc);
duke@435 83 tl->link_tail(tc);
duke@435 84 tl->set_count(1);
jmasa@4196 85
duke@435 86 return tl;
duke@435 87 }
ysr@1580 88
jmasa@4196 89
jmasa@4196 90 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 91 TreeList<Chunk_t, FreeList_t>*
jmasa@4196 92 get_chunk(size_t size, enum FreeBlockDictionary<Chunk_t>::Dither dither) {
jmasa@4196 93 FreeBlockDictionary<Chunk_t>::verify_par_locked();
jmasa@4196 94 Chunk_t* res = get_chunk_from_tree(size, dither);
jmasa@4196 95 assert(res == NULL || res->is_free(),
jmasa@4196 96 "Should be returning a free chunk");
jmasa@4196 97 assert(dither != FreeBlockDictionary<Chunk_t>::exactly ||
jmasa@4196 98 res->size() == size, "Not correct size");
jmasa@4196 99 return res;
jmasa@4196 100 }
jmasa@4196 101
jmasa@4196 102 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 103 TreeList<Chunk_t, FreeList_t>*
jmasa@4196 104 TreeList<Chunk_t, FreeList_t>::as_TreeList(HeapWord* addr, size_t size) {
jmasa@4196 105 TreeChunk<Chunk_t, FreeList_t>* tc = (TreeChunk<Chunk_t, FreeList_t>*) addr;
jmasa@4196 106 assert((size >= TreeChunk<Chunk_t, FreeList_t>::min_size()),
jmasa@4196 107 "Chunk is too small for a TreeChunk");
jmasa@4196 108 // The space will have been mangled initially but
jmasa@4196 109 // is not remangled when a Chunk_t is returned to the free list
jmasa@698 110 // (since it is used to maintain the chunk on the free list).
jmasa@4196 111 tc->assert_is_mangled();
jmasa@3732 112 tc->set_size(size);
jmasa@3732 113 tc->link_prev(NULL);
jmasa@3732 114 tc->link_next(NULL);
jmasa@4196 115 TreeList<Chunk_t, FreeList_t>* tl = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);
duke@435 116 return tl;
duke@435 117 }
duke@435 118
duke@435 119
jmasa@4196 120 #ifndef SERIALGC
jmasa@4196 121 // Specialize for AdaptiveFreeList which tries to avoid
jmasa@4196 122 // splitting a chunk of a size that is under populated in favor of
jmasa@4196 123 // an over populated size. The general get_better_list() just returns
jmasa@4196 124 // the current list.
jmasa@4196 125 template <>
jmasa@4196 126 TreeList<FreeChunk, AdaptiveFreeList>*
jmasa@4196 127 TreeList<FreeChunk, AdaptiveFreeList>::get_better_list(
jmasa@4196 128 BinaryTreeDictionary<FreeChunk, ::AdaptiveFreeList>* dictionary) {
jmasa@4196 129 // A candidate chunk has been found. If it is already under
jmasa@4196 130 // populated, get a chunk associated with the hint for this
jmasa@4196 131 // chunk.
jmasa@4196 132
jmasa@4196 133 TreeList<FreeChunk, ::AdaptiveFreeList>* curTL = this;
jmasa@4196 134 if (surplus() <= 0) {
jmasa@4196 135 /* Use the hint to find a size with a surplus, and reset the hint. */
jmasa@4196 136 TreeList<FreeChunk, ::AdaptiveFreeList>* hintTL = this;
jmasa@4196 137 while (hintTL->hint() != 0) {
jmasa@4196 138 assert(hintTL->hint() > hintTL->size(),
jmasa@4196 139 "hint points in the wrong direction");
jmasa@4196 140 hintTL = dictionary->find_list(hintTL->hint());
jmasa@4196 141 assert(curTL != hintTL, "Infinite loop");
jmasa@4196 142 if (hintTL == NULL ||
jmasa@4196 143 hintTL == curTL /* Should not happen but protect against it */ ) {
jmasa@4196 144 // No useful hint. Set the hint to NULL and go on.
jmasa@4196 145 curTL->set_hint(0);
jmasa@4196 146 break;
jmasa@4196 147 }
jmasa@4196 148 assert(hintTL->size() > curTL->size(), "hint is inconsistent");
jmasa@4196 149 if (hintTL->surplus() > 0) {
jmasa@4196 150 // The hint led to a list that has a surplus. Use it.
jmasa@4196 151 // Set the hint for the candidate to an overpopulated
jmasa@4196 152 // size.
jmasa@4196 153 curTL->set_hint(hintTL->size());
jmasa@4196 154 // Change the candidate.
jmasa@4196 155 curTL = hintTL;
jmasa@4196 156 break;
jmasa@4196 157 }
jmasa@4196 158 }
jmasa@4196 159 }
jmasa@4196 160 return curTL;
jmasa@4196 161 }
jmasa@4196 162 #endif // SERIALGC
jmasa@4196 163
jmasa@4196 164 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 165 TreeList<Chunk_t, FreeList_t>*
jmasa@4196 166 TreeList<Chunk_t, FreeList_t>::get_better_list(
jmasa@4196 167 BinaryTreeDictionary<Chunk_t, FreeList_t>* dictionary) {
jmasa@4196 168 return this;
jmasa@4196 169 }
jmasa@4196 170
jmasa@4196 171 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 172 TreeList<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::remove_chunk_replace_if_needed(TreeChunk<Chunk_t, FreeList_t>* tc) {
jmasa@4196 173
jmasa@4196 174 TreeList<Chunk_t, FreeList_t>* retTL = this;
jmasa@4196 175 Chunk_t* list = head();
duke@435 176 assert(!list || list != list->next(), "Chunk on list twice");
duke@435 177 assert(tc != NULL, "Chunk being removed is NULL");
duke@435 178 assert(parent() == NULL || this == parent()->left() ||
duke@435 179 this == parent()->right(), "list is inconsistent");
jmasa@3732 180 assert(tc->is_free(), "Header is not marked correctly");
duke@435 181 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 182 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 183
jmasa@4196 184 Chunk_t* prevFC = tc->prev();
jmasa@4196 185 TreeChunk<Chunk_t, FreeList_t>* nextTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(tc->next());
duke@435 186 assert(list != NULL, "should have at least the target chunk");
duke@435 187
duke@435 188 // Is this the first item on the list?
duke@435 189 if (tc == list) {
jmasa@4196 190 // The "getChunk..." functions for a TreeList<Chunk_t, FreeList_t> will not return the
duke@435 191 // first chunk in the list unless it is the last chunk in the list
duke@435 192 // because the first chunk is also acting as the tree node.
duke@435 193 // When coalescing happens, however, the first chunk in the a tree
duke@435 194 // list can be the start of a free range. Free ranges are removed
duke@435 195 // from the free lists so that they are not available to be
duke@435 196 // allocated when the sweeper yields (giving up the free list lock)
duke@435 197 // to allow mutator activity. If this chunk is the first in the
duke@435 198 // list and is not the last in the list, do the work to copy the
jmasa@4196 199 // TreeList<Chunk_t, FreeList_t> from the first chunk to the next chunk and update all
jmasa@4196 200 // the TreeList<Chunk_t, FreeList_t> pointers in the chunks in the list.
duke@435 201 if (nextTC == NULL) {
jcoomes@1844 202 assert(prevFC == NULL, "Not last chunk in the list");
duke@435 203 set_tail(NULL);
duke@435 204 set_head(NULL);
duke@435 205 } else {
duke@435 206 // copy embedded list.
duke@435 207 nextTC->set_embedded_list(tc->embedded_list());
duke@435 208 retTL = nextTC->embedded_list();
duke@435 209 // Fix the pointer to the list in each chunk in the list.
duke@435 210 // This can be slow for a long list. Consider having
duke@435 211 // an option that does not allow the first chunk on the
duke@435 212 // list to be coalesced.
jmasa@4196 213 for (TreeChunk<Chunk_t, FreeList_t>* curTC = nextTC; curTC != NULL;
jmasa@4196 214 curTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(curTC->next())) {
duke@435 215 curTC->set_list(retTL);
duke@435 216 }
jmasa@4196 217 // Fix the parent to point to the new TreeList<Chunk_t, FreeList_t>.
duke@435 218 if (retTL->parent() != NULL) {
duke@435 219 if (this == retTL->parent()->left()) {
jmasa@3732 220 retTL->parent()->set_left(retTL);
duke@435 221 } else {
duke@435 222 assert(this == retTL->parent()->right(), "Parent is incorrect");
jmasa@3732 223 retTL->parent()->set_right(retTL);
duke@435 224 }
duke@435 225 }
duke@435 226 // Fix the children's parent pointers to point to the
duke@435 227 // new list.
duke@435 228 assert(right() == retTL->right(), "Should have been copied");
duke@435 229 if (retTL->right() != NULL) {
jmasa@3732 230 retTL->right()->set_parent(retTL);
duke@435 231 }
duke@435 232 assert(left() == retTL->left(), "Should have been copied");
duke@435 233 if (retTL->left() != NULL) {
jmasa@3732 234 retTL->left()->set_parent(retTL);
duke@435 235 }
duke@435 236 retTL->link_head(nextTC);
jmasa@3732 237 assert(nextTC->is_free(), "Should be a free chunk");
duke@435 238 }
duke@435 239 } else {
duke@435 240 if (nextTC == NULL) {
duke@435 241 // Removing chunk at tail of list
coleenp@4265 242 this->link_tail(prevFC);
duke@435 243 }
duke@435 244 // Chunk is interior to the list
jmasa@3732 245 prevFC->link_after(nextTC);
duke@435 246 }
duke@435 247
jmasa@4196 248 // Below this point the embeded TreeList<Chunk_t, FreeList_t> being used for the
duke@435 249 // tree node may have changed. Don't use "this"
jmasa@4196 250 // TreeList<Chunk_t, FreeList_t>*.
duke@435 251 // chunk should still be a free chunk (bit set in _prev)
duke@435 252 assert(!retTL->head() || retTL->size() == retTL->head()->size(),
duke@435 253 "Wrong sized chunk in list");
duke@435 254 debug_only(
jmasa@3732 255 tc->link_prev(NULL);
jmasa@3732 256 tc->link_next(NULL);
duke@435 257 tc->set_list(NULL);
duke@435 258 bool prev_found = false;
duke@435 259 bool next_found = false;
jmasa@4196 260 for (Chunk_t* curFC = retTL->head();
duke@435 261 curFC != NULL; curFC = curFC->next()) {
duke@435 262 assert(curFC != tc, "Chunk is still in list");
duke@435 263 if (curFC == prevFC) {
duke@435 264 prev_found = true;
duke@435 265 }
duke@435 266 if (curFC == nextTC) {
duke@435 267 next_found = true;
duke@435 268 }
duke@435 269 }
duke@435 270 assert(prevFC == NULL || prev_found, "Chunk was lost from list");
duke@435 271 assert(nextTC == NULL || next_found, "Chunk was lost from list");
duke@435 272 assert(retTL->parent() == NULL ||
duke@435 273 retTL == retTL->parent()->left() ||
duke@435 274 retTL == retTL->parent()->right(),
duke@435 275 "list is inconsistent");
duke@435 276 )
duke@435 277 retTL->decrement_count();
duke@435 278
jmasa@3732 279 assert(tc->is_free(), "Should still be a free chunk");
duke@435 280 assert(retTL->head() == NULL || retTL->head()->prev() == NULL,
duke@435 281 "list invariant");
duke@435 282 assert(retTL->tail() == NULL || retTL->tail()->next() == NULL,
duke@435 283 "list invariant");
duke@435 284 return retTL;
duke@435 285 }
jmasa@3730 286
jmasa@4196 287 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 288 void TreeList<Chunk_t, FreeList_t>::return_chunk_at_tail(TreeChunk<Chunk_t, FreeList_t>* chunk) {
duke@435 289 assert(chunk != NULL, "returning NULL chunk");
duke@435 290 assert(chunk->list() == this, "list should be set for chunk");
duke@435 291 assert(tail() != NULL, "The tree list is embedded in the first chunk");
duke@435 292 // which means that the list can never be empty.
coleenp@4297 293 assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
duke@435 294 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 295 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 296
jmasa@4196 297 Chunk_t* fc = tail();
jmasa@3732 298 fc->link_after(chunk);
coleenp@4265 299 this->link_tail(chunk);
duke@435 300
duke@435 301 assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list");
jmasa@4196 302 FreeList_t<Chunk_t>::increment_count();
coleenp@4297 303 debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
duke@435 304 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 305 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 306 }
duke@435 307
duke@435 308 // Add this chunk at the head of the list. "At the head of the list"
duke@435 309 // is defined to be after the chunk pointer to by head(). This is
jmasa@4196 310 // because the TreeList<Chunk_t, FreeList_t> is embedded in the first TreeChunk<Chunk_t, FreeList_t> in the
jmasa@4196 311 // list. See the definition of TreeChunk<Chunk_t, FreeList_t>.
jmasa@4196 312 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 313 void TreeList<Chunk_t, FreeList_t>::return_chunk_at_head(TreeChunk<Chunk_t, FreeList_t>* chunk) {
duke@435 314 assert(chunk->list() == this, "list should be set for chunk");
duke@435 315 assert(head() != NULL, "The tree list is embedded in the first chunk");
duke@435 316 assert(chunk != NULL, "returning NULL chunk");
coleenp@4297 317 assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
duke@435 318 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 319 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 320
jmasa@4196 321 Chunk_t* fc = head()->next();
duke@435 322 if (fc != NULL) {
jmasa@3732 323 chunk->link_after(fc);
duke@435 324 } else {
duke@435 325 assert(tail() == NULL, "List is inconsistent");
coleenp@4265 326 this->link_tail(chunk);
duke@435 327 }
jmasa@3732 328 head()->link_after(chunk);
duke@435 329 assert(!head() || size() == head()->size(), "Wrong sized chunk in list");
jmasa@4196 330 FreeList_t<Chunk_t>::increment_count();
coleenp@4297 331 debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
duke@435 332 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 333 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 334 }
duke@435 335
jmasa@4196 336 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 337 void TreeChunk<Chunk_t, FreeList_t>::assert_is_mangled() const {
jmasa@4196 338 assert((ZapUnusedHeapArea &&
jmasa@4196 339 SpaceMangler::is_mangled((HeapWord*) Chunk_t::size_addr()) &&
jmasa@4196 340 SpaceMangler::is_mangled((HeapWord*) Chunk_t::prev_addr()) &&
jmasa@4196 341 SpaceMangler::is_mangled((HeapWord*) Chunk_t::next_addr())) ||
jmasa@4196 342 (size() == 0 && prev() == NULL && next() == NULL),
jmasa@4196 343 "Space should be clear or mangled");
duke@435 344 }
duke@435 345
jmasa@4196 346 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 347 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::head_as_TreeChunk() {
jmasa@4196 348 assert(head() == NULL || (TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head())->list() == this),
jmasa@4196 349 "Wrong type of chunk?");
jmasa@4196 350 return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head());
jmasa@4196 351 }
jmasa@4196 352
jmasa@4196 353 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 354 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::first_available() {
ysr@2132 355 assert(head() != NULL, "The head of the list cannot be NULL");
jmasa@4196 356 Chunk_t* fc = head()->next();
jmasa@4196 357 TreeChunk<Chunk_t, FreeList_t>* retTC;
duke@435 358 if (fc == NULL) {
duke@435 359 retTC = head_as_TreeChunk();
duke@435 360 } else {
jmasa@4196 361 retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);
duke@435 362 }
duke@435 363 assert(retTC->list() == this, "Wrong type of chunk.");
duke@435 364 return retTC;
duke@435 365 }
duke@435 366
ysr@1580 367 // Returns the block with the largest heap address amongst
ysr@1580 368 // those in the list for this size; potentially slow and expensive,
ysr@1580 369 // use with caution!
jmasa@4196 370 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 371 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::largest_address() {
ysr@2132 372 assert(head() != NULL, "The head of the list cannot be NULL");
jmasa@4196 373 Chunk_t* fc = head()->next();
jmasa@4196 374 TreeChunk<Chunk_t, FreeList_t>* retTC;
ysr@1580 375 if (fc == NULL) {
ysr@1580 376 retTC = head_as_TreeChunk();
ysr@1580 377 } else {
ysr@1580 378 // walk down the list and return the one with the highest
ysr@1580 379 // heap address among chunks of this size.
jmasa@4196 380 Chunk_t* last = fc;
ysr@1580 381 while (fc->next() != NULL) {
ysr@1580 382 if ((HeapWord*)last < (HeapWord*)fc) {
ysr@1580 383 last = fc;
ysr@1580 384 }
ysr@1580 385 fc = fc->next();
ysr@1580 386 }
jmasa@4196 387 retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(last);
ysr@1580 388 }
ysr@1580 389 assert(retTC->list() == this, "Wrong type of chunk.");
ysr@1580 390 return retTC;
ysr@1580 391 }
ysr@1580 392
jmasa@4196 393 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 394 BinaryTreeDictionary<Chunk_t, FreeList_t>::BinaryTreeDictionary(MemRegion mr) {
jmasa@4196 395 assert((mr.byte_size() > min_size()), "minimum chunk size");
duke@435 396
duke@435 397 reset(mr);
duke@435 398 assert(root()->left() == NULL, "reset check failed");
duke@435 399 assert(root()->right() == NULL, "reset check failed");
duke@435 400 assert(root()->head()->next() == NULL, "reset check failed");
duke@435 401 assert(root()->head()->prev() == NULL, "reset check failed");
jmasa@3732 402 assert(total_size() == root()->size(), "reset check failed");
jmasa@3732 403 assert(total_free_blocks() == 1, "reset check failed");
duke@435 404 }
duke@435 405
jmasa@4196 406 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 407 void BinaryTreeDictionary<Chunk_t, FreeList_t>::inc_total_size(size_t inc) {
jmasa@3732 408 _total_size = _total_size + inc;
duke@435 409 }
duke@435 410
jmasa@4196 411 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 412 void BinaryTreeDictionary<Chunk_t, FreeList_t>::dec_total_size(size_t dec) {
jmasa@3732 413 _total_size = _total_size - dec;
duke@435 414 }
duke@435 415
jmasa@4196 416 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 417 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(MemRegion mr) {
jmasa@4196 418 assert((mr.byte_size() > min_size()), "minimum chunk size");
jmasa@4196 419 set_root(TreeList<Chunk_t, FreeList_t>::as_TreeList(mr.start(), mr.word_size()));
jmasa@3732 420 set_total_size(mr.word_size());
jmasa@3732 421 set_total_free_blocks(1);
duke@435 422 }
duke@435 423
jmasa@4196 424 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 425 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(HeapWord* addr, size_t byte_size) {
duke@435 426 MemRegion mr(addr, heap_word_size(byte_size));
duke@435 427 reset(mr);
duke@435 428 }
duke@435 429
jmasa@4196 430 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 431 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset() {
duke@435 432 set_root(NULL);
jmasa@3732 433 set_total_size(0);
jmasa@3732 434 set_total_free_blocks(0);
duke@435 435 }
duke@435 436
duke@435 437 // Get a free block of size at least size from tree, or NULL.
jmasa@4196 438 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 439 TreeChunk<Chunk_t, FreeList_t>*
jmasa@4196 440 BinaryTreeDictionary<Chunk_t, FreeList_t>::get_chunk_from_tree(
jmasa@4196 441 size_t size,
jmasa@4196 442 enum FreeBlockDictionary<Chunk_t>::Dither dither)
duke@435 443 {
jmasa@4196 444 TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
jmasa@4196 445 TreeChunk<Chunk_t, FreeList_t>* retTC = NULL;
jmasa@4196 446
jmasa@4196 447 assert((size >= min_size()), "minimum chunk size");
duke@435 448 if (FLSVerifyDictionary) {
jmasa@3732 449 verify_tree();
duke@435 450 }
duke@435 451 // starting at the root, work downwards trying to find match.
duke@435 452 // Remember the last node of size too great or too small.
duke@435 453 for (prevTL = curTL = root(); curTL != NULL;) {
duke@435 454 if (curTL->size() == size) { // exact match
duke@435 455 break;
duke@435 456 }
duke@435 457 prevTL = curTL;
duke@435 458 if (curTL->size() < size) { // proceed to right sub-tree
duke@435 459 curTL = curTL->right();
duke@435 460 } else { // proceed to left sub-tree
duke@435 461 assert(curTL->size() > size, "size inconsistency");
duke@435 462 curTL = curTL->left();
duke@435 463 }
duke@435 464 }
duke@435 465 if (curTL == NULL) { // couldn't find exact match
jmasa@3730 466
jmasa@4196 467 if (dither == FreeBlockDictionary<Chunk_t>::exactly) return NULL;
jmasa@3730 468
duke@435 469 // try and find the next larger size by walking back up the search path
duke@435 470 for (curTL = prevTL; curTL != NULL;) {
duke@435 471 if (curTL->size() >= size) break;
duke@435 472 else curTL = curTL->parent();
duke@435 473 }
duke@435 474 assert(curTL == NULL || curTL->count() > 0,
duke@435 475 "An empty list should not be in the tree");
duke@435 476 }
duke@435 477 if (curTL != NULL) {
duke@435 478 assert(curTL->size() >= size, "size inconsistency");
duke@435 479
jmasa@4196 480 curTL = curTL->get_better_list(this);
jmasa@4196 481
duke@435 482 retTC = curTL->first_available();
duke@435 483 assert((retTC != NULL) && (curTL->count() > 0),
duke@435 484 "A list in the binary tree should not be NULL");
duke@435 485 assert(retTC->size() >= size,
duke@435 486 "A chunk of the wrong size was found");
jmasa@3732 487 remove_chunk_from_tree(retTC);
jmasa@3732 488 assert(retTC->is_free(), "Header is not marked correctly");
duke@435 489 }
duke@435 490
duke@435 491 if (FLSVerifyDictionary) {
duke@435 492 verify();
duke@435 493 }
duke@435 494 return retTC;
duke@435 495 }
duke@435 496
jmasa@4196 497 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 498 TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_list(size_t size) const {
jmasa@4196 499 TreeList<Chunk_t, FreeList_t>* curTL;
duke@435 500 for (curTL = root(); curTL != NULL;) {
duke@435 501 if (curTL->size() == size) { // exact match
duke@435 502 break;
duke@435 503 }
duke@435 504
duke@435 505 if (curTL->size() < size) { // proceed to right sub-tree
duke@435 506 curTL = curTL->right();
duke@435 507 } else { // proceed to left sub-tree
duke@435 508 assert(curTL->size() > size, "size inconsistency");
duke@435 509 curTL = curTL->left();
duke@435 510 }
duke@435 511 }
duke@435 512 return curTL;
duke@435 513 }
duke@435 514
duke@435 515
jmasa@4196 516 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 517 bool BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_chunk_in_free_list(Chunk_t* tc) const {
duke@435 518 size_t size = tc->size();
jmasa@4196 519 TreeList<Chunk_t, FreeList_t>* tl = find_list(size);
duke@435 520 if (tl == NULL) {
duke@435 521 return false;
duke@435 522 } else {
jmasa@3732 523 return tl->verify_chunk_in_free_list(tc);
duke@435 524 }
duke@435 525 }
duke@435 526
jmasa@4196 527 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 528 Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_largest_dict() const {
jmasa@4196 529 TreeList<Chunk_t, FreeList_t> *curTL = root();
duke@435 530 if (curTL != NULL) {
duke@435 531 while(curTL->right() != NULL) curTL = curTL->right();
ysr@1580 532 return curTL->largest_address();
duke@435 533 } else {
duke@435 534 return NULL;
duke@435 535 }
duke@435 536 }
duke@435 537
duke@435 538 // Remove the current chunk from the tree. If it is not the last
duke@435 539 // chunk in a list on a tree node, just unlink it.
duke@435 540 // If it is the last chunk in the list (the next link is NULL),
duke@435 541 // remove the node and repair the tree.
jmasa@4196 542 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 543 TreeChunk<Chunk_t, FreeList_t>*
jmasa@4196 544 BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_chunk_from_tree(TreeChunk<Chunk_t, FreeList_t>* tc) {
duke@435 545 assert(tc != NULL, "Should not call with a NULL chunk");
jmasa@3732 546 assert(tc->is_free(), "Header is not marked correctly");
duke@435 547
jmasa@4196 548 TreeList<Chunk_t, FreeList_t> *newTL, *parentTL;
jmasa@4196 549 TreeChunk<Chunk_t, FreeList_t>* retTC;
jmasa@4196 550 TreeList<Chunk_t, FreeList_t>* tl = tc->list();
duke@435 551 debug_only(
duke@435 552 bool removing_only_chunk = false;
duke@435 553 if (tl == _root) {
duke@435 554 if ((_root->left() == NULL) && (_root->right() == NULL)) {
duke@435 555 if (_root->count() == 1) {
duke@435 556 assert(_root->head() == tc, "Should only be this one chunk");
duke@435 557 removing_only_chunk = true;
duke@435 558 }
duke@435 559 }
duke@435 560 }
duke@435 561 )
duke@435 562 assert(tl != NULL, "List should be set");
duke@435 563 assert(tl->parent() == NULL || tl == tl->parent()->left() ||
duke@435 564 tl == tl->parent()->right(), "list is inconsistent");
duke@435 565
jmasa@3732 566 bool complicated_splice = false;
duke@435 567
duke@435 568 retTC = tc;
duke@435 569 // Removing this chunk can have the side effect of changing the node
jmasa@4196 570 // (TreeList<Chunk_t, FreeList_t>*) in the tree. If the node is the root, update it.
jmasa@4196 571 TreeList<Chunk_t, FreeList_t>* replacementTL = tl->remove_chunk_replace_if_needed(tc);
jmasa@3732 572 assert(tc->is_free(), "Chunk should still be free");
duke@435 573 assert(replacementTL->parent() == NULL ||
duke@435 574 replacementTL == replacementTL->parent()->left() ||
duke@435 575 replacementTL == replacementTL->parent()->right(),
duke@435 576 "list is inconsistent");
duke@435 577 if (tl == root()) {
duke@435 578 assert(replacementTL->parent() == NULL, "Incorrectly replacing root");
duke@435 579 set_root(replacementTL);
duke@435 580 }
jmasa@4196 581 #ifdef ASSERT
duke@435 582 if (tl != replacementTL) {
duke@435 583 assert(replacementTL->head() != NULL,
duke@435 584 "If the tree list was replaced, it should not be a NULL list");
jmasa@4196 585 TreeList<Chunk_t, FreeList_t>* rhl = replacementTL->head_as_TreeChunk()->list();
jmasa@4196 586 TreeList<Chunk_t, FreeList_t>* rtl =
jmasa@4196 587 TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(replacementTL->tail())->list();
duke@435 588 assert(rhl == replacementTL, "Broken head");
duke@435 589 assert(rtl == replacementTL, "Broken tail");
duke@435 590 assert(replacementTL->size() == tc->size(), "Broken size");
duke@435 591 }
jmasa@4196 592 #endif
duke@435 593
duke@435 594 // Does the tree need to be repaired?
duke@435 595 if (replacementTL->count() == 0) {
duke@435 596 assert(replacementTL->head() == NULL &&
duke@435 597 replacementTL->tail() == NULL, "list count is incorrect");
duke@435 598 // Find the replacement node for the (soon to be empty) node being removed.
duke@435 599 // if we have a single (or no) child, splice child in our stead
duke@435 600 if (replacementTL->left() == NULL) {
duke@435 601 // left is NULL so pick right. right may also be NULL.
duke@435 602 newTL = replacementTL->right();
jmasa@3732 603 debug_only(replacementTL->clear_right();)
duke@435 604 } else if (replacementTL->right() == NULL) {
duke@435 605 // right is NULL
duke@435 606 newTL = replacementTL->left();
jmasa@4196 607 debug_only(replacementTL->clear_left();)
duke@435 608 } else { // we have both children, so, by patriarchal convention,
duke@435 609 // my replacement is least node in right sub-tree
jmasa@3732 610 complicated_splice = true;
jmasa@3732 611 newTL = remove_tree_minimum(replacementTL->right());
duke@435 612 assert(newTL != NULL && newTL->left() == NULL &&
duke@435 613 newTL->right() == NULL, "sub-tree minimum exists");
duke@435 614 }
duke@435 615 // newTL is the replacement for the (soon to be empty) node.
duke@435 616 // newTL may be NULL.
duke@435 617 // should verify; we just cleanly excised our replacement
duke@435 618 if (FLSVerifyDictionary) {
jmasa@3732 619 verify_tree();
duke@435 620 }
duke@435 621 // first make newTL my parent's child
duke@435 622 if ((parentTL = replacementTL->parent()) == NULL) {
duke@435 623 // newTL should be root
duke@435 624 assert(tl == root(), "Incorrectly replacing root");
duke@435 625 set_root(newTL);
duke@435 626 if (newTL != NULL) {
jmasa@3732 627 newTL->clear_parent();
duke@435 628 }
duke@435 629 } else if (parentTL->right() == replacementTL) {
duke@435 630 // replacementTL is a right child
jmasa@3732 631 parentTL->set_right(newTL);
duke@435 632 } else { // replacementTL is a left child
duke@435 633 assert(parentTL->left() == replacementTL, "should be left child");
jmasa@3732 634 parentTL->set_left(newTL);
duke@435 635 }
jmasa@3732 636 debug_only(replacementTL->clear_parent();)
jmasa@3732 637 if (complicated_splice) { // we need newTL to get replacementTL's
duke@435 638 // two children
duke@435 639 assert(newTL != NULL &&
duke@435 640 newTL->left() == NULL && newTL->right() == NULL,
duke@435 641 "newTL should not have encumbrances from the past");
duke@435 642 // we'd like to assert as below:
duke@435 643 // assert(replacementTL->left() != NULL && replacementTL->right() != NULL,
jmasa@3732 644 // "else !complicated_splice");
duke@435 645 // ... however, the above assertion is too strong because we aren't
duke@435 646 // guaranteed that replacementTL->right() is still NULL.
duke@435 647 // Recall that we removed
duke@435 648 // the right sub-tree minimum from replacementTL.
duke@435 649 // That may well have been its right
duke@435 650 // child! So we'll just assert half of the above:
jmasa@3732 651 assert(replacementTL->left() != NULL, "else !complicated_splice");
jmasa@3732 652 newTL->set_left(replacementTL->left());
jmasa@3732 653 newTL->set_right(replacementTL->right());
duke@435 654 debug_only(
jmasa@3732 655 replacementTL->clear_right();
jmasa@4196 656 replacementTL->clear_left();
duke@435 657 )
duke@435 658 }
duke@435 659 assert(replacementTL->right() == NULL &&
duke@435 660 replacementTL->left() == NULL &&
duke@435 661 replacementTL->parent() == NULL,
duke@435 662 "delete without encumbrances");
duke@435 663 }
duke@435 664
jmasa@3732 665 assert(total_size() >= retTC->size(), "Incorrect total size");
jmasa@3732 666 dec_total_size(retTC->size()); // size book-keeping
jmasa@3732 667 assert(total_free_blocks() > 0, "Incorrect total count");
jmasa@3732 668 set_total_free_blocks(total_free_blocks() - 1);
duke@435 669
duke@435 670 assert(retTC != NULL, "null chunk?");
duke@435 671 assert(retTC->prev() == NULL && retTC->next() == NULL,
duke@435 672 "should return without encumbrances");
duke@435 673 if (FLSVerifyDictionary) {
jmasa@3732 674 verify_tree();
duke@435 675 }
duke@435 676 assert(!removing_only_chunk || _root == NULL, "root should be NULL");
jmasa@4196 677 return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(retTC);
duke@435 678 }
duke@435 679
duke@435 680 // Remove the leftmost node (lm) in the tree and return it.
duke@435 681 // If lm has a right child, link it to the left node of
duke@435 682 // the parent of lm.
jmasa@4196 683 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 684 TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_tree_minimum(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 685 assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree");
duke@435 686 // locate the subtree minimum by walking down left branches
jmasa@4196 687 TreeList<Chunk_t, FreeList_t>* curTL = tl;
duke@435 688 for (; curTL->left() != NULL; curTL = curTL->left());
duke@435 689 // obviously curTL now has at most one child, a right child
duke@435 690 if (curTL != root()) { // Should this test just be removed?
jmasa@4196 691 TreeList<Chunk_t, FreeList_t>* parentTL = curTL->parent();
duke@435 692 if (parentTL->left() == curTL) { // curTL is a left child
jmasa@3732 693 parentTL->set_left(curTL->right());
duke@435 694 } else {
duke@435 695 // If the list tl has no left child, then curTL may be
duke@435 696 // the right child of parentTL.
duke@435 697 assert(parentTL->right() == curTL, "should be a right child");
jmasa@3732 698 parentTL->set_right(curTL->right());
duke@435 699 }
duke@435 700 } else {
duke@435 701 // The only use of this method would not pass the root of the
duke@435 702 // tree (as indicated by the assertion above that the tree list
duke@435 703 // has a parent) but the specification does not explicitly exclude the
duke@435 704 // passing of the root so accomodate it.
duke@435 705 set_root(NULL);
duke@435 706 }
duke@435 707 debug_only(
jmasa@3732 708 curTL->clear_parent(); // Test if this needs to be cleared
jmasa@3732 709 curTL->clear_right(); // recall, above, left child is already null
duke@435 710 )
duke@435 711 // we just excised a (non-root) node, we should still verify all tree invariants
duke@435 712 if (FLSVerifyDictionary) {
jmasa@3732 713 verify_tree();
duke@435 714 }
duke@435 715 return curTL;
duke@435 716 }
duke@435 717
jmasa@4196 718 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 719 void BinaryTreeDictionary<Chunk_t, FreeList_t>::insert_chunk_in_tree(Chunk_t* fc) {
jmasa@4196 720 TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
duke@435 721 size_t size = fc->size();
duke@435 722
jmasa@4196 723 assert((size >= min_size()),
jmasa@4196 724 err_msg(SIZE_FORMAT " is too small to be a TreeChunk<Chunk_t, FreeList_t> " SIZE_FORMAT,
jmasa@4196 725 size, min_size()));
duke@435 726 if (FLSVerifyDictionary) {
jmasa@3732 727 verify_tree();
duke@435 728 }
duke@435 729
jmasa@3732 730 fc->clear_next();
jmasa@3732 731 fc->link_prev(NULL);
duke@435 732
duke@435 733 // work down from the _root, looking for insertion point
duke@435 734 for (prevTL = curTL = root(); curTL != NULL;) {
duke@435 735 if (curTL->size() == size) // exact match
duke@435 736 break;
duke@435 737 prevTL = curTL;
duke@435 738 if (curTL->size() > size) { // follow left branch
duke@435 739 curTL = curTL->left();
duke@435 740 } else { // follow right branch
duke@435 741 assert(curTL->size() < size, "size inconsistency");
duke@435 742 curTL = curTL->right();
duke@435 743 }
duke@435 744 }
jmasa@4196 745 TreeChunk<Chunk_t, FreeList_t>* tc = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);
ysr@1580 746 // This chunk is being returned to the binary tree. Its embedded
jmasa@4196 747 // TreeList<Chunk_t, FreeList_t> should be unused at this point.
duke@435 748 tc->initialize();
duke@435 749 if (curTL != NULL) { // exact match
duke@435 750 tc->set_list(curTL);
jmasa@3732 751 curTL->return_chunk_at_tail(tc);
duke@435 752 } else { // need a new node in tree
jmasa@3732 753 tc->clear_next();
jmasa@3732 754 tc->link_prev(NULL);
jmasa@4196 755 TreeList<Chunk_t, FreeList_t>* newTL = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);
jmasa@4196 756 assert(((TreeChunk<Chunk_t, FreeList_t>*)tc)->list() == newTL,
duke@435 757 "List was not initialized correctly");
duke@435 758 if (prevTL == NULL) { // we are the only tree node
duke@435 759 assert(root() == NULL, "control point invariant");
duke@435 760 set_root(newTL);
duke@435 761 } else { // insert under prevTL ...
duke@435 762 if (prevTL->size() < size) { // am right child
duke@435 763 assert(prevTL->right() == NULL, "control point invariant");
jmasa@3732 764 prevTL->set_right(newTL);
duke@435 765 } else { // am left child
duke@435 766 assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv");
jmasa@3732 767 prevTL->set_left(newTL);
duke@435 768 }
duke@435 769 }
duke@435 770 }
duke@435 771 assert(tc->list() != NULL, "Tree list should be set");
duke@435 772
jmasa@3732 773 inc_total_size(size);
jmasa@3732 774 // Method 'total_size_in_tree' walks through the every block in the
duke@435 775 // tree, so it can cause significant performance loss if there are
duke@435 776 // many blocks in the tree
jmasa@3732 777 assert(!FLSVerifyDictionary || total_size_in_tree(root()) == total_size(), "_total_size inconsistency");
jmasa@3732 778 set_total_free_blocks(total_free_blocks() + 1);
duke@435 779 if (FLSVerifyDictionary) {
jmasa@3732 780 verify_tree();
duke@435 781 }
duke@435 782 }
duke@435 783
jmasa@4196 784 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 785 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::max_chunk_size() const {
jmasa@4196 786 FreeBlockDictionary<Chunk_t>::verify_par_locked();
jmasa@4196 787 TreeList<Chunk_t, FreeList_t>* tc = root();
duke@435 788 if (tc == NULL) return 0;
duke@435 789 for (; tc->right() != NULL; tc = tc->right());
duke@435 790 return tc->size();
duke@435 791 }
duke@435 792
jmasa@4196 793 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 794 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_list_length(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 795 size_t res;
duke@435 796 res = tl->count();
duke@435 797 #ifdef ASSERT
duke@435 798 size_t cnt;
jmasa@4196 799 Chunk_t* tc = tl->head();
duke@435 800 for (cnt = 0; tc != NULL; tc = tc->next(), cnt++);
duke@435 801 assert(res == cnt, "The count is not being maintained correctly");
duke@435 802 #endif
duke@435 803 return res;
duke@435 804 }
duke@435 805
jmasa@4196 806 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 807 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_size_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 808 if (tl == NULL)
duke@435 809 return 0;
jmasa@3732 810 return (tl->size() * total_list_length(tl)) +
jmasa@3732 811 total_size_in_tree(tl->left()) +
jmasa@3732 812 total_size_in_tree(tl->right());
duke@435 813 }
duke@435 814
jmasa@4196 815 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 816 double BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_of_squared_block_sizes(TreeList<Chunk_t, FreeList_t>* const tl) const {
duke@435 817 if (tl == NULL) {
duke@435 818 return 0.0;
duke@435 819 }
duke@435 820 double size = (double)(tl->size());
jmasa@3732 821 double curr = size * size * total_list_length(tl);
duke@435 822 curr += sum_of_squared_block_sizes(tl->left());
duke@435 823 curr += sum_of_squared_block_sizes(tl->right());
duke@435 824 return curr;
duke@435 825 }
duke@435 826
jmasa@4196 827 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 828 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_free_blocks_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 829 if (tl == NULL)
duke@435 830 return 0;
jmasa@3732 831 return total_list_length(tl) +
jmasa@3732 832 total_free_blocks_in_tree(tl->left()) +
jmasa@3732 833 total_free_blocks_in_tree(tl->right());
duke@435 834 }
duke@435 835
jmasa@4196 836 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 837 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::num_free_blocks() const {
jmasa@3732 838 assert(total_free_blocks_in_tree(root()) == total_free_blocks(),
jmasa@3732 839 "_total_free_blocks inconsistency");
jmasa@3732 840 return total_free_blocks();
duke@435 841 }
duke@435 842
jmasa@4196 843 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 844 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 845 if (tl == NULL)
duke@435 846 return 0;
jmasa@3732 847 return 1 + MAX2(tree_height_helper(tl->left()),
jmasa@3732 848 tree_height_helper(tl->right()));
duke@435 849 }
duke@435 850
jmasa@4196 851 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 852 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height() const {
jmasa@3732 853 return tree_height_helper(root());
duke@435 854 }
duke@435 855
jmasa@4196 856 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 857 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 858 if (tl == NULL) {
duke@435 859 return 0;
duke@435 860 }
jmasa@3732 861 return 1 + total_nodes_helper(tl->left()) +
jmasa@3732 862 total_nodes_helper(tl->right());
duke@435 863 }
duke@435 864
jmasa@4196 865 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 866 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
jmasa@3732 867 return total_nodes_helper(root());
duke@435 868 }
duke@435 869
jmasa@4196 870 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 871 void BinaryTreeDictionary<Chunk_t, FreeList_t>::dict_census_update(size_t size, bool split, bool birth){}
jmasa@4196 872
jmasa@4196 873 #ifndef SERIALGC
jmasa@4196 874 template <>
jmasa@4196 875 void BinaryTreeDictionary<FreeChunk, AdaptiveFreeList>::dict_census_update(size_t size, bool split, bool birth){
jmasa@4196 876 TreeList<FreeChunk, AdaptiveFreeList>* nd = find_list(size);
duke@435 877 if (nd) {
duke@435 878 if (split) {
duke@435 879 if (birth) {
jmasa@3732 880 nd->increment_split_births();
duke@435 881 nd->increment_surplus();
duke@435 882 } else {
jmasa@3732 883 nd->increment_split_deaths();
duke@435 884 nd->decrement_surplus();
duke@435 885 }
duke@435 886 } else {
duke@435 887 if (birth) {
jmasa@3732 888 nd->increment_coal_births();
duke@435 889 nd->increment_surplus();
duke@435 890 } else {
jmasa@3732 891 nd->increment_coal_deaths();
duke@435 892 nd->decrement_surplus();
duke@435 893 }
duke@435 894 }
duke@435 895 }
duke@435 896 // A list for this size may not be found (nd == 0) if
duke@435 897 // This is a death where the appropriate list is now
duke@435 898 // empty and has been removed from the list.
duke@435 899 // This is a birth associated with a LinAB. The chunk
duke@435 900 // for the LinAB is not in the dictionary.
duke@435 901 }
jmasa@4196 902 #endif // SERIALGC
duke@435 903
jmasa@4196 904 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 905 bool BinaryTreeDictionary<Chunk_t, FreeList_t>::coal_dict_over_populated(size_t size) {
jmasa@4196 906 // For the general type of freelists, encourage coalescing by
jmasa@4196 907 // returning true.
jmasa@4196 908 return true;
jmasa@4196 909 }
jmasa@4196 910
jmasa@4196 911 #ifndef SERIALGC
jmasa@4196 912 template <>
jmasa@4196 913 bool BinaryTreeDictionary<FreeChunk, AdaptiveFreeList>::coal_dict_over_populated(size_t size) {
ysr@1580 914 if (FLSAlwaysCoalesceLarge) return true;
ysr@1580 915
jmasa@4196 916 TreeList<FreeChunk, AdaptiveFreeList>* list_of_size = find_list(size);
duke@435 917 // None of requested size implies overpopulated.
jmasa@3732 918 return list_of_size == NULL || list_of_size->coal_desired() <= 0 ||
jmasa@3732 919 list_of_size->count() > list_of_size->coal_desired();
duke@435 920 }
jmasa@4196 921 #endif // SERIALGC
duke@435 922
duke@435 923 // Closures for walking the binary tree.
duke@435 924 // do_list() walks the free list in a node applying the closure
duke@435 925 // to each free chunk in the list
duke@435 926 // do_tree() walks the nodes in the binary tree applying do_list()
duke@435 927 // to each list at each node.
duke@435 928
jmasa@4196 929 template <class Chunk_t, template <class> class FreeList_t>
duke@435 930 class TreeCensusClosure : public StackObj {
duke@435 931 protected:
jmasa@4196 932 virtual void do_list(FreeList_t<Chunk_t>* fl) = 0;
duke@435 933 public:
jmasa@4196 934 virtual void do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
duke@435 935 };
duke@435 936
jmasa@4196 937 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 938 class AscendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 939 public:
jmasa@4196 940 void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 941 if (tl != NULL) {
duke@435 942 do_tree(tl->left());
coleenp@4265 943 this->do_list(tl);
duke@435 944 do_tree(tl->right());
duke@435 945 }
duke@435 946 }
duke@435 947 };
duke@435 948
jmasa@4196 949 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 950 class DescendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 951 public:
jmasa@4196 952 void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 953 if (tl != NULL) {
duke@435 954 do_tree(tl->right());
coleenp@4265 955 this->do_list(tl);
duke@435 956 do_tree(tl->left());
duke@435 957 }
duke@435 958 }
duke@435 959 };
duke@435 960
duke@435 961 // For each list in the tree, calculate the desired, desired
duke@435 962 // coalesce, count before sweep, and surplus before sweep.
jmasa@4196 963 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 964 class BeginSweepClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 965 double _percentage;
duke@435 966 float _inter_sweep_current;
duke@435 967 float _inter_sweep_estimate;
ysr@1580 968 float _intra_sweep_estimate;
duke@435 969
duke@435 970 public:
duke@435 971 BeginSweepClosure(double p, float inter_sweep_current,
ysr@1580 972 float inter_sweep_estimate,
ysr@1580 973 float intra_sweep_estimate) :
duke@435 974 _percentage(p),
duke@435 975 _inter_sweep_current(inter_sweep_current),
ysr@1580 976 _inter_sweep_estimate(inter_sweep_estimate),
ysr@1580 977 _intra_sweep_estimate(intra_sweep_estimate) { }
duke@435 978
jmasa@4196 979 void do_list(FreeList<Chunk_t>* fl) {}
jmasa@4196 980
jmasa@4196 981 #ifndef SERIALGC
jmasa@4196 982 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
duke@435 983 double coalSurplusPercent = _percentage;
ysr@1580 984 fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate, _intra_sweep_estimate);
jmasa@3732 985 fl->set_coal_desired((ssize_t)((double)fl->desired() * coalSurplusPercent));
jmasa@3732 986 fl->set_before_sweep(fl->count());
jmasa@3732 987 fl->set_bfr_surp(fl->surplus());
duke@435 988 }
jmasa@4196 989 #endif // SERIALGC
duke@435 990 };
duke@435 991
duke@435 992 // Used to search the tree until a condition is met.
duke@435 993 // Similar to TreeCensusClosure but searches the
duke@435 994 // tree and returns promptly when found.
duke@435 995
jmasa@4196 996 template <class Chunk_t, template <class> class FreeList_t>
duke@435 997 class TreeSearchClosure : public StackObj {
duke@435 998 protected:
jmasa@4196 999 virtual bool do_list(FreeList_t<Chunk_t>* fl) = 0;
duke@435 1000 public:
jmasa@4196 1001 virtual bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
duke@435 1002 };
duke@435 1003
duke@435 1004 #if 0 // Don't need this yet but here for symmetry.
jmasa@4196 1005 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1006 class AscendTreeSearchClosure : public TreeSearchClosure<Chunk_t> {
duke@435 1007 public:
jmasa@4196 1008 bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 1009 if (tl != NULL) {
duke@435 1010 if (do_tree(tl->left())) return true;
duke@435 1011 if (do_list(tl)) return true;
duke@435 1012 if (do_tree(tl->right())) return true;
duke@435 1013 }
duke@435 1014 return false;
duke@435 1015 }
duke@435 1016 };
duke@435 1017 #endif
duke@435 1018
jmasa@4196 1019 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1020 class DescendTreeSearchClosure : public TreeSearchClosure<Chunk_t, FreeList_t> {
duke@435 1021 public:
jmasa@4196 1022 bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 1023 if (tl != NULL) {
duke@435 1024 if (do_tree(tl->right())) return true;
coleenp@4265 1025 if (this->do_list(tl)) return true;
duke@435 1026 if (do_tree(tl->left())) return true;
duke@435 1027 }
duke@435 1028 return false;
duke@435 1029 }
duke@435 1030 };
duke@435 1031
duke@435 1032 // Searches the tree for a chunk that ends at the
duke@435 1033 // specified address.
jmasa@4196 1034 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1035 class EndTreeSearchClosure : public DescendTreeSearchClosure<Chunk_t, FreeList_t> {
duke@435 1036 HeapWord* _target;
jmasa@4196 1037 Chunk_t* _found;
duke@435 1038
duke@435 1039 public:
duke@435 1040 EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {}
jmasa@4196 1041 bool do_list(FreeList_t<Chunk_t>* fl) {
jmasa@4196 1042 Chunk_t* item = fl->head();
duke@435 1043 while (item != NULL) {
jmasa@4196 1044 if (item->end() == (uintptr_t*) _target) {
duke@435 1045 _found = item;
duke@435 1046 return true;
duke@435 1047 }
duke@435 1048 item = item->next();
duke@435 1049 }
duke@435 1050 return false;
duke@435 1051 }
jmasa@4196 1052 Chunk_t* found() { return _found; }
duke@435 1053 };
duke@435 1054
jmasa@4196 1055 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1056 Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_chunk_ends_at(HeapWord* target) const {
jmasa@4196 1057 EndTreeSearchClosure<Chunk_t, FreeList_t> etsc(target);
duke@435 1058 bool found_target = etsc.do_tree(root());
duke@435 1059 assert(found_target || etsc.found() == NULL, "Consistency check");
duke@435 1060 assert(!found_target || etsc.found() != NULL, "Consistency check");
duke@435 1061 return etsc.found();
duke@435 1062 }
duke@435 1063
jmasa@4196 1064 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1065 void BinaryTreeDictionary<Chunk_t, FreeList_t>::begin_sweep_dict_census(double coalSurplusPercent,
ysr@1580 1066 float inter_sweep_current, float inter_sweep_estimate, float intra_sweep_estimate) {
jmasa@4196 1067 BeginSweepClosure<Chunk_t, FreeList_t> bsc(coalSurplusPercent, inter_sweep_current,
ysr@1580 1068 inter_sweep_estimate,
ysr@1580 1069 intra_sweep_estimate);
duke@435 1070 bsc.do_tree(root());
duke@435 1071 }
duke@435 1072
duke@435 1073 // Closures and methods for calculating total bytes returned to the
duke@435 1074 // free lists in the tree.
jmasa@3730 1075 #ifndef PRODUCT
jmasa@4196 1076 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1077 class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 1078 public:
jmasa@4196 1079 void do_list(FreeList_t<Chunk_t>* fl) {
jmasa@3732 1080 fl->set_returned_bytes(0);
jmasa@3730 1081 }
jmasa@3730 1082 };
duke@435 1083
jmasa@4196 1084 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1085 void BinaryTreeDictionary<Chunk_t, FreeList_t>::initialize_dict_returned_bytes() {
jmasa@4196 1086 InitializeDictReturnedBytesClosure<Chunk_t, FreeList_t> idrb;
jmasa@3730 1087 idrb.do_tree(root());
jmasa@3730 1088 }
jmasa@3730 1089
jmasa@4196 1090 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1091 class ReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
jmasa@3732 1092 size_t _dict_returned_bytes;
jmasa@3730 1093 public:
jmasa@3732 1094 ReturnedBytesClosure() { _dict_returned_bytes = 0; }
jmasa@4196 1095 void do_list(FreeList_t<Chunk_t>* fl) {
jmasa@3732 1096 _dict_returned_bytes += fl->returned_bytes();
duke@435 1097 }
jmasa@3732 1098 size_t dict_returned_bytes() { return _dict_returned_bytes; }
jmasa@3730 1099 };
duke@435 1100
jmasa@4196 1101 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1102 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_dict_returned_bytes() {
jmasa@4196 1103 ReturnedBytesClosure<Chunk_t, FreeList_t> rbc;
jmasa@3730 1104 rbc.do_tree(root());
duke@435 1105
jmasa@3732 1106 return rbc.dict_returned_bytes();
jmasa@3730 1107 }
duke@435 1108
jmasa@3730 1109 // Count the number of entries in the tree.
jmasa@4196 1110 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1111 class treeCountClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
jmasa@3730 1112 public:
jmasa@3730 1113 uint count;
jmasa@3730 1114 treeCountClosure(uint c) { count = c; }
jmasa@4196 1115 void do_list(FreeList_t<Chunk_t>* fl) {
jmasa@3730 1116 count++;
duke@435 1117 }
jmasa@3730 1118 };
duke@435 1119
jmasa@4196 1120 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1121 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_count() {
jmasa@4196 1122 treeCountClosure<Chunk_t, FreeList_t> ctc(0);
jmasa@3730 1123 ctc.do_tree(root());
jmasa@3730 1124 return ctc.count;
jmasa@3730 1125 }
jmasa@3730 1126 #endif // PRODUCT
duke@435 1127
duke@435 1128 // Calculate surpluses for the lists in the tree.
jmasa@4196 1129 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1130 class setTreeSurplusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 1131 double percentage;
duke@435 1132 public:
duke@435 1133 setTreeSurplusClosure(double v) { percentage = v; }
jmasa@4196 1134 void do_list(FreeList<Chunk_t>* fl) {}
jmasa@4196 1135
jmasa@4196 1136 #ifndef SERIALGC
jmasa@4196 1137 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
duke@435 1138 double splitSurplusPercent = percentage;
duke@435 1139 fl->set_surplus(fl->count() -
duke@435 1140 (ssize_t)((double)fl->desired() * splitSurplusPercent));
duke@435 1141 }
jmasa@4196 1142 #endif // SERIALGC
duke@435 1143 };
duke@435 1144
jmasa@4196 1145 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1146 void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_surplus(double splitSurplusPercent) {
jmasa@4196 1147 setTreeSurplusClosure<Chunk_t, FreeList_t> sts(splitSurplusPercent);
duke@435 1148 sts.do_tree(root());
duke@435 1149 }
duke@435 1150
duke@435 1151 // Set hints for the lists in the tree.
jmasa@4196 1152 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1153 class setTreeHintsClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
duke@435 1154 size_t hint;
duke@435 1155 public:
duke@435 1156 setTreeHintsClosure(size_t v) { hint = v; }
jmasa@4196 1157 void do_list(FreeList<Chunk_t>* fl) {}
jmasa@4196 1158
jmasa@4196 1159 #ifndef SERIALGC
jmasa@4196 1160 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
duke@435 1161 fl->set_hint(hint);
duke@435 1162 assert(fl->hint() == 0 || fl->hint() > fl->size(),
duke@435 1163 "Current hint is inconsistent");
duke@435 1164 if (fl->surplus() > 0) {
duke@435 1165 hint = fl->size();
duke@435 1166 }
duke@435 1167 }
jmasa@4196 1168 #endif // SERIALGC
duke@435 1169 };
duke@435 1170
jmasa@4196 1171 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1172 void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_hints(void) {
jmasa@4196 1173 setTreeHintsClosure<Chunk_t, FreeList_t> sth(0);
duke@435 1174 sth.do_tree(root());
duke@435 1175 }
duke@435 1176
duke@435 1177 // Save count before previous sweep and splits and coalesces.
jmasa@4196 1178 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1179 class clearTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
jmasa@4196 1180 void do_list(FreeList<Chunk_t>* fl) {}
jmasa@4196 1181
jmasa@4196 1182 #ifndef SERIALGC
jmasa@4196 1183 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
jmasa@3732 1184 fl->set_prev_sweep(fl->count());
jmasa@3732 1185 fl->set_coal_births(0);
jmasa@3732 1186 fl->set_coal_deaths(0);
jmasa@3732 1187 fl->set_split_births(0);
jmasa@3732 1188 fl->set_split_deaths(0);
duke@435 1189 }
jmasa@4196 1190 #endif // SERIALGC
duke@435 1191 };
duke@435 1192
jmasa@4196 1193 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1194 void BinaryTreeDictionary<Chunk_t, FreeList_t>::clear_tree_census(void) {
jmasa@4196 1195 clearTreeCensusClosure<Chunk_t, FreeList_t> ctc;
duke@435 1196 ctc.do_tree(root());
duke@435 1197 }
duke@435 1198
duke@435 1199 // Do reporting and post sweep clean up.
jmasa@4196 1200 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1201 void BinaryTreeDictionary<Chunk_t, FreeList_t>::end_sweep_dict_census(double splitSurplusPercent) {
duke@435 1202 // Does walking the tree 3 times hurt?
jmasa@3732 1203 set_tree_surplus(splitSurplusPercent);
jmasa@3732 1204 set_tree_hints();
duke@435 1205 if (PrintGC && Verbose) {
jmasa@3732 1206 report_statistics();
duke@435 1207 }
jmasa@3732 1208 clear_tree_census();
duke@435 1209 }
duke@435 1210
duke@435 1211 // Print summary statistics
jmasa@4196 1212 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1213 void BinaryTreeDictionary<Chunk_t, FreeList_t>::report_statistics() const {
jmasa@4196 1214 FreeBlockDictionary<Chunk_t>::verify_par_locked();
duke@435 1215 gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n"
duke@435 1216 "------------------------------------\n");
jmasa@3732 1217 size_t total_size = total_chunk_size(debug_only(NULL));
jmasa@3732 1218 size_t free_blocks = num_free_blocks();
jmasa@3732 1219 gclog_or_tty->print("Total Free Space: %d\n", total_size);
jmasa@3732 1220 gclog_or_tty->print("Max Chunk Size: %d\n", max_chunk_size());
jmasa@3732 1221 gclog_or_tty->print("Number of Blocks: %d\n", free_blocks);
jmasa@3732 1222 if (free_blocks > 0) {
jmasa@3732 1223 gclog_or_tty->print("Av. Block Size: %d\n", total_size/free_blocks);
duke@435 1224 }
jmasa@4196 1225 gclog_or_tty->print("Tree Height: %d\n", tree_height());
duke@435 1226 }
duke@435 1227
duke@435 1228 // Print census information - counts, births, deaths, etc.
duke@435 1229 // for each list in the tree. Also print some summary
duke@435 1230 // information.
jmasa@4196 1231 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1232 class PrintTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
ysr@447 1233 int _print_line;
jmasa@3732 1234 size_t _total_free;
jmasa@4196 1235 FreeList_t<Chunk_t> _total;
duke@435 1236
duke@435 1237 public:
ysr@1580 1238 PrintTreeCensusClosure() {
ysr@447 1239 _print_line = 0;
jmasa@3732 1240 _total_free = 0;
duke@435 1241 }
jmasa@4196 1242 FreeList_t<Chunk_t>* total() { return &_total; }
jmasa@3732 1243 size_t total_free() { return _total_free; }
jmasa@4196 1244 void do_list(FreeList<Chunk_t>* fl) {
ysr@447 1245 if (++_print_line >= 40) {
jmasa@4196 1246 FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, "size");
ysr@447 1247 _print_line = 0;
ysr@447 1248 }
ysr@447 1249 fl->print_on(gclog_or_tty);
jmasa@3732 1250 _total_free += fl->count() * fl->size() ;
ysr@447 1251 total()->set_count( total()->count() + fl->count() );
jmasa@4196 1252 }
jmasa@4196 1253
jmasa@4196 1254 #ifndef SERIALGC
jmasa@4196 1255 void do_list(AdaptiveFreeList<Chunk_t>* fl) {
jmasa@4196 1256 if (++_print_line >= 40) {
jmasa@4196 1257 FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, "size");
jmasa@4196 1258 _print_line = 0;
jmasa@4196 1259 }
jmasa@4196 1260 fl->print_on(gclog_or_tty);
jmasa@4196 1261 _total_free += fl->count() * fl->size() ;
jmasa@4196 1262 total()->set_count( total()->count() + fl->count() );
jmasa@4196 1263 total()->set_bfr_surp( total()->bfr_surp() + fl->bfr_surp() );
jmasa@3732 1264 total()->set_surplus( total()->split_deaths() + fl->surplus() );
jmasa@4196 1265 total()->set_desired( total()->desired() + fl->desired() );
jmasa@3732 1266 total()->set_prev_sweep( total()->prev_sweep() + fl->prev_sweep() );
jmasa@3732 1267 total()->set_before_sweep(total()->before_sweep() + fl->before_sweep());
jmasa@3732 1268 total()->set_coal_births( total()->coal_births() + fl->coal_births() );
jmasa@3732 1269 total()->set_coal_deaths( total()->coal_deaths() + fl->coal_deaths() );
jmasa@3732 1270 total()->set_split_births(total()->split_births() + fl->split_births());
jmasa@3732 1271 total()->set_split_deaths(total()->split_deaths() + fl->split_deaths());
duke@435 1272 }
jmasa@4196 1273 #endif // SERIALGC
duke@435 1274 };
duke@435 1275
jmasa@4196 1276 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1277 void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_dict_census(void) const {
duke@435 1278
duke@435 1279 gclog_or_tty->print("\nBinaryTree\n");
jmasa@4196 1280 FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, "size");
jmasa@4196 1281 PrintTreeCensusClosure<Chunk_t, FreeList_t> ptc;
duke@435 1282 ptc.do_tree(root());
duke@435 1283
jmasa@4196 1284 FreeList_t<Chunk_t>* total = ptc.total();
jmasa@4196 1285 FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, " ");
jmasa@4196 1286 }
jmasa@4196 1287
jmasa@4196 1288 #ifndef SERIALGC
jmasa@4196 1289 template <>
jmasa@4196 1290 void BinaryTreeDictionary<FreeChunk, AdaptiveFreeList>::print_dict_census(void) const {
jmasa@4196 1291
jmasa@4196 1292 gclog_or_tty->print("\nBinaryTree\n");
jmasa@4196 1293 AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, "size");
jmasa@4196 1294 PrintTreeCensusClosure<FreeChunk, AdaptiveFreeList> ptc;
jmasa@4196 1295 ptc.do_tree(root());
jmasa@4196 1296
jmasa@4196 1297 AdaptiveFreeList<FreeChunk>* total = ptc.total();
jmasa@4196 1298 AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, " ");
ysr@447 1299 total->print_on(gclog_or_tty, "TOTAL\t");
duke@435 1300 gclog_or_tty->print(
jmasa@3732 1301 "total_free(words): " SIZE_FORMAT_W(16)
ysr@447 1302 " growth: %8.5f deficit: %8.5f\n",
jmasa@3732 1303 ptc.total_free(),
jmasa@3732 1304 (double)(total->split_births() + total->coal_births()
jmasa@3732 1305 - total->split_deaths() - total->coal_deaths())
jmasa@3732 1306 /(total->prev_sweep() != 0 ? (double)total->prev_sweep() : 1.0),
ysr@447 1307 (double)(total->desired() - total->count())
ysr@447 1308 /(total->desired() != 0 ? (double)total->desired() : 1.0));
duke@435 1309 }
jmasa@4196 1310 #endif // SERIALGC
duke@435 1311
jmasa@4196 1312 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1313 class PrintFreeListsClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
ysr@1580 1314 outputStream* _st;
ysr@1580 1315 int _print_line;
ysr@1580 1316
ysr@1580 1317 public:
ysr@1580 1318 PrintFreeListsClosure(outputStream* st) {
ysr@1580 1319 _st = st;
ysr@1580 1320 _print_line = 0;
ysr@1580 1321 }
jmasa@4196 1322 void do_list(FreeList_t<Chunk_t>* fl) {
ysr@1580 1323 if (++_print_line >= 40) {
jmasa@4196 1324 FreeList_t<Chunk_t>::print_labels_on(_st, "size");
ysr@1580 1325 _print_line = 0;
ysr@1580 1326 }
ysr@1580 1327 fl->print_on(gclog_or_tty);
ysr@1580 1328 size_t sz = fl->size();
jmasa@4196 1329 for (Chunk_t* fc = fl->head(); fc != NULL;
ysr@1580 1330 fc = fc->next()) {
ysr@1580 1331 _st->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ") %s",
ysr@1580 1332 fc, (HeapWord*)fc + sz,
ysr@1580 1333 fc->cantCoalesce() ? "\t CC" : "");
ysr@1580 1334 }
ysr@1580 1335 }
ysr@1580 1336 };
ysr@1580 1337
jmasa@4196 1338 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1339 void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_free_lists(outputStream* st) const {
ysr@1580 1340
jmasa@4196 1341 FreeList_t<Chunk_t>::print_labels_on(st, "size");
jmasa@4196 1342 PrintFreeListsClosure<Chunk_t, FreeList_t> pflc(st);
ysr@1580 1343 pflc.do_tree(root());
ysr@1580 1344 }
ysr@1580 1345
duke@435 1346 // Verify the following tree invariants:
duke@435 1347 // . _root has no parent
duke@435 1348 // . parent and child point to each other
duke@435 1349 // . each node's key correctly related to that of its child(ren)
jmasa@4196 1350 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1351 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree() const {
jmasa@3732 1352 guarantee(root() == NULL || total_free_blocks() == 0 ||
jmasa@3732 1353 total_size() != 0, "_total_size should't be 0?");
duke@435 1354 guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent");
jmasa@3732 1355 verify_tree_helper(root());
duke@435 1356 }
duke@435 1357
jmasa@4196 1358 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1359 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_prev_free_ptrs(TreeList<Chunk_t, FreeList_t>* tl) {
duke@435 1360 size_t ct = 0;
jmasa@4196 1361 for (Chunk_t* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) {
duke@435 1362 ct++;
jmasa@3732 1363 assert(curFC->prev() == NULL || curFC->prev()->is_free(),
duke@435 1364 "Chunk should be free");
duke@435 1365 }
duke@435 1366 return ct;
duke@435 1367 }
duke@435 1368
duke@435 1369 // Note: this helper is recursive rather than iterative, so use with
duke@435 1370 // caution on very deep trees; and watch out for stack overflow errors;
duke@435 1371 // In general, to be used only for debugging.
jmasa@4196 1372 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1373 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
duke@435 1374 if (tl == NULL)
duke@435 1375 return;
duke@435 1376 guarantee(tl->size() != 0, "A list must has a size");
duke@435 1377 guarantee(tl->left() == NULL || tl->left()->parent() == tl,
duke@435 1378 "parent<-/->left");
duke@435 1379 guarantee(tl->right() == NULL || tl->right()->parent() == tl,
duke@435 1380 "parent<-/->right");;
duke@435 1381 guarantee(tl->left() == NULL || tl->left()->size() < tl->size(),
duke@435 1382 "parent !> left");
duke@435 1383 guarantee(tl->right() == NULL || tl->right()->size() > tl->size(),
duke@435 1384 "parent !< left");
jmasa@3732 1385 guarantee(tl->head() == NULL || tl->head()->is_free(), "!Free");
duke@435 1386 guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl,
duke@435 1387 "list inconsistency");
duke@435 1388 guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL),
duke@435 1389 "list count is inconsistent");
duke@435 1390 guarantee(tl->count() > 1 || tl->head() == tl->tail(),
duke@435 1391 "list is incorrectly constructed");
jmasa@3732 1392 size_t count = verify_prev_free_ptrs(tl);
duke@435 1393 guarantee(count == (size_t)tl->count(), "Node count is incorrect");
duke@435 1394 if (tl->head() != NULL) {
jmasa@3732 1395 tl->head_as_TreeChunk()->verify_tree_chunk_list();
duke@435 1396 }
jmasa@3732 1397 verify_tree_helper(tl->left());
jmasa@3732 1398 verify_tree_helper(tl->right());
duke@435 1399 }
duke@435 1400
jmasa@4196 1401 template <class Chunk_t, template <class> class FreeList_t>
jmasa@4196 1402 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify() const {
jmasa@3732 1403 verify_tree();
jmasa@3732 1404 guarantee(total_size() == total_size_in_tree(root()), "Total Size inconsistency");
duke@435 1405 }
jmasa@3730 1406
jmasa@4196 1407 template class TreeList<Metablock, FreeList>;
jmasa@4196 1408 template class BinaryTreeDictionary<Metablock, FreeList>;
jmasa@4196 1409 template class TreeChunk<Metablock, FreeList>;
jmasa@4196 1410
jmasa@4196 1411 template class TreeList<Metachunk, FreeList>;
jmasa@4196 1412 template class BinaryTreeDictionary<Metachunk, FreeList>;
jmasa@4196 1413 template class TreeChunk<Metachunk, FreeList>;
jmasa@4196 1414
jmasa@4196 1415
jmasa@3730 1416 #ifndef SERIALGC
jmasa@3730 1417 // Explicitly instantiate these types for FreeChunk.
jmasa@4196 1418 template class TreeList<FreeChunk, AdaptiveFreeList>;
jmasa@4196 1419 template class BinaryTreeDictionary<FreeChunk, AdaptiveFreeList>;
jmasa@4196 1420 template class TreeChunk<FreeChunk, AdaptiveFreeList>;
jmasa@4196 1421
jmasa@3730 1422 #endif // SERIALGC

mercurial