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