Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/binaryTreeDictionary.cpp
32285 views
/*1* Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "utilities/macros.hpp"26#include "gc_implementation/shared/allocationStats.hpp"27#include "memory/binaryTreeDictionary.hpp"28#include "memory/freeList.hpp"29#include "memory/freeBlockDictionary.hpp"30#include "memory/metachunk.hpp"31#include "runtime/globals.hpp"32#include "utilities/ostream.hpp"33#include "utilities/macros.hpp"34#include "gc_implementation/shared/spaceDecorator.hpp"35#if INCLUDE_ALL_GCS36#include "gc_implementation/concurrentMarkSweep/adaptiveFreeList.hpp"37#include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"38#include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"39#endif // INCLUDE_ALL_GCS4041////////////////////////////////////////////////////////////////////////////////42// A binary tree based search structure for free blocks.43// This is currently used in the Concurrent Mark&Sweep implementation.44////////////////////////////////////////////////////////////////////////////////4546template <class Chunk_t, class FreeList_t>47size_t TreeChunk<Chunk_t, FreeList_t>::_min_tree_chunk_size = sizeof(TreeChunk<Chunk_t, FreeList_t>)/HeapWordSize;4849template <class Chunk_t, class FreeList_t>50TreeChunk<Chunk_t, FreeList_t>* TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(Chunk_t* fc) {51// Do some assertion checking here.52return (TreeChunk<Chunk_t, FreeList_t>*) fc;53}5455template <class Chunk_t, class FreeList_t>56void TreeChunk<Chunk_t, FreeList_t>::verify_tree_chunk_list() const {57TreeChunk<Chunk_t, FreeList_t>* nextTC = (TreeChunk<Chunk_t, FreeList_t>*)next();58if (prev() != NULL) { // interior list node shouldn'r have tree fields59guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL &&60embedded_list()->right() == NULL, "should be clear");61}62if (nextTC != NULL) {63guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain");64guarantee(nextTC->size() == size(), "wrong size");65nextTC->verify_tree_chunk_list();66}67}6869template <class Chunk_t, class FreeList_t>70TreeList<Chunk_t, FreeList_t>::TreeList() : _parent(NULL),71_left(NULL), _right(NULL) {}7273template <class Chunk_t, class FreeList_t>74TreeList<Chunk_t, FreeList_t>*75TreeList<Chunk_t, FreeList_t>::as_TreeList(TreeChunk<Chunk_t,FreeList_t>* tc) {76// This first free chunk in the list will be the tree list.77assert((tc->size() >= (TreeChunk<Chunk_t, FreeList_t>::min_size())),78"Chunk is too small for a TreeChunk");79TreeList<Chunk_t, FreeList_t>* tl = tc->embedded_list();80tl->initialize();81tc->set_list(tl);82tl->set_size(tc->size());83tl->link_head(tc);84tl->link_tail(tc);85tl->set_count(1);86assert(tl->parent() == NULL, "Should be clear");87return tl;88}8990template <class Chunk_t, class FreeList_t>91TreeList<Chunk_t, FreeList_t>*92TreeList<Chunk_t, FreeList_t>::as_TreeList(HeapWord* addr, size_t size) {93TreeChunk<Chunk_t, FreeList_t>* tc = (TreeChunk<Chunk_t, FreeList_t>*) addr;94assert((size >= TreeChunk<Chunk_t, FreeList_t>::min_size()),95"Chunk is too small for a TreeChunk");96// The space will have been mangled initially but97// is not remangled when a Chunk_t is returned to the free list98// (since it is used to maintain the chunk on the free list).99tc->assert_is_mangled();100tc->set_size(size);101tc->link_prev(NULL);102tc->link_next(NULL);103TreeList<Chunk_t, FreeList_t>* tl = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);104return tl;105}106107108#if INCLUDE_ALL_GCS109// Specialize for AdaptiveFreeList which tries to avoid110// splitting a chunk of a size that is under populated in favor of111// an over populated size. The general get_better_list() just returns112// the current list.113template <>114TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >*115TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >::get_better_list(116BinaryTreeDictionary<FreeChunk, ::AdaptiveFreeList<FreeChunk> >* dictionary) {117// A candidate chunk has been found. If it is already under118// populated, get a chunk associated with the hint for this119// chunk.120121TreeList<FreeChunk, ::AdaptiveFreeList<FreeChunk> >* curTL = this;122if (surplus() <= 0) {123/* Use the hint to find a size with a surplus, and reset the hint. */124TreeList<FreeChunk, ::AdaptiveFreeList<FreeChunk> >* hintTL = this;125while (hintTL->hint() != 0) {126assert(hintTL->hint() > hintTL->size(),127"hint points in the wrong direction");128hintTL = dictionary->find_list(hintTL->hint());129assert(curTL != hintTL, "Infinite loop");130if (hintTL == NULL ||131hintTL == curTL /* Should not happen but protect against it */ ) {132// No useful hint. Set the hint to NULL and go on.133curTL->set_hint(0);134break;135}136assert(hintTL->size() > curTL->size(), "hint is inconsistent");137if (hintTL->surplus() > 0) {138// The hint led to a list that has a surplus. Use it.139// Set the hint for the candidate to an overpopulated140// size.141curTL->set_hint(hintTL->size());142// Change the candidate.143curTL = hintTL;144break;145}146}147}148return curTL;149}150#endif // INCLUDE_ALL_GCS151152template <class Chunk_t, class FreeList_t>153TreeList<Chunk_t, FreeList_t>*154TreeList<Chunk_t, FreeList_t>::get_better_list(155BinaryTreeDictionary<Chunk_t, FreeList_t>* dictionary) {156return this;157}158159template <class Chunk_t, class FreeList_t>160TreeList<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::remove_chunk_replace_if_needed(TreeChunk<Chunk_t, FreeList_t>* tc) {161162TreeList<Chunk_t, FreeList_t>* retTL = this;163Chunk_t* list = head();164assert(!list || list != list->next(), "Chunk on list twice");165assert(tc != NULL, "Chunk being removed is NULL");166assert(parent() == NULL || this == parent()->left() ||167this == parent()->right(), "list is inconsistent");168assert(tc->is_free(), "Header is not marked correctly");169assert(head() == NULL || head()->prev() == NULL, "list invariant");170assert(tail() == NULL || tail()->next() == NULL, "list invariant");171172Chunk_t* prevFC = tc->prev();173TreeChunk<Chunk_t, FreeList_t>* nextTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(tc->next());174assert(list != NULL, "should have at least the target chunk");175176// Is this the first item on the list?177if (tc == list) {178// The "getChunk..." functions for a TreeList<Chunk_t, FreeList_t> will not return the179// first chunk in the list unless it is the last chunk in the list180// because the first chunk is also acting as the tree node.181// When coalescing happens, however, the first chunk in the a tree182// list can be the start of a free range. Free ranges are removed183// from the free lists so that they are not available to be184// allocated when the sweeper yields (giving up the free list lock)185// to allow mutator activity. If this chunk is the first in the186// list and is not the last in the list, do the work to copy the187// TreeList<Chunk_t, FreeList_t> from the first chunk to the next chunk and update all188// the TreeList<Chunk_t, FreeList_t> pointers in the chunks in the list.189if (nextTC == NULL) {190assert(prevFC == NULL, "Not last chunk in the list");191set_tail(NULL);192set_head(NULL);193} else {194// copy embedded list.195nextTC->set_embedded_list(tc->embedded_list());196retTL = nextTC->embedded_list();197// Fix the pointer to the list in each chunk in the list.198// This can be slow for a long list. Consider having199// an option that does not allow the first chunk on the200// list to be coalesced.201for (TreeChunk<Chunk_t, FreeList_t>* curTC = nextTC; curTC != NULL;202curTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(curTC->next())) {203curTC->set_list(retTL);204}205// Fix the parent to point to the new TreeList<Chunk_t, FreeList_t>.206if (retTL->parent() != NULL) {207if (this == retTL->parent()->left()) {208retTL->parent()->set_left(retTL);209} else {210assert(this == retTL->parent()->right(), "Parent is incorrect");211retTL->parent()->set_right(retTL);212}213}214// Fix the children's parent pointers to point to the215// new list.216assert(right() == retTL->right(), "Should have been copied");217if (retTL->right() != NULL) {218retTL->right()->set_parent(retTL);219}220assert(left() == retTL->left(), "Should have been copied");221if (retTL->left() != NULL) {222retTL->left()->set_parent(retTL);223}224retTL->link_head(nextTC);225assert(nextTC->is_free(), "Should be a free chunk");226}227} else {228if (nextTC == NULL) {229// Removing chunk at tail of list230this->link_tail(prevFC);231}232// Chunk is interior to the list233prevFC->link_after(nextTC);234}235236// Below this point the embeded TreeList<Chunk_t, FreeList_t> being used for the237// tree node may have changed. Don't use "this"238// TreeList<Chunk_t, FreeList_t>*.239// chunk should still be a free chunk (bit set in _prev)240assert(!retTL->head() || retTL->size() == retTL->head()->size(),241"Wrong sized chunk in list");242debug_only(243tc->link_prev(NULL);244tc->link_next(NULL);245tc->set_list(NULL);246bool prev_found = false;247bool next_found = false;248for (Chunk_t* curFC = retTL->head();249curFC != NULL; curFC = curFC->next()) {250assert(curFC != tc, "Chunk is still in list");251if (curFC == prevFC) {252prev_found = true;253}254if (curFC == nextTC) {255next_found = true;256}257}258assert(prevFC == NULL || prev_found, "Chunk was lost from list");259assert(nextTC == NULL || next_found, "Chunk was lost from list");260assert(retTL->parent() == NULL ||261retTL == retTL->parent()->left() ||262retTL == retTL->parent()->right(),263"list is inconsistent");264)265retTL->decrement_count();266267assert(tc->is_free(), "Should still be a free chunk");268assert(retTL->head() == NULL || retTL->head()->prev() == NULL,269"list invariant");270assert(retTL->tail() == NULL || retTL->tail()->next() == NULL,271"list invariant");272return retTL;273}274275template <class Chunk_t, class FreeList_t>276void TreeList<Chunk_t, FreeList_t>::return_chunk_at_tail(TreeChunk<Chunk_t, FreeList_t>* chunk) {277assert(chunk != NULL, "returning NULL chunk");278assert(chunk->list() == this, "list should be set for chunk");279assert(tail() != NULL, "The tree list is embedded in the first chunk");280// which means that the list can never be empty.281assert(!this->verify_chunk_in_free_list(chunk), "Double entry");282assert(head() == NULL || head()->prev() == NULL, "list invariant");283assert(tail() == NULL || tail()->next() == NULL, "list invariant");284285Chunk_t* fc = tail();286fc->link_after(chunk);287this->link_tail(chunk);288289assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list");290FreeList_t::increment_count();291debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)292assert(head() == NULL || head()->prev() == NULL, "list invariant");293assert(tail() == NULL || tail()->next() == NULL, "list invariant");294}295296// Add this chunk at the head of the list. "At the head of the list"297// is defined to be after the chunk pointer to by head(). This is298// because the TreeList<Chunk_t, FreeList_t> is embedded in the first TreeChunk<Chunk_t, FreeList_t> in the299// list. See the definition of TreeChunk<Chunk_t, FreeList_t>.300template <class Chunk_t, class FreeList_t>301void TreeList<Chunk_t, FreeList_t>::return_chunk_at_head(TreeChunk<Chunk_t, FreeList_t>* chunk) {302assert(chunk->list() == this, "list should be set for chunk");303assert(head() != NULL, "The tree list is embedded in the first chunk");304assert(chunk != NULL, "returning NULL chunk");305assert(!this->verify_chunk_in_free_list(chunk), "Double entry");306assert(head() == NULL || head()->prev() == NULL, "list invariant");307assert(tail() == NULL || tail()->next() == NULL, "list invariant");308309Chunk_t* fc = head()->next();310if (fc != NULL) {311chunk->link_after(fc);312} else {313assert(tail() == NULL, "List is inconsistent");314this->link_tail(chunk);315}316head()->link_after(chunk);317assert(!head() || size() == head()->size(), "Wrong sized chunk in list");318FreeList_t::increment_count();319debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)320assert(head() == NULL || head()->prev() == NULL, "list invariant");321assert(tail() == NULL || tail()->next() == NULL, "list invariant");322}323324template <class Chunk_t, class FreeList_t>325void TreeChunk<Chunk_t, FreeList_t>::assert_is_mangled() const {326assert((ZapUnusedHeapArea &&327SpaceMangler::is_mangled((HeapWord*) Chunk_t::size_addr()) &&328SpaceMangler::is_mangled((HeapWord*) Chunk_t::prev_addr()) &&329SpaceMangler::is_mangled((HeapWord*) Chunk_t::next_addr())) ||330(size() == 0 && prev() == NULL && next() == NULL),331"Space should be clear or mangled");332}333334template <class Chunk_t, class FreeList_t>335TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::head_as_TreeChunk() {336assert(head() == NULL || (TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head())->list() == this),337"Wrong type of chunk?");338return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head());339}340341template <class Chunk_t, class FreeList_t>342TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::first_available() {343assert(head() != NULL, "The head of the list cannot be NULL");344Chunk_t* fc = head()->next();345TreeChunk<Chunk_t, FreeList_t>* retTC;346if (fc == NULL) {347retTC = head_as_TreeChunk();348} else {349retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);350}351assert(retTC->list() == this, "Wrong type of chunk.");352return retTC;353}354355// Returns the block with the largest heap address amongst356// those in the list for this size; potentially slow and expensive,357// use with caution!358template <class Chunk_t, class FreeList_t>359TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::largest_address() {360assert(head() != NULL, "The head of the list cannot be NULL");361Chunk_t* fc = head()->next();362TreeChunk<Chunk_t, FreeList_t>* retTC;363if (fc == NULL) {364retTC = head_as_TreeChunk();365} else {366// walk down the list and return the one with the highest367// heap address among chunks of this size.368Chunk_t* last = fc;369while (fc->next() != NULL) {370if ((HeapWord*)last < (HeapWord*)fc) {371last = fc;372}373fc = fc->next();374}375retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(last);376}377assert(retTC->list() == this, "Wrong type of chunk.");378return retTC;379}380381template <class Chunk_t, class FreeList_t>382BinaryTreeDictionary<Chunk_t, FreeList_t>::BinaryTreeDictionary(MemRegion mr) {383assert((mr.byte_size() > min_size()), "minimum chunk size");384385reset(mr);386assert(root()->left() == NULL, "reset check failed");387assert(root()->right() == NULL, "reset check failed");388assert(root()->head()->next() == NULL, "reset check failed");389assert(root()->head()->prev() == NULL, "reset check failed");390assert(total_size() == root()->size(), "reset check failed");391assert(total_free_blocks() == 1, "reset check failed");392}393394template <class Chunk_t, class FreeList_t>395void BinaryTreeDictionary<Chunk_t, FreeList_t>::inc_total_size(size_t inc) {396_total_size = _total_size + inc;397}398399template <class Chunk_t, class FreeList_t>400void BinaryTreeDictionary<Chunk_t, FreeList_t>::dec_total_size(size_t dec) {401_total_size = _total_size - dec;402}403404template <class Chunk_t, class FreeList_t>405void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(MemRegion mr) {406assert((mr.byte_size() > min_size()), "minimum chunk size");407set_root(TreeList<Chunk_t, FreeList_t>::as_TreeList(mr.start(), mr.word_size()));408set_total_size(mr.word_size());409set_total_free_blocks(1);410}411412template <class Chunk_t, class FreeList_t>413void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(HeapWord* addr, size_t byte_size) {414MemRegion mr(addr, heap_word_size(byte_size));415reset(mr);416}417418template <class Chunk_t, class FreeList_t>419void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset() {420set_root(NULL);421set_total_size(0);422set_total_free_blocks(0);423}424425// Get a free block of size at least size from tree, or NULL.426template <class Chunk_t, class FreeList_t>427TreeChunk<Chunk_t, FreeList_t>*428BinaryTreeDictionary<Chunk_t, FreeList_t>::get_chunk_from_tree(429size_t size,430enum FreeBlockDictionary<Chunk_t>::Dither dither)431{432TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;433TreeChunk<Chunk_t, FreeList_t>* retTC = NULL;434435assert((size >= min_size()), "minimum chunk size");436if (FLSVerifyDictionary) {437verify_tree();438}439// starting at the root, work downwards trying to find match.440// Remember the last node of size too great or too small.441for (prevTL = curTL = root(); curTL != NULL;) {442if (curTL->size() == size) { // exact match443break;444}445prevTL = curTL;446if (curTL->size() < size) { // proceed to right sub-tree447curTL = curTL->right();448} else { // proceed to left sub-tree449assert(curTL->size() > size, "size inconsistency");450curTL = curTL->left();451}452}453if (curTL == NULL) { // couldn't find exact match454455if (dither == FreeBlockDictionary<Chunk_t>::exactly) return NULL;456457// try and find the next larger size by walking back up the search path458for (curTL = prevTL; curTL != NULL;) {459if (curTL->size() >= size) break;460else curTL = curTL->parent();461}462assert(curTL == NULL || curTL->count() > 0,463"An empty list should not be in the tree");464}465if (curTL != NULL) {466assert(curTL->size() >= size, "size inconsistency");467468curTL = curTL->get_better_list(this);469470retTC = curTL->first_available();471assert((retTC != NULL) && (curTL->count() > 0),472"A list in the binary tree should not be NULL");473assert(retTC->size() >= size,474"A chunk of the wrong size was found");475remove_chunk_from_tree(retTC);476assert(retTC->is_free(), "Header is not marked correctly");477}478479if (FLSVerifyDictionary) {480verify();481}482return retTC;483}484485template <class Chunk_t, class FreeList_t>486TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_list(size_t size) const {487TreeList<Chunk_t, FreeList_t>* curTL;488for (curTL = root(); curTL != NULL;) {489if (curTL->size() == size) { // exact match490break;491}492493if (curTL->size() < size) { // proceed to right sub-tree494curTL = curTL->right();495} else { // proceed to left sub-tree496assert(curTL->size() > size, "size inconsistency");497curTL = curTL->left();498}499}500return curTL;501}502503504template <class Chunk_t, class FreeList_t>505bool BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_chunk_in_free_list(Chunk_t* tc) const {506size_t size = tc->size();507TreeList<Chunk_t, FreeList_t>* tl = find_list(size);508if (tl == NULL) {509return false;510} else {511return tl->verify_chunk_in_free_list(tc);512}513}514515template <class Chunk_t, class FreeList_t>516Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_largest_dict() const {517TreeList<Chunk_t, FreeList_t> *curTL = root();518if (curTL != NULL) {519while(curTL->right() != NULL) curTL = curTL->right();520return curTL->largest_address();521} else {522return NULL;523}524}525526// Remove the current chunk from the tree. If it is not the last527// chunk in a list on a tree node, just unlink it.528// If it is the last chunk in the list (the next link is NULL),529// remove the node and repair the tree.530template <class Chunk_t, class FreeList_t>531TreeChunk<Chunk_t, FreeList_t>*532BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_chunk_from_tree(TreeChunk<Chunk_t, FreeList_t>* tc) {533assert(tc != NULL, "Should not call with a NULL chunk");534assert(tc->is_free(), "Header is not marked correctly");535536TreeList<Chunk_t, FreeList_t> *newTL, *parentTL;537TreeChunk<Chunk_t, FreeList_t>* retTC;538TreeList<Chunk_t, FreeList_t>* tl = tc->list();539debug_only(540bool removing_only_chunk = false;541if (tl == _root) {542if ((_root->left() == NULL) && (_root->right() == NULL)) {543if (_root->count() == 1) {544assert(_root->head() == tc, "Should only be this one chunk");545removing_only_chunk = true;546}547}548}549)550assert(tl != NULL, "List should be set");551assert(tl->parent() == NULL || tl == tl->parent()->left() ||552tl == tl->parent()->right(), "list is inconsistent");553554bool complicated_splice = false;555556retTC = tc;557// Removing this chunk can have the side effect of changing the node558// (TreeList<Chunk_t, FreeList_t>*) in the tree. If the node is the root, update it.559TreeList<Chunk_t, FreeList_t>* replacementTL = tl->remove_chunk_replace_if_needed(tc);560assert(tc->is_free(), "Chunk should still be free");561assert(replacementTL->parent() == NULL ||562replacementTL == replacementTL->parent()->left() ||563replacementTL == replacementTL->parent()->right(),564"list is inconsistent");565if (tl == root()) {566assert(replacementTL->parent() == NULL, "Incorrectly replacing root");567set_root(replacementTL);568}569#ifdef ASSERT570if (tl != replacementTL) {571assert(replacementTL->head() != NULL,572"If the tree list was replaced, it should not be a NULL list");573TreeList<Chunk_t, FreeList_t>* rhl = replacementTL->head_as_TreeChunk()->list();574TreeList<Chunk_t, FreeList_t>* rtl =575TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(replacementTL->tail())->list();576assert(rhl == replacementTL, "Broken head");577assert(rtl == replacementTL, "Broken tail");578assert(replacementTL->size() == tc->size(), "Broken size");579}580#endif581582// Does the tree need to be repaired?583if (replacementTL->count() == 0) {584assert(replacementTL->head() == NULL &&585replacementTL->tail() == NULL, "list count is incorrect");586// Find the replacement node for the (soon to be empty) node being removed.587// if we have a single (or no) child, splice child in our stead588if (replacementTL->left() == NULL) {589// left is NULL so pick right. right may also be NULL.590newTL = replacementTL->right();591debug_only(replacementTL->clear_right();)592} else if (replacementTL->right() == NULL) {593// right is NULL594newTL = replacementTL->left();595debug_only(replacementTL->clear_left();)596} else { // we have both children, so, by patriarchal convention,597// my replacement is least node in right sub-tree598complicated_splice = true;599newTL = remove_tree_minimum(replacementTL->right());600assert(newTL != NULL && newTL->left() == NULL &&601newTL->right() == NULL, "sub-tree minimum exists");602}603// newTL is the replacement for the (soon to be empty) node.604// newTL may be NULL.605// should verify; we just cleanly excised our replacement606if (FLSVerifyDictionary) {607verify_tree();608}609// first make newTL my parent's child610if ((parentTL = replacementTL->parent()) == NULL) {611// newTL should be root612assert(tl == root(), "Incorrectly replacing root");613set_root(newTL);614if (newTL != NULL) {615newTL->clear_parent();616}617} else if (parentTL->right() == replacementTL) {618// replacementTL is a right child619parentTL->set_right(newTL);620} else { // replacementTL is a left child621assert(parentTL->left() == replacementTL, "should be left child");622parentTL->set_left(newTL);623}624debug_only(replacementTL->clear_parent();)625if (complicated_splice) { // we need newTL to get replacementTL's626// two children627assert(newTL != NULL &&628newTL->left() == NULL && newTL->right() == NULL,629"newTL should not have encumbrances from the past");630// we'd like to assert as below:631// assert(replacementTL->left() != NULL && replacementTL->right() != NULL,632// "else !complicated_splice");633// ... however, the above assertion is too strong because we aren't634// guaranteed that replacementTL->right() is still NULL.635// Recall that we removed636// the right sub-tree minimum from replacementTL.637// That may well have been its right638// child! So we'll just assert half of the above:639assert(replacementTL->left() != NULL, "else !complicated_splice");640newTL->set_left(replacementTL->left());641newTL->set_right(replacementTL->right());642debug_only(643replacementTL->clear_right();644replacementTL->clear_left();645)646}647assert(replacementTL->right() == NULL &&648replacementTL->left() == NULL &&649replacementTL->parent() == NULL,650"delete without encumbrances");651}652653assert(total_size() >= retTC->size(), "Incorrect total size");654dec_total_size(retTC->size()); // size book-keeping655assert(total_free_blocks() > 0, "Incorrect total count");656set_total_free_blocks(total_free_blocks() - 1);657658assert(retTC != NULL, "null chunk?");659assert(retTC->prev() == NULL && retTC->next() == NULL,660"should return without encumbrances");661if (FLSVerifyDictionary) {662verify_tree();663}664assert(!removing_only_chunk || _root == NULL, "root should be NULL");665return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(retTC);666}667668// Remove the leftmost node (lm) in the tree and return it.669// If lm has a right child, link it to the left node of670// the parent of lm.671template <class Chunk_t, class FreeList_t>672TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_tree_minimum(TreeList<Chunk_t, FreeList_t>* tl) {673assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree");674// locate the subtree minimum by walking down left branches675TreeList<Chunk_t, FreeList_t>* curTL = tl;676for (; curTL->left() != NULL; curTL = curTL->left());677// obviously curTL now has at most one child, a right child678if (curTL != root()) { // Should this test just be removed?679TreeList<Chunk_t, FreeList_t>* parentTL = curTL->parent();680if (parentTL->left() == curTL) { // curTL is a left child681parentTL->set_left(curTL->right());682} else {683// If the list tl has no left child, then curTL may be684// the right child of parentTL.685assert(parentTL->right() == curTL, "should be a right child");686parentTL->set_right(curTL->right());687}688} else {689// The only use of this method would not pass the root of the690// tree (as indicated by the assertion above that the tree list691// has a parent) but the specification does not explicitly exclude the692// passing of the root so accomodate it.693set_root(NULL);694}695debug_only(696curTL->clear_parent(); // Test if this needs to be cleared697curTL->clear_right(); // recall, above, left child is already null698)699// we just excised a (non-root) node, we should still verify all tree invariants700if (FLSVerifyDictionary) {701verify_tree();702}703return curTL;704}705706template <class Chunk_t, class FreeList_t>707void BinaryTreeDictionary<Chunk_t, FreeList_t>::insert_chunk_in_tree(Chunk_t* fc) {708TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;709size_t size = fc->size();710711assert((size >= min_size()),712err_msg(SIZE_FORMAT " is too small to be a TreeChunk<Chunk_t, FreeList_t> " SIZE_FORMAT,713size, min_size()));714if (FLSVerifyDictionary) {715verify_tree();716}717718fc->clear_next();719fc->link_prev(NULL);720721// work down from the _root, looking for insertion point722for (prevTL = curTL = root(); curTL != NULL;) {723if (curTL->size() == size) // exact match724break;725prevTL = curTL;726if (curTL->size() > size) { // follow left branch727curTL = curTL->left();728} else { // follow right branch729assert(curTL->size() < size, "size inconsistency");730curTL = curTL->right();731}732}733TreeChunk<Chunk_t, FreeList_t>* tc = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);734// This chunk is being returned to the binary tree. Its embedded735// TreeList<Chunk_t, FreeList_t> should be unused at this point.736tc->initialize();737if (curTL != NULL) { // exact match738tc->set_list(curTL);739curTL->return_chunk_at_tail(tc);740} else { // need a new node in tree741tc->clear_next();742tc->link_prev(NULL);743TreeList<Chunk_t, FreeList_t>* newTL = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);744assert(((TreeChunk<Chunk_t, FreeList_t>*)tc)->list() == newTL,745"List was not initialized correctly");746if (prevTL == NULL) { // we are the only tree node747assert(root() == NULL, "control point invariant");748set_root(newTL);749} else { // insert under prevTL ...750if (prevTL->size() < size) { // am right child751assert(prevTL->right() == NULL, "control point invariant");752prevTL->set_right(newTL);753} else { // am left child754assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv");755prevTL->set_left(newTL);756}757}758}759assert(tc->list() != NULL, "Tree list should be set");760761inc_total_size(size);762// Method 'total_size_in_tree' walks through the every block in the763// tree, so it can cause significant performance loss if there are764// many blocks in the tree765assert(!FLSVerifyDictionary || total_size_in_tree(root()) == total_size(), "_total_size inconsistency");766set_total_free_blocks(total_free_blocks() + 1);767if (FLSVerifyDictionary) {768verify_tree();769}770}771772template <class Chunk_t, class FreeList_t>773size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::max_chunk_size() const {774FreeBlockDictionary<Chunk_t>::verify_par_locked();775TreeList<Chunk_t, FreeList_t>* tc = root();776if (tc == NULL) return 0;777for (; tc->right() != NULL; tc = tc->right());778return tc->size();779}780781template <class Chunk_t, class FreeList_t>782size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_list_length(TreeList<Chunk_t, FreeList_t>* tl) const {783size_t res;784res = tl->count();785#ifdef ASSERT786size_t cnt;787Chunk_t* tc = tl->head();788for (cnt = 0; tc != NULL; tc = tc->next(), cnt++);789assert(res == cnt, "The count is not being maintained correctly");790#endif791return res;792}793794template <class Chunk_t, class FreeList_t>795size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_size_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {796if (tl == NULL)797return 0;798return (tl->size() * total_list_length(tl)) +799total_size_in_tree(tl->left()) +800total_size_in_tree(tl->right());801}802803template <class Chunk_t, class FreeList_t>804double BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_of_squared_block_sizes(TreeList<Chunk_t, FreeList_t>* const tl) const {805if (tl == NULL) {806return 0.0;807}808double size = (double)(tl->size());809double curr = size * size * total_list_length(tl);810curr += sum_of_squared_block_sizes(tl->left());811curr += sum_of_squared_block_sizes(tl->right());812return curr;813}814815template <class Chunk_t, class FreeList_t>816size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_free_blocks_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {817if (tl == NULL)818return 0;819return total_list_length(tl) +820total_free_blocks_in_tree(tl->left()) +821total_free_blocks_in_tree(tl->right());822}823824template <class Chunk_t, class FreeList_t>825size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::num_free_blocks() const {826assert(total_free_blocks_in_tree(root()) == total_free_blocks(),827"_total_free_blocks inconsistency");828return total_free_blocks();829}830831template <class Chunk_t, class FreeList_t>832size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height_helper(TreeList<Chunk_t, FreeList_t>* tl) const {833if (tl == NULL)834return 0;835return 1 + MAX2(tree_height_helper(tl->left()),836tree_height_helper(tl->right()));837}838839template <class Chunk_t, class FreeList_t>840size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height() const {841return tree_height_helper(root());842}843844template <class Chunk_t, class FreeList_t>845size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_helper(TreeList<Chunk_t, FreeList_t>* tl) const {846if (tl == NULL) {847return 0;848}849return 1 + total_nodes_helper(tl->left()) +850total_nodes_helper(tl->right());851}852853template <class Chunk_t, class FreeList_t>854size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {855return total_nodes_helper(root());856}857858template <class Chunk_t, class FreeList_t>859void BinaryTreeDictionary<Chunk_t, FreeList_t>::dict_census_update(size_t size, bool split, bool birth){}860861#if INCLUDE_ALL_GCS862template <>863void AFLBinaryTreeDictionary::dict_census_update(size_t size, bool split, bool birth) {864TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >* nd = find_list(size);865if (nd) {866if (split) {867if (birth) {868nd->increment_split_births();869nd->increment_surplus();870} else {871nd->increment_split_deaths();872nd->decrement_surplus();873}874} else {875if (birth) {876nd->increment_coal_births();877nd->increment_surplus();878} else {879nd->increment_coal_deaths();880nd->decrement_surplus();881}882}883}884// A list for this size may not be found (nd == 0) if885// This is a death where the appropriate list is now886// empty and has been removed from the list.887// This is a birth associated with a LinAB. The chunk888// for the LinAB is not in the dictionary.889}890#endif // INCLUDE_ALL_GCS891892template <class Chunk_t, class FreeList_t>893bool BinaryTreeDictionary<Chunk_t, FreeList_t>::coal_dict_over_populated(size_t size) {894// For the general type of freelists, encourage coalescing by895// returning true.896return true;897}898899#if INCLUDE_ALL_GCS900template <>901bool AFLBinaryTreeDictionary::coal_dict_over_populated(size_t size) {902if (FLSAlwaysCoalesceLarge) return true;903904TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >* list_of_size = find_list(size);905// None of requested size implies overpopulated.906return list_of_size == NULL || list_of_size->coal_desired() <= 0 ||907list_of_size->count() > list_of_size->coal_desired();908}909#endif // INCLUDE_ALL_GCS910911// Closures for walking the binary tree.912// do_list() walks the free list in a node applying the closure913// to each free chunk in the list914// do_tree() walks the nodes in the binary tree applying do_list()915// to each list at each node.916917template <class Chunk_t, class FreeList_t>918class TreeCensusClosure : public StackObj {919protected:920virtual void do_list(FreeList_t* fl) = 0;921public:922virtual void do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;923};924925template <class Chunk_t, class FreeList_t>926class AscendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {927public:928void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {929if (tl != NULL) {930do_tree(tl->left());931this->do_list(tl);932do_tree(tl->right());933}934}935};936937template <class Chunk_t, class FreeList_t>938class DescendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {939public:940void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {941if (tl != NULL) {942do_tree(tl->right());943this->do_list(tl);944do_tree(tl->left());945}946}947};948949// For each list in the tree, calculate the desired, desired950// coalesce, count before sweep, and surplus before sweep.951template <class Chunk_t, class FreeList_t>952class BeginSweepClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {953double _percentage;954float _inter_sweep_current;955float _inter_sweep_estimate;956float _intra_sweep_estimate;957958public:959BeginSweepClosure(double p, float inter_sweep_current,960float inter_sweep_estimate,961float intra_sweep_estimate) :962_percentage(p),963_inter_sweep_current(inter_sweep_current),964_inter_sweep_estimate(inter_sweep_estimate),965_intra_sweep_estimate(intra_sweep_estimate) { }966967void do_list(FreeList<Chunk_t>* fl) {}968969#if INCLUDE_ALL_GCS970void do_list(AdaptiveFreeList<Chunk_t>* fl) {971double coalSurplusPercent = _percentage;972fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate, _intra_sweep_estimate);973fl->set_coal_desired((ssize_t)((double)fl->desired() * coalSurplusPercent));974fl->set_before_sweep(fl->count());975fl->set_bfr_surp(fl->surplus());976}977#endif // INCLUDE_ALL_GCS978};979980// Used to search the tree until a condition is met.981// Similar to TreeCensusClosure but searches the982// tree and returns promptly when found.983984template <class Chunk_t, class FreeList_t>985class TreeSearchClosure : public StackObj {986protected:987virtual bool do_list(FreeList_t* fl) = 0;988public:989virtual bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;990};991992#if 0 // Don't need this yet but here for symmetry.993template <class Chunk_t, class FreeList_t>994class AscendTreeSearchClosure : public TreeSearchClosure<Chunk_t> {995public:996bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {997if (tl != NULL) {998if (do_tree(tl->left())) return true;999if (do_list(tl)) return true;1000if (do_tree(tl->right())) return true;1001}1002return false;1003}1004};1005#endif10061007template <class Chunk_t, class FreeList_t>1008class DescendTreeSearchClosure : public TreeSearchClosure<Chunk_t, FreeList_t> {1009public:1010bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {1011if (tl != NULL) {1012if (do_tree(tl->right())) return true;1013if (this->do_list(tl)) return true;1014if (do_tree(tl->left())) return true;1015}1016return false;1017}1018};10191020// Searches the tree for a chunk that ends at the1021// specified address.1022template <class Chunk_t, class FreeList_t>1023class EndTreeSearchClosure : public DescendTreeSearchClosure<Chunk_t, FreeList_t> {1024HeapWord* _target;1025Chunk_t* _found;10261027public:1028EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {}1029bool do_list(FreeList_t* fl) {1030Chunk_t* item = fl->head();1031while (item != NULL) {1032if (item->end() == (uintptr_t*) _target) {1033_found = item;1034return true;1035}1036item = item->next();1037}1038return false;1039}1040Chunk_t* found() { return _found; }1041};10421043template <class Chunk_t, class FreeList_t>1044Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_chunk_ends_at(HeapWord* target) const {1045EndTreeSearchClosure<Chunk_t, FreeList_t> etsc(target);1046bool found_target = etsc.do_tree(root());1047assert(found_target || etsc.found() == NULL, "Consistency check");1048assert(!found_target || etsc.found() != NULL, "Consistency check");1049return etsc.found();1050}10511052template <class Chunk_t, class FreeList_t>1053void BinaryTreeDictionary<Chunk_t, FreeList_t>::begin_sweep_dict_census(double coalSurplusPercent,1054float inter_sweep_current, float inter_sweep_estimate, float intra_sweep_estimate) {1055BeginSweepClosure<Chunk_t, FreeList_t> bsc(coalSurplusPercent, inter_sweep_current,1056inter_sweep_estimate,1057intra_sweep_estimate);1058bsc.do_tree(root());1059}10601061// Closures and methods for calculating total bytes returned to the1062// free lists in the tree.1063#ifndef PRODUCT1064template <class Chunk_t, class FreeList_t>1065class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {1066public:1067void do_list(FreeList_t* fl) {1068fl->set_returned_bytes(0);1069}1070};10711072template <class Chunk_t, class FreeList_t>1073void BinaryTreeDictionary<Chunk_t, FreeList_t>::initialize_dict_returned_bytes() {1074InitializeDictReturnedBytesClosure<Chunk_t, FreeList_t> idrb;1075idrb.do_tree(root());1076}10771078template <class Chunk_t, class FreeList_t>1079class ReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {1080size_t _dict_returned_bytes;1081public:1082ReturnedBytesClosure() { _dict_returned_bytes = 0; }1083void do_list(FreeList_t* fl) {1084_dict_returned_bytes += fl->returned_bytes();1085}1086size_t dict_returned_bytes() { return _dict_returned_bytes; }1087};10881089template <class Chunk_t, class FreeList_t>1090size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_dict_returned_bytes() {1091ReturnedBytesClosure<Chunk_t, FreeList_t> rbc;1092rbc.do_tree(root());10931094return rbc.dict_returned_bytes();1095}10961097// Count the number of entries in the tree.1098template <class Chunk_t, class FreeList_t>1099class treeCountClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {1100public:1101uint count;1102treeCountClosure(uint c) { count = c; }1103void do_list(FreeList_t* fl) {1104count++;1105}1106};11071108template <class Chunk_t, class FreeList_t>1109size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_count() {1110treeCountClosure<Chunk_t, FreeList_t> ctc(0);1111ctc.do_tree(root());1112return ctc.count;1113}1114#endif // PRODUCT11151116// Calculate surpluses for the lists in the tree.1117template <class Chunk_t, class FreeList_t>1118class setTreeSurplusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {1119double percentage;1120public:1121setTreeSurplusClosure(double v) { percentage = v; }1122void do_list(FreeList<Chunk_t>* fl) {}11231124#if INCLUDE_ALL_GCS1125void do_list(AdaptiveFreeList<Chunk_t>* fl) {1126double splitSurplusPercent = percentage;1127fl->set_surplus(fl->count() -1128(ssize_t)((double)fl->desired() * splitSurplusPercent));1129}1130#endif // INCLUDE_ALL_GCS1131};11321133template <class Chunk_t, class FreeList_t>1134void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_surplus(double splitSurplusPercent) {1135setTreeSurplusClosure<Chunk_t, FreeList_t> sts(splitSurplusPercent);1136sts.do_tree(root());1137}11381139// Set hints for the lists in the tree.1140template <class Chunk_t, class FreeList_t>1141class setTreeHintsClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {1142size_t hint;1143public:1144setTreeHintsClosure(size_t v) { hint = v; }1145void do_list(FreeList<Chunk_t>* fl) {}11461147#if INCLUDE_ALL_GCS1148void do_list(AdaptiveFreeList<Chunk_t>* fl) {1149fl->set_hint(hint);1150assert(fl->hint() == 0 || fl->hint() > fl->size(),1151"Current hint is inconsistent");1152if (fl->surplus() > 0) {1153hint = fl->size();1154}1155}1156#endif // INCLUDE_ALL_GCS1157};11581159template <class Chunk_t, class FreeList_t>1160void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_hints(void) {1161setTreeHintsClosure<Chunk_t, FreeList_t> sth(0);1162sth.do_tree(root());1163}11641165// Save count before previous sweep and splits and coalesces.1166template <class Chunk_t, class FreeList_t>1167class clearTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {1168void do_list(FreeList<Chunk_t>* fl) {}11691170#if INCLUDE_ALL_GCS1171void do_list(AdaptiveFreeList<Chunk_t>* fl) {1172fl->set_prev_sweep(fl->count());1173fl->set_coal_births(0);1174fl->set_coal_deaths(0);1175fl->set_split_births(0);1176fl->set_split_deaths(0);1177}1178#endif // INCLUDE_ALL_GCS1179};11801181template <class Chunk_t, class FreeList_t>1182void BinaryTreeDictionary<Chunk_t, FreeList_t>::clear_tree_census(void) {1183clearTreeCensusClosure<Chunk_t, FreeList_t> ctc;1184ctc.do_tree(root());1185}11861187// Do reporting and post sweep clean up.1188template <class Chunk_t, class FreeList_t>1189void BinaryTreeDictionary<Chunk_t, FreeList_t>::end_sweep_dict_census(double splitSurplusPercent) {1190// Does walking the tree 3 times hurt?1191set_tree_surplus(splitSurplusPercent);1192set_tree_hints();1193if (PrintGC && Verbose) {1194report_statistics();1195}1196clear_tree_census();1197}11981199// Print summary statistics1200template <class Chunk_t, class FreeList_t>1201void BinaryTreeDictionary<Chunk_t, FreeList_t>::report_statistics() const {1202FreeBlockDictionary<Chunk_t>::verify_par_locked();1203gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n"1204"------------------------------------\n");1205size_t total_size = total_chunk_size(debug_only(NULL));1206size_t free_blocks = num_free_blocks();1207gclog_or_tty->print("Total Free Space: " SIZE_FORMAT "\n", total_size);1208gclog_or_tty->print("Max Chunk Size: " SIZE_FORMAT "\n", max_chunk_size());1209gclog_or_tty->print("Number of Blocks: " SIZE_FORMAT "\n", free_blocks);1210if (free_blocks > 0) {1211gclog_or_tty->print("Av. Block Size: " SIZE_FORMAT "\n", total_size/free_blocks);1212}1213gclog_or_tty->print("Tree Height: " SIZE_FORMAT "\n", tree_height());1214}12151216// Print census information - counts, births, deaths, etc.1217// for each list in the tree. Also print some summary1218// information.1219template <class Chunk_t, class FreeList_t>1220class PrintTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {1221int _print_line;1222size_t _total_free;1223FreeList_t _total;12241225public:1226PrintTreeCensusClosure() {1227_print_line = 0;1228_total_free = 0;1229}1230FreeList_t* total() { return &_total; }1231size_t total_free() { return _total_free; }1232void do_list(FreeList<Chunk_t>* fl) {1233if (++_print_line >= 40) {1234FreeList_t::print_labels_on(gclog_or_tty, "size");1235_print_line = 0;1236}1237fl->print_on(gclog_or_tty);1238_total_free += fl->count() * fl->size() ;1239total()->set_count( total()->count() + fl->count() );1240}12411242#if INCLUDE_ALL_GCS1243void do_list(AdaptiveFreeList<Chunk_t>* fl) {1244if (++_print_line >= 40) {1245FreeList_t::print_labels_on(gclog_or_tty, "size");1246_print_line = 0;1247}1248fl->print_on(gclog_or_tty);1249_total_free += fl->count() * fl->size() ;1250total()->set_count( total()->count() + fl->count() );1251total()->set_bfr_surp( total()->bfr_surp() + fl->bfr_surp() );1252total()->set_surplus( total()->split_deaths() + fl->surplus() );1253total()->set_desired( total()->desired() + fl->desired() );1254total()->set_prev_sweep( total()->prev_sweep() + fl->prev_sweep() );1255total()->set_before_sweep(total()->before_sweep() + fl->before_sweep());1256total()->set_coal_births( total()->coal_births() + fl->coal_births() );1257total()->set_coal_deaths( total()->coal_deaths() + fl->coal_deaths() );1258total()->set_split_births(total()->split_births() + fl->split_births());1259total()->set_split_deaths(total()->split_deaths() + fl->split_deaths());1260}1261#endif // INCLUDE_ALL_GCS1262};12631264template <class Chunk_t, class FreeList_t>1265void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_dict_census(void) const {12661267gclog_or_tty->print("\nBinaryTree\n");1268FreeList_t::print_labels_on(gclog_or_tty, "size");1269PrintTreeCensusClosure<Chunk_t, FreeList_t> ptc;1270ptc.do_tree(root());12711272FreeList_t* total = ptc.total();1273FreeList_t::print_labels_on(gclog_or_tty, " ");1274}12751276#if INCLUDE_ALL_GCS1277template <>1278void AFLBinaryTreeDictionary::print_dict_census(void) const {12791280gclog_or_tty->print("\nBinaryTree\n");1281AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, "size");1282PrintTreeCensusClosure<FreeChunk, AdaptiveFreeList<FreeChunk> > ptc;1283ptc.do_tree(root());12841285AdaptiveFreeList<FreeChunk>* total = ptc.total();1286AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, " ");1287total->print_on(gclog_or_tty, "TOTAL\t");1288gclog_or_tty->print(1289"total_free(words): " SIZE_FORMAT_W(16)1290" growth: %8.5f deficit: %8.5f\n",1291ptc.total_free(),1292(double)(total->split_births() + total->coal_births()1293- total->split_deaths() - total->coal_deaths())1294/(total->prev_sweep() != 0 ? (double)total->prev_sweep() : 1.0),1295(double)(total->desired() - total->count())1296/(total->desired() != 0 ? (double)total->desired() : 1.0));1297}1298#endif // INCLUDE_ALL_GCS12991300template <class Chunk_t, class FreeList_t>1301class PrintFreeListsClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {1302outputStream* _st;1303int _print_line;13041305public:1306PrintFreeListsClosure(outputStream* st) {1307_st = st;1308_print_line = 0;1309}1310void do_list(FreeList_t* fl) {1311if (++_print_line >= 40) {1312FreeList_t::print_labels_on(_st, "size");1313_print_line = 0;1314}1315fl->print_on(gclog_or_tty);1316size_t sz = fl->size();1317for (Chunk_t* fc = fl->head(); fc != NULL;1318fc = fc->next()) {1319_st->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ") %s",1320p2i(fc), p2i((HeapWord*)fc + sz),1321fc->cantCoalesce() ? "\t CC" : "");1322}1323}1324};13251326template <class Chunk_t, class FreeList_t>1327void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_free_lists(outputStream* st) const {13281329FreeList_t::print_labels_on(st, "size");1330PrintFreeListsClosure<Chunk_t, FreeList_t> pflc(st);1331pflc.do_tree(root());1332}13331334// Verify the following tree invariants:1335// . _root has no parent1336// . parent and child point to each other1337// . each node's key correctly related to that of its child(ren)1338template <class Chunk_t, class FreeList_t>1339void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree() const {1340guarantee(root() == NULL || total_free_blocks() == 0 ||1341total_size() != 0, "_total_size should't be 0?");1342guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent");1343verify_tree_helper(root());1344}13451346template <class Chunk_t, class FreeList_t>1347size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_prev_free_ptrs(TreeList<Chunk_t, FreeList_t>* tl) {1348size_t ct = 0;1349for (Chunk_t* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) {1350ct++;1351assert(curFC->prev() == NULL || curFC->prev()->is_free(),1352"Chunk should be free");1353}1354return ct;1355}13561357// Note: this helper is recursive rather than iterative, so use with1358// caution on very deep trees; and watch out for stack overflow errors;1359// In general, to be used only for debugging.1360template <class Chunk_t, class FreeList_t>1361void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree_helper(TreeList<Chunk_t, FreeList_t>* tl) const {1362if (tl == NULL)1363return;1364guarantee(tl->size() != 0, "A list must has a size");1365guarantee(tl->left() == NULL || tl->left()->parent() == tl,1366"parent<-/->left");1367guarantee(tl->right() == NULL || tl->right()->parent() == tl,1368"parent<-/->right");;1369guarantee(tl->left() == NULL || tl->left()->size() < tl->size(),1370"parent !> left");1371guarantee(tl->right() == NULL || tl->right()->size() > tl->size(),1372"parent !< left");1373guarantee(tl->head() == NULL || tl->head()->is_free(), "!Free");1374guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl,1375"list inconsistency");1376guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL),1377"list count is inconsistent");1378guarantee(tl->count() > 1 || tl->head() == tl->tail(),1379"list is incorrectly constructed");1380size_t count = verify_prev_free_ptrs(tl);1381guarantee(count == (size_t)tl->count(), "Node count is incorrect");1382if (tl->head() != NULL) {1383tl->head_as_TreeChunk()->verify_tree_chunk_list();1384}1385verify_tree_helper(tl->left());1386verify_tree_helper(tl->right());1387}13881389template <class Chunk_t, class FreeList_t>1390void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify() const {1391verify_tree();1392guarantee(total_size() == total_size_in_tree(root()), "Total Size inconsistency");1393}13941395template class TreeList<Metablock, FreeList<Metablock> >;1396template class BinaryTreeDictionary<Metablock, FreeList<Metablock> >;1397template class TreeChunk<Metablock, FreeList<Metablock> >;13981399template class TreeList<Metachunk, FreeList<Metachunk> >;1400template class BinaryTreeDictionary<Metachunk, FreeList<Metachunk> >;1401template class TreeChunk<Metachunk, FreeList<Metachunk> >;140214031404#if INCLUDE_ALL_GCS1405// Explicitly instantiate these types for FreeChunk.1406template class TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >;1407template class BinaryTreeDictionary<FreeChunk, AdaptiveFreeList<FreeChunk> >;1408template class TreeChunk<FreeChunk, AdaptiveFreeList<FreeChunk> >;14091410#endif // INCLUDE_ALL_GCS141114121413