src/share/vm/memory/binaryTreeDictionary.cpp

Print this page
rev 5878 : 8034171: Remove use of template template parameters from binaryTreeDictionary.
Contributed-by: Matthias.Baesken@sap.com


  27 #include "gc_implementation/shared/allocationStats.hpp"
  28 #include "memory/binaryTreeDictionary.hpp"
  29 #include "memory/freeList.hpp"
  30 #include "memory/freeBlockDictionary.hpp"
  31 #include "memory/metachunk.hpp"
  32 #include "runtime/globals.hpp"
  33 #include "utilities/ostream.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "gc_implementation/shared/spaceDecorator.hpp"
  36 #if INCLUDE_ALL_GCS
  37 #include "gc_implementation/concurrentMarkSweep/adaptiveFreeList.hpp"
  38 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
  39 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
  40 #endif // INCLUDE_ALL_GCS
  41 
  42 ////////////////////////////////////////////////////////////////////////////////
  43 // A binary tree based search structure for free blocks.
  44 // This is currently used in the Concurrent Mark&Sweep implementation.
  45 ////////////////////////////////////////////////////////////////////////////////
  46 
  47 template <class Chunk_t, template <class> class FreeList_t>
  48 size_t TreeChunk<Chunk_t, FreeList_t>::_min_tree_chunk_size = sizeof(TreeChunk<Chunk_t,  FreeList_t>)/HeapWordSize;
  49 
  50 template <class Chunk_t, template <class> class FreeList_t>
  51 TreeChunk<Chunk_t, FreeList_t>* TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(Chunk_t* fc) {
  52   // Do some assertion checking here.
  53   return (TreeChunk<Chunk_t, FreeList_t>*) fc;
  54 }
  55 
  56 template <class Chunk_t, template <class> class FreeList_t>
  57 void TreeChunk<Chunk_t, FreeList_t>::verify_tree_chunk_list() const {
  58   TreeChunk<Chunk_t, FreeList_t>* nextTC = (TreeChunk<Chunk_t, FreeList_t>*)next();
  59   if (prev() != NULL) { // interior list node shouldn't have tree fields
  60     guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL &&
  61               embedded_list()->right()  == NULL, "should be clear");
  62   }
  63   if (nextTC != NULL) {
  64     guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain");
  65     guarantee(nextTC->size() == size(), "wrong size");
  66     nextTC->verify_tree_chunk_list();
  67   }
  68 }
  69 
  70 template <class Chunk_t, template <class> class FreeList_t>
  71 TreeList<Chunk_t, FreeList_t>::TreeList() : _parent(NULL),
  72   _left(NULL), _right(NULL) {}
  73 
  74 template <class Chunk_t, template <class> class FreeList_t>
  75 TreeList<Chunk_t, FreeList_t>*
  76 TreeList<Chunk_t, FreeList_t>::as_TreeList(TreeChunk<Chunk_t,FreeList_t>* tc) {
  77   // This first free chunk in the list will be the tree list.
  78   assert((tc->size() >= (TreeChunk<Chunk_t, FreeList_t>::min_size())),
  79     "Chunk is too small for a TreeChunk");
  80   TreeList<Chunk_t, FreeList_t>* tl = tc->embedded_list();
  81   tl->initialize();
  82   tc->set_list(tl);
  83   tl->set_size(tc->size());
  84   tl->link_head(tc);
  85   tl->link_tail(tc);
  86   tl->set_count(1);
  87   assert(tl->parent() == NULL, "Should be clear");
  88   return tl;
  89 }
  90 
  91 
  92 template <class Chunk_t, template <class> class FreeList_t>
  93 TreeList<Chunk_t, FreeList_t>*
  94 get_chunk(size_t size, enum FreeBlockDictionary<Chunk_t>::Dither dither) {
  95   FreeBlockDictionary<Chunk_t>::verify_par_locked();
  96   Chunk_t* res = get_chunk_from_tree(size, dither);
  97   assert(res == NULL || res->is_free(),
  98          "Should be returning a free chunk");
  99   assert(dither != FreeBlockDictionary<Chunk_t>::exactly ||
 100          res->size() == size, "Not correct size");
 101   return res;
 102 }
 103 
 104 template <class Chunk_t, template <class> class FreeList_t>
 105 TreeList<Chunk_t, FreeList_t>*
 106 TreeList<Chunk_t, FreeList_t>::as_TreeList(HeapWord* addr, size_t size) {
 107   TreeChunk<Chunk_t, FreeList_t>* tc = (TreeChunk<Chunk_t, FreeList_t>*) addr;
 108   assert((size >= TreeChunk<Chunk_t, FreeList_t>::min_size()),
 109     "Chunk is too small for a TreeChunk");
 110   // The space will have been mangled initially but
 111   // is not remangled when a Chunk_t is returned to the free list
 112   // (since it is used to maintain the chunk on the free list).
 113   tc->assert_is_mangled();
 114   tc->set_size(size);
 115   tc->link_prev(NULL);
 116   tc->link_next(NULL);
 117   TreeList<Chunk_t, FreeList_t>* tl = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);
 118   return tl;
 119 }
 120 
 121 
 122 #if INCLUDE_ALL_GCS
 123 // Specialize for AdaptiveFreeList which tries to avoid
 124 // splitting a chunk of a size that is under populated in favor of
 125 // an over populated size.  The general get_better_list() just returns
 126 // the current list.
 127 template <>
 128 TreeList<FreeChunk, AdaptiveFreeList>*
 129 TreeList<FreeChunk, AdaptiveFreeList>::get_better_list(
 130   BinaryTreeDictionary<FreeChunk, ::AdaptiveFreeList>* dictionary) {
 131   // A candidate chunk has been found.  If it is already under
 132   // populated, get a chunk associated with the hint for this
 133   // chunk.
 134 
 135   TreeList<FreeChunk, ::AdaptiveFreeList>* curTL = this;
 136   if (surplus() <= 0) {
 137     /* Use the hint to find a size with a surplus, and reset the hint. */
 138     TreeList<FreeChunk, ::AdaptiveFreeList>* hintTL = this;
 139     while (hintTL->hint() != 0) {
 140       assert(hintTL->hint() > hintTL->size(),
 141         "hint points in the wrong direction");
 142       hintTL = dictionary->find_list(hintTL->hint());
 143       assert(curTL != hintTL, "Infinite loop");
 144       if (hintTL == NULL ||
 145           hintTL == curTL /* Should not happen but protect against it */ ) {
 146         // No useful hint.  Set the hint to NULL and go on.
 147         curTL->set_hint(0);
 148         break;
 149       }
 150       assert(hintTL->size() > curTL->size(), "hint is inconsistent");
 151       if (hintTL->surplus() > 0) {
 152         // The hint led to a list that has a surplus.  Use it.
 153         // Set the hint for the candidate to an overpopulated
 154         // size.
 155         curTL->set_hint(hintTL->size());
 156         // Change the candidate.
 157         curTL = hintTL;
 158         break;
 159       }
 160     }
 161   }
 162   return curTL;
 163 }
 164 #endif // INCLUDE_ALL_GCS
 165 
 166 template <class Chunk_t, template <class> class FreeList_t>
 167 TreeList<Chunk_t, FreeList_t>*
 168 TreeList<Chunk_t, FreeList_t>::get_better_list(
 169   BinaryTreeDictionary<Chunk_t, FreeList_t>* dictionary) {
 170   return this;
 171 }
 172 
 173 template <class Chunk_t, template <class> class FreeList_t>
 174 TreeList<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::remove_chunk_replace_if_needed(TreeChunk<Chunk_t, FreeList_t>* tc) {
 175 
 176   TreeList<Chunk_t, FreeList_t>* retTL = this;
 177   Chunk_t* list = head();
 178   assert(!list || list != list->next(), "Chunk on list twice");
 179   assert(tc != NULL, "Chunk being removed is NULL");
 180   assert(parent() == NULL || this == parent()->left() ||
 181     this == parent()->right(), "list is inconsistent");
 182   assert(tc->is_free(), "Header is not marked correctly");
 183   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 184   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 185 
 186   Chunk_t* prevFC = tc->prev();
 187   TreeChunk<Chunk_t, FreeList_t>* nextTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(tc->next());
 188   assert(list != NULL, "should have at least the target chunk");
 189 
 190   // Is this the first item on the list?
 191   if (tc == list) {
 192     // The "getChunk..." functions for a TreeList<Chunk_t, FreeList_t> will not return the
 193     // first chunk in the list unless it is the last chunk in the list


 269         next_found = true;
 270       }
 271     }
 272     assert(prevFC == NULL || prev_found, "Chunk was lost from list");
 273     assert(nextTC == NULL || next_found, "Chunk was lost from list");
 274     assert(retTL->parent() == NULL ||
 275            retTL == retTL->parent()->left() ||
 276            retTL == retTL->parent()->right(),
 277            "list is inconsistent");
 278   )
 279   retTL->decrement_count();
 280 
 281   assert(tc->is_free(), "Should still be a free chunk");
 282   assert(retTL->head() == NULL || retTL->head()->prev() == NULL,
 283     "list invariant");
 284   assert(retTL->tail() == NULL || retTL->tail()->next() == NULL,
 285     "list invariant");
 286   return retTL;
 287 }
 288 
 289 template <class Chunk_t, template <class> class FreeList_t>
 290 void TreeList<Chunk_t, FreeList_t>::return_chunk_at_tail(TreeChunk<Chunk_t, FreeList_t>* chunk) {
 291   assert(chunk != NULL, "returning NULL chunk");
 292   assert(chunk->list() == this, "list should be set for chunk");
 293   assert(tail() != NULL, "The tree list is embedded in the first chunk");
 294   // which means that the list can never be empty.
 295   assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
 296   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 297   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 298 
 299   Chunk_t* fc = tail();
 300   fc->link_after(chunk);
 301   this->link_tail(chunk);
 302 
 303   assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list");
 304   FreeList_t<Chunk_t>::increment_count();
 305   debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
 306   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 307   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 308 }
 309 
 310 // Add this chunk at the head of the list.  "At the head of the list"
 311 // is defined to be after the chunk pointer to by head().  This is
 312 // because the TreeList<Chunk_t, FreeList_t> is embedded in the first TreeChunk<Chunk_t, FreeList_t> in the
 313 // list.  See the definition of TreeChunk<Chunk_t, FreeList_t>.
 314 template <class Chunk_t, template <class> class FreeList_t>
 315 void TreeList<Chunk_t, FreeList_t>::return_chunk_at_head(TreeChunk<Chunk_t, FreeList_t>* chunk) {
 316   assert(chunk->list() == this, "list should be set for chunk");
 317   assert(head() != NULL, "The tree list is embedded in the first chunk");
 318   assert(chunk != NULL, "returning NULL chunk");
 319   assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
 320   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 321   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 322 
 323   Chunk_t* fc = head()->next();
 324   if (fc != NULL) {
 325     chunk->link_after(fc);
 326   } else {
 327     assert(tail() == NULL, "List is inconsistent");
 328     this->link_tail(chunk);
 329   }
 330   head()->link_after(chunk);
 331   assert(!head() || size() == head()->size(), "Wrong sized chunk in list");
 332   FreeList_t<Chunk_t>::increment_count();
 333   debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
 334   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 335   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 336 }
 337 
 338 template <class Chunk_t, template <class> class FreeList_t>
 339 void TreeChunk<Chunk_t, FreeList_t>::assert_is_mangled() const {
 340   assert((ZapUnusedHeapArea &&
 341           SpaceMangler::is_mangled((HeapWord*) Chunk_t::size_addr()) &&
 342           SpaceMangler::is_mangled((HeapWord*) Chunk_t::prev_addr()) &&
 343           SpaceMangler::is_mangled((HeapWord*) Chunk_t::next_addr())) ||
 344           (size() == 0 && prev() == NULL && next() == NULL),
 345     "Space should be clear or mangled");
 346 }
 347 
 348 template <class Chunk_t, template <class> class FreeList_t>
 349 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::head_as_TreeChunk() {
 350   assert(head() == NULL || (TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head())->list() == this),
 351     "Wrong type of chunk?");
 352   return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head());
 353 }
 354 
 355 template <class Chunk_t, template <class> class FreeList_t>
 356 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::first_available() {
 357   assert(head() != NULL, "The head of the list cannot be NULL");
 358   Chunk_t* fc = head()->next();
 359   TreeChunk<Chunk_t, FreeList_t>* retTC;
 360   if (fc == NULL) {
 361     retTC = head_as_TreeChunk();
 362   } else {
 363     retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);
 364   }
 365   assert(retTC->list() == this, "Wrong type of chunk.");
 366   return retTC;
 367 }
 368 
 369 // Returns the block with the largest heap address amongst
 370 // those in the list for this size; potentially slow and expensive,
 371 // use with caution!
 372 template <class Chunk_t, template <class> class FreeList_t>
 373 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::largest_address() {
 374   assert(head() != NULL, "The head of the list cannot be NULL");
 375   Chunk_t* fc = head()->next();
 376   TreeChunk<Chunk_t, FreeList_t>* retTC;
 377   if (fc == NULL) {
 378     retTC = head_as_TreeChunk();
 379   } else {
 380     // walk down the list and return the one with the highest
 381     // heap address among chunks of this size.
 382     Chunk_t* last = fc;
 383     while (fc->next() != NULL) {
 384       if ((HeapWord*)last < (HeapWord*)fc) {
 385         last = fc;
 386       }
 387       fc = fc->next();
 388     }
 389     retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(last);
 390   }
 391   assert(retTC->list() == this, "Wrong type of chunk.");
 392   return retTC;
 393 }
 394 
 395 template <class Chunk_t, template <class> class FreeList_t>
 396 BinaryTreeDictionary<Chunk_t, FreeList_t>::BinaryTreeDictionary(MemRegion mr) {
 397   assert((mr.byte_size() > min_size()), "minimum chunk size");
 398 
 399   reset(mr);
 400   assert(root()->left() == NULL, "reset check failed");
 401   assert(root()->right() == NULL, "reset check failed");
 402   assert(root()->head()->next() == NULL, "reset check failed");
 403   assert(root()->head()->prev() == NULL, "reset check failed");
 404   assert(total_size() == root()->size(), "reset check failed");
 405   assert(total_free_blocks() == 1, "reset check failed");
 406 }
 407 
 408 template <class Chunk_t, template <class> class FreeList_t>
 409 void BinaryTreeDictionary<Chunk_t, FreeList_t>::inc_total_size(size_t inc) {
 410   _total_size = _total_size + inc;
 411 }
 412 
 413 template <class Chunk_t, template <class> class FreeList_t>
 414 void BinaryTreeDictionary<Chunk_t, FreeList_t>::dec_total_size(size_t dec) {
 415   _total_size = _total_size - dec;
 416 }
 417 
 418 template <class Chunk_t, template <class> class FreeList_t>
 419 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(MemRegion mr) {
 420   assert((mr.byte_size() > min_size()), "minimum chunk size");
 421   set_root(TreeList<Chunk_t, FreeList_t>::as_TreeList(mr.start(), mr.word_size()));
 422   set_total_size(mr.word_size());
 423   set_total_free_blocks(1);
 424 }
 425 
 426 template <class Chunk_t, template <class> class FreeList_t>
 427 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(HeapWord* addr, size_t byte_size) {
 428   MemRegion mr(addr, heap_word_size(byte_size));
 429   reset(mr);
 430 }
 431 
 432 template <class Chunk_t, template <class> class FreeList_t>
 433 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset() {
 434   set_root(NULL);
 435   set_total_size(0);
 436   set_total_free_blocks(0);
 437 }
 438 
 439 // Get a free block of size at least size from tree, or NULL.
 440 template <class Chunk_t, template <class> class FreeList_t>
 441 TreeChunk<Chunk_t, FreeList_t>*
 442 BinaryTreeDictionary<Chunk_t, FreeList_t>::get_chunk_from_tree(
 443                               size_t size,
 444                               enum FreeBlockDictionary<Chunk_t>::Dither dither)
 445 {
 446   TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
 447   TreeChunk<Chunk_t, FreeList_t>* retTC = NULL;
 448 
 449   assert((size >= min_size()), "minimum chunk size");
 450   if (FLSVerifyDictionary) {
 451     verify_tree();
 452   }
 453   // starting at the root, work downwards trying to find match.
 454   // Remember the last node of size too great or too small.
 455   for (prevTL = curTL = root(); curTL != NULL;) {
 456     if (curTL->size() == size) {        // exact match
 457       break;
 458     }
 459     prevTL = curTL;
 460     if (curTL->size() < size) {        // proceed to right sub-tree


 479   if (curTL != NULL) {
 480     assert(curTL->size() >= size, "size inconsistency");
 481 
 482     curTL = curTL->get_better_list(this);
 483 
 484     retTC = curTL->first_available();
 485     assert((retTC != NULL) && (curTL->count() > 0),
 486       "A list in the binary tree should not be NULL");
 487     assert(retTC->size() >= size,
 488       "A chunk of the wrong size was found");
 489     remove_chunk_from_tree(retTC);
 490     assert(retTC->is_free(), "Header is not marked correctly");
 491   }
 492 
 493   if (FLSVerifyDictionary) {
 494     verify();
 495   }
 496   return retTC;
 497 }
 498 
 499 template <class Chunk_t, template <class> class FreeList_t>
 500 TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_list(size_t size) const {
 501   TreeList<Chunk_t, FreeList_t>* curTL;
 502   for (curTL = root(); curTL != NULL;) {
 503     if (curTL->size() == size) {        // exact match
 504       break;
 505     }
 506 
 507     if (curTL->size() < size) {        // proceed to right sub-tree
 508       curTL = curTL->right();
 509     } else {                           // proceed to left sub-tree
 510       assert(curTL->size() > size, "size inconsistency");
 511       curTL = curTL->left();
 512     }
 513   }
 514   return curTL;
 515 }
 516 
 517 
 518 template <class Chunk_t, template <class> class FreeList_t>
 519 bool BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_chunk_in_free_list(Chunk_t* tc) const {
 520   size_t size = tc->size();
 521   TreeList<Chunk_t, FreeList_t>* tl = find_list(size);
 522   if (tl == NULL) {
 523     return false;
 524   } else {
 525     return tl->verify_chunk_in_free_list(tc);
 526   }
 527 }
 528 
 529 template <class Chunk_t, template <class> class FreeList_t>
 530 Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_largest_dict() const {
 531   TreeList<Chunk_t, FreeList_t> *curTL = root();
 532   if (curTL != NULL) {
 533     while(curTL->right() != NULL) curTL = curTL->right();
 534     return curTL->largest_address();
 535   } else {
 536     return NULL;
 537   }
 538 }
 539 
 540 // Remove the current chunk from the tree.  If it is not the last
 541 // chunk in a list on a tree node, just unlink it.
 542 // If it is the last chunk in the list (the next link is NULL),
 543 // remove the node and repair the tree.
 544 template <class Chunk_t, template <class> class FreeList_t>
 545 TreeChunk<Chunk_t, FreeList_t>*
 546 BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_chunk_from_tree(TreeChunk<Chunk_t, FreeList_t>* tc) {
 547   assert(tc != NULL, "Should not call with a NULL chunk");
 548   assert(tc->is_free(), "Header is not marked correctly");
 549 
 550   TreeList<Chunk_t, FreeList_t> *newTL, *parentTL;
 551   TreeChunk<Chunk_t, FreeList_t>* retTC;
 552   TreeList<Chunk_t, FreeList_t>* tl = tc->list();
 553   debug_only(
 554     bool removing_only_chunk = false;
 555     if (tl == _root) {
 556       if ((_root->left() == NULL) && (_root->right() == NULL)) {
 557         if (_root->count() == 1) {
 558           assert(_root->head() == tc, "Should only be this one chunk");
 559           removing_only_chunk = true;
 560         }
 561       }
 562     }
 563   )
 564   assert(tl != NULL, "List should be set");


 665   }
 666 
 667   assert(total_size() >= retTC->size(), "Incorrect total size");
 668   dec_total_size(retTC->size());     // size book-keeping
 669   assert(total_free_blocks() > 0, "Incorrect total count");
 670   set_total_free_blocks(total_free_blocks() - 1);
 671 
 672   assert(retTC != NULL, "null chunk?");
 673   assert(retTC->prev() == NULL && retTC->next() == NULL,
 674          "should return without encumbrances");
 675   if (FLSVerifyDictionary) {
 676     verify_tree();
 677   }
 678   assert(!removing_only_chunk || _root == NULL, "root should be NULL");
 679   return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(retTC);
 680 }
 681 
 682 // Remove the leftmost node (lm) in the tree and return it.
 683 // If lm has a right child, link it to the left node of
 684 // the parent of lm.
 685 template <class Chunk_t, template <class> class FreeList_t>
 686 TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_tree_minimum(TreeList<Chunk_t, FreeList_t>* tl) {
 687   assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree");
 688   // locate the subtree minimum by walking down left branches
 689   TreeList<Chunk_t, FreeList_t>* curTL = tl;
 690   for (; curTL->left() != NULL; curTL = curTL->left());
 691   // obviously curTL now has at most one child, a right child
 692   if (curTL != root()) {  // Should this test just be removed?
 693     TreeList<Chunk_t, FreeList_t>* parentTL = curTL->parent();
 694     if (parentTL->left() == curTL) { // curTL is a left child
 695       parentTL->set_left(curTL->right());
 696     } else {
 697       // If the list tl has no left child, then curTL may be
 698       // the right child of parentTL.
 699       assert(parentTL->right() == curTL, "should be a right child");
 700       parentTL->set_right(curTL->right());
 701     }
 702   } else {
 703     // The only use of this method would not pass the root of the
 704     // tree (as indicated by the assertion above that the tree list
 705     // has a parent) but the specification does not explicitly exclude the
 706     // passing of the root so accommodate it.
 707     set_root(NULL);
 708   }
 709   debug_only(
 710     curTL->clear_parent();  // Test if this needs to be cleared
 711     curTL->clear_right();    // recall, above, left child is already null
 712   )
 713   // we just excised a (non-root) node, we should still verify all tree invariants
 714   if (FLSVerifyDictionary) {
 715     verify_tree();
 716   }
 717   return curTL;
 718 }
 719 
 720 template <class Chunk_t, template <class> class FreeList_t>
 721 void BinaryTreeDictionary<Chunk_t, FreeList_t>::insert_chunk_in_tree(Chunk_t* fc) {
 722   TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
 723   size_t size = fc->size();
 724 
 725   assert((size >= min_size()),
 726     err_msg(SIZE_FORMAT " is too small to be a TreeChunk<Chunk_t, FreeList_t> " SIZE_FORMAT,
 727       size, min_size()));
 728   if (FLSVerifyDictionary) {
 729     verify_tree();
 730   }
 731 
 732   fc->clear_next();
 733   fc->link_prev(NULL);
 734 
 735   // work down from the _root, looking for insertion point
 736   for (prevTL = curTL = root(); curTL != NULL;) {
 737     if (curTL->size() == size)  // exact match
 738       break;
 739     prevTL = curTL;
 740     if (curTL->size() > size) { // follow left branch


 766         prevTL->set_right(newTL);
 767       } else {                       // am left child
 768         assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv");
 769         prevTL->set_left(newTL);
 770       }
 771     }
 772   }
 773   assert(tc->list() != NULL, "Tree list should be set");
 774 
 775   inc_total_size(size);
 776   // Method 'total_size_in_tree' walks through the every block in the
 777   // tree, so it can cause significant performance loss if there are
 778   // many blocks in the tree
 779   assert(!FLSVerifyDictionary || total_size_in_tree(root()) == total_size(), "_total_size inconsistency");
 780   set_total_free_blocks(total_free_blocks() + 1);
 781   if (FLSVerifyDictionary) {
 782     verify_tree();
 783   }
 784 }
 785 
 786 template <class Chunk_t, template <class> class FreeList_t>
 787 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::max_chunk_size() const {
 788   FreeBlockDictionary<Chunk_t>::verify_par_locked();
 789   TreeList<Chunk_t, FreeList_t>* tc = root();
 790   if (tc == NULL) return 0;
 791   for (; tc->right() != NULL; tc = tc->right());
 792   return tc->size();
 793 }
 794 
 795 template <class Chunk_t, template <class> class FreeList_t>
 796 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_list_length(TreeList<Chunk_t, FreeList_t>* tl) const {
 797   size_t res;
 798   res = tl->count();
 799 #ifdef ASSERT
 800   size_t cnt;
 801   Chunk_t* tc = tl->head();
 802   for (cnt = 0; tc != NULL; tc = tc->next(), cnt++);
 803   assert(res == cnt, "The count is not being maintained correctly");
 804 #endif
 805   return res;
 806 }
 807 
 808 template <class Chunk_t, template <class> class FreeList_t>
 809 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_size_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
 810   if (tl == NULL)
 811     return 0;
 812   return (tl->size() * total_list_length(tl)) +
 813          total_size_in_tree(tl->left())    +
 814          total_size_in_tree(tl->right());
 815 }
 816 
 817 template <class Chunk_t, template <class> class FreeList_t>
 818 double BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_of_squared_block_sizes(TreeList<Chunk_t, FreeList_t>* const tl) const {
 819   if (tl == NULL) {
 820     return 0.0;
 821   }
 822   double size = (double)(tl->size());
 823   double curr = size * size * total_list_length(tl);
 824   curr += sum_of_squared_block_sizes(tl->left());
 825   curr += sum_of_squared_block_sizes(tl->right());
 826   return curr;
 827 }
 828 
 829 template <class Chunk_t, template <class> class FreeList_t>
 830 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_free_blocks_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
 831   if (tl == NULL)
 832     return 0;
 833   return total_list_length(tl) +
 834          total_free_blocks_in_tree(tl->left()) +
 835          total_free_blocks_in_tree(tl->right());
 836 }
 837 
 838 template <class Chunk_t, template <class> class FreeList_t>
 839 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::num_free_blocks() const {
 840   assert(total_free_blocks_in_tree(root()) == total_free_blocks(),
 841          "_total_free_blocks inconsistency");
 842   return total_free_blocks();
 843 }
 844 
 845 template <class Chunk_t, template <class> class FreeList_t>
 846 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
 847   if (tl == NULL)
 848     return 0;
 849   return 1 + MAX2(tree_height_helper(tl->left()),
 850                   tree_height_helper(tl->right()));
 851 }
 852 
 853 template <class Chunk_t, template <class> class FreeList_t>
 854 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height() const {
 855   return tree_height_helper(root());
 856 }
 857 
 858 template <class Chunk_t, template <class> class FreeList_t>
 859 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
 860   if (tl == NULL) {
 861     return 0;
 862   }
 863   return 1 + total_nodes_helper(tl->left()) +
 864     total_nodes_helper(tl->right());
 865 }
 866 
 867 template <class Chunk_t, template <class> class FreeList_t>
 868 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
 869   return total_nodes_helper(root());
 870 }
 871 
 872 template <class Chunk_t, template <class> class FreeList_t>
 873 void BinaryTreeDictionary<Chunk_t, FreeList_t>::dict_census_update(size_t size, bool split, bool birth){}
 874 
 875 #if INCLUDE_ALL_GCS
 876 template <>
 877 void AFLBinaryTreeDictionary::dict_census_update(size_t size, bool split, bool birth){
 878   TreeList<FreeChunk, AdaptiveFreeList>* nd = find_list(size);
 879   if (nd) {
 880     if (split) {
 881       if (birth) {
 882         nd->increment_split_births();
 883         nd->increment_surplus();
 884       }  else {
 885         nd->increment_split_deaths();
 886         nd->decrement_surplus();
 887       }
 888     } else {
 889       if (birth) {
 890         nd->increment_coal_births();
 891         nd->increment_surplus();
 892       } else {
 893         nd->increment_coal_deaths();
 894         nd->decrement_surplus();
 895       }
 896     }
 897   }
 898   // A list for this size may not be found (nd == 0) if
 899   //   This is a death where the appropriate list is now
 900   //     empty and has been removed from the list.
 901   //   This is a birth associated with a LinAB.  The chunk
 902   //     for the LinAB is not in the dictionary.
 903 }
 904 #endif // INCLUDE_ALL_GCS
 905 
 906 template <class Chunk_t, template <class> class FreeList_t>
 907 bool BinaryTreeDictionary<Chunk_t, FreeList_t>::coal_dict_over_populated(size_t size) {
 908   // For the general type of freelists, encourage coalescing by
 909   // returning true.
 910   return true;
 911 }
 912 
 913 #if INCLUDE_ALL_GCS
 914 template <>
 915 bool AFLBinaryTreeDictionary::coal_dict_over_populated(size_t size) {
 916   if (FLSAlwaysCoalesceLarge) return true;
 917 
 918   TreeList<FreeChunk, AdaptiveFreeList>* list_of_size = find_list(size);
 919   // None of requested size implies overpopulated.
 920   return list_of_size == NULL || list_of_size->coal_desired() <= 0 ||
 921          list_of_size->count() > list_of_size->coal_desired();
 922 }
 923 #endif // INCLUDE_ALL_GCS
 924 
 925 // Closures for walking the binary tree.
 926 //   do_list() walks the free list in a node applying the closure
 927 //     to each free chunk in the list
 928 //   do_tree() walks the nodes in the binary tree applying do_list()
 929 //     to each list at each node.
 930 
 931 template <class Chunk_t, template <class> class FreeList_t>
 932 class TreeCensusClosure : public StackObj {
 933  protected:
 934   virtual void do_list(FreeList_t<Chunk_t>* fl) = 0;
 935  public:
 936   virtual void do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
 937 };
 938 
 939 template <class Chunk_t, template <class> class FreeList_t>
 940 class AscendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
 941  public:
 942   void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
 943     if (tl != NULL) {
 944       do_tree(tl->left());
 945       this->do_list(tl);
 946       do_tree(tl->right());
 947     }
 948   }
 949 };
 950 
 951 template <class Chunk_t, template <class> class FreeList_t>
 952 class DescendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
 953  public:
 954   void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
 955     if (tl != NULL) {
 956       do_tree(tl->right());
 957       this->do_list(tl);
 958       do_tree(tl->left());
 959     }
 960   }
 961 };
 962 
 963 // For each list in the tree, calculate the desired, desired
 964 // coalesce, count before sweep, and surplus before sweep.
 965 template <class Chunk_t, template <class> class FreeList_t>
 966 class BeginSweepClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
 967   double _percentage;
 968   float _inter_sweep_current;
 969   float _inter_sweep_estimate;
 970   float _intra_sweep_estimate;
 971 
 972  public:
 973   BeginSweepClosure(double p, float inter_sweep_current,
 974                               float inter_sweep_estimate,
 975                               float intra_sweep_estimate) :
 976    _percentage(p),
 977    _inter_sweep_current(inter_sweep_current),
 978    _inter_sweep_estimate(inter_sweep_estimate),
 979    _intra_sweep_estimate(intra_sweep_estimate) { }
 980 
 981   void do_list(FreeList<Chunk_t>* fl) {}
 982 
 983 #if INCLUDE_ALL_GCS
 984   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
 985     double coalSurplusPercent = _percentage;
 986     fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate, _intra_sweep_estimate);
 987     fl->set_coal_desired((ssize_t)((double)fl->desired() * coalSurplusPercent));
 988     fl->set_before_sweep(fl->count());
 989     fl->set_bfr_surp(fl->surplus());
 990   }
 991 #endif // INCLUDE_ALL_GCS
 992 };
 993 
 994 // Used to search the tree until a condition is met.
 995 // Similar to TreeCensusClosure but searches the
 996 // tree and returns promptly when found.
 997 
 998 template <class Chunk_t, template <class> class FreeList_t>
 999 class TreeSearchClosure : public StackObj {
1000  protected:
1001   virtual bool do_list(FreeList_t<Chunk_t>* fl) = 0;
1002  public:
1003   virtual bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
1004 };
1005 
1006 #if 0 //  Don't need this yet but here for symmetry.
1007 template <class Chunk_t, template <class> class FreeList_t>
1008 class AscendTreeSearchClosure : public TreeSearchClosure<Chunk_t> {
1009  public:
1010   bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
1011     if (tl != NULL) {
1012       if (do_tree(tl->left())) return true;
1013       if (do_list(tl)) return true;
1014       if (do_tree(tl->right())) return true;
1015     }
1016     return false;
1017   }
1018 };
1019 #endif
1020 
1021 template <class Chunk_t, template <class> class FreeList_t>
1022 class DescendTreeSearchClosure : public TreeSearchClosure<Chunk_t, FreeList_t> {
1023  public:
1024   bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
1025     if (tl != NULL) {
1026       if (do_tree(tl->right())) return true;
1027       if (this->do_list(tl)) return true;
1028       if (do_tree(tl->left())) return true;
1029     }
1030     return false;
1031   }
1032 };
1033 
1034 // Searches the tree for a chunk that ends at the
1035 // specified address.
1036 template <class Chunk_t, template <class> class FreeList_t>
1037 class EndTreeSearchClosure : public DescendTreeSearchClosure<Chunk_t, FreeList_t> {
1038   HeapWord* _target;
1039   Chunk_t* _found;
1040 
1041  public:
1042   EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {}
1043   bool do_list(FreeList_t<Chunk_t>* fl) {
1044     Chunk_t* item = fl->head();
1045     while (item != NULL) {
1046       if (item->end() == (uintptr_t*) _target) {
1047         _found = item;
1048         return true;
1049       }
1050       item = item->next();
1051     }
1052     return false;
1053   }
1054   Chunk_t* found() { return _found; }
1055 };
1056 
1057 template <class Chunk_t, template <class> class FreeList_t>
1058 Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_chunk_ends_at(HeapWord* target) const {
1059   EndTreeSearchClosure<Chunk_t, FreeList_t> etsc(target);
1060   bool found_target = etsc.do_tree(root());
1061   assert(found_target || etsc.found() == NULL, "Consistency check");
1062   assert(!found_target || etsc.found() != NULL, "Consistency check");
1063   return etsc.found();
1064 }
1065 
1066 template <class Chunk_t, template <class> class FreeList_t>
1067 void BinaryTreeDictionary<Chunk_t, FreeList_t>::begin_sweep_dict_census(double coalSurplusPercent,
1068   float inter_sweep_current, float inter_sweep_estimate, float intra_sweep_estimate) {
1069   BeginSweepClosure<Chunk_t, FreeList_t> bsc(coalSurplusPercent, inter_sweep_current,
1070                                             inter_sweep_estimate,
1071                                             intra_sweep_estimate);
1072   bsc.do_tree(root());
1073 }
1074 
1075 // Closures and methods for calculating total bytes returned to the
1076 // free lists in the tree.
1077 #ifndef PRODUCT
1078 template <class Chunk_t, template <class> class FreeList_t>
1079 class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1080    public:
1081   void do_list(FreeList_t<Chunk_t>* fl) {
1082     fl->set_returned_bytes(0);
1083   }
1084 };
1085 
1086 template <class Chunk_t, template <class> class FreeList_t>
1087 void BinaryTreeDictionary<Chunk_t, FreeList_t>::initialize_dict_returned_bytes() {
1088   InitializeDictReturnedBytesClosure<Chunk_t, FreeList_t> idrb;
1089   idrb.do_tree(root());
1090 }
1091 
1092 template <class Chunk_t, template <class> class FreeList_t>
1093 class ReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1094   size_t _dict_returned_bytes;
1095  public:
1096   ReturnedBytesClosure() { _dict_returned_bytes = 0; }
1097   void do_list(FreeList_t<Chunk_t>* fl) {
1098     _dict_returned_bytes += fl->returned_bytes();
1099   }
1100   size_t dict_returned_bytes() { return _dict_returned_bytes; }
1101 };
1102 
1103 template <class Chunk_t, template <class> class FreeList_t>
1104 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_dict_returned_bytes() {
1105   ReturnedBytesClosure<Chunk_t, FreeList_t> rbc;
1106   rbc.do_tree(root());
1107 
1108   return rbc.dict_returned_bytes();
1109 }
1110 
1111 // Count the number of entries in the tree.
1112 template <class Chunk_t, template <class> class FreeList_t>
1113 class treeCountClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
1114  public:
1115   uint count;
1116   treeCountClosure(uint c) { count = c; }
1117   void do_list(FreeList_t<Chunk_t>* fl) {
1118     count++;
1119   }
1120 };
1121 
1122 template <class Chunk_t, template <class> class FreeList_t>
1123 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_count() {
1124   treeCountClosure<Chunk_t, FreeList_t> ctc(0);
1125   ctc.do_tree(root());
1126   return ctc.count;
1127 }
1128 #endif // PRODUCT
1129 
1130 // Calculate surpluses for the lists in the tree.
1131 template <class Chunk_t, template <class> class FreeList_t>
1132 class setTreeSurplusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1133   double percentage;
1134  public:
1135   setTreeSurplusClosure(double v) { percentage = v; }
1136   void do_list(FreeList<Chunk_t>* fl) {}
1137 
1138 #if INCLUDE_ALL_GCS
1139   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1140     double splitSurplusPercent = percentage;
1141     fl->set_surplus(fl->count() -
1142                    (ssize_t)((double)fl->desired() * splitSurplusPercent));
1143   }
1144 #endif // INCLUDE_ALL_GCS
1145 };
1146 
1147 template <class Chunk_t, template <class> class FreeList_t>
1148 void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_surplus(double splitSurplusPercent) {
1149   setTreeSurplusClosure<Chunk_t, FreeList_t> sts(splitSurplusPercent);
1150   sts.do_tree(root());
1151 }
1152 
1153 // Set hints for the lists in the tree.
1154 template <class Chunk_t, template <class> class FreeList_t>
1155 class setTreeHintsClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
1156   size_t hint;
1157  public:
1158   setTreeHintsClosure(size_t v) { hint = v; }
1159   void do_list(FreeList<Chunk_t>* fl) {}
1160 
1161 #if INCLUDE_ALL_GCS
1162   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1163     fl->set_hint(hint);
1164     assert(fl->hint() == 0 || fl->hint() > fl->size(),
1165       "Current hint is inconsistent");
1166     if (fl->surplus() > 0) {
1167       hint = fl->size();
1168     }
1169   }
1170 #endif // INCLUDE_ALL_GCS
1171 };
1172 
1173 template <class Chunk_t, template <class> class FreeList_t>
1174 void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_hints(void) {
1175   setTreeHintsClosure<Chunk_t, FreeList_t> sth(0);
1176   sth.do_tree(root());
1177 }
1178 
1179 // Save count before previous sweep and splits and coalesces.
1180 template <class Chunk_t, template <class> class FreeList_t>
1181 class clearTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1182   void do_list(FreeList<Chunk_t>* fl) {}
1183 
1184 #if INCLUDE_ALL_GCS
1185   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1186     fl->set_prev_sweep(fl->count());
1187     fl->set_coal_births(0);
1188     fl->set_coal_deaths(0);
1189     fl->set_split_births(0);
1190     fl->set_split_deaths(0);
1191   }
1192 #endif // INCLUDE_ALL_GCS
1193 };
1194 
1195 template <class Chunk_t, template <class> class FreeList_t>
1196 void BinaryTreeDictionary<Chunk_t, FreeList_t>::clear_tree_census(void) {
1197   clearTreeCensusClosure<Chunk_t, FreeList_t> ctc;
1198   ctc.do_tree(root());
1199 }
1200 
1201 // Do reporting and post sweep clean up.
1202 template <class Chunk_t, template <class> class FreeList_t>
1203 void BinaryTreeDictionary<Chunk_t, FreeList_t>::end_sweep_dict_census(double splitSurplusPercent) {
1204   // Does walking the tree 3 times hurt?
1205   set_tree_surplus(splitSurplusPercent);
1206   set_tree_hints();
1207   if (PrintGC && Verbose) {
1208     report_statistics();
1209   }
1210   clear_tree_census();
1211 }
1212 
1213 // Print summary statistics
1214 template <class Chunk_t, template <class> class FreeList_t>
1215 void BinaryTreeDictionary<Chunk_t, FreeList_t>::report_statistics() const {
1216   FreeBlockDictionary<Chunk_t>::verify_par_locked();
1217   gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n"
1218          "------------------------------------\n");
1219   size_t total_size = total_chunk_size(debug_only(NULL));
1220   size_t    free_blocks = num_free_blocks();
1221   gclog_or_tty->print("Total Free Space: %d\n", total_size);
1222   gclog_or_tty->print("Max   Chunk Size: %d\n", max_chunk_size());
1223   gclog_or_tty->print("Number of Blocks: %d\n", free_blocks);
1224   if (free_blocks > 0) {
1225     gclog_or_tty->print("Av.  Block  Size: %d\n", total_size/free_blocks);
1226   }
1227   gclog_or_tty->print("Tree      Height: %d\n", tree_height());
1228 }
1229 
1230 // Print census information - counts, births, deaths, etc.
1231 // for each list in the tree.  Also print some summary
1232 // information.
1233 template <class Chunk_t, template <class> class FreeList_t>
1234 class PrintTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1235   int _print_line;
1236   size_t _total_free;
1237   FreeList_t<Chunk_t> _total;
1238 
1239  public:
1240   PrintTreeCensusClosure() {
1241     _print_line = 0;
1242     _total_free = 0;
1243   }
1244   FreeList_t<Chunk_t>* total() { return &_total; }
1245   size_t total_free() { return _total_free; }
1246   void do_list(FreeList<Chunk_t>* fl) {
1247     if (++_print_line >= 40) {
1248       FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, "size");
1249       _print_line = 0;
1250     }
1251     fl->print_on(gclog_or_tty);
1252     _total_free +=            fl->count()            * fl->size()        ;
1253     total()->set_count(      total()->count()       + fl->count()      );
1254   }
1255 
1256 #if INCLUDE_ALL_GCS
1257   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1258     if (++_print_line >= 40) {
1259       FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, "size");
1260       _print_line = 0;
1261     }
1262     fl->print_on(gclog_or_tty);
1263     _total_free +=           fl->count()             * fl->size()        ;
1264     total()->set_count(      total()->count()        + fl->count()      );
1265     total()->set_bfr_surp(   total()->bfr_surp()     + fl->bfr_surp()    );
1266     total()->set_surplus(    total()->split_deaths() + fl->surplus()    );
1267     total()->set_desired(    total()->desired()      + fl->desired()    );
1268     total()->set_prev_sweep(  total()->prev_sweep()   + fl->prev_sweep()  );
1269     total()->set_before_sweep(total()->before_sweep() + fl->before_sweep());
1270     total()->set_coal_births( total()->coal_births()  + fl->coal_births() );
1271     total()->set_coal_deaths( total()->coal_deaths()  + fl->coal_deaths() );
1272     total()->set_split_births(total()->split_births() + fl->split_births());
1273     total()->set_split_deaths(total()->split_deaths() + fl->split_deaths());
1274   }
1275 #endif // INCLUDE_ALL_GCS
1276 };
1277 
1278 template <class Chunk_t, template <class> class FreeList_t>
1279 void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_dict_census(void) const {
1280 
1281   gclog_or_tty->print("\nBinaryTree\n");
1282   FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, "size");
1283   PrintTreeCensusClosure<Chunk_t, FreeList_t> ptc;
1284   ptc.do_tree(root());
1285 
1286   FreeList_t<Chunk_t>* total = ptc.total();
1287   FreeList_t<Chunk_t>::print_labels_on(gclog_or_tty, " ");
1288 }
1289 
1290 #if INCLUDE_ALL_GCS
1291 template <>
1292 void AFLBinaryTreeDictionary::print_dict_census(void) const {
1293 
1294   gclog_or_tty->print("\nBinaryTree\n");
1295   AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, "size");
1296   PrintTreeCensusClosure<FreeChunk, AdaptiveFreeList> ptc;
1297   ptc.do_tree(root());
1298 
1299   AdaptiveFreeList<FreeChunk>* total = ptc.total();
1300   AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, " ");
1301   total->print_on(gclog_or_tty, "TOTAL\t");
1302   gclog_or_tty->print(
1303               "total_free(words): " SIZE_FORMAT_W(16)
1304               " growth: %8.5f  deficit: %8.5f\n",
1305               ptc.total_free(),
1306               (double)(total->split_births() + total->coal_births()
1307                      - total->split_deaths() - total->coal_deaths())
1308               /(total->prev_sweep() != 0 ? (double)total->prev_sweep() : 1.0),
1309              (double)(total->desired() - total->count())
1310              /(total->desired() != 0 ? (double)total->desired() : 1.0));
1311 }
1312 #endif // INCLUDE_ALL_GCS
1313 
1314 template <class Chunk_t, template <class> class FreeList_t>
1315 class PrintFreeListsClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1316   outputStream* _st;
1317   int _print_line;
1318 
1319  public:
1320   PrintFreeListsClosure(outputStream* st) {
1321     _st = st;
1322     _print_line = 0;
1323   }
1324   void do_list(FreeList_t<Chunk_t>* fl) {
1325     if (++_print_line >= 40) {
1326       FreeList_t<Chunk_t>::print_labels_on(_st, "size");
1327       _print_line = 0;
1328     }
1329     fl->print_on(gclog_or_tty);
1330     size_t sz = fl->size();
1331     for (Chunk_t* fc = fl->head(); fc != NULL;
1332          fc = fc->next()) {
1333       _st->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ")  %s",
1334                     fc, (HeapWord*)fc + sz,
1335                     fc->cantCoalesce() ? "\t CC" : "");
1336     }
1337   }
1338 };
1339 
1340 template <class Chunk_t, template <class> class FreeList_t>
1341 void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_free_lists(outputStream* st) const {
1342 
1343   FreeList_t<Chunk_t>::print_labels_on(st, "size");
1344   PrintFreeListsClosure<Chunk_t, FreeList_t> pflc(st);
1345   pflc.do_tree(root());
1346 }
1347 
1348 // Verify the following tree invariants:
1349 // . _root has no parent
1350 // . parent and child point to each other
1351 // . each node's key correctly related to that of its child(ren)
1352 template <class Chunk_t, template <class> class FreeList_t>
1353 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree() const {
1354   guarantee(root() == NULL || total_free_blocks() == 0 ||
1355     total_size() != 0, "_total_size should't be 0?");
1356   guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent");
1357   verify_tree_helper(root());
1358 }
1359 
1360 template <class Chunk_t, template <class> class FreeList_t>
1361 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_prev_free_ptrs(TreeList<Chunk_t, FreeList_t>* tl) {
1362   size_t ct = 0;
1363   for (Chunk_t* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) {
1364     ct++;
1365     assert(curFC->prev() == NULL || curFC->prev()->is_free(),
1366       "Chunk should be free");
1367   }
1368   return ct;
1369 }
1370 
1371 // Note: this helper is recursive rather than iterative, so use with
1372 // caution on very deep trees; and watch out for stack overflow errors;
1373 // In general, to be used only for debugging.
1374 template <class Chunk_t, template <class> class FreeList_t>
1375 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
1376   if (tl == NULL)
1377     return;
1378   guarantee(tl->size() != 0, "A list must has a size");
1379   guarantee(tl->left()  == NULL || tl->left()->parent()  == tl,
1380          "parent<-/->left");
1381   guarantee(tl->right() == NULL || tl->right()->parent() == tl,
1382          "parent<-/->right");;
1383   guarantee(tl->left() == NULL  || tl->left()->size()    <  tl->size(),
1384          "parent !> left");
1385   guarantee(tl->right() == NULL || tl->right()->size()   >  tl->size(),
1386          "parent !< left");
1387   guarantee(tl->head() == NULL || tl->head()->is_free(), "!Free");
1388   guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl,
1389     "list inconsistency");
1390   guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL),
1391     "list count is inconsistent");
1392   guarantee(tl->count() > 1 || tl->head() == tl->tail(),
1393     "list is incorrectly constructed");
1394   size_t count = verify_prev_free_ptrs(tl);
1395   guarantee(count == (size_t)tl->count(), "Node count is incorrect");
1396   if (tl->head() != NULL) {
1397     tl->head_as_TreeChunk()->verify_tree_chunk_list();
1398   }
1399   verify_tree_helper(tl->left());
1400   verify_tree_helper(tl->right());
1401 }
1402 
1403 template <class Chunk_t, template <class> class FreeList_t>
1404 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify() const {
1405   verify_tree();
1406   guarantee(total_size() == total_size_in_tree(root()), "Total Size inconsistency");
1407 }
1408 
1409 template class TreeList<Metablock, FreeList>;
1410 template class BinaryTreeDictionary<Metablock, FreeList>;
1411 template class TreeChunk<Metablock, FreeList>;
1412 
1413 template class TreeList<Metachunk, FreeList>;
1414 template class BinaryTreeDictionary<Metachunk, FreeList>;
1415 template class TreeChunk<Metachunk, FreeList>;
1416 
1417 
1418 #if INCLUDE_ALL_GCS
1419 // Explicitly instantiate these types for FreeChunk.
1420 template class TreeList<FreeChunk, AdaptiveFreeList>;
1421 template class BinaryTreeDictionary<FreeChunk, AdaptiveFreeList>;
1422 template class TreeChunk<FreeChunk, AdaptiveFreeList>;
1423 
1424 #endif // INCLUDE_ALL_GCS


  27 #include "gc_implementation/shared/allocationStats.hpp"
  28 #include "memory/binaryTreeDictionary.hpp"
  29 #include "memory/freeList.hpp"
  30 #include "memory/freeBlockDictionary.hpp"
  31 #include "memory/metachunk.hpp"
  32 #include "runtime/globals.hpp"
  33 #include "utilities/ostream.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "gc_implementation/shared/spaceDecorator.hpp"
  36 #if INCLUDE_ALL_GCS
  37 #include "gc_implementation/concurrentMarkSweep/adaptiveFreeList.hpp"
  38 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
  39 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
  40 #endif // INCLUDE_ALL_GCS
  41 
  42 ////////////////////////////////////////////////////////////////////////////////
  43 // A binary tree based search structure for free blocks.
  44 // This is currently used in the Concurrent Mark&Sweep implementation.
  45 ////////////////////////////////////////////////////////////////////////////////
  46 
  47 template <class Chunk_t, class FreeList_t>
  48 size_t TreeChunk<Chunk_t, FreeList_t>::_min_tree_chunk_size = sizeof(TreeChunk<Chunk_t,  FreeList_t>)/HeapWordSize;
  49 
  50 template <class Chunk_t, class FreeList_t>
  51 TreeChunk<Chunk_t, FreeList_t>* TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(Chunk_t* fc) {
  52   // Do some assertion checking here.
  53   return (TreeChunk<Chunk_t, FreeList_t>*) fc;
  54 }
  55 
  56 template <class Chunk_t, class FreeList_t>
  57 void TreeChunk<Chunk_t, FreeList_t>::verify_tree_chunk_list() const {
  58   TreeChunk<Chunk_t, FreeList_t>* nextTC = (TreeChunk<Chunk_t, FreeList_t>*)next();
  59   if (prev() != NULL) { // interior list node shouldn't have tree fields
  60     guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL &&
  61               embedded_list()->right()  == NULL, "should be clear");
  62   }
  63   if (nextTC != NULL) {
  64     guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain");
  65     guarantee(nextTC->size() == size(), "wrong size");
  66     nextTC->verify_tree_chunk_list();
  67   }
  68 }
  69 
  70 template <class Chunk_t, class FreeList_t>
  71 TreeList<Chunk_t, FreeList_t>::TreeList() : _parent(NULL),
  72   _left(NULL), _right(NULL) {}
  73 
  74 template <class Chunk_t, class FreeList_t>
  75 TreeList<Chunk_t, FreeList_t>*
  76 TreeList<Chunk_t, FreeList_t>::as_TreeList(TreeChunk<Chunk_t,FreeList_t>* tc) {
  77   // This first free chunk in the list will be the tree list.
  78   assert((tc->size() >= (TreeChunk<Chunk_t, FreeList_t>::min_size())),
  79     "Chunk is too small for a TreeChunk");
  80   TreeList<Chunk_t, FreeList_t>* tl = tc->embedded_list();
  81   tl->initialize();
  82   tc->set_list(tl);
  83   tl->set_size(tc->size());
  84   tl->link_head(tc);
  85   tl->link_tail(tc);
  86   tl->set_count(1);
  87   assert(tl->parent() == NULL, "Should be clear");
  88   return tl;
  89 }
  90 
  91 template <class Chunk_t, class FreeList_t>













  92 TreeList<Chunk_t, FreeList_t>*
  93 TreeList<Chunk_t, FreeList_t>::as_TreeList(HeapWord* addr, size_t size) {
  94   TreeChunk<Chunk_t, FreeList_t>* tc = (TreeChunk<Chunk_t, FreeList_t>*) addr;
  95   assert((size >= TreeChunk<Chunk_t, FreeList_t>::min_size()),
  96     "Chunk is too small for a TreeChunk");
  97   // The space will have been mangled initially but
  98   // is not remangled when a Chunk_t is returned to the free list
  99   // (since it is used to maintain the chunk on the free list).
 100   tc->assert_is_mangled();
 101   tc->set_size(size);
 102   tc->link_prev(NULL);
 103   tc->link_next(NULL);
 104   TreeList<Chunk_t, FreeList_t>* tl = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);
 105   return tl;
 106 }
 107 
 108 
 109 #if INCLUDE_ALL_GCS
 110 // Specialize for AdaptiveFreeList which tries to avoid
 111 // splitting a chunk of a size that is under populated in favor of
 112 // an over populated size.  The general get_better_list() just returns
 113 // the current list.
 114 template <>
 115 TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >*
 116 TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >::get_better_list(
 117   BinaryTreeDictionary<FreeChunk, ::AdaptiveFreeList<FreeChunk> >* dictionary) {
 118   // A candidate chunk has been found.  If it is already under
 119   // populated, get a chunk associated with the hint for this
 120   // chunk.
 121 
 122   TreeList<FreeChunk, ::AdaptiveFreeList<FreeChunk> >* curTL = this;
 123   if (surplus() <= 0) {
 124     /* Use the hint to find a size with a surplus, and reset the hint. */
 125     TreeList<FreeChunk, ::AdaptiveFreeList<FreeChunk> >* hintTL = this;
 126     while (hintTL->hint() != 0) {
 127       assert(hintTL->hint() > hintTL->size(),
 128         "hint points in the wrong direction");
 129       hintTL = dictionary->find_list(hintTL->hint());
 130       assert(curTL != hintTL, "Infinite loop");
 131       if (hintTL == NULL ||
 132           hintTL == curTL /* Should not happen but protect against it */ ) {
 133         // No useful hint.  Set the hint to NULL and go on.
 134         curTL->set_hint(0);
 135         break;
 136       }
 137       assert(hintTL->size() > curTL->size(), "hint is inconsistent");
 138       if (hintTL->surplus() > 0) {
 139         // The hint led to a list that has a surplus.  Use it.
 140         // Set the hint for the candidate to an overpopulated
 141         // size.
 142         curTL->set_hint(hintTL->size());
 143         // Change the candidate.
 144         curTL = hintTL;
 145         break;
 146       }
 147     }
 148   }
 149   return curTL;
 150 }
 151 #endif // INCLUDE_ALL_GCS
 152 
 153 template <class Chunk_t, class FreeList_t>
 154 TreeList<Chunk_t, FreeList_t>*
 155 TreeList<Chunk_t, FreeList_t>::get_better_list(
 156   BinaryTreeDictionary<Chunk_t, FreeList_t>* dictionary) {
 157   return this;
 158 }
 159 
 160 template <class Chunk_t, class FreeList_t>
 161 TreeList<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::remove_chunk_replace_if_needed(TreeChunk<Chunk_t, FreeList_t>* tc) {
 162 
 163   TreeList<Chunk_t, FreeList_t>* retTL = this;
 164   Chunk_t* list = head();
 165   assert(!list || list != list->next(), "Chunk on list twice");
 166   assert(tc != NULL, "Chunk being removed is NULL");
 167   assert(parent() == NULL || this == parent()->left() ||
 168     this == parent()->right(), "list is inconsistent");
 169   assert(tc->is_free(), "Header is not marked correctly");
 170   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 171   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 172 
 173   Chunk_t* prevFC = tc->prev();
 174   TreeChunk<Chunk_t, FreeList_t>* nextTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(tc->next());
 175   assert(list != NULL, "should have at least the target chunk");
 176 
 177   // Is this the first item on the list?
 178   if (tc == list) {
 179     // The "getChunk..." functions for a TreeList<Chunk_t, FreeList_t> will not return the
 180     // first chunk in the list unless it is the last chunk in the list


 256         next_found = true;
 257       }
 258     }
 259     assert(prevFC == NULL || prev_found, "Chunk was lost from list");
 260     assert(nextTC == NULL || next_found, "Chunk was lost from list");
 261     assert(retTL->parent() == NULL ||
 262            retTL == retTL->parent()->left() ||
 263            retTL == retTL->parent()->right(),
 264            "list is inconsistent");
 265   )
 266   retTL->decrement_count();
 267 
 268   assert(tc->is_free(), "Should still be a free chunk");
 269   assert(retTL->head() == NULL || retTL->head()->prev() == NULL,
 270     "list invariant");
 271   assert(retTL->tail() == NULL || retTL->tail()->next() == NULL,
 272     "list invariant");
 273   return retTL;
 274 }
 275 
 276 template <class Chunk_t, class FreeList_t>
 277 void TreeList<Chunk_t, FreeList_t>::return_chunk_at_tail(TreeChunk<Chunk_t, FreeList_t>* chunk) {
 278   assert(chunk != NULL, "returning NULL chunk");
 279   assert(chunk->list() == this, "list should be set for chunk");
 280   assert(tail() != NULL, "The tree list is embedded in the first chunk");
 281   // which means that the list can never be empty.
 282   assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
 283   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 284   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 285 
 286   Chunk_t* fc = tail();
 287   fc->link_after(chunk);
 288   this->link_tail(chunk);
 289 
 290   assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list");
 291   FreeList_t::increment_count();
 292   debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
 293   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 294   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 295 }
 296 
 297 // Add this chunk at the head of the list.  "At the head of the list"
 298 // is defined to be after the chunk pointer to by head().  This is
 299 // because the TreeList<Chunk_t, FreeList_t> is embedded in the first TreeChunk<Chunk_t, FreeList_t> in the
 300 // list.  See the definition of TreeChunk<Chunk_t, FreeList_t>.
 301 template <class Chunk_t, class FreeList_t>
 302 void TreeList<Chunk_t, FreeList_t>::return_chunk_at_head(TreeChunk<Chunk_t, FreeList_t>* chunk) {
 303   assert(chunk->list() == this, "list should be set for chunk");
 304   assert(head() != NULL, "The tree list is embedded in the first chunk");
 305   assert(chunk != NULL, "returning NULL chunk");
 306   assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
 307   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 308   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 309 
 310   Chunk_t* fc = head()->next();
 311   if (fc != NULL) {
 312     chunk->link_after(fc);
 313   } else {
 314     assert(tail() == NULL, "List is inconsistent");
 315     this->link_tail(chunk);
 316   }
 317   head()->link_after(chunk);
 318   assert(!head() || size() == head()->size(), "Wrong sized chunk in list");
 319   FreeList_t::increment_count();
 320   debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
 321   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 322   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 323 }
 324 
 325 template <class Chunk_t, class FreeList_t>
 326 void TreeChunk<Chunk_t, FreeList_t>::assert_is_mangled() const {
 327   assert((ZapUnusedHeapArea &&
 328           SpaceMangler::is_mangled((HeapWord*) Chunk_t::size_addr()) &&
 329           SpaceMangler::is_mangled((HeapWord*) Chunk_t::prev_addr()) &&
 330           SpaceMangler::is_mangled((HeapWord*) Chunk_t::next_addr())) ||
 331           (size() == 0 && prev() == NULL && next() == NULL),
 332     "Space should be clear or mangled");
 333 }
 334 
 335 template <class Chunk_t, class FreeList_t>
 336 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::head_as_TreeChunk() {
 337   assert(head() == NULL || (TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head())->list() == this),
 338     "Wrong type of chunk?");
 339   return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head());
 340 }
 341 
 342 template <class Chunk_t, class FreeList_t>
 343 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::first_available() {
 344   assert(head() != NULL, "The head of the list cannot be NULL");
 345   Chunk_t* fc = head()->next();
 346   TreeChunk<Chunk_t, FreeList_t>* retTC;
 347   if (fc == NULL) {
 348     retTC = head_as_TreeChunk();
 349   } else {
 350     retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);
 351   }
 352   assert(retTC->list() == this, "Wrong type of chunk.");
 353   return retTC;
 354 }
 355 
 356 // Returns the block with the largest heap address amongst
 357 // those in the list for this size; potentially slow and expensive,
 358 // use with caution!
 359 template <class Chunk_t, class FreeList_t>
 360 TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::largest_address() {
 361   assert(head() != NULL, "The head of the list cannot be NULL");
 362   Chunk_t* fc = head()->next();
 363   TreeChunk<Chunk_t, FreeList_t>* retTC;
 364   if (fc == NULL) {
 365     retTC = head_as_TreeChunk();
 366   } else {
 367     // walk down the list and return the one with the highest
 368     // heap address among chunks of this size.
 369     Chunk_t* last = fc;
 370     while (fc->next() != NULL) {
 371       if ((HeapWord*)last < (HeapWord*)fc) {
 372         last = fc;
 373       }
 374       fc = fc->next();
 375     }
 376     retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(last);
 377   }
 378   assert(retTC->list() == this, "Wrong type of chunk.");
 379   return retTC;
 380 }
 381 
 382 template <class Chunk_t, class FreeList_t>
 383 BinaryTreeDictionary<Chunk_t, FreeList_t>::BinaryTreeDictionary(MemRegion mr) {
 384   assert((mr.byte_size() > min_size()), "minimum chunk size");
 385 
 386   reset(mr);
 387   assert(root()->left() == NULL, "reset check failed");
 388   assert(root()->right() == NULL, "reset check failed");
 389   assert(root()->head()->next() == NULL, "reset check failed");
 390   assert(root()->head()->prev() == NULL, "reset check failed");
 391   assert(total_size() == root()->size(), "reset check failed");
 392   assert(total_free_blocks() == 1, "reset check failed");
 393 }
 394 
 395 template <class Chunk_t, class FreeList_t>
 396 void BinaryTreeDictionary<Chunk_t, FreeList_t>::inc_total_size(size_t inc) {
 397   _total_size = _total_size + inc;
 398 }
 399 
 400 template <class Chunk_t, class FreeList_t>
 401 void BinaryTreeDictionary<Chunk_t, FreeList_t>::dec_total_size(size_t dec) {
 402   _total_size = _total_size - dec;
 403 }
 404 
 405 template <class Chunk_t, class FreeList_t>
 406 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(MemRegion mr) {
 407   assert((mr.byte_size() > min_size()), "minimum chunk size");
 408   set_root(TreeList<Chunk_t, FreeList_t>::as_TreeList(mr.start(), mr.word_size()));
 409   set_total_size(mr.word_size());
 410   set_total_free_blocks(1);
 411 }
 412 
 413 template <class Chunk_t, class FreeList_t>
 414 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(HeapWord* addr, size_t byte_size) {
 415   MemRegion mr(addr, heap_word_size(byte_size));
 416   reset(mr);
 417 }
 418 
 419 template <class Chunk_t, class FreeList_t>
 420 void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset() {
 421   set_root(NULL);
 422   set_total_size(0);
 423   set_total_free_blocks(0);
 424 }
 425 
 426 // Get a free block of size at least size from tree, or NULL.
 427 template <class Chunk_t, class FreeList_t>
 428 TreeChunk<Chunk_t, FreeList_t>*
 429 BinaryTreeDictionary<Chunk_t, FreeList_t>::get_chunk_from_tree(
 430                               size_t size,
 431                               enum FreeBlockDictionary<Chunk_t>::Dither dither)
 432 {
 433   TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
 434   TreeChunk<Chunk_t, FreeList_t>* retTC = NULL;
 435 
 436   assert((size >= min_size()), "minimum chunk size");
 437   if (FLSVerifyDictionary) {
 438     verify_tree();
 439   }
 440   // starting at the root, work downwards trying to find match.
 441   // Remember the last node of size too great or too small.
 442   for (prevTL = curTL = root(); curTL != NULL;) {
 443     if (curTL->size() == size) {        // exact match
 444       break;
 445     }
 446     prevTL = curTL;
 447     if (curTL->size() < size) {        // proceed to right sub-tree


 466   if (curTL != NULL) {
 467     assert(curTL->size() >= size, "size inconsistency");
 468 
 469     curTL = curTL->get_better_list(this);
 470 
 471     retTC = curTL->first_available();
 472     assert((retTC != NULL) && (curTL->count() > 0),
 473       "A list in the binary tree should not be NULL");
 474     assert(retTC->size() >= size,
 475       "A chunk of the wrong size was found");
 476     remove_chunk_from_tree(retTC);
 477     assert(retTC->is_free(), "Header is not marked correctly");
 478   }
 479 
 480   if (FLSVerifyDictionary) {
 481     verify();
 482   }
 483   return retTC;
 484 }
 485 
 486 template <class Chunk_t, class FreeList_t>
 487 TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_list(size_t size) const {
 488   TreeList<Chunk_t, FreeList_t>* curTL;
 489   for (curTL = root(); curTL != NULL;) {
 490     if (curTL->size() == size) {        // exact match
 491       break;
 492     }
 493 
 494     if (curTL->size() < size) {        // proceed to right sub-tree
 495       curTL = curTL->right();
 496     } else {                           // proceed to left sub-tree
 497       assert(curTL->size() > size, "size inconsistency");
 498       curTL = curTL->left();
 499     }
 500   }
 501   return curTL;
 502 }
 503 
 504 
 505 template <class Chunk_t, class FreeList_t>
 506 bool BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_chunk_in_free_list(Chunk_t* tc) const {
 507   size_t size = tc->size();
 508   TreeList<Chunk_t, FreeList_t>* tl = find_list(size);
 509   if (tl == NULL) {
 510     return false;
 511   } else {
 512     return tl->verify_chunk_in_free_list(tc);
 513   }
 514 }
 515 
 516 template <class Chunk_t, class FreeList_t>
 517 Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_largest_dict() const {
 518   TreeList<Chunk_t, FreeList_t> *curTL = root();
 519   if (curTL != NULL) {
 520     while(curTL->right() != NULL) curTL = curTL->right();
 521     return curTL->largest_address();
 522   } else {
 523     return NULL;
 524   }
 525 }
 526 
 527 // Remove the current chunk from the tree.  If it is not the last
 528 // chunk in a list on a tree node, just unlink it.
 529 // If it is the last chunk in the list (the next link is NULL),
 530 // remove the node and repair the tree.
 531 template <class Chunk_t, class FreeList_t>
 532 TreeChunk<Chunk_t, FreeList_t>*
 533 BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_chunk_from_tree(TreeChunk<Chunk_t, FreeList_t>* tc) {
 534   assert(tc != NULL, "Should not call with a NULL chunk");
 535   assert(tc->is_free(), "Header is not marked correctly");
 536 
 537   TreeList<Chunk_t, FreeList_t> *newTL, *parentTL;
 538   TreeChunk<Chunk_t, FreeList_t>* retTC;
 539   TreeList<Chunk_t, FreeList_t>* tl = tc->list();
 540   debug_only(
 541     bool removing_only_chunk = false;
 542     if (tl == _root) {
 543       if ((_root->left() == NULL) && (_root->right() == NULL)) {
 544         if (_root->count() == 1) {
 545           assert(_root->head() == tc, "Should only be this one chunk");
 546           removing_only_chunk = true;
 547         }
 548       }
 549     }
 550   )
 551   assert(tl != NULL, "List should be set");


 652   }
 653 
 654   assert(total_size() >= retTC->size(), "Incorrect total size");
 655   dec_total_size(retTC->size());     // size book-keeping
 656   assert(total_free_blocks() > 0, "Incorrect total count");
 657   set_total_free_blocks(total_free_blocks() - 1);
 658 
 659   assert(retTC != NULL, "null chunk?");
 660   assert(retTC->prev() == NULL && retTC->next() == NULL,
 661          "should return without encumbrances");
 662   if (FLSVerifyDictionary) {
 663     verify_tree();
 664   }
 665   assert(!removing_only_chunk || _root == NULL, "root should be NULL");
 666   return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(retTC);
 667 }
 668 
 669 // Remove the leftmost node (lm) in the tree and return it.
 670 // If lm has a right child, link it to the left node of
 671 // the parent of lm.
 672 template <class Chunk_t, class FreeList_t>
 673 TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_tree_minimum(TreeList<Chunk_t, FreeList_t>* tl) {
 674   assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree");
 675   // locate the subtree minimum by walking down left branches
 676   TreeList<Chunk_t, FreeList_t>* curTL = tl;
 677   for (; curTL->left() != NULL; curTL = curTL->left());
 678   // obviously curTL now has at most one child, a right child
 679   if (curTL != root()) {  // Should this test just be removed?
 680     TreeList<Chunk_t, FreeList_t>* parentTL = curTL->parent();
 681     if (parentTL->left() == curTL) { // curTL is a left child
 682       parentTL->set_left(curTL->right());
 683     } else {
 684       // If the list tl has no left child, then curTL may be
 685       // the right child of parentTL.
 686       assert(parentTL->right() == curTL, "should be a right child");
 687       parentTL->set_right(curTL->right());
 688     }
 689   } else {
 690     // The only use of this method would not pass the root of the
 691     // tree (as indicated by the assertion above that the tree list
 692     // has a parent) but the specification does not explicitly exclude the
 693     // passing of the root so accommodate it.
 694     set_root(NULL);
 695   }
 696   debug_only(
 697     curTL->clear_parent();  // Test if this needs to be cleared
 698     curTL->clear_right();    // recall, above, left child is already null
 699   )
 700   // we just excised a (non-root) node, we should still verify all tree invariants
 701   if (FLSVerifyDictionary) {
 702     verify_tree();
 703   }
 704   return curTL;
 705 }
 706 
 707 template <class Chunk_t, class FreeList_t>
 708 void BinaryTreeDictionary<Chunk_t, FreeList_t>::insert_chunk_in_tree(Chunk_t* fc) {
 709   TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
 710   size_t size = fc->size();
 711 
 712   assert((size >= min_size()),
 713     err_msg(SIZE_FORMAT " is too small to be a TreeChunk<Chunk_t, FreeList_t> " SIZE_FORMAT,
 714       size, min_size()));
 715   if (FLSVerifyDictionary) {
 716     verify_tree();
 717   }
 718 
 719   fc->clear_next();
 720   fc->link_prev(NULL);
 721 
 722   // work down from the _root, looking for insertion point
 723   for (prevTL = curTL = root(); curTL != NULL;) {
 724     if (curTL->size() == size)  // exact match
 725       break;
 726     prevTL = curTL;
 727     if (curTL->size() > size) { // follow left branch


 753         prevTL->set_right(newTL);
 754       } else {                       // am left child
 755         assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv");
 756         prevTL->set_left(newTL);
 757       }
 758     }
 759   }
 760   assert(tc->list() != NULL, "Tree list should be set");
 761 
 762   inc_total_size(size);
 763   // Method 'total_size_in_tree' walks through the every block in the
 764   // tree, so it can cause significant performance loss if there are
 765   // many blocks in the tree
 766   assert(!FLSVerifyDictionary || total_size_in_tree(root()) == total_size(), "_total_size inconsistency");
 767   set_total_free_blocks(total_free_blocks() + 1);
 768   if (FLSVerifyDictionary) {
 769     verify_tree();
 770   }
 771 }
 772 
 773 template <class Chunk_t, class FreeList_t>
 774 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::max_chunk_size() const {
 775   FreeBlockDictionary<Chunk_t>::verify_par_locked();
 776   TreeList<Chunk_t, FreeList_t>* tc = root();
 777   if (tc == NULL) return 0;
 778   for (; tc->right() != NULL; tc = tc->right());
 779   return tc->size();
 780 }
 781 
 782 template <class Chunk_t, class FreeList_t>
 783 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_list_length(TreeList<Chunk_t, FreeList_t>* tl) const {
 784   size_t res;
 785   res = tl->count();
 786 #ifdef ASSERT
 787   size_t cnt;
 788   Chunk_t* tc = tl->head();
 789   for (cnt = 0; tc != NULL; tc = tc->next(), cnt++);
 790   assert(res == cnt, "The count is not being maintained correctly");
 791 #endif
 792   return res;
 793 }
 794 
 795 template <class Chunk_t, class FreeList_t>
 796 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_size_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
 797   if (tl == NULL)
 798     return 0;
 799   return (tl->size() * total_list_length(tl)) +
 800          total_size_in_tree(tl->left())    +
 801          total_size_in_tree(tl->right());
 802 }
 803 
 804 template <class Chunk_t, class FreeList_t>
 805 double BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_of_squared_block_sizes(TreeList<Chunk_t, FreeList_t>* const tl) const {
 806   if (tl == NULL) {
 807     return 0.0;
 808   }
 809   double size = (double)(tl->size());
 810   double curr = size * size * total_list_length(tl);
 811   curr += sum_of_squared_block_sizes(tl->left());
 812   curr += sum_of_squared_block_sizes(tl->right());
 813   return curr;
 814 }
 815 
 816 template <class Chunk_t, class FreeList_t>
 817 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_free_blocks_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
 818   if (tl == NULL)
 819     return 0;
 820   return total_list_length(tl) +
 821          total_free_blocks_in_tree(tl->left()) +
 822          total_free_blocks_in_tree(tl->right());
 823 }
 824 
 825 template <class Chunk_t, class FreeList_t>
 826 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::num_free_blocks() const {
 827   assert(total_free_blocks_in_tree(root()) == total_free_blocks(),
 828          "_total_free_blocks inconsistency");
 829   return total_free_blocks();
 830 }
 831 
 832 template <class Chunk_t, class FreeList_t>
 833 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
 834   if (tl == NULL)
 835     return 0;
 836   return 1 + MAX2(tree_height_helper(tl->left()),
 837                   tree_height_helper(tl->right()));
 838 }
 839 
 840 template <class Chunk_t, class FreeList_t>
 841 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height() const {
 842   return tree_height_helper(root());
 843 }
 844 
 845 template <class Chunk_t, class FreeList_t>
 846 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
 847   if (tl == NULL) {
 848     return 0;
 849   }
 850   return 1 + total_nodes_helper(tl->left()) +
 851     total_nodes_helper(tl->right());
 852 }
 853 
 854 template <class Chunk_t, class FreeList_t>
 855 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
 856   return total_nodes_helper(root());
 857 }
 858 
 859 template <class Chunk_t, class FreeList_t>
 860 void BinaryTreeDictionary<Chunk_t, FreeList_t>::dict_census_update(size_t size, bool split, bool birth){}
 861 
 862 #if INCLUDE_ALL_GCS
 863 template <>
 864 void AFLBinaryTreeDictionary::dict_census_update(size_t size, bool split, bool birth) {
 865   TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >* nd = find_list(size);
 866   if (nd) {
 867     if (split) {
 868       if (birth) {
 869         nd->increment_split_births();
 870         nd->increment_surplus();
 871       }  else {
 872         nd->increment_split_deaths();
 873         nd->decrement_surplus();
 874       }
 875     } else {
 876       if (birth) {
 877         nd->increment_coal_births();
 878         nd->increment_surplus();
 879       } else {
 880         nd->increment_coal_deaths();
 881         nd->decrement_surplus();
 882       }
 883     }
 884   }
 885   // A list for this size may not be found (nd == 0) if
 886   //   This is a death where the appropriate list is now
 887   //     empty and has been removed from the list.
 888   //   This is a birth associated with a LinAB.  The chunk
 889   //     for the LinAB is not in the dictionary.
 890 }
 891 #endif // INCLUDE_ALL_GCS
 892 
 893 template <class Chunk_t, class FreeList_t>
 894 bool BinaryTreeDictionary<Chunk_t, FreeList_t>::coal_dict_over_populated(size_t size) {
 895   // For the general type of freelists, encourage coalescing by
 896   // returning true.
 897   return true;
 898 }
 899 
 900 #if INCLUDE_ALL_GCS
 901 template <>
 902 bool AFLBinaryTreeDictionary::coal_dict_over_populated(size_t size) {
 903   if (FLSAlwaysCoalesceLarge) return true;
 904 
 905   TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >* list_of_size = find_list(size);
 906   // None of requested size implies overpopulated.
 907   return list_of_size == NULL || list_of_size->coal_desired() <= 0 ||
 908          list_of_size->count() > list_of_size->coal_desired();
 909 }
 910 #endif // INCLUDE_ALL_GCS
 911 
 912 // Closures for walking the binary tree.
 913 //   do_list() walks the free list in a node applying the closure
 914 //     to each free chunk in the list
 915 //   do_tree() walks the nodes in the binary tree applying do_list()
 916 //     to each list at each node.
 917 
 918 template <class Chunk_t, class FreeList_t>
 919 class TreeCensusClosure : public StackObj {
 920  protected:
 921   virtual void do_list(FreeList_t* fl) = 0;
 922  public:
 923   virtual void do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
 924 };
 925 
 926 template <class Chunk_t, class FreeList_t>
 927 class AscendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
 928  public:
 929   void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
 930     if (tl != NULL) {
 931       do_tree(tl->left());
 932       this->do_list(tl);
 933       do_tree(tl->right());
 934     }
 935   }
 936 };
 937 
 938 template <class Chunk_t, class FreeList_t>
 939 class DescendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
 940  public:
 941   void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
 942     if (tl != NULL) {
 943       do_tree(tl->right());
 944       this->do_list(tl);
 945       do_tree(tl->left());
 946     }
 947   }
 948 };
 949 
 950 // For each list in the tree, calculate the desired, desired
 951 // coalesce, count before sweep, and surplus before sweep.
 952 template <class Chunk_t, class FreeList_t>
 953 class BeginSweepClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
 954   double _percentage;
 955   float _inter_sweep_current;
 956   float _inter_sweep_estimate;
 957   float _intra_sweep_estimate;
 958 
 959  public:
 960   BeginSweepClosure(double p, float inter_sweep_current,
 961                               float inter_sweep_estimate,
 962                               float intra_sweep_estimate) :
 963    _percentage(p),
 964    _inter_sweep_current(inter_sweep_current),
 965    _inter_sweep_estimate(inter_sweep_estimate),
 966    _intra_sweep_estimate(intra_sweep_estimate) { }
 967 
 968   void do_list(FreeList<Chunk_t>* fl) {}
 969 
 970 #if INCLUDE_ALL_GCS
 971   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
 972     double coalSurplusPercent = _percentage;
 973     fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate, _intra_sweep_estimate);
 974     fl->set_coal_desired((ssize_t)((double)fl->desired() * coalSurplusPercent));
 975     fl->set_before_sweep(fl->count());
 976     fl->set_bfr_surp(fl->surplus());
 977   }
 978 #endif // INCLUDE_ALL_GCS
 979 };
 980 
 981 // Used to search the tree until a condition is met.
 982 // Similar to TreeCensusClosure but searches the
 983 // tree and returns promptly when found.
 984 
 985 template <class Chunk_t, class FreeList_t>
 986 class TreeSearchClosure : public StackObj {
 987  protected:
 988   virtual bool do_list(FreeList_t* fl) = 0;
 989  public:
 990   virtual bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
 991 };
 992 
 993 #if 0 //  Don't need this yet but here for symmetry.
 994 template <class Chunk_t, class FreeList_t>
 995 class AscendTreeSearchClosure : public TreeSearchClosure<Chunk_t> {
 996  public:
 997   bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
 998     if (tl != NULL) {
 999       if (do_tree(tl->left())) return true;
1000       if (do_list(tl)) return true;
1001       if (do_tree(tl->right())) return true;
1002     }
1003     return false;
1004   }
1005 };
1006 #endif
1007 
1008 template <class Chunk_t, class FreeList_t>
1009 class DescendTreeSearchClosure : public TreeSearchClosure<Chunk_t, FreeList_t> {
1010  public:
1011   bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
1012     if (tl != NULL) {
1013       if (do_tree(tl->right())) return true;
1014       if (this->do_list(tl)) return true;
1015       if (do_tree(tl->left())) return true;
1016     }
1017     return false;
1018   }
1019 };
1020 
1021 // Searches the tree for a chunk that ends at the
1022 // specified address.
1023 template <class Chunk_t, class FreeList_t>
1024 class EndTreeSearchClosure : public DescendTreeSearchClosure<Chunk_t, FreeList_t> {
1025   HeapWord* _target;
1026   Chunk_t* _found;
1027 
1028  public:
1029   EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {}
1030   bool do_list(FreeList_t* fl) {
1031     Chunk_t* item = fl->head();
1032     while (item != NULL) {
1033       if (item->end() == (uintptr_t*) _target) {
1034         _found = item;
1035         return true;
1036       }
1037       item = item->next();
1038     }
1039     return false;
1040   }
1041   Chunk_t* found() { return _found; }
1042 };
1043 
1044 template <class Chunk_t, class FreeList_t>
1045 Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_chunk_ends_at(HeapWord* target) const {
1046   EndTreeSearchClosure<Chunk_t, FreeList_t> etsc(target);
1047   bool found_target = etsc.do_tree(root());
1048   assert(found_target || etsc.found() == NULL, "Consistency check");
1049   assert(!found_target || etsc.found() != NULL, "Consistency check");
1050   return etsc.found();
1051 }
1052 
1053 template <class Chunk_t, class FreeList_t>
1054 void BinaryTreeDictionary<Chunk_t, FreeList_t>::begin_sweep_dict_census(double coalSurplusPercent,
1055   float inter_sweep_current, float inter_sweep_estimate, float intra_sweep_estimate) {
1056   BeginSweepClosure<Chunk_t, FreeList_t> bsc(coalSurplusPercent, inter_sweep_current,
1057                                             inter_sweep_estimate,
1058                                             intra_sweep_estimate);
1059   bsc.do_tree(root());
1060 }
1061 
1062 // Closures and methods for calculating total bytes returned to the
1063 // free lists in the tree.
1064 #ifndef PRODUCT
1065 template <class Chunk_t, class FreeList_t>
1066 class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1067    public:
1068   void do_list(FreeList_t* fl) {
1069     fl->set_returned_bytes(0);
1070   }
1071 };
1072 
1073 template <class Chunk_t, class FreeList_t>
1074 void BinaryTreeDictionary<Chunk_t, FreeList_t>::initialize_dict_returned_bytes() {
1075   InitializeDictReturnedBytesClosure<Chunk_t, FreeList_t> idrb;
1076   idrb.do_tree(root());
1077 }
1078 
1079 template <class Chunk_t, class FreeList_t>
1080 class ReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1081   size_t _dict_returned_bytes;
1082  public:
1083   ReturnedBytesClosure() { _dict_returned_bytes = 0; }
1084   void do_list(FreeList_t* fl) {
1085     _dict_returned_bytes += fl->returned_bytes();
1086   }
1087   size_t dict_returned_bytes() { return _dict_returned_bytes; }
1088 };
1089 
1090 template <class Chunk_t, class FreeList_t>
1091 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_dict_returned_bytes() {
1092   ReturnedBytesClosure<Chunk_t, FreeList_t> rbc;
1093   rbc.do_tree(root());
1094 
1095   return rbc.dict_returned_bytes();
1096 }
1097 
1098 // Count the number of entries in the tree.
1099 template <class Chunk_t, class FreeList_t>
1100 class treeCountClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
1101  public:
1102   uint count;
1103   treeCountClosure(uint c) { count = c; }
1104   void do_list(FreeList_t* fl) {
1105     count++;
1106   }
1107 };
1108 
1109 template <class Chunk_t, class FreeList_t>
1110 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_count() {
1111   treeCountClosure<Chunk_t, FreeList_t> ctc(0);
1112   ctc.do_tree(root());
1113   return ctc.count;
1114 }
1115 #endif // PRODUCT
1116 
1117 // Calculate surpluses for the lists in the tree.
1118 template <class Chunk_t, class FreeList_t>
1119 class setTreeSurplusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1120   double percentage;
1121  public:
1122   setTreeSurplusClosure(double v) { percentage = v; }
1123   void do_list(FreeList<Chunk_t>* fl) {}
1124 
1125 #if INCLUDE_ALL_GCS
1126   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1127     double splitSurplusPercent = percentage;
1128     fl->set_surplus(fl->count() -
1129                    (ssize_t)((double)fl->desired() * splitSurplusPercent));
1130   }
1131 #endif // INCLUDE_ALL_GCS
1132 };
1133 
1134 template <class Chunk_t, class FreeList_t>
1135 void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_surplus(double splitSurplusPercent) {
1136   setTreeSurplusClosure<Chunk_t, FreeList_t> sts(splitSurplusPercent);
1137   sts.do_tree(root());
1138 }
1139 
1140 // Set hints for the lists in the tree.
1141 template <class Chunk_t, class FreeList_t>
1142 class setTreeHintsClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
1143   size_t hint;
1144  public:
1145   setTreeHintsClosure(size_t v) { hint = v; }
1146   void do_list(FreeList<Chunk_t>* fl) {}
1147 
1148 #if INCLUDE_ALL_GCS
1149   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1150     fl->set_hint(hint);
1151     assert(fl->hint() == 0 || fl->hint() > fl->size(),
1152       "Current hint is inconsistent");
1153     if (fl->surplus() > 0) {
1154       hint = fl->size();
1155     }
1156   }
1157 #endif // INCLUDE_ALL_GCS
1158 };
1159 
1160 template <class Chunk_t, class FreeList_t>
1161 void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_hints(void) {
1162   setTreeHintsClosure<Chunk_t, FreeList_t> sth(0);
1163   sth.do_tree(root());
1164 }
1165 
1166 // Save count before previous sweep and splits and coalesces.
1167 template <class Chunk_t, class FreeList_t>
1168 class clearTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1169   void do_list(FreeList<Chunk_t>* fl) {}
1170 
1171 #if INCLUDE_ALL_GCS
1172   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1173     fl->set_prev_sweep(fl->count());
1174     fl->set_coal_births(0);
1175     fl->set_coal_deaths(0);
1176     fl->set_split_births(0);
1177     fl->set_split_deaths(0);
1178   }
1179 #endif // INCLUDE_ALL_GCS
1180 };
1181 
1182 template <class Chunk_t, class FreeList_t>
1183 void BinaryTreeDictionary<Chunk_t, FreeList_t>::clear_tree_census(void) {
1184   clearTreeCensusClosure<Chunk_t, FreeList_t> ctc;
1185   ctc.do_tree(root());
1186 }
1187 
1188 // Do reporting and post sweep clean up.
1189 template <class Chunk_t, class FreeList_t>
1190 void BinaryTreeDictionary<Chunk_t, FreeList_t>::end_sweep_dict_census(double splitSurplusPercent) {
1191   // Does walking the tree 3 times hurt?
1192   set_tree_surplus(splitSurplusPercent);
1193   set_tree_hints();
1194   if (PrintGC && Verbose) {
1195     report_statistics();
1196   }
1197   clear_tree_census();
1198 }
1199 
1200 // Print summary statistics
1201 template <class Chunk_t, class FreeList_t>
1202 void BinaryTreeDictionary<Chunk_t, FreeList_t>::report_statistics() const {
1203   FreeBlockDictionary<Chunk_t>::verify_par_locked();
1204   gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n"
1205          "------------------------------------\n");
1206   size_t total_size = total_chunk_size(debug_only(NULL));
1207   size_t    free_blocks = num_free_blocks();
1208   gclog_or_tty->print("Total Free Space: %d\n", total_size);
1209   gclog_or_tty->print("Max   Chunk Size: %d\n", max_chunk_size());
1210   gclog_or_tty->print("Number of Blocks: %d\n", free_blocks);
1211   if (free_blocks > 0) {
1212     gclog_or_tty->print("Av.  Block  Size: %d\n", total_size/free_blocks);
1213   }
1214   gclog_or_tty->print("Tree      Height: %d\n", tree_height());
1215 }
1216 
1217 // Print census information - counts, births, deaths, etc.
1218 // for each list in the tree.  Also print some summary
1219 // information.
1220 template <class Chunk_t, class FreeList_t>
1221 class PrintTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1222   int _print_line;
1223   size_t _total_free;
1224   FreeList_t _total;
1225 
1226  public:
1227   PrintTreeCensusClosure() {
1228     _print_line = 0;
1229     _total_free = 0;
1230   }
1231   FreeList_t* total() { return &_total; }
1232   size_t total_free() { return _total_free; }
1233   void do_list(FreeList<Chunk_t>* fl) {
1234     if (++_print_line >= 40) {
1235       FreeList_t::print_labels_on(gclog_or_tty, "size");
1236       _print_line = 0;
1237     }
1238     fl->print_on(gclog_or_tty);
1239     _total_free +=            fl->count()            * fl->size()        ;
1240     total()->set_count(      total()->count()       + fl->count()      );
1241   }
1242 
1243 #if INCLUDE_ALL_GCS
1244   void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1245     if (++_print_line >= 40) {
1246       FreeList_t::print_labels_on(gclog_or_tty, "size");
1247       _print_line = 0;
1248     }
1249     fl->print_on(gclog_or_tty);
1250     _total_free +=           fl->count()             * fl->size()        ;
1251     total()->set_count(      total()->count()        + fl->count()      );
1252     total()->set_bfr_surp(   total()->bfr_surp()     + fl->bfr_surp()    );
1253     total()->set_surplus(    total()->split_deaths() + fl->surplus()    );
1254     total()->set_desired(    total()->desired()      + fl->desired()    );
1255     total()->set_prev_sweep(  total()->prev_sweep()   + fl->prev_sweep()  );
1256     total()->set_before_sweep(total()->before_sweep() + fl->before_sweep());
1257     total()->set_coal_births( total()->coal_births()  + fl->coal_births() );
1258     total()->set_coal_deaths( total()->coal_deaths()  + fl->coal_deaths() );
1259     total()->set_split_births(total()->split_births() + fl->split_births());
1260     total()->set_split_deaths(total()->split_deaths() + fl->split_deaths());
1261   }
1262 #endif // INCLUDE_ALL_GCS
1263 };
1264 
1265 template <class Chunk_t, class FreeList_t>
1266 void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_dict_census(void) const {
1267 
1268   gclog_or_tty->print("\nBinaryTree\n");
1269   FreeList_t::print_labels_on(gclog_or_tty, "size");
1270   PrintTreeCensusClosure<Chunk_t, FreeList_t> ptc;
1271   ptc.do_tree(root());
1272 
1273   FreeList_t* total = ptc.total();
1274   FreeList_t::print_labels_on(gclog_or_tty, " ");
1275 }
1276 
1277 #if INCLUDE_ALL_GCS
1278 template <>
1279 void AFLBinaryTreeDictionary::print_dict_census(void) const {
1280 
1281   gclog_or_tty->print("\nBinaryTree\n");
1282   AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, "size");
1283   PrintTreeCensusClosure<FreeChunk, AdaptiveFreeList<FreeChunk> > ptc;
1284   ptc.do_tree(root());
1285 
1286   AdaptiveFreeList<FreeChunk>* total = ptc.total();
1287   AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, " ");
1288   total->print_on(gclog_or_tty, "TOTAL\t");
1289   gclog_or_tty->print(
1290               "total_free(words): " SIZE_FORMAT_W(16)
1291               " growth: %8.5f  deficit: %8.5f\n",
1292               ptc.total_free(),
1293               (double)(total->split_births() + total->coal_births()
1294                      - total->split_deaths() - total->coal_deaths())
1295               /(total->prev_sweep() != 0 ? (double)total->prev_sweep() : 1.0),
1296              (double)(total->desired() - total->count())
1297              /(total->desired() != 0 ? (double)total->desired() : 1.0));
1298 }
1299 #endif // INCLUDE_ALL_GCS
1300 
1301 template <class Chunk_t, class FreeList_t>
1302 class PrintFreeListsClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1303   outputStream* _st;
1304   int _print_line;
1305 
1306  public:
1307   PrintFreeListsClosure(outputStream* st) {
1308     _st = st;
1309     _print_line = 0;
1310   }
1311   void do_list(FreeList_t* fl) {
1312     if (++_print_line >= 40) {
1313       FreeList_t::print_labels_on(_st, "size");
1314       _print_line = 0;
1315     }
1316     fl->print_on(gclog_or_tty);
1317     size_t sz = fl->size();
1318     for (Chunk_t* fc = fl->head(); fc != NULL;
1319          fc = fc->next()) {
1320       _st->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ")  %s",
1321                     fc, (HeapWord*)fc + sz,
1322                     fc->cantCoalesce() ? "\t CC" : "");
1323     }
1324   }
1325 };
1326 
1327 template <class Chunk_t, class FreeList_t>
1328 void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_free_lists(outputStream* st) const {
1329 
1330   FreeList_t::print_labels_on(st, "size");
1331   PrintFreeListsClosure<Chunk_t, FreeList_t> pflc(st);
1332   pflc.do_tree(root());
1333 }
1334 
1335 // Verify the following tree invariants:
1336 // . _root has no parent
1337 // . parent and child point to each other
1338 // . each node's key correctly related to that of its child(ren)
1339 template <class Chunk_t, class FreeList_t>
1340 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree() const {
1341   guarantee(root() == NULL || total_free_blocks() == 0 ||
1342     total_size() != 0, "_total_size should't be 0?");
1343   guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent");
1344   verify_tree_helper(root());
1345 }
1346 
1347 template <class Chunk_t, class FreeList_t>
1348 size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_prev_free_ptrs(TreeList<Chunk_t, FreeList_t>* tl) {
1349   size_t ct = 0;
1350   for (Chunk_t* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) {
1351     ct++;
1352     assert(curFC->prev() == NULL || curFC->prev()->is_free(),
1353       "Chunk should be free");
1354   }
1355   return ct;
1356 }
1357 
1358 // Note: this helper is recursive rather than iterative, so use with
1359 // caution on very deep trees; and watch out for stack overflow errors;
1360 // In general, to be used only for debugging.
1361 template <class Chunk_t, class FreeList_t>
1362 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
1363   if (tl == NULL)
1364     return;
1365   guarantee(tl->size() != 0, "A list must has a size");
1366   guarantee(tl->left()  == NULL || tl->left()->parent()  == tl,
1367          "parent<-/->left");
1368   guarantee(tl->right() == NULL || tl->right()->parent() == tl,
1369          "parent<-/->right");;
1370   guarantee(tl->left() == NULL  || tl->left()->size()    <  tl->size(),
1371          "parent !> left");
1372   guarantee(tl->right() == NULL || tl->right()->size()   >  tl->size(),
1373          "parent !< left");
1374   guarantee(tl->head() == NULL || tl->head()->is_free(), "!Free");
1375   guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl,
1376     "list inconsistency");
1377   guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL),
1378     "list count is inconsistent");
1379   guarantee(tl->count() > 1 || tl->head() == tl->tail(),
1380     "list is incorrectly constructed");
1381   size_t count = verify_prev_free_ptrs(tl);
1382   guarantee(count == (size_t)tl->count(), "Node count is incorrect");
1383   if (tl->head() != NULL) {
1384     tl->head_as_TreeChunk()->verify_tree_chunk_list();
1385   }
1386   verify_tree_helper(tl->left());
1387   verify_tree_helper(tl->right());
1388 }
1389 
1390 template <class Chunk_t, class FreeList_t>
1391 void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify() const {
1392   verify_tree();
1393   guarantee(total_size() == total_size_in_tree(root()), "Total Size inconsistency");
1394 }
1395 
1396 template class TreeList<Metablock, FreeList<Metablock> >;
1397 template class BinaryTreeDictionary<Metablock, FreeList<Metablock> >;
1398 template class TreeChunk<Metablock, FreeList<Metablock> >;
1399 
1400 template class TreeList<Metachunk, FreeList<Metachunk> >;
1401 template class BinaryTreeDictionary<Metachunk, FreeList<Metachunk> >;
1402 template class TreeChunk<Metachunk, FreeList<Metachunk> >;
1403 
1404 
1405 #if INCLUDE_ALL_GCS
1406 // Explicitly instantiate these types for FreeChunk.
1407 template class TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >;
1408 template class BinaryTreeDictionary<FreeChunk, AdaptiveFreeList<FreeChunk> >;
1409 template class TreeChunk<FreeChunk, AdaptiveFreeList<FreeChunk> >;
1410 
1411 #endif // INCLUDE_ALL_GCS