0N/A/*
3695N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "gc_implementation/shared/allocationStats.hpp"
3695N/A#include "memory/binaryTreeDictionary.hpp"
1879N/A#include "runtime/globals.hpp"
1879N/A#include "utilities/ostream.hpp"
3695N/A#ifndef SERIALGC
3695N/A#include "gc_implementation/shared/spaceDecorator.hpp"
3695N/A#include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
3695N/A#endif // SERIALGC
0N/A
0N/A////////////////////////////////////////////////////////////////////////////////
0N/A// A binary tree based search structure for free blocks.
0N/A// This is currently used in the Concurrent Mark&Sweep implementation.
0N/A////////////////////////////////////////////////////////////////////////////////
0N/A
3695N/Atemplate <class Chunk>
3695N/ATreeChunk<Chunk>* TreeChunk<Chunk>::as_TreeChunk(Chunk* fc) {
0N/A // Do some assertion checking here.
3695N/A return (TreeChunk<Chunk>*) fc;
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid TreeChunk<Chunk>::verify_tree_chunk_list() const {
3695N/A TreeChunk<Chunk>* nextTC = (TreeChunk<Chunk>*)next();
0N/A if (prev() != NULL) { // interior list node shouldn'r have tree fields
0N/A guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL &&
0N/A embedded_list()->right() == NULL, "should be clear");
0N/A }
0N/A if (nextTC != NULL) {
0N/A guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain");
0N/A guarantee(nextTC->size() == size(), "wrong size");
3697N/A nextTC->verify_tree_chunk_list();
0N/A }
0N/A}
0N/A
0N/A
3695N/Atemplate <class Chunk>
3695N/ATreeList<Chunk>* TreeList<Chunk>::as_TreeList(TreeChunk<Chunk>* tc) {
0N/A // This first free chunk in the list will be the tree list.
3695N/A assert(tc->size() >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "Chunk is too small for a TreeChunk");
3695N/A TreeList<Chunk>* tl = tc->embedded_list();
0N/A tc->set_list(tl);
0N/A#ifdef ASSERT
0N/A tl->set_protecting_lock(NULL);
0N/A#endif
0N/A tl->set_hint(0);
0N/A tl->set_size(tc->size());
0N/A tl->link_head(tc);
0N/A tl->link_tail(tc);
0N/A tl->set_count(1);
1145N/A tl->init_statistics(true /* split_birth */);
3697N/A tl->set_parent(NULL);
3697N/A tl->set_left(NULL);
3697N/A tl->set_right(NULL);
0N/A return tl;
0N/A}
1145N/A
3695N/Atemplate <class Chunk>
3695N/ATreeList<Chunk>* TreeList<Chunk>::as_TreeList(HeapWord* addr, size_t size) {
3695N/A TreeChunk<Chunk>* tc = (TreeChunk<Chunk>*) addr;
3695N/A assert(size >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "Chunk is too small for a TreeChunk");
263N/A // The space in the heap will have been mangled initially but
263N/A // is not remangled when a free chunk is returned to the free list
263N/A // (since it is used to maintain the chunk on the free list).
263N/A assert((ZapUnusedHeapArea &&
263N/A SpaceMangler::is_mangled((HeapWord*) tc->size_addr()) &&
263N/A SpaceMangler::is_mangled((HeapWord*) tc->prev_addr()) &&
263N/A SpaceMangler::is_mangled((HeapWord*) tc->next_addr())) ||
263N/A (tc->size() == 0 && tc->prev() == NULL && tc->next() == NULL),
263N/A "Space should be clear or mangled");
3697N/A tc->set_size(size);
3697N/A tc->link_prev(NULL);
3697N/A tc->link_next(NULL);
3695N/A TreeList<Chunk>* tl = TreeList<Chunk>::as_TreeList(tc);
0N/A return tl;
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/ATreeList<Chunk>* TreeList<Chunk>::remove_chunk_replace_if_needed(TreeChunk<Chunk>* tc) {
0N/A
3695N/A TreeList<Chunk>* retTL = this;
3695N/A Chunk* list = head();
0N/A assert(!list || list != list->next(), "Chunk on list twice");
0N/A assert(tc != NULL, "Chunk being removed is NULL");
0N/A assert(parent() == NULL || this == parent()->left() ||
0N/A this == parent()->right(), "list is inconsistent");
3697N/A assert(tc->is_free(), "Header is not marked correctly");
0N/A assert(head() == NULL || head()->prev() == NULL, "list invariant");
0N/A assert(tail() == NULL || tail()->next() == NULL, "list invariant");
0N/A
3695N/A Chunk* prevFC = tc->prev();
3695N/A TreeChunk<Chunk>* nextTC = TreeChunk<Chunk>::as_TreeChunk(tc->next());
0N/A assert(list != NULL, "should have at least the target chunk");
0N/A
0N/A // Is this the first item on the list?
0N/A if (tc == list) {
3695N/A // The "getChunk..." functions for a TreeList<Chunk> will not return the
0N/A // first chunk in the list unless it is the last chunk in the list
0N/A // because the first chunk is also acting as the tree node.
0N/A // When coalescing happens, however, the first chunk in the a tree
0N/A // list can be the start of a free range. Free ranges are removed
0N/A // from the free lists so that they are not available to be
0N/A // allocated when the sweeper yields (giving up the free list lock)
0N/A // to allow mutator activity. If this chunk is the first in the
0N/A // list and is not the last in the list, do the work to copy the
3695N/A // TreeList<Chunk> from the first chunk to the next chunk and update all
3695N/A // the TreeList<Chunk> pointers in the chunks in the list.
0N/A if (nextTC == NULL) {
1409N/A assert(prevFC == NULL, "Not last chunk in the list");
0N/A set_tail(NULL);
0N/A set_head(NULL);
0N/A } else {
0N/A // copy embedded list.
0N/A nextTC->set_embedded_list(tc->embedded_list());
0N/A retTL = nextTC->embedded_list();
0N/A // Fix the pointer to the list in each chunk in the list.
0N/A // This can be slow for a long list. Consider having
0N/A // an option that does not allow the first chunk on the
0N/A // list to be coalesced.
3695N/A for (TreeChunk<Chunk>* curTC = nextTC; curTC != NULL;
3695N/A curTC = TreeChunk<Chunk>::as_TreeChunk(curTC->next())) {
0N/A curTC->set_list(retTL);
0N/A }
3695N/A // Fix the parent to point to the new TreeList<Chunk>.
0N/A if (retTL->parent() != NULL) {
0N/A if (this == retTL->parent()->left()) {
3697N/A retTL->parent()->set_left(retTL);
0N/A } else {
0N/A assert(this == retTL->parent()->right(), "Parent is incorrect");
3697N/A retTL->parent()->set_right(retTL);
0N/A }
0N/A }
0N/A // Fix the children's parent pointers to point to the
0N/A // new list.
0N/A assert(right() == retTL->right(), "Should have been copied");
0N/A if (retTL->right() != NULL) {
3697N/A retTL->right()->set_parent(retTL);
0N/A }
0N/A assert(left() == retTL->left(), "Should have been copied");
0N/A if (retTL->left() != NULL) {
3697N/A retTL->left()->set_parent(retTL);
0N/A }
0N/A retTL->link_head(nextTC);
3697N/A assert(nextTC->is_free(), "Should be a free chunk");
0N/A }
0N/A } else {
0N/A if (nextTC == NULL) {
0N/A // Removing chunk at tail of list
0N/A link_tail(prevFC);
0N/A }
0N/A // Chunk is interior to the list
3697N/A prevFC->link_after(nextTC);
0N/A }
0N/A
3695N/A // Below this point the embeded TreeList<Chunk> being used for the
0N/A // tree node may have changed. Don't use "this"
3695N/A // TreeList<Chunk>*.
0N/A // chunk should still be a free chunk (bit set in _prev)
0N/A assert(!retTL->head() || retTL->size() == retTL->head()->size(),
0N/A "Wrong sized chunk in list");
0N/A debug_only(
3697N/A tc->link_prev(NULL);
3697N/A tc->link_next(NULL);
0N/A tc->set_list(NULL);
0N/A bool prev_found = false;
0N/A bool next_found = false;
3695N/A for (Chunk* curFC = retTL->head();
0N/A curFC != NULL; curFC = curFC->next()) {
0N/A assert(curFC != tc, "Chunk is still in list");
0N/A if (curFC == prevFC) {
0N/A prev_found = true;
0N/A }
0N/A if (curFC == nextTC) {
0N/A next_found = true;
0N/A }
0N/A }
0N/A assert(prevFC == NULL || prev_found, "Chunk was lost from list");
0N/A assert(nextTC == NULL || next_found, "Chunk was lost from list");
0N/A assert(retTL->parent() == NULL ||
0N/A retTL == retTL->parent()->left() ||
0N/A retTL == retTL->parent()->right(),
0N/A "list is inconsistent");
0N/A )
0N/A retTL->decrement_count();
0N/A
3697N/A assert(tc->is_free(), "Should still be a free chunk");
0N/A assert(retTL->head() == NULL || retTL->head()->prev() == NULL,
0N/A "list invariant");
0N/A assert(retTL->tail() == NULL || retTL->tail()->next() == NULL,
0N/A "list invariant");
0N/A return retTL;
0N/A}
3695N/A
3695N/Atemplate <class Chunk>
3697N/Avoid TreeList<Chunk>::return_chunk_at_tail(TreeChunk<Chunk>* chunk) {
0N/A assert(chunk != NULL, "returning NULL chunk");
0N/A assert(chunk->list() == this, "list should be set for chunk");
0N/A assert(tail() != NULL, "The tree list is embedded in the first chunk");
0N/A // which means that the list can never be empty.
3697N/A assert(!verify_chunk_in_free_list(chunk), "Double entry");
0N/A assert(head() == NULL || head()->prev() == NULL, "list invariant");
0N/A assert(tail() == NULL || tail()->next() == NULL, "list invariant");
0N/A
3695N/A Chunk* fc = tail();
3697N/A fc->link_after(chunk);
0N/A link_tail(chunk);
0N/A
0N/A assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list");
3786N/A increment_count();
3697N/A debug_only(increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
0N/A assert(head() == NULL || head()->prev() == NULL, "list invariant");
0N/A assert(tail() == NULL || tail()->next() == NULL, "list invariant");
0N/A}
0N/A
0N/A// Add this chunk at the head of the list. "At the head of the list"
0N/A// is defined to be after the chunk pointer to by head(). This is
3695N/A// because the TreeList<Chunk> is embedded in the first TreeChunk<Chunk> in the
3695N/A// list. See the definition of TreeChunk<Chunk>.
3695N/Atemplate <class Chunk>
3697N/Avoid TreeList<Chunk>::return_chunk_at_head(TreeChunk<Chunk>* chunk) {
0N/A assert(chunk->list() == this, "list should be set for chunk");
0N/A assert(head() != NULL, "The tree list is embedded in the first chunk");
0N/A assert(chunk != NULL, "returning NULL chunk");
3697N/A assert(!verify_chunk_in_free_list(chunk), "Double entry");
0N/A assert(head() == NULL || head()->prev() == NULL, "list invariant");
0N/A assert(tail() == NULL || tail()->next() == NULL, "list invariant");
0N/A
3695N/A Chunk* fc = head()->next();
0N/A if (fc != NULL) {
3697N/A chunk->link_after(fc);
0N/A } else {
0N/A assert(tail() == NULL, "List is inconsistent");
0N/A link_tail(chunk);
0N/A }
3697N/A head()->link_after(chunk);
0N/A assert(!head() || size() == head()->size(), "Wrong sized chunk in list");
3786N/A increment_count();
3697N/A debug_only(increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
0N/A assert(head() == NULL || head()->prev() == NULL, "list invariant");
0N/A assert(tail() == NULL || tail()->next() == NULL, "list invariant");
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/ATreeChunk<Chunk>* TreeList<Chunk>::head_as_TreeChunk() {
3695N/A assert(head() == NULL || TreeChunk<Chunk>::as_TreeChunk(head())->list() == this,
0N/A "Wrong type of chunk?");
3695N/A return TreeChunk<Chunk>::as_TreeChunk(head());
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/ATreeChunk<Chunk>* TreeList<Chunk>::first_available() {
1697N/A assert(head() != NULL, "The head of the list cannot be NULL");
3695N/A Chunk* fc = head()->next();
3695N/A TreeChunk<Chunk>* retTC;
0N/A if (fc == NULL) {
0N/A retTC = head_as_TreeChunk();
0N/A } else {
3695N/A retTC = TreeChunk<Chunk>::as_TreeChunk(fc);
0N/A }
0N/A assert(retTC->list() == this, "Wrong type of chunk.");
0N/A return retTC;
0N/A}
0N/A
1145N/A// Returns the block with the largest heap address amongst
1145N/A// those in the list for this size; potentially slow and expensive,
1145N/A// use with caution!
3695N/Atemplate <class Chunk>
3695N/ATreeChunk<Chunk>* TreeList<Chunk>::largest_address() {
1697N/A assert(head() != NULL, "The head of the list cannot be NULL");
3695N/A Chunk* fc = head()->next();
3695N/A TreeChunk<Chunk>* retTC;
1145N/A if (fc == NULL) {
1145N/A retTC = head_as_TreeChunk();
1145N/A } else {
1145N/A // walk down the list and return the one with the highest
1145N/A // heap address among chunks of this size.
3695N/A Chunk* last = fc;
1145N/A while (fc->next() != NULL) {
1145N/A if ((HeapWord*)last < (HeapWord*)fc) {
1145N/A last = fc;
1145N/A }
1145N/A fc = fc->next();
1145N/A }
3695N/A retTC = TreeChunk<Chunk>::as_TreeChunk(last);
1145N/A }
1145N/A assert(retTC->list() == this, "Wrong type of chunk.");
1145N/A return retTC;
1145N/A}
1145N/A
3695N/Atemplate <class Chunk>
3695N/ABinaryTreeDictionary<Chunk>::BinaryTreeDictionary(bool adaptive_freelists, bool splay) :
3695N/A _splay(splay), _adaptive_freelists(adaptive_freelists),
3697N/A _total_size(0), _total_free_blocks(0), _root(0) {}
3695N/A
3695N/Atemplate <class Chunk>
3695N/ABinaryTreeDictionary<Chunk>::BinaryTreeDictionary(MemRegion mr,
3695N/A bool adaptive_freelists,
3695N/A bool splay):
3695N/A _adaptive_freelists(adaptive_freelists), _splay(splay)
0N/A{
3695N/A assert(mr.word_size() >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "minimum chunk size");
0N/A
0N/A reset(mr);
0N/A assert(root()->left() == NULL, "reset check failed");
0N/A assert(root()->right() == NULL, "reset check failed");
0N/A assert(root()->head()->next() == NULL, "reset check failed");
0N/A assert(root()->head()->prev() == NULL, "reset check failed");
3697N/A assert(total_size() == root()->size(), "reset check failed");
3697N/A assert(total_free_blocks() == 1, "reset check failed");
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::inc_total_size(size_t inc) {
3697N/A _total_size = _total_size + inc;
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::dec_total_size(size_t dec) {
3697N/A _total_size = _total_size - dec;
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/Avoid BinaryTreeDictionary<Chunk>::reset(MemRegion mr) {
3695N/A assert(mr.word_size() >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "minimum chunk size");
3695N/A set_root(TreeList<Chunk>::as_TreeList(mr.start(), mr.word_size()));
3697N/A set_total_size(mr.word_size());
3697N/A set_total_free_blocks(1);
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/Avoid BinaryTreeDictionary<Chunk>::reset(HeapWord* addr, size_t byte_size) {
0N/A MemRegion mr(addr, heap_word_size(byte_size));
0N/A reset(mr);
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/Avoid BinaryTreeDictionary<Chunk>::reset() {
0N/A set_root(NULL);
3697N/A set_total_size(0);
3697N/A set_total_free_blocks(0);
0N/A}
0N/A
0N/A// Get a free block of size at least size from tree, or NULL.
0N/A// If a splay step is requested, the removal algorithm (only) incorporates
0N/A// a splay step as follows:
0N/A// . the search proceeds down the tree looking for a possible
0N/A// match. At the (closest) matching location, an appropriate splay step is applied
0N/A// (zig, zig-zig or zig-zag). A chunk of the appropriate size is then returned
0N/A// if available, and if it's the last chunk, the node is deleted. A deteleted
0N/A// node is replaced in place by its tree successor.
3695N/Atemplate <class Chunk>
3695N/ATreeChunk<Chunk>*
3697N/ABinaryTreeDictionary<Chunk>::get_chunk_from_tree(size_t size, enum FreeBlockDictionary<Chunk>::Dither dither, bool splay)
0N/A{
3695N/A TreeList<Chunk> *curTL, *prevTL;
3695N/A TreeChunk<Chunk>* retTC = NULL;
3695N/A assert(size >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "minimum chunk size");
0N/A if (FLSVerifyDictionary) {
3697N/A verify_tree();
0N/A }
0N/A // starting at the root, work downwards trying to find match.
0N/A // Remember the last node of size too great or too small.
0N/A for (prevTL = curTL = root(); curTL != NULL;) {
0N/A if (curTL->size() == size) { // exact match
0N/A break;
0N/A }
0N/A prevTL = curTL;
0N/A if (curTL->size() < size) { // proceed to right sub-tree
0N/A curTL = curTL->right();
0N/A } else { // proceed to left sub-tree
0N/A assert(curTL->size() > size, "size inconsistency");
0N/A curTL = curTL->left();
0N/A }
0N/A }
0N/A if (curTL == NULL) { // couldn't find exact match
3695N/A
3695N/A if (dither == FreeBlockDictionary<Chunk>::exactly) return NULL;
3695N/A
0N/A // try and find the next larger size by walking back up the search path
0N/A for (curTL = prevTL; curTL != NULL;) {
0N/A if (curTL->size() >= size) break;
0N/A else curTL = curTL->parent();
0N/A }
0N/A assert(curTL == NULL || curTL->count() > 0,
0N/A "An empty list should not be in the tree");
0N/A }
0N/A if (curTL != NULL) {
0N/A assert(curTL->size() >= size, "size inconsistency");
3695N/A if (adaptive_freelists()) {
0N/A
0N/A // A candidate chunk has been found. If it is already under
0N/A // populated, get a chunk associated with the hint for this
0N/A // chunk.
0N/A if (curTL->surplus() <= 0) {
0N/A /* Use the hint to find a size with a surplus, and reset the hint. */
3695N/A TreeList<Chunk>* hintTL = curTL;
0N/A while (hintTL->hint() != 0) {
0N/A assert(hintTL->hint() == 0 || hintTL->hint() > hintTL->size(),
0N/A "hint points in the wrong direction");
3697N/A hintTL = find_list(hintTL->hint());
0N/A assert(curTL != hintTL, "Infinite loop");
0N/A if (hintTL == NULL ||
0N/A hintTL == curTL /* Should not happen but protect against it */ ) {
0N/A // No useful hint. Set the hint to NULL and go on.
0N/A curTL->set_hint(0);
0N/A break;
0N/A }
0N/A assert(hintTL->size() > size, "hint is inconsistent");
0N/A if (hintTL->surplus() > 0) {
0N/A // The hint led to a list that has a surplus. Use it.
0N/A // Set the hint for the candidate to an overpopulated
0N/A // size.
0N/A curTL->set_hint(hintTL->size());
0N/A // Change the candidate.
0N/A curTL = hintTL;
0N/A break;
0N/A }
0N/A // The evm code reset the hint of the candidate as
1145N/A // at an interim point. Why? Seems like this leaves
0N/A // the hint pointing to a list that didn't work.
0N/A // curTL->set_hint(hintTL->size());
0N/A }
0N/A }
0N/A }
0N/A // don't waste time splaying if chunk's singleton
0N/A if (splay && curTL->head()->next() != NULL) {
3697N/A semi_splay_step(curTL);
0N/A }
0N/A retTC = curTL->first_available();
0N/A assert((retTC != NULL) && (curTL->count() > 0),
0N/A "A list in the binary tree should not be NULL");
0N/A assert(retTC->size() >= size,
0N/A "A chunk of the wrong size was found");
3697N/A remove_chunk_from_tree(retTC);
3697N/A assert(retTC->is_free(), "Header is not marked correctly");
0N/A }
0N/A
0N/A if (FLSVerifyDictionary) {
0N/A verify();
0N/A }
0N/A return retTC;
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/ATreeList<Chunk>* BinaryTreeDictionary<Chunk>::find_list(size_t size) const {
3695N/A TreeList<Chunk>* curTL;
0N/A for (curTL = root(); curTL != NULL;) {
0N/A if (curTL->size() == size) { // exact match
0N/A break;
0N/A }
0N/A
0N/A if (curTL->size() < size) { // proceed to right sub-tree
0N/A curTL = curTL->right();
0N/A } else { // proceed to left sub-tree
0N/A assert(curTL->size() > size, "size inconsistency");
0N/A curTL = curTL->left();
0N/A }
0N/A }
0N/A return curTL;
0N/A}
0N/A
0N/A
3695N/Atemplate <class Chunk>
3697N/Abool BinaryTreeDictionary<Chunk>::verify_chunk_in_free_list(Chunk* tc) const {
0N/A size_t size = tc->size();
3697N/A TreeList<Chunk>* tl = find_list(size);
0N/A if (tl == NULL) {
0N/A return false;
0N/A } else {
3697N/A return tl->verify_chunk_in_free_list(tc);
0N/A }
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/AChunk* BinaryTreeDictionary<Chunk>::find_largest_dict() const {
3695N/A TreeList<Chunk> *curTL = root();
0N/A if (curTL != NULL) {
0N/A while(curTL->right() != NULL) curTL = curTL->right();
1145N/A return curTL->largest_address();
0N/A } else {
0N/A return NULL;
0N/A }
0N/A}
0N/A
0N/A// Remove the current chunk from the tree. If it is not the last
0N/A// chunk in a list on a tree node, just unlink it.
0N/A// If it is the last chunk in the list (the next link is NULL),
0N/A// remove the node and repair the tree.
3695N/Atemplate <class Chunk>
3695N/ATreeChunk<Chunk>*
3697N/ABinaryTreeDictionary<Chunk>::remove_chunk_from_tree(TreeChunk<Chunk>* tc) {
0N/A assert(tc != NULL, "Should not call with a NULL chunk");
3697N/A assert(tc->is_free(), "Header is not marked correctly");
0N/A
3695N/A TreeList<Chunk> *newTL, *parentTL;
3695N/A TreeChunk<Chunk>* retTC;
3695N/A TreeList<Chunk>* tl = tc->list();
0N/A debug_only(
0N/A bool removing_only_chunk = false;
0N/A if (tl == _root) {
0N/A if ((_root->left() == NULL) && (_root->right() == NULL)) {
0N/A if (_root->count() == 1) {
0N/A assert(_root->head() == tc, "Should only be this one chunk");
0N/A removing_only_chunk = true;
0N/A }
0N/A }
0N/A }
0N/A )
0N/A assert(tl != NULL, "List should be set");
0N/A assert(tl->parent() == NULL || tl == tl->parent()->left() ||
0N/A tl == tl->parent()->right(), "list is inconsistent");
0N/A
3697N/A bool complicated_splice = false;
0N/A
0N/A retTC = tc;
0N/A // Removing this chunk can have the side effect of changing the node
3695N/A // (TreeList<Chunk>*) in the tree. If the node is the root, update it.
3697N/A TreeList<Chunk>* replacementTL = tl->remove_chunk_replace_if_needed(tc);
3697N/A assert(tc->is_free(), "Chunk should still be free");
0N/A assert(replacementTL->parent() == NULL ||
0N/A replacementTL == replacementTL->parent()->left() ||
0N/A replacementTL == replacementTL->parent()->right(),
0N/A "list is inconsistent");
0N/A if (tl == root()) {
0N/A assert(replacementTL->parent() == NULL, "Incorrectly replacing root");
0N/A set_root(replacementTL);
0N/A }
0N/A debug_only(
0N/A if (tl != replacementTL) {
0N/A assert(replacementTL->head() != NULL,
0N/A "If the tree list was replaced, it should not be a NULL list");
3695N/A TreeList<Chunk>* rhl = replacementTL->head_as_TreeChunk()->list();
3695N/A TreeList<Chunk>* rtl = TreeChunk<Chunk>::as_TreeChunk(replacementTL->tail())->list();
0N/A assert(rhl == replacementTL, "Broken head");
0N/A assert(rtl == replacementTL, "Broken tail");
0N/A assert(replacementTL->size() == tc->size(), "Broken size");
0N/A }
0N/A )
0N/A
0N/A // Does the tree need to be repaired?
0N/A if (replacementTL->count() == 0) {
0N/A assert(replacementTL->head() == NULL &&
0N/A replacementTL->tail() == NULL, "list count is incorrect");
0N/A // Find the replacement node for the (soon to be empty) node being removed.
0N/A // if we have a single (or no) child, splice child in our stead
0N/A if (replacementTL->left() == NULL) {
0N/A // left is NULL so pick right. right may also be NULL.
0N/A newTL = replacementTL->right();
3697N/A debug_only(replacementTL->clear_right();)
0N/A } else if (replacementTL->right() == NULL) {
0N/A // right is NULL
0N/A newTL = replacementTL->left();
0N/A debug_only(replacementTL->clearLeft();)
0N/A } else { // we have both children, so, by patriarchal convention,
0N/A // my replacement is least node in right sub-tree
3697N/A complicated_splice = true;
3697N/A newTL = remove_tree_minimum(replacementTL->right());
0N/A assert(newTL != NULL && newTL->left() == NULL &&
0N/A newTL->right() == NULL, "sub-tree minimum exists");
0N/A }
0N/A // newTL is the replacement for the (soon to be empty) node.
0N/A // newTL may be NULL.
0N/A // should verify; we just cleanly excised our replacement
0N/A if (FLSVerifyDictionary) {
3697N/A verify_tree();
0N/A }
0N/A // first make newTL my parent's child
0N/A if ((parentTL = replacementTL->parent()) == NULL) {
0N/A // newTL should be root
0N/A assert(tl == root(), "Incorrectly replacing root");
0N/A set_root(newTL);
0N/A if (newTL != NULL) {
3697N/A newTL->clear_parent();
0N/A }
0N/A } else if (parentTL->right() == replacementTL) {
0N/A // replacementTL is a right child
3697N/A parentTL->set_right(newTL);
0N/A } else { // replacementTL is a left child
0N/A assert(parentTL->left() == replacementTL, "should be left child");
3697N/A parentTL->set_left(newTL);
0N/A }
3697N/A debug_only(replacementTL->clear_parent();)
3697N/A if (complicated_splice) { // we need newTL to get replacementTL's
0N/A // two children
0N/A assert(newTL != NULL &&
0N/A newTL->left() == NULL && newTL->right() == NULL,
0N/A "newTL should not have encumbrances from the past");
0N/A // we'd like to assert as below:
0N/A // assert(replacementTL->left() != NULL && replacementTL->right() != NULL,
3697N/A // "else !complicated_splice");
0N/A // ... however, the above assertion is too strong because we aren't
0N/A // guaranteed that replacementTL->right() is still NULL.
0N/A // Recall that we removed
0N/A // the right sub-tree minimum from replacementTL.
0N/A // That may well have been its right
0N/A // child! So we'll just assert half of the above:
3697N/A assert(replacementTL->left() != NULL, "else !complicated_splice");
3697N/A newTL->set_left(replacementTL->left());
3697N/A newTL->set_right(replacementTL->right());
0N/A debug_only(
3697N/A replacementTL->clear_right();
0N/A replacementTL->clearLeft();
0N/A )
0N/A }
0N/A assert(replacementTL->right() == NULL &&
0N/A replacementTL->left() == NULL &&
0N/A replacementTL->parent() == NULL,
0N/A "delete without encumbrances");
0N/A }
0N/A
3697N/A assert(total_size() >= retTC->size(), "Incorrect total size");
3697N/A dec_total_size(retTC->size()); // size book-keeping
3697N/A assert(total_free_blocks() > 0, "Incorrect total count");
3697N/A set_total_free_blocks(total_free_blocks() - 1);
0N/A
0N/A assert(retTC != NULL, "null chunk?");
0N/A assert(retTC->prev() == NULL && retTC->next() == NULL,
0N/A "should return without encumbrances");
0N/A if (FLSVerifyDictionary) {
3697N/A verify_tree();
0N/A }
0N/A assert(!removing_only_chunk || _root == NULL, "root should be NULL");
3695N/A return TreeChunk<Chunk>::as_TreeChunk(retTC);
0N/A}
0N/A
0N/A// Remove the leftmost node (lm) in the tree and return it.
0N/A// If lm has a right child, link it to the left node of
0N/A// the parent of lm.
3695N/Atemplate <class Chunk>
3697N/ATreeList<Chunk>* BinaryTreeDictionary<Chunk>::remove_tree_minimum(TreeList<Chunk>* tl) {
0N/A assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree");
0N/A // locate the subtree minimum by walking down left branches
3695N/A TreeList<Chunk>* curTL = tl;
0N/A for (; curTL->left() != NULL; curTL = curTL->left());
0N/A // obviously curTL now has at most one child, a right child
0N/A if (curTL != root()) { // Should this test just be removed?
3695N/A TreeList<Chunk>* parentTL = curTL->parent();
0N/A if (parentTL->left() == curTL) { // curTL is a left child
3697N/A parentTL->set_left(curTL->right());
0N/A } else {
0N/A // If the list tl has no left child, then curTL may be
0N/A // the right child of parentTL.
0N/A assert(parentTL->right() == curTL, "should be a right child");
3697N/A parentTL->set_right(curTL->right());
0N/A }
0N/A } else {
0N/A // The only use of this method would not pass the root of the
0N/A // tree (as indicated by the assertion above that the tree list
0N/A // has a parent) but the specification does not explicitly exclude the
0N/A // passing of the root so accomodate it.
0N/A set_root(NULL);
0N/A }
0N/A debug_only(
3697N/A curTL->clear_parent(); // Test if this needs to be cleared
3697N/A curTL->clear_right(); // recall, above, left child is already null
0N/A )
0N/A // we just excised a (non-root) node, we should still verify all tree invariants
0N/A if (FLSVerifyDictionary) {
3697N/A verify_tree();
0N/A }
0N/A return curTL;
0N/A}
0N/A
0N/A// Based on a simplification of the algorithm by Sleator and Tarjan (JACM 1985).
0N/A// The simplifications are the following:
0N/A// . we splay only when we delete (not when we insert)
0N/A// . we apply a single spay step per deletion/access
0N/A// By doing such partial splaying, we reduce the amount of restructuring,
0N/A// while getting a reasonably efficient search tree (we think).
0N/A// [Measurements will be needed to (in)validate this expectation.]
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::semi_splay_step(TreeList<Chunk>* tc) {
0N/A // apply a semi-splay step at the given node:
0N/A // . if root, norting needs to be done
0N/A // . if child of root, splay once
0N/A // . else zig-zig or sig-zag depending on path from grandparent
0N/A if (root() == tc) return;
0N/A warning("*** Splaying not yet implemented; "
0N/A "tree operations may be inefficient ***");
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::insert_chunk_in_tree(Chunk* fc) {
3695N/A TreeList<Chunk> *curTL, *prevTL;
0N/A size_t size = fc->size();
0N/A
3695N/A assert(size >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "too small to be a TreeList<Chunk>");
0N/A if (FLSVerifyDictionary) {
3697N/A verify_tree();
0N/A }
0N/A
3697N/A fc->clear_next();
3697N/A fc->link_prev(NULL);
0N/A
0N/A // work down from the _root, looking for insertion point
0N/A for (prevTL = curTL = root(); curTL != NULL;) {
0N/A if (curTL->size() == size) // exact match
0N/A break;
0N/A prevTL = curTL;
0N/A if (curTL->size() > size) { // follow left branch
0N/A curTL = curTL->left();
0N/A } else { // follow right branch
0N/A assert(curTL->size() < size, "size inconsistency");
0N/A curTL = curTL->right();
0N/A }
0N/A }
3695N/A TreeChunk<Chunk>* tc = TreeChunk<Chunk>::as_TreeChunk(fc);
1145N/A // This chunk is being returned to the binary tree. Its embedded
3695N/A // TreeList<Chunk> should be unused at this point.
0N/A tc->initialize();
0N/A if (curTL != NULL) { // exact match
0N/A tc->set_list(curTL);
3697N/A curTL->return_chunk_at_tail(tc);
0N/A } else { // need a new node in tree
3697N/A tc->clear_next();
3697N/A tc->link_prev(NULL);
3695N/A TreeList<Chunk>* newTL = TreeList<Chunk>::as_TreeList(tc);
3695N/A assert(((TreeChunk<Chunk>*)tc)->list() == newTL,
0N/A "List was not initialized correctly");
0N/A if (prevTL == NULL) { // we are the only tree node
0N/A assert(root() == NULL, "control point invariant");
0N/A set_root(newTL);
0N/A } else { // insert under prevTL ...
0N/A if (prevTL->size() < size) { // am right child
0N/A assert(prevTL->right() == NULL, "control point invariant");
3697N/A prevTL->set_right(newTL);
0N/A } else { // am left child
0N/A assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv");
3697N/A prevTL->set_left(newTL);
0N/A }
0N/A }
0N/A }
0N/A assert(tc->list() != NULL, "Tree list should be set");
0N/A
3697N/A inc_total_size(size);
3697N/A // Method 'total_size_in_tree' walks through the every block in the
0N/A // tree, so it can cause significant performance loss if there are
0N/A // many blocks in the tree
3697N/A assert(!FLSVerifyDictionary || total_size_in_tree(root()) == total_size(), "_total_size inconsistency");
3697N/A set_total_free_blocks(total_free_blocks() + 1);
0N/A if (FLSVerifyDictionary) {
3697N/A verify_tree();
0N/A }
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::max_chunk_size() const {
3695N/A FreeBlockDictionary<Chunk>::verify_par_locked();
3695N/A TreeList<Chunk>* tc = root();
0N/A if (tc == NULL) return 0;
0N/A for (; tc->right() != NULL; tc = tc->right());
0N/A return tc->size();
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::total_list_length(TreeList<Chunk>* tl) const {
0N/A size_t res;
0N/A res = tl->count();
0N/A#ifdef ASSERT
0N/A size_t cnt;
3695N/A Chunk* tc = tl->head();
0N/A for (cnt = 0; tc != NULL; tc = tc->next(), cnt++);
0N/A assert(res == cnt, "The count is not being maintained correctly");
0N/A#endif
0N/A return res;
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::total_size_in_tree(TreeList<Chunk>* tl) const {
0N/A if (tl == NULL)
0N/A return 0;
3697N/A return (tl->size() * total_list_length(tl)) +
3697N/A total_size_in_tree(tl->left()) +
3697N/A total_size_in_tree(tl->right());
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/Adouble BinaryTreeDictionary<Chunk>::sum_of_squared_block_sizes(TreeList<Chunk>* const tl) const {
0N/A if (tl == NULL) {
0N/A return 0.0;
0N/A }
0N/A double size = (double)(tl->size());
3697N/A double curr = size * size * total_list_length(tl);
0N/A curr += sum_of_squared_block_sizes(tl->left());
0N/A curr += sum_of_squared_block_sizes(tl->right());
0N/A return curr;
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::total_free_blocks_in_tree(TreeList<Chunk>* tl) const {
0N/A if (tl == NULL)
0N/A return 0;
3697N/A return total_list_length(tl) +
3697N/A total_free_blocks_in_tree(tl->left()) +
3697N/A total_free_blocks_in_tree(tl->right());
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::num_free_blocks() const {
3697N/A assert(total_free_blocks_in_tree(root()) == total_free_blocks(),
3697N/A "_total_free_blocks inconsistency");
3697N/A return total_free_blocks();
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::tree_height_helper(TreeList<Chunk>* tl) const {
0N/A if (tl == NULL)
0N/A return 0;
3697N/A return 1 + MAX2(tree_height_helper(tl->left()),
3697N/A tree_height_helper(tl->right()));
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/Asize_t BinaryTreeDictionary<Chunk>::treeHeight() const {
3697N/A return tree_height_helper(root());
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::total_nodes_helper(TreeList<Chunk>* tl) const {
0N/A if (tl == NULL) {
0N/A return 0;
0N/A }
3697N/A return 1 + total_nodes_helper(tl->left()) +
3697N/A total_nodes_helper(tl->right());
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::total_nodes_in_tree(TreeList<Chunk>* tl) const {
3697N/A return total_nodes_helper(root());
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::dict_census_udpate(size_t size, bool split, bool birth){
3697N/A TreeList<Chunk>* nd = find_list(size);
0N/A if (nd) {
0N/A if (split) {
0N/A if (birth) {
3697N/A nd->increment_split_births();
0N/A nd->increment_surplus();
0N/A } else {
3697N/A nd->increment_split_deaths();
0N/A nd->decrement_surplus();
0N/A }
0N/A } else {
0N/A if (birth) {
3697N/A nd->increment_coal_births();
0N/A nd->increment_surplus();
0N/A } else {
3697N/A nd->increment_coal_deaths();
0N/A nd->decrement_surplus();
0N/A }
0N/A }
0N/A }
0N/A // A list for this size may not be found (nd == 0) if
0N/A // This is a death where the appropriate list is now
0N/A // empty and has been removed from the list.
0N/A // This is a birth associated with a LinAB. The chunk
0N/A // for the LinAB is not in the dictionary.
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Abool BinaryTreeDictionary<Chunk>::coal_dict_over_populated(size_t size) {
1145N/A if (FLSAlwaysCoalesceLarge) return true;
1145N/A
3697N/A TreeList<Chunk>* list_of_size = find_list(size);
0N/A // None of requested size implies overpopulated.
3697N/A return list_of_size == NULL || list_of_size->coal_desired() <= 0 ||
3697N/A list_of_size->count() > list_of_size->coal_desired();
0N/A}
0N/A
0N/A// Closures for walking the binary tree.
0N/A// do_list() walks the free list in a node applying the closure
0N/A// to each free chunk in the list
0N/A// do_tree() walks the nodes in the binary tree applying do_list()
0N/A// to each list at each node.
0N/A
3695N/Atemplate <class Chunk>
0N/Aclass TreeCensusClosure : public StackObj {
0N/A protected:
3695N/A virtual void do_list(FreeList<Chunk>* fl) = 0;
0N/A public:
3695N/A virtual void do_tree(TreeList<Chunk>* tl) = 0;
0N/A};
0N/A
3695N/Atemplate <class Chunk>
3695N/Aclass AscendTreeCensusClosure : public TreeCensusClosure<Chunk> {
3786N/A using TreeCensusClosure<Chunk>::do_list;
0N/A public:
3695N/A void do_tree(TreeList<Chunk>* tl) {
0N/A if (tl != NULL) {
0N/A do_tree(tl->left());
0N/A do_list(tl);
0N/A do_tree(tl->right());
0N/A }
0N/A }
0N/A};
0N/A
3695N/Atemplate <class Chunk>
3695N/Aclass DescendTreeCensusClosure : public TreeCensusClosure<Chunk> {
3786N/A using TreeCensusClosure<Chunk>::do_list;
0N/A public:
3695N/A void do_tree(TreeList<Chunk>* tl) {
0N/A if (tl != NULL) {
0N/A do_tree(tl->right());
0N/A do_list(tl);
0N/A do_tree(tl->left());
0N/A }
0N/A }
0N/A};
0N/A
0N/A// For each list in the tree, calculate the desired, desired
0N/A// coalesce, count before sweep, and surplus before sweep.
3695N/Atemplate <class Chunk>
3695N/Aclass BeginSweepClosure : public AscendTreeCensusClosure<Chunk> {
0N/A double _percentage;
0N/A float _inter_sweep_current;
0N/A float _inter_sweep_estimate;
1145N/A float _intra_sweep_estimate;
0N/A
0N/A public:
0N/A BeginSweepClosure(double p, float inter_sweep_current,
1145N/A float inter_sweep_estimate,
1145N/A float intra_sweep_estimate) :
0N/A _percentage(p),
0N/A _inter_sweep_current(inter_sweep_current),
1145N/A _inter_sweep_estimate(inter_sweep_estimate),
1145N/A _intra_sweep_estimate(intra_sweep_estimate) { }
0N/A
3695N/A void do_list(FreeList<Chunk>* fl) {
0N/A double coalSurplusPercent = _percentage;
1145N/A fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate, _intra_sweep_estimate);
3697N/A fl->set_coal_desired((ssize_t)((double)fl->desired() * coalSurplusPercent));
3697N/A fl->set_before_sweep(fl->count());
3697N/A fl->set_bfr_surp(fl->surplus());
0N/A }
0N/A};
0N/A
0N/A// Used to search the tree until a condition is met.
0N/A// Similar to TreeCensusClosure but searches the
0N/A// tree and returns promptly when found.
0N/A
3695N/Atemplate <class Chunk>
0N/Aclass TreeSearchClosure : public StackObj {
0N/A protected:
3695N/A virtual bool do_list(FreeList<Chunk>* fl) = 0;
0N/A public:
3695N/A virtual bool do_tree(TreeList<Chunk>* tl) = 0;
0N/A};
0N/A
0N/A#if 0 // Don't need this yet but here for symmetry.
3695N/Atemplate <class Chunk>
0N/Aclass AscendTreeSearchClosure : public TreeSearchClosure {
0N/A public:
3695N/A bool do_tree(TreeList<Chunk>* tl) {
0N/A if (tl != NULL) {
0N/A if (do_tree(tl->left())) return true;
0N/A if (do_list(tl)) return true;
0N/A if (do_tree(tl->right())) return true;
0N/A }
0N/A return false;
0N/A }
0N/A};
0N/A#endif
0N/A
3695N/Atemplate <class Chunk>
3695N/Aclass DescendTreeSearchClosure : public TreeSearchClosure<Chunk> {
3786N/A using TreeSearchClosure<Chunk>::do_list;
0N/A public:
3695N/A bool do_tree(TreeList<Chunk>* tl) {
0N/A if (tl != NULL) {
0N/A if (do_tree(tl->right())) return true;
0N/A if (do_list(tl)) return true;
0N/A if (do_tree(tl->left())) return true;
0N/A }
0N/A return false;
0N/A }
0N/A};
0N/A
0N/A// Searches the tree for a chunk that ends at the
0N/A// specified address.
3695N/Atemplate <class Chunk>
3695N/Aclass EndTreeSearchClosure : public DescendTreeSearchClosure<Chunk> {
0N/A HeapWord* _target;
3695N/A Chunk* _found;
0N/A
0N/A public:
0N/A EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {}
3695N/A bool do_list(FreeList<Chunk>* fl) {
3695N/A Chunk* item = fl->head();
0N/A while (item != NULL) {
0N/A if (item->end() == _target) {
0N/A _found = item;
0N/A return true;
0N/A }
0N/A item = item->next();
0N/A }
0N/A return false;
0N/A }
3695N/A Chunk* found() { return _found; }
0N/A};
0N/A
3695N/Atemplate <class Chunk>
3695N/AChunk* BinaryTreeDictionary<Chunk>::find_chunk_ends_at(HeapWord* target) const {
3695N/A EndTreeSearchClosure<Chunk> etsc(target);
0N/A bool found_target = etsc.do_tree(root());
0N/A assert(found_target || etsc.found() == NULL, "Consistency check");
0N/A assert(!found_target || etsc.found() != NULL, "Consistency check");
0N/A return etsc.found();
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::begin_sweep_dict_census(double coalSurplusPercent,
1145N/A float inter_sweep_current, float inter_sweep_estimate, float intra_sweep_estimate) {
3695N/A BeginSweepClosure<Chunk> bsc(coalSurplusPercent, inter_sweep_current,
1145N/A inter_sweep_estimate,
1145N/A intra_sweep_estimate);
0N/A bsc.do_tree(root());
0N/A}
0N/A
0N/A// Closures and methods for calculating total bytes returned to the
0N/A// free lists in the tree.
3695N/A#ifndef PRODUCT
3695N/Atemplate <class Chunk>
3695N/Aclass InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure<Chunk> {
0N/A public:
3695N/A void do_list(FreeList<Chunk>* fl) {
3697N/A fl->set_returned_bytes(0);
3695N/A }
3695N/A};
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::initialize_dict_returned_bytes() {
3695N/A InitializeDictReturnedBytesClosure<Chunk> idrb;
3695N/A idrb.do_tree(root());
3695N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/Aclass ReturnedBytesClosure : public AscendTreeCensusClosure<Chunk> {
3697N/A size_t _dict_returned_bytes;
3695N/A public:
3697N/A ReturnedBytesClosure() { _dict_returned_bytes = 0; }
3695N/A void do_list(FreeList<Chunk>* fl) {
3697N/A _dict_returned_bytes += fl->returned_bytes();
3695N/A }
3697N/A size_t dict_returned_bytes() { return _dict_returned_bytes; }
3695N/A};
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::sum_dict_returned_bytes() {
3695N/A ReturnedBytesClosure<Chunk> rbc;
3695N/A rbc.do_tree(root());
0N/A
3697N/A return rbc.dict_returned_bytes();
3695N/A}
0N/A
3695N/A// Count the number of entries in the tree.
3695N/Atemplate <class Chunk>
3695N/Aclass treeCountClosure : public DescendTreeCensusClosure<Chunk> {
3695N/A public:
3695N/A uint count;
3695N/A treeCountClosure(uint c) { count = c; }
3695N/A void do_list(FreeList<Chunk>* fl) {
3695N/A count++;
3695N/A }
3695N/A};
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::total_count() {
3695N/A treeCountClosure<Chunk> ctc(0);
3695N/A ctc.do_tree(root());
3695N/A return ctc.count;
3695N/A}
3695N/A#endif // PRODUCT
0N/A
0N/A// Calculate surpluses for the lists in the tree.
3695N/Atemplate <class Chunk>
3695N/Aclass setTreeSurplusClosure : public AscendTreeCensusClosure<Chunk> {
0N/A double percentage;
0N/A public:
0N/A setTreeSurplusClosure(double v) { percentage = v; }
3695N/A void do_list(FreeList<Chunk>* fl) {
0N/A double splitSurplusPercent = percentage;
0N/A fl->set_surplus(fl->count() -
0N/A (ssize_t)((double)fl->desired() * splitSurplusPercent));
0N/A }
0N/A};
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::set_tree_surplus(double splitSurplusPercent) {
3695N/A setTreeSurplusClosure<Chunk> sts(splitSurplusPercent);
0N/A sts.do_tree(root());
0N/A}
0N/A
0N/A// Set hints for the lists in the tree.
3695N/Atemplate <class Chunk>
3695N/Aclass setTreeHintsClosure : public DescendTreeCensusClosure<Chunk> {
0N/A size_t hint;
0N/A public:
0N/A setTreeHintsClosure(size_t v) { hint = v; }
3695N/A void do_list(FreeList<Chunk>* fl) {
0N/A fl->set_hint(hint);
0N/A assert(fl->hint() == 0 || fl->hint() > fl->size(),
0N/A "Current hint is inconsistent");
0N/A if (fl->surplus() > 0) {
0N/A hint = fl->size();
0N/A }
0N/A }
0N/A};
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::set_tree_hints(void) {
3695N/A setTreeHintsClosure<Chunk> sth(0);
0N/A sth.do_tree(root());
0N/A}
0N/A
0N/A// Save count before previous sweep and splits and coalesces.
3695N/Atemplate <class Chunk>
3695N/Aclass clearTreeCensusClosure : public AscendTreeCensusClosure<Chunk> {
3695N/A void do_list(FreeList<Chunk>* fl) {
3697N/A fl->set_prev_sweep(fl->count());
3697N/A fl->set_coal_births(0);
3697N/A fl->set_coal_deaths(0);
3697N/A fl->set_split_births(0);
3697N/A fl->set_split_deaths(0);
0N/A }
0N/A};
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::clear_tree_census(void) {
3695N/A clearTreeCensusClosure<Chunk> ctc;
0N/A ctc.do_tree(root());
0N/A}
0N/A
0N/A// Do reporting and post sweep clean up.
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::end_sweep_dict_census(double splitSurplusPercent) {
0N/A // Does walking the tree 3 times hurt?
3697N/A set_tree_surplus(splitSurplusPercent);
3697N/A set_tree_hints();
0N/A if (PrintGC && Verbose) {
3697N/A report_statistics();
0N/A }
3697N/A clear_tree_census();
0N/A}
0N/A
0N/A// Print summary statistics
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::report_statistics() const {
3695N/A FreeBlockDictionary<Chunk>::verify_par_locked();
0N/A gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n"
0N/A "------------------------------------\n");
3697N/A size_t total_size = total_chunk_size(debug_only(NULL));
3697N/A size_t free_blocks = num_free_blocks();
3697N/A gclog_or_tty->print("Total Free Space: %d\n", total_size);
3697N/A gclog_or_tty->print("Max Chunk Size: %d\n", max_chunk_size());
3697N/A gclog_or_tty->print("Number of Blocks: %d\n", free_blocks);
3697N/A if (free_blocks > 0) {
3697N/A gclog_or_tty->print("Av. Block Size: %d\n", total_size/free_blocks);
0N/A }
0N/A gclog_or_tty->print("Tree Height: %d\n", treeHeight());
0N/A}
0N/A
0N/A// Print census information - counts, births, deaths, etc.
0N/A// for each list in the tree. Also print some summary
0N/A// information.
3695N/Atemplate <class Chunk>
3695N/Aclass PrintTreeCensusClosure : public AscendTreeCensusClosure<Chunk> {
12N/A int _print_line;
3697N/A size_t _total_free;
3695N/A FreeList<Chunk> _total;
0N/A
0N/A public:
1145N/A PrintTreeCensusClosure() {
12N/A _print_line = 0;
3697N/A _total_free = 0;
0N/A }
3695N/A FreeList<Chunk>* total() { return &_total; }
3697N/A size_t total_free() { return _total_free; }
3695N/A void do_list(FreeList<Chunk>* fl) {
12N/A if (++_print_line >= 40) {
3695N/A FreeList<Chunk>::print_labels_on(gclog_or_tty, "size");
12N/A _print_line = 0;
12N/A }
12N/A fl->print_on(gclog_or_tty);
3697N/A _total_free += fl->count() * fl->size() ;
12N/A total()->set_count( total()->count() + fl->count() );
3697N/A total()->set_bfr_surp( total()->bfr_surp() + fl->bfr_surp() );
3697N/A total()->set_surplus( total()->split_deaths() + fl->surplus() );
12N/A total()->set_desired( total()->desired() + fl->desired() );
3697N/A total()->set_prev_sweep( total()->prev_sweep() + fl->prev_sweep() );
3697N/A total()->set_before_sweep(total()->before_sweep() + fl->before_sweep());
3697N/A total()->set_coal_births( total()->coal_births() + fl->coal_births() );
3697N/A total()->set_coal_deaths( total()->coal_deaths() + fl->coal_deaths() );
3697N/A total()->set_split_births(total()->split_births() + fl->split_births());
3697N/A total()->set_split_deaths(total()->split_deaths() + fl->split_deaths());
0N/A }
0N/A};
0N/A
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::print_dict_census(void) const {
0N/A
0N/A gclog_or_tty->print("\nBinaryTree\n");
3695N/A FreeList<Chunk>::print_labels_on(gclog_or_tty, "size");
3695N/A PrintTreeCensusClosure<Chunk> ptc;
0N/A ptc.do_tree(root());
0N/A
3695N/A FreeList<Chunk>* total = ptc.total();
3695N/A FreeList<Chunk>::print_labels_on(gclog_or_tty, " ");
12N/A total->print_on(gclog_or_tty, "TOTAL\t");
0N/A gclog_or_tty->print(
3697N/A "total_free(words): " SIZE_FORMAT_W(16)
12N/A " growth: %8.5f deficit: %8.5f\n",
3697N/A ptc.total_free(),
3697N/A (double)(total->split_births() + total->coal_births()
3697N/A - total->split_deaths() - total->coal_deaths())
3697N/A /(total->prev_sweep() != 0 ? (double)total->prev_sweep() : 1.0),
12N/A (double)(total->desired() - total->count())
12N/A /(total->desired() != 0 ? (double)total->desired() : 1.0));
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/Aclass PrintFreeListsClosure : public AscendTreeCensusClosure<Chunk> {
1145N/A outputStream* _st;
1145N/A int _print_line;
1145N/A
1145N/A public:
1145N/A PrintFreeListsClosure(outputStream* st) {
1145N/A _st = st;
1145N/A _print_line = 0;
1145N/A }
3695N/A void do_list(FreeList<Chunk>* fl) {
1145N/A if (++_print_line >= 40) {
3695N/A FreeList<Chunk>::print_labels_on(_st, "size");
1145N/A _print_line = 0;
1145N/A }
1145N/A fl->print_on(gclog_or_tty);
1145N/A size_t sz = fl->size();
3695N/A for (Chunk* fc = fl->head(); fc != NULL;
1145N/A fc = fc->next()) {
1145N/A _st->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ") %s",
1145N/A fc, (HeapWord*)fc + sz,
1145N/A fc->cantCoalesce() ? "\t CC" : "");
1145N/A }
1145N/A }
1145N/A};
1145N/A
3695N/Atemplate <class Chunk>
3695N/Avoid BinaryTreeDictionary<Chunk>::print_free_lists(outputStream* st) const {
1145N/A
3695N/A FreeList<Chunk>::print_labels_on(st, "size");
3695N/A PrintFreeListsClosure<Chunk> pflc(st);
1145N/A pflc.do_tree(root());
1145N/A}
1145N/A
0N/A// Verify the following tree invariants:
0N/A// . _root has no parent
0N/A// . parent and child point to each other
0N/A// . each node's key correctly related to that of its child(ren)
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::verify_tree() const {
3697N/A guarantee(root() == NULL || total_free_blocks() == 0 ||
3697N/A total_size() != 0, "_total_size should't be 0?");
0N/A guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent");
3697N/A verify_tree_helper(root());
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3697N/Asize_t BinaryTreeDictionary<Chunk>::verify_prev_free_ptrs(TreeList<Chunk>* tl) {
0N/A size_t ct = 0;
3695N/A for (Chunk* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) {
0N/A ct++;
3697N/A assert(curFC->prev() == NULL || curFC->prev()->is_free(),
0N/A "Chunk should be free");
0N/A }
0N/A return ct;
0N/A}
0N/A
0N/A// Note: this helper is recursive rather than iterative, so use with
0N/A// caution on very deep trees; and watch out for stack overflow errors;
0N/A// In general, to be used only for debugging.
3695N/Atemplate <class Chunk>
3697N/Avoid BinaryTreeDictionary<Chunk>::verify_tree_helper(TreeList<Chunk>* tl) const {
0N/A if (tl == NULL)
0N/A return;
0N/A guarantee(tl->size() != 0, "A list must has a size");
0N/A guarantee(tl->left() == NULL || tl->left()->parent() == tl,
0N/A "parent<-/->left");
0N/A guarantee(tl->right() == NULL || tl->right()->parent() == tl,
0N/A "parent<-/->right");;
0N/A guarantee(tl->left() == NULL || tl->left()->size() < tl->size(),
0N/A "parent !> left");
0N/A guarantee(tl->right() == NULL || tl->right()->size() > tl->size(),
0N/A "parent !< left");
3697N/A guarantee(tl->head() == NULL || tl->head()->is_free(), "!Free");
0N/A guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl,
0N/A "list inconsistency");
0N/A guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL),
0N/A "list count is inconsistent");
0N/A guarantee(tl->count() > 1 || tl->head() == tl->tail(),
0N/A "list is incorrectly constructed");
3697N/A size_t count = verify_prev_free_ptrs(tl);
0N/A guarantee(count == (size_t)tl->count(), "Node count is incorrect");
0N/A if (tl->head() != NULL) {
3697N/A tl->head_as_TreeChunk()->verify_tree_chunk_list();
0N/A }
3697N/A verify_tree_helper(tl->left());
3697N/A verify_tree_helper(tl->right());
0N/A}
0N/A
3695N/Atemplate <class Chunk>
3695N/Avoid BinaryTreeDictionary<Chunk>::verify() const {
3697N/A verify_tree();
3697N/A guarantee(total_size() == total_size_in_tree(root()), "Total Size inconsistency");
0N/A}
3695N/A
3695N/A#ifndef SERIALGC
3695N/A// Explicitly instantiate these types for FreeChunk.
3695N/Atemplate class BinaryTreeDictionary<FreeChunk>;
3695N/Atemplate class TreeChunk<FreeChunk>;
3695N/Atemplate class TreeList<FreeChunk>;
3695N/A#endif // SERIALGC