duke@435: /* duke@435: * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_binaryTreeDictionary.cpp.incl" duke@435: duke@435: //////////////////////////////////////////////////////////////////////////////// duke@435: // A binary tree based search structure for free blocks. duke@435: // This is currently used in the Concurrent Mark&Sweep implementation. duke@435: //////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: TreeChunk* TreeChunk::as_TreeChunk(FreeChunk* fc) { duke@435: // Do some assertion checking here. duke@435: return (TreeChunk*) fc; duke@435: } duke@435: duke@435: void TreeChunk::verifyTreeChunkList() const { duke@435: TreeChunk* nextTC = (TreeChunk*)next(); duke@435: if (prev() != NULL) { // interior list node shouldn'r have tree fields duke@435: guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL && duke@435: embedded_list()->right() == NULL, "should be clear"); duke@435: } duke@435: if (nextTC != NULL) { duke@435: guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain"); duke@435: guarantee(nextTC->size() == size(), "wrong size"); duke@435: nextTC->verifyTreeChunkList(); duke@435: } duke@435: } duke@435: duke@435: duke@435: TreeList* TreeList::as_TreeList(TreeChunk* tc) { duke@435: // This first free chunk in the list will be the tree list. duke@435: assert(tc->size() >= sizeof(TreeChunk), "Chunk is too small for a TreeChunk"); duke@435: TreeList* tl = tc->embedded_list(); duke@435: tc->set_list(tl); duke@435: #ifdef ASSERT duke@435: tl->set_protecting_lock(NULL); duke@435: #endif duke@435: tl->set_hint(0); duke@435: tl->set_size(tc->size()); duke@435: tl->link_head(tc); duke@435: tl->link_tail(tc); duke@435: tl->set_count(1); duke@435: tl->init_statistics(); duke@435: tl->setParent(NULL); duke@435: tl->setLeft(NULL); duke@435: tl->setRight(NULL); duke@435: return tl; duke@435: } duke@435: TreeList* TreeList::as_TreeList(HeapWord* addr, size_t size) { duke@435: TreeChunk* tc = (TreeChunk*) addr; duke@435: assert(size >= sizeof(TreeChunk), "Chunk is too small for a TreeChunk"); jmasa@698: // The space in the heap will have been mangled initially but jmasa@698: // is not remangled when a free chunk is returned to the free list jmasa@698: // (since it is used to maintain the chunk on the free list). jmasa@698: assert((ZapUnusedHeapArea && jmasa@698: SpaceMangler::is_mangled((HeapWord*) tc->size_addr()) && jmasa@698: SpaceMangler::is_mangled((HeapWord*) tc->prev_addr()) && jmasa@698: SpaceMangler::is_mangled((HeapWord*) tc->next_addr())) || jmasa@698: (tc->size() == 0 && tc->prev() == NULL && tc->next() == NULL), jmasa@698: "Space should be clear or mangled"); duke@435: tc->setSize(size); duke@435: tc->linkPrev(NULL); duke@435: tc->linkNext(NULL); duke@435: TreeList* tl = TreeList::as_TreeList(tc); duke@435: return tl; duke@435: } duke@435: duke@435: TreeList* TreeList::removeChunkReplaceIfNeeded(TreeChunk* tc) { duke@435: duke@435: TreeList* retTL = this; duke@435: FreeChunk* list = head(); duke@435: assert(!list || list != list->next(), "Chunk on list twice"); duke@435: assert(tc != NULL, "Chunk being removed is NULL"); duke@435: assert(parent() == NULL || this == parent()->left() || duke@435: this == parent()->right(), "list is inconsistent"); duke@435: assert(tc->isFree(), "Header is not marked correctly"); duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: duke@435: FreeChunk* prevFC = tc->prev(); duke@435: TreeChunk* nextTC = TreeChunk::as_TreeChunk(tc->next()); duke@435: assert(list != NULL, "should have at least the target chunk"); duke@435: duke@435: // Is this the first item on the list? duke@435: if (tc == list) { duke@435: // The "getChunk..." functions for a TreeList will not return the duke@435: // first chunk in the list unless it is the last chunk in the list duke@435: // because the first chunk is also acting as the tree node. duke@435: // When coalescing happens, however, the first chunk in the a tree duke@435: // list can be the start of a free range. Free ranges are removed duke@435: // from the free lists so that they are not available to be duke@435: // allocated when the sweeper yields (giving up the free list lock) duke@435: // to allow mutator activity. If this chunk is the first in the duke@435: // list and is not the last in the list, do the work to copy the duke@435: // TreeList from the first chunk to the next chunk and update all duke@435: // the TreeList pointers in the chunks in the list. duke@435: if (nextTC == NULL) { duke@435: assert(prevFC == NULL, "Not last chunk in the list") duke@435: set_tail(NULL); duke@435: set_head(NULL); duke@435: } else { duke@435: // copy embedded list. duke@435: nextTC->set_embedded_list(tc->embedded_list()); duke@435: retTL = nextTC->embedded_list(); duke@435: // Fix the pointer to the list in each chunk in the list. duke@435: // This can be slow for a long list. Consider having duke@435: // an option that does not allow the first chunk on the duke@435: // list to be coalesced. duke@435: for (TreeChunk* curTC = nextTC; curTC != NULL; duke@435: curTC = TreeChunk::as_TreeChunk(curTC->next())) { duke@435: curTC->set_list(retTL); duke@435: } duke@435: // Fix the parent to point to the new TreeList. duke@435: if (retTL->parent() != NULL) { duke@435: if (this == retTL->parent()->left()) { duke@435: retTL->parent()->setLeft(retTL); duke@435: } else { duke@435: assert(this == retTL->parent()->right(), "Parent is incorrect"); duke@435: retTL->parent()->setRight(retTL); duke@435: } duke@435: } duke@435: // Fix the children's parent pointers to point to the duke@435: // new list. duke@435: assert(right() == retTL->right(), "Should have been copied"); duke@435: if (retTL->right() != NULL) { duke@435: retTL->right()->setParent(retTL); duke@435: } duke@435: assert(left() == retTL->left(), "Should have been copied"); duke@435: if (retTL->left() != NULL) { duke@435: retTL->left()->setParent(retTL); duke@435: } duke@435: retTL->link_head(nextTC); duke@435: assert(nextTC->isFree(), "Should be a free chunk"); duke@435: } duke@435: } else { duke@435: if (nextTC == NULL) { duke@435: // Removing chunk at tail of list duke@435: link_tail(prevFC); duke@435: } duke@435: // Chunk is interior to the list duke@435: prevFC->linkAfter(nextTC); duke@435: } duke@435: duke@435: // Below this point the embeded TreeList being used for the duke@435: // tree node may have changed. Don't use "this" duke@435: // TreeList*. duke@435: // chunk should still be a free chunk (bit set in _prev) duke@435: assert(!retTL->head() || retTL->size() == retTL->head()->size(), duke@435: "Wrong sized chunk in list"); duke@435: debug_only( duke@435: tc->linkPrev(NULL); duke@435: tc->linkNext(NULL); duke@435: tc->set_list(NULL); duke@435: bool prev_found = false; duke@435: bool next_found = false; duke@435: for (FreeChunk* curFC = retTL->head(); duke@435: curFC != NULL; curFC = curFC->next()) { duke@435: assert(curFC != tc, "Chunk is still in list"); duke@435: if (curFC == prevFC) { duke@435: prev_found = true; duke@435: } duke@435: if (curFC == nextTC) { duke@435: next_found = true; duke@435: } duke@435: } duke@435: assert(prevFC == NULL || prev_found, "Chunk was lost from list"); duke@435: assert(nextTC == NULL || next_found, "Chunk was lost from list"); duke@435: assert(retTL->parent() == NULL || duke@435: retTL == retTL->parent()->left() || duke@435: retTL == retTL->parent()->right(), duke@435: "list is inconsistent"); duke@435: ) duke@435: retTL->decrement_count(); duke@435: duke@435: assert(tc->isFree(), "Should still be a free chunk"); duke@435: assert(retTL->head() == NULL || retTL->head()->prev() == NULL, duke@435: "list invariant"); duke@435: assert(retTL->tail() == NULL || retTL->tail()->next() == NULL, duke@435: "list invariant"); duke@435: return retTL; duke@435: } duke@435: void TreeList::returnChunkAtTail(TreeChunk* chunk) { duke@435: assert(chunk != NULL, "returning NULL chunk"); duke@435: assert(chunk->list() == this, "list should be set for chunk"); duke@435: assert(tail() != NULL, "The tree list is embedded in the first chunk"); duke@435: // which means that the list can never be empty. duke@435: assert(!verifyChunkInFreeLists(chunk), "Double entry"); duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: duke@435: FreeChunk* fc = tail(); duke@435: fc->linkAfter(chunk); duke@435: link_tail(chunk); duke@435: duke@435: assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list"); duke@435: increment_count(); duke@435: debug_only(increment_returnedBytes_by(chunk->size()*sizeof(HeapWord));) duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: } duke@435: duke@435: // Add this chunk at the head of the list. "At the head of the list" duke@435: // is defined to be after the chunk pointer to by head(). This is duke@435: // because the TreeList is embedded in the first TreeChunk in the duke@435: // list. See the definition of TreeChunk. duke@435: void TreeList::returnChunkAtHead(TreeChunk* chunk) { duke@435: assert(chunk->list() == this, "list should be set for chunk"); duke@435: assert(head() != NULL, "The tree list is embedded in the first chunk"); duke@435: assert(chunk != NULL, "returning NULL chunk"); duke@435: assert(!verifyChunkInFreeLists(chunk), "Double entry"); duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: duke@435: FreeChunk* fc = head()->next(); duke@435: if (fc != NULL) { duke@435: chunk->linkAfter(fc); duke@435: } else { duke@435: assert(tail() == NULL, "List is inconsistent"); duke@435: link_tail(chunk); duke@435: } duke@435: head()->linkAfter(chunk); duke@435: assert(!head() || size() == head()->size(), "Wrong sized chunk in list"); duke@435: increment_count(); duke@435: debug_only(increment_returnedBytes_by(chunk->size()*sizeof(HeapWord));) duke@435: assert(head() == NULL || head()->prev() == NULL, "list invariant"); duke@435: assert(tail() == NULL || tail()->next() == NULL, "list invariant"); duke@435: } duke@435: duke@435: TreeChunk* TreeList::head_as_TreeChunk() { duke@435: assert(head() == NULL || TreeChunk::as_TreeChunk(head())->list() == this, duke@435: "Wrong type of chunk?"); duke@435: return TreeChunk::as_TreeChunk(head()); duke@435: } duke@435: duke@435: TreeChunk* TreeList::first_available() { duke@435: guarantee(head() != NULL, "The head of the list cannot be NULL"); duke@435: FreeChunk* fc = head()->next(); duke@435: TreeChunk* retTC; duke@435: if (fc == NULL) { duke@435: retTC = head_as_TreeChunk(); duke@435: } else { duke@435: retTC = TreeChunk::as_TreeChunk(fc); duke@435: } duke@435: assert(retTC->list() == this, "Wrong type of chunk."); duke@435: return retTC; duke@435: } duke@435: duke@435: BinaryTreeDictionary::BinaryTreeDictionary(MemRegion mr, bool splay): duke@435: _splay(splay) duke@435: { duke@435: assert(mr.byte_size() > MIN_TREE_CHUNK_SIZE, "minimum chunk size"); duke@435: duke@435: reset(mr); duke@435: assert(root()->left() == NULL, "reset check failed"); duke@435: assert(root()->right() == NULL, "reset check failed"); duke@435: assert(root()->head()->next() == NULL, "reset check failed"); duke@435: assert(root()->head()->prev() == NULL, "reset check failed"); duke@435: assert(totalSize() == root()->size(), "reset check failed"); duke@435: assert(totalFreeBlocks() == 1, "reset check failed"); duke@435: } duke@435: duke@435: void BinaryTreeDictionary::inc_totalSize(size_t inc) { duke@435: _totalSize = _totalSize + inc; duke@435: } duke@435: duke@435: void BinaryTreeDictionary::dec_totalSize(size_t dec) { duke@435: _totalSize = _totalSize - dec; duke@435: } duke@435: duke@435: void BinaryTreeDictionary::reset(MemRegion mr) { duke@435: assert(mr.byte_size() > MIN_TREE_CHUNK_SIZE, "minimum chunk size"); duke@435: set_root(TreeList::as_TreeList(mr.start(), mr.word_size())); duke@435: set_totalSize(mr.word_size()); duke@435: set_totalFreeBlocks(1); duke@435: } duke@435: duke@435: void BinaryTreeDictionary::reset(HeapWord* addr, size_t byte_size) { duke@435: MemRegion mr(addr, heap_word_size(byte_size)); duke@435: reset(mr); duke@435: } duke@435: duke@435: void BinaryTreeDictionary::reset() { duke@435: set_root(NULL); duke@435: set_totalSize(0); duke@435: set_totalFreeBlocks(0); duke@435: } duke@435: duke@435: // Get a free block of size at least size from tree, or NULL. duke@435: // If a splay step is requested, the removal algorithm (only) incorporates duke@435: // a splay step as follows: duke@435: // . the search proceeds down the tree looking for a possible duke@435: // match. At the (closest) matching location, an appropriate splay step is applied duke@435: // (zig, zig-zig or zig-zag). A chunk of the appropriate size is then returned duke@435: // if available, and if it's the last chunk, the node is deleted. A deteleted duke@435: // node is replaced in place by its tree successor. duke@435: TreeChunk* duke@435: BinaryTreeDictionary::getChunkFromTree(size_t size, Dither dither, bool splay) duke@435: { duke@435: TreeList *curTL, *prevTL; duke@435: TreeChunk* retTC = NULL; duke@435: assert(size >= MIN_TREE_CHUNK_SIZE, "minimum chunk size"); duke@435: if (FLSVerifyDictionary) { duke@435: verifyTree(); duke@435: } duke@435: // starting at the root, work downwards trying to find match. duke@435: // Remember the last node of size too great or too small. duke@435: for (prevTL = curTL = root(); curTL != NULL;) { duke@435: if (curTL->size() == size) { // exact match duke@435: break; duke@435: } duke@435: prevTL = curTL; duke@435: if (curTL->size() < size) { // proceed to right sub-tree duke@435: curTL = curTL->right(); duke@435: } else { // proceed to left sub-tree duke@435: assert(curTL->size() > size, "size inconsistency"); duke@435: curTL = curTL->left(); duke@435: } duke@435: } duke@435: if (curTL == NULL) { // couldn't find exact match duke@435: // try and find the next larger size by walking back up the search path duke@435: for (curTL = prevTL; curTL != NULL;) { duke@435: if (curTL->size() >= size) break; duke@435: else curTL = curTL->parent(); duke@435: } duke@435: assert(curTL == NULL || curTL->count() > 0, duke@435: "An empty list should not be in the tree"); duke@435: } duke@435: if (curTL != NULL) { duke@435: assert(curTL->size() >= size, "size inconsistency"); duke@435: if (UseCMSAdaptiveFreeLists) { duke@435: duke@435: // A candidate chunk has been found. If it is already under duke@435: // populated, get a chunk associated with the hint for this duke@435: // chunk. duke@435: if (curTL->surplus() <= 0) { duke@435: /* Use the hint to find a size with a surplus, and reset the hint. */ duke@435: TreeList* hintTL = curTL; duke@435: while (hintTL->hint() != 0) { duke@435: assert(hintTL->hint() == 0 || hintTL->hint() > hintTL->size(), duke@435: "hint points in the wrong direction"); duke@435: hintTL = findList(hintTL->hint()); duke@435: assert(curTL != hintTL, "Infinite loop"); duke@435: if (hintTL == NULL || duke@435: hintTL == curTL /* Should not happen but protect against it */ ) { duke@435: // No useful hint. Set the hint to NULL and go on. duke@435: curTL->set_hint(0); duke@435: break; duke@435: } duke@435: assert(hintTL->size() > size, "hint is inconsistent"); duke@435: if (hintTL->surplus() > 0) { duke@435: // The hint led to a list that has a surplus. Use it. duke@435: // Set the hint for the candidate to an overpopulated duke@435: // size. duke@435: curTL->set_hint(hintTL->size()); duke@435: // Change the candidate. duke@435: curTL = hintTL; duke@435: break; duke@435: } duke@435: // The evm code reset the hint of the candidate as duke@435: // at an interrim point. Why? Seems like this leaves duke@435: // the hint pointing to a list that didn't work. duke@435: // curTL->set_hint(hintTL->size()); duke@435: } duke@435: } duke@435: } duke@435: // don't waste time splaying if chunk's singleton duke@435: if (splay && curTL->head()->next() != NULL) { duke@435: semiSplayStep(curTL); duke@435: } duke@435: retTC = curTL->first_available(); duke@435: assert((retTC != NULL) && (curTL->count() > 0), duke@435: "A list in the binary tree should not be NULL"); duke@435: assert(retTC->size() >= size, duke@435: "A chunk of the wrong size was found"); duke@435: removeChunkFromTree(retTC); duke@435: assert(retTC->isFree(), "Header is not marked correctly"); duke@435: } duke@435: duke@435: if (FLSVerifyDictionary) { duke@435: verify(); duke@435: } duke@435: return retTC; duke@435: } duke@435: duke@435: TreeList* BinaryTreeDictionary::findList(size_t size) const { duke@435: TreeList* curTL; duke@435: for (curTL = root(); curTL != NULL;) { duke@435: if (curTL->size() == size) { // exact match duke@435: break; duke@435: } duke@435: duke@435: if (curTL->size() < size) { // proceed to right sub-tree duke@435: curTL = curTL->right(); duke@435: } else { // proceed to left sub-tree duke@435: assert(curTL->size() > size, "size inconsistency"); duke@435: curTL = curTL->left(); duke@435: } duke@435: } duke@435: return curTL; duke@435: } duke@435: duke@435: duke@435: bool BinaryTreeDictionary::verifyChunkInFreeLists(FreeChunk* tc) const { duke@435: size_t size = tc->size(); duke@435: TreeList* tl = findList(size); duke@435: if (tl == NULL) { duke@435: return false; duke@435: } else { duke@435: return tl->verifyChunkInFreeLists(tc); duke@435: } duke@435: } duke@435: duke@435: FreeChunk* BinaryTreeDictionary::findLargestDict() const { duke@435: TreeList *curTL = root(); duke@435: if (curTL != NULL) { duke@435: while(curTL->right() != NULL) curTL = curTL->right(); duke@435: return curTL->first_available(); duke@435: } else { duke@435: return NULL; duke@435: } duke@435: } duke@435: duke@435: // Remove the current chunk from the tree. If it is not the last duke@435: // chunk in a list on a tree node, just unlink it. duke@435: // If it is the last chunk in the list (the next link is NULL), duke@435: // remove the node and repair the tree. duke@435: TreeChunk* duke@435: BinaryTreeDictionary::removeChunkFromTree(TreeChunk* tc) { duke@435: assert(tc != NULL, "Should not call with a NULL chunk"); duke@435: assert(tc->isFree(), "Header is not marked correctly"); duke@435: duke@435: TreeList *newTL, *parentTL; duke@435: TreeChunk* retTC; duke@435: TreeList* tl = tc->list(); duke@435: debug_only( duke@435: bool removing_only_chunk = false; duke@435: if (tl == _root) { duke@435: if ((_root->left() == NULL) && (_root->right() == NULL)) { duke@435: if (_root->count() == 1) { duke@435: assert(_root->head() == tc, "Should only be this one chunk"); duke@435: removing_only_chunk = true; duke@435: } duke@435: } duke@435: } duke@435: ) duke@435: assert(tl != NULL, "List should be set"); duke@435: assert(tl->parent() == NULL || tl == tl->parent()->left() || duke@435: tl == tl->parent()->right(), "list is inconsistent"); duke@435: duke@435: bool complicatedSplice = false; duke@435: duke@435: retTC = tc; duke@435: // Removing this chunk can have the side effect of changing the node duke@435: // (TreeList*) in the tree. If the node is the root, update it. duke@435: TreeList* replacementTL = tl->removeChunkReplaceIfNeeded(tc); duke@435: assert(tc->isFree(), "Chunk should still be free"); duke@435: assert(replacementTL->parent() == NULL || duke@435: replacementTL == replacementTL->parent()->left() || duke@435: replacementTL == replacementTL->parent()->right(), duke@435: "list is inconsistent"); duke@435: if (tl == root()) { duke@435: assert(replacementTL->parent() == NULL, "Incorrectly replacing root"); duke@435: set_root(replacementTL); duke@435: } duke@435: debug_only( duke@435: if (tl != replacementTL) { duke@435: assert(replacementTL->head() != NULL, duke@435: "If the tree list was replaced, it should not be a NULL list"); duke@435: TreeList* rhl = replacementTL->head_as_TreeChunk()->list(); duke@435: TreeList* rtl = TreeChunk::as_TreeChunk(replacementTL->tail())->list(); duke@435: assert(rhl == replacementTL, "Broken head"); duke@435: assert(rtl == replacementTL, "Broken tail"); duke@435: assert(replacementTL->size() == tc->size(), "Broken size"); duke@435: } duke@435: ) duke@435: duke@435: // Does the tree need to be repaired? duke@435: if (replacementTL->count() == 0) { duke@435: assert(replacementTL->head() == NULL && duke@435: replacementTL->tail() == NULL, "list count is incorrect"); duke@435: // Find the replacement node for the (soon to be empty) node being removed. duke@435: // if we have a single (or no) child, splice child in our stead duke@435: if (replacementTL->left() == NULL) { duke@435: // left is NULL so pick right. right may also be NULL. duke@435: newTL = replacementTL->right(); duke@435: debug_only(replacementTL->clearRight();) duke@435: } else if (replacementTL->right() == NULL) { duke@435: // right is NULL duke@435: newTL = replacementTL->left(); duke@435: debug_only(replacementTL->clearLeft();) duke@435: } else { // we have both children, so, by patriarchal convention, duke@435: // my replacement is least node in right sub-tree duke@435: complicatedSplice = true; duke@435: newTL = removeTreeMinimum(replacementTL->right()); duke@435: assert(newTL != NULL && newTL->left() == NULL && duke@435: newTL->right() == NULL, "sub-tree minimum exists"); duke@435: } duke@435: // newTL is the replacement for the (soon to be empty) node. duke@435: // newTL may be NULL. duke@435: // should verify; we just cleanly excised our replacement duke@435: if (FLSVerifyDictionary) { duke@435: verifyTree(); duke@435: } duke@435: // first make newTL my parent's child duke@435: if ((parentTL = replacementTL->parent()) == NULL) { duke@435: // newTL should be root duke@435: assert(tl == root(), "Incorrectly replacing root"); duke@435: set_root(newTL); duke@435: if (newTL != NULL) { duke@435: newTL->clearParent(); duke@435: } duke@435: } else if (parentTL->right() == replacementTL) { duke@435: // replacementTL is a right child duke@435: parentTL->setRight(newTL); duke@435: } else { // replacementTL is a left child duke@435: assert(parentTL->left() == replacementTL, "should be left child"); duke@435: parentTL->setLeft(newTL); duke@435: } duke@435: debug_only(replacementTL->clearParent();) duke@435: if (complicatedSplice) { // we need newTL to get replacementTL's duke@435: // two children duke@435: assert(newTL != NULL && duke@435: newTL->left() == NULL && newTL->right() == NULL, duke@435: "newTL should not have encumbrances from the past"); duke@435: // we'd like to assert as below: duke@435: // assert(replacementTL->left() != NULL && replacementTL->right() != NULL, duke@435: // "else !complicatedSplice"); duke@435: // ... however, the above assertion is too strong because we aren't duke@435: // guaranteed that replacementTL->right() is still NULL. duke@435: // Recall that we removed duke@435: // the right sub-tree minimum from replacementTL. duke@435: // That may well have been its right duke@435: // child! So we'll just assert half of the above: duke@435: assert(replacementTL->left() != NULL, "else !complicatedSplice"); duke@435: newTL->setLeft(replacementTL->left()); duke@435: newTL->setRight(replacementTL->right()); duke@435: debug_only( duke@435: replacementTL->clearRight(); duke@435: replacementTL->clearLeft(); duke@435: ) duke@435: } duke@435: assert(replacementTL->right() == NULL && duke@435: replacementTL->left() == NULL && duke@435: replacementTL->parent() == NULL, duke@435: "delete without encumbrances"); duke@435: } duke@435: duke@435: assert(totalSize() >= retTC->size(), "Incorrect total size"); duke@435: dec_totalSize(retTC->size()); // size book-keeping duke@435: assert(totalFreeBlocks() > 0, "Incorrect total count"); duke@435: set_totalFreeBlocks(totalFreeBlocks() - 1); duke@435: duke@435: assert(retTC != NULL, "null chunk?"); duke@435: assert(retTC->prev() == NULL && retTC->next() == NULL, duke@435: "should return without encumbrances"); duke@435: if (FLSVerifyDictionary) { duke@435: verifyTree(); duke@435: } duke@435: assert(!removing_only_chunk || _root == NULL, "root should be NULL"); duke@435: return TreeChunk::as_TreeChunk(retTC); duke@435: } duke@435: duke@435: // Remove the leftmost node (lm) in the tree and return it. duke@435: // If lm has a right child, link it to the left node of duke@435: // the parent of lm. duke@435: TreeList* BinaryTreeDictionary::removeTreeMinimum(TreeList* tl) { duke@435: assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree"); duke@435: // locate the subtree minimum by walking down left branches duke@435: TreeList* curTL = tl; duke@435: for (; curTL->left() != NULL; curTL = curTL->left()); duke@435: // obviously curTL now has at most one child, a right child duke@435: if (curTL != root()) { // Should this test just be removed? duke@435: TreeList* parentTL = curTL->parent(); duke@435: if (parentTL->left() == curTL) { // curTL is a left child duke@435: parentTL->setLeft(curTL->right()); duke@435: } else { duke@435: // If the list tl has no left child, then curTL may be duke@435: // the right child of parentTL. duke@435: assert(parentTL->right() == curTL, "should be a right child"); duke@435: parentTL->setRight(curTL->right()); duke@435: } duke@435: } else { duke@435: // The only use of this method would not pass the root of the duke@435: // tree (as indicated by the assertion above that the tree list duke@435: // has a parent) but the specification does not explicitly exclude the duke@435: // passing of the root so accomodate it. duke@435: set_root(NULL); duke@435: } duke@435: debug_only( duke@435: curTL->clearParent(); // Test if this needs to be cleared duke@435: curTL->clearRight(); // recall, above, left child is already null duke@435: ) duke@435: // we just excised a (non-root) node, we should still verify all tree invariants duke@435: if (FLSVerifyDictionary) { duke@435: verifyTree(); duke@435: } duke@435: return curTL; duke@435: } duke@435: duke@435: // Based on a simplification of the algorithm by Sleator and Tarjan (JACM 1985). duke@435: // The simplifications are the following: duke@435: // . we splay only when we delete (not when we insert) duke@435: // . we apply a single spay step per deletion/access duke@435: // By doing such partial splaying, we reduce the amount of restructuring, duke@435: // while getting a reasonably efficient search tree (we think). duke@435: // [Measurements will be needed to (in)validate this expectation.] duke@435: duke@435: void BinaryTreeDictionary::semiSplayStep(TreeList* tc) { duke@435: // apply a semi-splay step at the given node: duke@435: // . if root, norting needs to be done duke@435: // . if child of root, splay once duke@435: // . else zig-zig or sig-zag depending on path from grandparent duke@435: if (root() == tc) return; duke@435: warning("*** Splaying not yet implemented; " duke@435: "tree operations may be inefficient ***"); duke@435: } duke@435: duke@435: void BinaryTreeDictionary::insertChunkInTree(FreeChunk* fc) { duke@435: TreeList *curTL, *prevTL; duke@435: size_t size = fc->size(); duke@435: duke@435: assert(size >= MIN_TREE_CHUNK_SIZE, "too small to be a TreeList"); duke@435: if (FLSVerifyDictionary) { duke@435: verifyTree(); duke@435: } duke@435: // XXX: do i need to clear the FreeChunk fields, let me do it just in case duke@435: // Revisit this later duke@435: duke@435: fc->clearNext(); duke@435: fc->linkPrev(NULL); duke@435: duke@435: // work down from the _root, looking for insertion point duke@435: for (prevTL = curTL = root(); curTL != NULL;) { duke@435: if (curTL->size() == size) // exact match duke@435: break; duke@435: prevTL = curTL; duke@435: if (curTL->size() > size) { // follow left branch duke@435: curTL = curTL->left(); duke@435: } else { // follow right branch duke@435: assert(curTL->size() < size, "size inconsistency"); duke@435: curTL = curTL->right(); duke@435: } duke@435: } duke@435: TreeChunk* tc = TreeChunk::as_TreeChunk(fc); duke@435: // This chunk is being returned to the binary try. It's embedded duke@435: // TreeList should be unused at this point. duke@435: tc->initialize(); duke@435: if (curTL != NULL) { // exact match duke@435: tc->set_list(curTL); duke@435: curTL->returnChunkAtTail(tc); duke@435: } else { // need a new node in tree duke@435: tc->clearNext(); duke@435: tc->linkPrev(NULL); duke@435: TreeList* newTL = TreeList::as_TreeList(tc); duke@435: assert(((TreeChunk*)tc)->list() == newTL, duke@435: "List was not initialized correctly"); duke@435: if (prevTL == NULL) { // we are the only tree node duke@435: assert(root() == NULL, "control point invariant"); duke@435: set_root(newTL); duke@435: } else { // insert under prevTL ... duke@435: if (prevTL->size() < size) { // am right child duke@435: assert(prevTL->right() == NULL, "control point invariant"); duke@435: prevTL->setRight(newTL); duke@435: } else { // am left child duke@435: assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv"); duke@435: prevTL->setLeft(newTL); duke@435: } duke@435: } duke@435: } duke@435: assert(tc->list() != NULL, "Tree list should be set"); duke@435: duke@435: inc_totalSize(size); duke@435: // Method 'totalSizeInTree' walks through the every block in the duke@435: // tree, so it can cause significant performance loss if there are duke@435: // many blocks in the tree duke@435: assert(!FLSVerifyDictionary || totalSizeInTree(root()) == totalSize(), "_totalSize inconsistency"); duke@435: set_totalFreeBlocks(totalFreeBlocks() + 1); duke@435: if (FLSVerifyDictionary) { duke@435: verifyTree(); duke@435: } duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::maxChunkSize() const { duke@435: verify_par_locked(); duke@435: TreeList* tc = root(); duke@435: if (tc == NULL) return 0; duke@435: for (; tc->right() != NULL; tc = tc->right()); duke@435: return tc->size(); duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::totalListLength(TreeList* tl) const { duke@435: size_t res; duke@435: res = tl->count(); duke@435: #ifdef ASSERT duke@435: size_t cnt; duke@435: FreeChunk* tc = tl->head(); duke@435: for (cnt = 0; tc != NULL; tc = tc->next(), cnt++); duke@435: assert(res == cnt, "The count is not being maintained correctly"); duke@435: #endif duke@435: return res; duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::totalSizeInTree(TreeList* tl) const { duke@435: if (tl == NULL) duke@435: return 0; duke@435: return (tl->size() * totalListLength(tl)) + duke@435: totalSizeInTree(tl->left()) + duke@435: totalSizeInTree(tl->right()); duke@435: } duke@435: duke@435: double BinaryTreeDictionary::sum_of_squared_block_sizes(TreeList* const tl) const { duke@435: if (tl == NULL) { duke@435: return 0.0; duke@435: } duke@435: double size = (double)(tl->size()); duke@435: double curr = size * size * totalListLength(tl); duke@435: curr += sum_of_squared_block_sizes(tl->left()); duke@435: curr += sum_of_squared_block_sizes(tl->right()); duke@435: return curr; duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::totalFreeBlocksInTree(TreeList* tl) const { duke@435: if (tl == NULL) duke@435: return 0; duke@435: return totalListLength(tl) + duke@435: totalFreeBlocksInTree(tl->left()) + duke@435: totalFreeBlocksInTree(tl->right()); duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::numFreeBlocks() const { duke@435: assert(totalFreeBlocksInTree(root()) == totalFreeBlocks(), duke@435: "_totalFreeBlocks inconsistency"); duke@435: return totalFreeBlocks(); duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::treeHeightHelper(TreeList* tl) const { duke@435: if (tl == NULL) duke@435: return 0; duke@435: return 1 + MAX2(treeHeightHelper(tl->left()), duke@435: treeHeightHelper(tl->right())); duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::treeHeight() const { duke@435: return treeHeightHelper(root()); duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::totalNodesHelper(TreeList* tl) const { duke@435: if (tl == NULL) { duke@435: return 0; duke@435: } duke@435: return 1 + totalNodesHelper(tl->left()) + duke@435: totalNodesHelper(tl->right()); duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::totalNodesInTree(TreeList* tl) const { duke@435: return totalNodesHelper(root()); duke@435: } duke@435: duke@435: void BinaryTreeDictionary::dictCensusUpdate(size_t size, bool split, bool birth){ duke@435: TreeList* nd = findList(size); duke@435: if (nd) { duke@435: if (split) { duke@435: if (birth) { duke@435: nd->increment_splitBirths(); duke@435: nd->increment_surplus(); duke@435: } else { duke@435: nd->increment_splitDeaths(); duke@435: nd->decrement_surplus(); duke@435: } duke@435: } else { duke@435: if (birth) { duke@435: nd->increment_coalBirths(); duke@435: nd->increment_surplus(); duke@435: } else { duke@435: nd->increment_coalDeaths(); duke@435: nd->decrement_surplus(); duke@435: } duke@435: } duke@435: } duke@435: // A list for this size may not be found (nd == 0) if duke@435: // This is a death where the appropriate list is now duke@435: // empty and has been removed from the list. duke@435: // This is a birth associated with a LinAB. The chunk duke@435: // for the LinAB is not in the dictionary. duke@435: } duke@435: duke@435: bool BinaryTreeDictionary::coalDictOverPopulated(size_t size) { duke@435: TreeList* list_of_size = findList(size); duke@435: // None of requested size implies overpopulated. duke@435: return list_of_size == NULL || list_of_size->coalDesired() <= 0 || duke@435: list_of_size->count() > list_of_size->coalDesired(); duke@435: } duke@435: duke@435: // Closures for walking the binary tree. duke@435: // do_list() walks the free list in a node applying the closure duke@435: // to each free chunk in the list duke@435: // do_tree() walks the nodes in the binary tree applying do_list() duke@435: // to each list at each node. duke@435: duke@435: class TreeCensusClosure : public StackObj { duke@435: protected: duke@435: virtual void do_list(FreeList* fl) = 0; duke@435: public: duke@435: virtual void do_tree(TreeList* tl) = 0; duke@435: }; duke@435: duke@435: class AscendTreeCensusClosure : public TreeCensusClosure { duke@435: public: duke@435: void do_tree(TreeList* tl) { duke@435: if (tl != NULL) { duke@435: do_tree(tl->left()); duke@435: do_list(tl); duke@435: do_tree(tl->right()); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: class DescendTreeCensusClosure : public TreeCensusClosure { duke@435: public: duke@435: void do_tree(TreeList* tl) { duke@435: if (tl != NULL) { duke@435: do_tree(tl->right()); duke@435: do_list(tl); duke@435: do_tree(tl->left()); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: // For each list in the tree, calculate the desired, desired duke@435: // coalesce, count before sweep, and surplus before sweep. duke@435: class BeginSweepClosure : public AscendTreeCensusClosure { duke@435: double _percentage; duke@435: float _inter_sweep_current; duke@435: float _inter_sweep_estimate; duke@435: duke@435: public: duke@435: BeginSweepClosure(double p, float inter_sweep_current, duke@435: float inter_sweep_estimate) : duke@435: _percentage(p), duke@435: _inter_sweep_current(inter_sweep_current), duke@435: _inter_sweep_estimate(inter_sweep_estimate) { } duke@435: duke@435: void do_list(FreeList* fl) { duke@435: double coalSurplusPercent = _percentage; duke@435: fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate); duke@435: fl->set_coalDesired((ssize_t)((double)fl->desired() * coalSurplusPercent)); duke@435: fl->set_beforeSweep(fl->count()); duke@435: fl->set_bfrSurp(fl->surplus()); duke@435: } duke@435: }; duke@435: duke@435: // Used to search the tree until a condition is met. duke@435: // Similar to TreeCensusClosure but searches the duke@435: // tree and returns promptly when found. duke@435: duke@435: class TreeSearchClosure : public StackObj { duke@435: protected: duke@435: virtual bool do_list(FreeList* fl) = 0; duke@435: public: duke@435: virtual bool do_tree(TreeList* tl) = 0; duke@435: }; duke@435: duke@435: #if 0 // Don't need this yet but here for symmetry. duke@435: class AscendTreeSearchClosure : public TreeSearchClosure { duke@435: public: duke@435: bool do_tree(TreeList* tl) { duke@435: if (tl != NULL) { duke@435: if (do_tree(tl->left())) return true; duke@435: if (do_list(tl)) return true; duke@435: if (do_tree(tl->right())) return true; duke@435: } duke@435: return false; duke@435: } duke@435: }; duke@435: #endif duke@435: duke@435: class DescendTreeSearchClosure : public TreeSearchClosure { duke@435: public: duke@435: bool do_tree(TreeList* tl) { duke@435: if (tl != NULL) { duke@435: if (do_tree(tl->right())) return true; duke@435: if (do_list(tl)) return true; duke@435: if (do_tree(tl->left())) return true; duke@435: } duke@435: return false; duke@435: } duke@435: }; duke@435: duke@435: // Searches the tree for a chunk that ends at the duke@435: // specified address. duke@435: class EndTreeSearchClosure : public DescendTreeSearchClosure { duke@435: HeapWord* _target; duke@435: FreeChunk* _found; duke@435: duke@435: public: duke@435: EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {} duke@435: bool do_list(FreeList* fl) { duke@435: FreeChunk* item = fl->head(); duke@435: while (item != NULL) { duke@435: if (item->end() == _target) { duke@435: _found = item; duke@435: return true; duke@435: } duke@435: item = item->next(); duke@435: } duke@435: return false; duke@435: } duke@435: FreeChunk* found() { return _found; } duke@435: }; duke@435: duke@435: FreeChunk* BinaryTreeDictionary::find_chunk_ends_at(HeapWord* target) const { duke@435: EndTreeSearchClosure etsc(target); duke@435: bool found_target = etsc.do_tree(root()); duke@435: assert(found_target || etsc.found() == NULL, "Consistency check"); duke@435: assert(!found_target || etsc.found() != NULL, "Consistency check"); duke@435: return etsc.found(); duke@435: } duke@435: duke@435: void BinaryTreeDictionary::beginSweepDictCensus(double coalSurplusPercent, duke@435: float inter_sweep_current, float inter_sweep_estimate) { duke@435: BeginSweepClosure bsc(coalSurplusPercent, inter_sweep_current, duke@435: inter_sweep_estimate); duke@435: bsc.do_tree(root()); duke@435: } duke@435: duke@435: // Closures and methods for calculating total bytes returned to the duke@435: // free lists in the tree. duke@435: NOT_PRODUCT( duke@435: class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure { duke@435: public: duke@435: void do_list(FreeList* fl) { duke@435: fl->set_returnedBytes(0); duke@435: } duke@435: }; duke@435: duke@435: void BinaryTreeDictionary::initializeDictReturnedBytes() { duke@435: InitializeDictReturnedBytesClosure idrb; duke@435: idrb.do_tree(root()); duke@435: } duke@435: duke@435: class ReturnedBytesClosure : public AscendTreeCensusClosure { duke@435: size_t _dictReturnedBytes; duke@435: public: duke@435: ReturnedBytesClosure() { _dictReturnedBytes = 0; } duke@435: void do_list(FreeList* fl) { duke@435: _dictReturnedBytes += fl->returnedBytes(); duke@435: } duke@435: size_t dictReturnedBytes() { return _dictReturnedBytes; } duke@435: }; duke@435: duke@435: size_t BinaryTreeDictionary::sumDictReturnedBytes() { duke@435: ReturnedBytesClosure rbc; duke@435: rbc.do_tree(root()); duke@435: duke@435: return rbc.dictReturnedBytes(); duke@435: } duke@435: duke@435: // Count the number of entries in the tree. duke@435: class treeCountClosure : public DescendTreeCensusClosure { duke@435: public: duke@435: uint count; duke@435: treeCountClosure(uint c) { count = c; } duke@435: void do_list(FreeList* fl) { duke@435: count++; duke@435: } duke@435: }; duke@435: duke@435: size_t BinaryTreeDictionary::totalCount() { duke@435: treeCountClosure ctc(0); duke@435: ctc.do_tree(root()); duke@435: return ctc.count; duke@435: } duke@435: ) duke@435: duke@435: // Calculate surpluses for the lists in the tree. duke@435: class setTreeSurplusClosure : public AscendTreeCensusClosure { duke@435: double percentage; duke@435: public: duke@435: setTreeSurplusClosure(double v) { percentage = v; } duke@435: void do_list(FreeList* fl) { duke@435: double splitSurplusPercent = percentage; duke@435: fl->set_surplus(fl->count() - duke@435: (ssize_t)((double)fl->desired() * splitSurplusPercent)); duke@435: } duke@435: }; duke@435: duke@435: void BinaryTreeDictionary::setTreeSurplus(double splitSurplusPercent) { duke@435: setTreeSurplusClosure sts(splitSurplusPercent); duke@435: sts.do_tree(root()); duke@435: } duke@435: duke@435: // Set hints for the lists in the tree. duke@435: class setTreeHintsClosure : public DescendTreeCensusClosure { duke@435: size_t hint; duke@435: public: duke@435: setTreeHintsClosure(size_t v) { hint = v; } duke@435: void do_list(FreeList* fl) { duke@435: fl->set_hint(hint); duke@435: assert(fl->hint() == 0 || fl->hint() > fl->size(), duke@435: "Current hint is inconsistent"); duke@435: if (fl->surplus() > 0) { duke@435: hint = fl->size(); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: void BinaryTreeDictionary::setTreeHints(void) { duke@435: setTreeHintsClosure sth(0); duke@435: sth.do_tree(root()); duke@435: } duke@435: duke@435: // Save count before previous sweep and splits and coalesces. duke@435: class clearTreeCensusClosure : public AscendTreeCensusClosure { duke@435: void do_list(FreeList* fl) { duke@435: fl->set_prevSweep(fl->count()); duke@435: fl->set_coalBirths(0); duke@435: fl->set_coalDeaths(0); duke@435: fl->set_splitBirths(0); duke@435: fl->set_splitDeaths(0); duke@435: } duke@435: }; duke@435: duke@435: void BinaryTreeDictionary::clearTreeCensus(void) { duke@435: clearTreeCensusClosure ctc; duke@435: ctc.do_tree(root()); duke@435: } duke@435: duke@435: // Do reporting and post sweep clean up. duke@435: void BinaryTreeDictionary::endSweepDictCensus(double splitSurplusPercent) { duke@435: // Does walking the tree 3 times hurt? duke@435: setTreeSurplus(splitSurplusPercent); duke@435: setTreeHints(); duke@435: if (PrintGC && Verbose) { duke@435: reportStatistics(); duke@435: } duke@435: clearTreeCensus(); duke@435: } duke@435: duke@435: // Print summary statistics duke@435: void BinaryTreeDictionary::reportStatistics() const { duke@435: verify_par_locked(); duke@435: gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n" duke@435: "------------------------------------\n"); duke@435: size_t totalSize = totalChunkSize(debug_only(NULL)); duke@435: size_t freeBlocks = numFreeBlocks(); duke@435: gclog_or_tty->print("Total Free Space: %d\n", totalSize); duke@435: gclog_or_tty->print("Max Chunk Size: %d\n", maxChunkSize()); duke@435: gclog_or_tty->print("Number of Blocks: %d\n", freeBlocks); duke@435: if (freeBlocks > 0) { duke@435: gclog_or_tty->print("Av. Block Size: %d\n", totalSize/freeBlocks); duke@435: } duke@435: gclog_or_tty->print("Tree Height: %d\n", treeHeight()); duke@435: } duke@435: duke@435: // Print census information - counts, births, deaths, etc. duke@435: // for each list in the tree. Also print some summary duke@435: // information. duke@435: class printTreeCensusClosure : public AscendTreeCensusClosure { ysr@447: int _print_line; duke@435: size_t _totalFree; ysr@447: FreeList _total; duke@435: duke@435: public: duke@435: printTreeCensusClosure() { ysr@447: _print_line = 0; duke@435: _totalFree = 0; duke@435: } ysr@447: FreeList* total() { return &_total; } duke@435: size_t totalFree() { return _totalFree; } duke@435: void do_list(FreeList* fl) { ysr@447: if (++_print_line >= 40) { ysr@447: FreeList::print_labels_on(gclog_or_tty, "size"); ysr@447: _print_line = 0; ysr@447: } ysr@447: fl->print_on(gclog_or_tty); ysr@447: _totalFree += fl->count() * fl->size() ; ysr@447: total()->set_count( total()->count() + fl->count() ); ysr@447: total()->set_bfrSurp( total()->bfrSurp() + fl->bfrSurp() ); ysr@447: total()->set_surplus( total()->splitDeaths() + fl->surplus() ); ysr@447: total()->set_desired( total()->desired() + fl->desired() ); ysr@447: total()->set_prevSweep( total()->prevSweep() + fl->prevSweep() ); ysr@447: total()->set_beforeSweep(total()->beforeSweep() + fl->beforeSweep()); ysr@447: total()->set_coalBirths( total()->coalBirths() + fl->coalBirths() ); ysr@447: total()->set_coalDeaths( total()->coalDeaths() + fl->coalDeaths() ); ysr@447: total()->set_splitBirths(total()->splitBirths() + fl->splitBirths()); ysr@447: total()->set_splitDeaths(total()->splitDeaths() + fl->splitDeaths()); duke@435: } duke@435: }; duke@435: duke@435: void BinaryTreeDictionary::printDictCensus(void) const { duke@435: duke@435: gclog_or_tty->print("\nBinaryTree\n"); ysr@447: FreeList::print_labels_on(gclog_or_tty, "size"); duke@435: printTreeCensusClosure ptc; duke@435: ptc.do_tree(root()); duke@435: ysr@447: FreeList* total = ptc.total(); ysr@447: FreeList::print_labels_on(gclog_or_tty, " "); ysr@447: total->print_on(gclog_or_tty, "TOTAL\t"); duke@435: gclog_or_tty->print( ysr@447: "totalFree(words): " SIZE_FORMAT_W(16) ysr@447: " growth: %8.5f deficit: %8.5f\n", duke@435: ptc.totalFree(), ysr@447: (double)(total->splitBirths() + total->coalBirths() ysr@447: - total->splitDeaths() - total->coalDeaths()) ysr@447: /(total->prevSweep() != 0 ? (double)total->prevSweep() : 1.0), ysr@447: (double)(total->desired() - total->count()) ysr@447: /(total->desired() != 0 ? (double)total->desired() : 1.0)); duke@435: } duke@435: duke@435: // Verify the following tree invariants: duke@435: // . _root has no parent duke@435: // . parent and child point to each other duke@435: // . each node's key correctly related to that of its child(ren) duke@435: void BinaryTreeDictionary::verifyTree() const { duke@435: guarantee(root() == NULL || totalFreeBlocks() == 0 || duke@435: totalSize() != 0, "_totalSize should't be 0?"); duke@435: guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent"); duke@435: verifyTreeHelper(root()); duke@435: } duke@435: duke@435: size_t BinaryTreeDictionary::verifyPrevFreePtrs(TreeList* tl) { duke@435: size_t ct = 0; duke@435: for (FreeChunk* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) { duke@435: ct++; duke@435: assert(curFC->prev() == NULL || curFC->prev()->isFree(), duke@435: "Chunk should be free"); duke@435: } duke@435: return ct; duke@435: } duke@435: duke@435: // Note: this helper is recursive rather than iterative, so use with duke@435: // caution on very deep trees; and watch out for stack overflow errors; duke@435: // In general, to be used only for debugging. duke@435: void BinaryTreeDictionary::verifyTreeHelper(TreeList* tl) const { duke@435: if (tl == NULL) duke@435: return; duke@435: guarantee(tl->size() != 0, "A list must has a size"); duke@435: guarantee(tl->left() == NULL || tl->left()->parent() == tl, duke@435: "parent<-/->left"); duke@435: guarantee(tl->right() == NULL || tl->right()->parent() == tl, duke@435: "parent<-/->right");; duke@435: guarantee(tl->left() == NULL || tl->left()->size() < tl->size(), duke@435: "parent !> left"); duke@435: guarantee(tl->right() == NULL || tl->right()->size() > tl->size(), duke@435: "parent !< left"); duke@435: guarantee(tl->head() == NULL || tl->head()->isFree(), "!Free"); duke@435: guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl, duke@435: "list inconsistency"); duke@435: guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL), duke@435: "list count is inconsistent"); duke@435: guarantee(tl->count() > 1 || tl->head() == tl->tail(), duke@435: "list is incorrectly constructed"); duke@435: size_t count = verifyPrevFreePtrs(tl); duke@435: guarantee(count == (size_t)tl->count(), "Node count is incorrect"); duke@435: if (tl->head() != NULL) { duke@435: tl->head_as_TreeChunk()->verifyTreeChunkList(); duke@435: } duke@435: verifyTreeHelper(tl->left()); duke@435: verifyTreeHelper(tl->right()); duke@435: } duke@435: duke@435: void BinaryTreeDictionary::verify() const { duke@435: verifyTree(); duke@435: guarantee(totalSize() == totalSizeInTree(root()), "Total Size inconsistency"); duke@435: }