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