comparison src/share/vm/memory/binaryTreeDictionary.cpp @ 6026:9f059abe8cf2

7131629: Generalize the CMS free list code Summary: Make the FreeChunk, FreeList, TreeList, and BinaryTreeDictionary classes usable outside CMS. Reviewed-by: brutisso, johnc, jwilhelm Contributed-by: coleen.phillimore@oracle.com
author jmasa
date Thu, 29 Mar 2012 19:46:24 -0700
parents src/share/vm/gc_implementation/concurrentMarkSweep/binaryTreeDictionary.cpp@f95d63e2154a
children f69a5d43dc19
comparison
equal deleted inserted replaced
6016:3c91f2c9fd21 6026:9f059abe8cf2
1 /*
2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "gc_implementation/shared/allocationStats.hpp"
27 #include "memory/binaryTreeDictionary.hpp"
28 #include "runtime/globals.hpp"
29 #include "utilities/ostream.hpp"
30 #ifndef SERIALGC
31 #include "gc_implementation/shared/spaceDecorator.hpp"
32 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
33 #endif // SERIALGC
34
35 ////////////////////////////////////////////////////////////////////////////////
36 // A binary tree based search structure for free blocks.
37 // This is currently used in the Concurrent Mark&Sweep implementation.
38 ////////////////////////////////////////////////////////////////////////////////
39
40 template <class Chunk>
41 TreeChunk<Chunk>* TreeChunk<Chunk>::as_TreeChunk(Chunk* fc) {
42 // Do some assertion checking here.
43 return (TreeChunk<Chunk>*) fc;
44 }
45
46 template <class Chunk>
47 void TreeChunk<Chunk>::verifyTreeChunkList() const {
48 TreeChunk<Chunk>* nextTC = (TreeChunk<Chunk>*)next();
49 if (prev() != NULL) { // interior list node shouldn'r have tree fields
50 guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL &&
51 embedded_list()->right() == NULL, "should be clear");
52 }
53 if (nextTC != NULL) {
54 guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain");
55 guarantee(nextTC->size() == size(), "wrong size");
56 nextTC->verifyTreeChunkList();
57 }
58 }
59
60
61 template <class Chunk>
62 TreeList<Chunk>* TreeList<Chunk>::as_TreeList(TreeChunk<Chunk>* tc) {
63 // This first free chunk in the list will be the tree list.
64 assert(tc->size() >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "Chunk is too small for a TreeChunk");
65 TreeList<Chunk>* tl = tc->embedded_list();
66 tc->set_list(tl);
67 #ifdef ASSERT
68 tl->set_protecting_lock(NULL);
69 #endif
70 tl->set_hint(0);
71 tl->set_size(tc->size());
72 tl->link_head(tc);
73 tl->link_tail(tc);
74 tl->set_count(1);
75 tl->init_statistics(true /* split_birth */);
76 tl->setParent(NULL);
77 tl->setLeft(NULL);
78 tl->setRight(NULL);
79 return tl;
80 }
81
82 template <class Chunk>
83 TreeList<Chunk>* TreeList<Chunk>::as_TreeList(HeapWord* addr, size_t size) {
84 TreeChunk<Chunk>* tc = (TreeChunk<Chunk>*) addr;
85 assert(size >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "Chunk is too small for a TreeChunk");
86 // The space in the heap will have been mangled initially but
87 // is not remangled when a free chunk is returned to the free list
88 // (since it is used to maintain the chunk on the free list).
89 assert((ZapUnusedHeapArea &&
90 SpaceMangler::is_mangled((HeapWord*) tc->size_addr()) &&
91 SpaceMangler::is_mangled((HeapWord*) tc->prev_addr()) &&
92 SpaceMangler::is_mangled((HeapWord*) tc->next_addr())) ||
93 (tc->size() == 0 && tc->prev() == NULL && tc->next() == NULL),
94 "Space should be clear or mangled");
95 tc->setSize(size);
96 tc->linkPrev(NULL);
97 tc->linkNext(NULL);
98 TreeList<Chunk>* tl = TreeList<Chunk>::as_TreeList(tc);
99 return tl;
100 }
101
102 template <class Chunk>
103 TreeList<Chunk>* TreeList<Chunk>::removeChunkReplaceIfNeeded(TreeChunk<Chunk>* tc) {
104
105 TreeList<Chunk>* retTL = this;
106 Chunk* list = head();
107 assert(!list || list != list->next(), "Chunk on list twice");
108 assert(tc != NULL, "Chunk being removed is NULL");
109 assert(parent() == NULL || this == parent()->left() ||
110 this == parent()->right(), "list is inconsistent");
111 assert(tc->isFree(), "Header is not marked correctly");
112 assert(head() == NULL || head()->prev() == NULL, "list invariant");
113 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
114
115 Chunk* prevFC = tc->prev();
116 TreeChunk<Chunk>* nextTC = TreeChunk<Chunk>::as_TreeChunk(tc->next());
117 assert(list != NULL, "should have at least the target chunk");
118
119 // Is this the first item on the list?
120 if (tc == list) {
121 // The "getChunk..." functions for a TreeList<Chunk> will not return the
122 // first chunk in the list unless it is the last chunk in the list
123 // because the first chunk is also acting as the tree node.
124 // When coalescing happens, however, the first chunk in the a tree
125 // list can be the start of a free range. Free ranges are removed
126 // from the free lists so that they are not available to be
127 // allocated when the sweeper yields (giving up the free list lock)
128 // to allow mutator activity. If this chunk is the first in the
129 // list and is not the last in the list, do the work to copy the
130 // TreeList<Chunk> from the first chunk to the next chunk and update all
131 // the TreeList<Chunk> pointers in the chunks in the list.
132 if (nextTC == NULL) {
133 assert(prevFC == NULL, "Not last chunk in the list");
134 set_tail(NULL);
135 set_head(NULL);
136 } else {
137 // copy embedded list.
138 nextTC->set_embedded_list(tc->embedded_list());
139 retTL = nextTC->embedded_list();
140 // Fix the pointer to the list in each chunk in the list.
141 // This can be slow for a long list. Consider having
142 // an option that does not allow the first chunk on the
143 // list to be coalesced.
144 for (TreeChunk<Chunk>* curTC = nextTC; curTC != NULL;
145 curTC = TreeChunk<Chunk>::as_TreeChunk(curTC->next())) {
146 curTC->set_list(retTL);
147 }
148 // Fix the parent to point to the new TreeList<Chunk>.
149 if (retTL->parent() != NULL) {
150 if (this == retTL->parent()->left()) {
151 retTL->parent()->setLeft(retTL);
152 } else {
153 assert(this == retTL->parent()->right(), "Parent is incorrect");
154 retTL->parent()->setRight(retTL);
155 }
156 }
157 // Fix the children's parent pointers to point to the
158 // new list.
159 assert(right() == retTL->right(), "Should have been copied");
160 if (retTL->right() != NULL) {
161 retTL->right()->setParent(retTL);
162 }
163 assert(left() == retTL->left(), "Should have been copied");
164 if (retTL->left() != NULL) {
165 retTL->left()->setParent(retTL);
166 }
167 retTL->link_head(nextTC);
168 assert(nextTC->isFree(), "Should be a free chunk");
169 }
170 } else {
171 if (nextTC == NULL) {
172 // Removing chunk at tail of list
173 link_tail(prevFC);
174 }
175 // Chunk is interior to the list
176 prevFC->linkAfter(nextTC);
177 }
178
179 // Below this point the embeded TreeList<Chunk> being used for the
180 // tree node may have changed. Don't use "this"
181 // TreeList<Chunk>*.
182 // chunk should still be a free chunk (bit set in _prev)
183 assert(!retTL->head() || retTL->size() == retTL->head()->size(),
184 "Wrong sized chunk in list");
185 debug_only(
186 tc->linkPrev(NULL);
187 tc->linkNext(NULL);
188 tc->set_list(NULL);
189 bool prev_found = false;
190 bool next_found = false;
191 for (Chunk* curFC = retTL->head();
192 curFC != NULL; curFC = curFC->next()) {
193 assert(curFC != tc, "Chunk is still in list");
194 if (curFC == prevFC) {
195 prev_found = true;
196 }
197 if (curFC == nextTC) {
198 next_found = true;
199 }
200 }
201 assert(prevFC == NULL || prev_found, "Chunk was lost from list");
202 assert(nextTC == NULL || next_found, "Chunk was lost from list");
203 assert(retTL->parent() == NULL ||
204 retTL == retTL->parent()->left() ||
205 retTL == retTL->parent()->right(),
206 "list is inconsistent");
207 )
208 retTL->decrement_count();
209
210 assert(tc->isFree(), "Should still be a free chunk");
211 assert(retTL->head() == NULL || retTL->head()->prev() == NULL,
212 "list invariant");
213 assert(retTL->tail() == NULL || retTL->tail()->next() == NULL,
214 "list invariant");
215 return retTL;
216 }
217
218 template <class Chunk>
219 void TreeList<Chunk>::returnChunkAtTail(TreeChunk<Chunk>* chunk) {
220 assert(chunk != NULL, "returning NULL chunk");
221 assert(chunk->list() == this, "list should be set for chunk");
222 assert(tail() != NULL, "The tree list is embedded in the first chunk");
223 // which means that the list can never be empty.
224 assert(!verifyChunkInFreeLists(chunk), "Double entry");
225 assert(head() == NULL || head()->prev() == NULL, "list invariant");
226 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
227
228 Chunk* fc = tail();
229 fc->linkAfter(chunk);
230 link_tail(chunk);
231
232 assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list");
233 FreeList<Chunk>::increment_count();
234 debug_only(increment_returnedBytes_by(chunk->size()*sizeof(HeapWord));)
235 assert(head() == NULL || head()->prev() == NULL, "list invariant");
236 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
237 }
238
239 // Add this chunk at the head of the list. "At the head of the list"
240 // is defined to be after the chunk pointer to by head(). This is
241 // because the TreeList<Chunk> is embedded in the first TreeChunk<Chunk> in the
242 // list. See the definition of TreeChunk<Chunk>.
243 template <class Chunk>
244 void TreeList<Chunk>::returnChunkAtHead(TreeChunk<Chunk>* chunk) {
245 assert(chunk->list() == this, "list should be set for chunk");
246 assert(head() != NULL, "The tree list is embedded in the first chunk");
247 assert(chunk != NULL, "returning NULL chunk");
248 assert(!verifyChunkInFreeLists(chunk), "Double entry");
249 assert(head() == NULL || head()->prev() == NULL, "list invariant");
250 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
251
252 Chunk* fc = head()->next();
253 if (fc != NULL) {
254 chunk->linkAfter(fc);
255 } else {
256 assert(tail() == NULL, "List is inconsistent");
257 link_tail(chunk);
258 }
259 head()->linkAfter(chunk);
260 assert(!head() || size() == head()->size(), "Wrong sized chunk in list");
261 FreeList<Chunk>::increment_count();
262 debug_only(increment_returnedBytes_by(chunk->size()*sizeof(HeapWord));)
263 assert(head() == NULL || head()->prev() == NULL, "list invariant");
264 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
265 }
266
267 template <class Chunk>
268 TreeChunk<Chunk>* TreeList<Chunk>::head_as_TreeChunk() {
269 assert(head() == NULL || TreeChunk<Chunk>::as_TreeChunk(head())->list() == this,
270 "Wrong type of chunk?");
271 return TreeChunk<Chunk>::as_TreeChunk(head());
272 }
273
274 template <class Chunk>
275 TreeChunk<Chunk>* TreeList<Chunk>::first_available() {
276 assert(head() != NULL, "The head of the list cannot be NULL");
277 Chunk* fc = head()->next();
278 TreeChunk<Chunk>* retTC;
279 if (fc == NULL) {
280 retTC = head_as_TreeChunk();
281 } else {
282 retTC = TreeChunk<Chunk>::as_TreeChunk(fc);
283 }
284 assert(retTC->list() == this, "Wrong type of chunk.");
285 return retTC;
286 }
287
288 // Returns the block with the largest heap address amongst
289 // those in the list for this size; potentially slow and expensive,
290 // use with caution!
291 template <class Chunk>
292 TreeChunk<Chunk>* TreeList<Chunk>::largest_address() {
293 assert(head() != NULL, "The head of the list cannot be NULL");
294 Chunk* fc = head()->next();
295 TreeChunk<Chunk>* retTC;
296 if (fc == NULL) {
297 retTC = head_as_TreeChunk();
298 } else {
299 // walk down the list and return the one with the highest
300 // heap address among chunks of this size.
301 Chunk* last = fc;
302 while (fc->next() != NULL) {
303 if ((HeapWord*)last < (HeapWord*)fc) {
304 last = fc;
305 }
306 fc = fc->next();
307 }
308 retTC = TreeChunk<Chunk>::as_TreeChunk(last);
309 }
310 assert(retTC->list() == this, "Wrong type of chunk.");
311 return retTC;
312 }
313
314 template <class Chunk>
315 BinaryTreeDictionary<Chunk>::BinaryTreeDictionary(bool adaptive_freelists, bool splay) :
316 _splay(splay), _adaptive_freelists(adaptive_freelists),
317 _totalSize(0), _totalFreeBlocks(0), _root(0) {}
318
319 template <class Chunk>
320 BinaryTreeDictionary<Chunk>::BinaryTreeDictionary(MemRegion mr,
321 bool adaptive_freelists,
322 bool splay):
323 _adaptive_freelists(adaptive_freelists), _splay(splay)
324 {
325 assert(mr.word_size() >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "minimum chunk size");
326
327 reset(mr);
328 assert(root()->left() == NULL, "reset check failed");
329 assert(root()->right() == NULL, "reset check failed");
330 assert(root()->head()->next() == NULL, "reset check failed");
331 assert(root()->head()->prev() == NULL, "reset check failed");
332 assert(totalSize() == root()->size(), "reset check failed");
333 assert(totalFreeBlocks() == 1, "reset check failed");
334 }
335
336 template <class Chunk>
337 void BinaryTreeDictionary<Chunk>::inc_totalSize(size_t inc) {
338 _totalSize = _totalSize + inc;
339 }
340
341 template <class Chunk>
342 void BinaryTreeDictionary<Chunk>::dec_totalSize(size_t dec) {
343 _totalSize = _totalSize - dec;
344 }
345
346 template <class Chunk>
347 void BinaryTreeDictionary<Chunk>::reset(MemRegion mr) {
348 assert(mr.word_size() >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "minimum chunk size");
349 set_root(TreeList<Chunk>::as_TreeList(mr.start(), mr.word_size()));
350 set_totalSize(mr.word_size());
351 set_totalFreeBlocks(1);
352 }
353
354 template <class Chunk>
355 void BinaryTreeDictionary<Chunk>::reset(HeapWord* addr, size_t byte_size) {
356 MemRegion mr(addr, heap_word_size(byte_size));
357 reset(mr);
358 }
359
360 template <class Chunk>
361 void BinaryTreeDictionary<Chunk>::reset() {
362 set_root(NULL);
363 set_totalSize(0);
364 set_totalFreeBlocks(0);
365 }
366
367 // Get a free block of size at least size from tree, or NULL.
368 // If a splay step is requested, the removal algorithm (only) incorporates
369 // a splay step as follows:
370 // . the search proceeds down the tree looking for a possible
371 // match. At the (closest) matching location, an appropriate splay step is applied
372 // (zig, zig-zig or zig-zag). A chunk of the appropriate size is then returned
373 // if available, and if it's the last chunk, the node is deleted. A deteleted
374 // node is replaced in place by its tree successor.
375 template <class Chunk>
376 TreeChunk<Chunk>*
377 BinaryTreeDictionary<Chunk>::getChunkFromTree(size_t size, enum FreeBlockDictionary<Chunk>::Dither dither, bool splay)
378 {
379 TreeList<Chunk> *curTL, *prevTL;
380 TreeChunk<Chunk>* retTC = NULL;
381 assert(size >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "minimum chunk size");
382 if (FLSVerifyDictionary) {
383 verifyTree();
384 }
385 // starting at the root, work downwards trying to find match.
386 // Remember the last node of size too great or too small.
387 for (prevTL = curTL = root(); curTL != NULL;) {
388 if (curTL->size() == size) { // exact match
389 break;
390 }
391 prevTL = curTL;
392 if (curTL->size() < size) { // proceed to right sub-tree
393 curTL = curTL->right();
394 } else { // proceed to left sub-tree
395 assert(curTL->size() > size, "size inconsistency");
396 curTL = curTL->left();
397 }
398 }
399 if (curTL == NULL) { // couldn't find exact match
400
401 if (dither == FreeBlockDictionary<Chunk>::exactly) return NULL;
402
403 // try and find the next larger size by walking back up the search path
404 for (curTL = prevTL; curTL != NULL;) {
405 if (curTL->size() >= size) break;
406 else curTL = curTL->parent();
407 }
408 assert(curTL == NULL || curTL->count() > 0,
409 "An empty list should not be in the tree");
410 }
411 if (curTL != NULL) {
412 assert(curTL->size() >= size, "size inconsistency");
413 if (adaptive_freelists()) {
414
415 // A candidate chunk has been found. If it is already under
416 // populated, get a chunk associated with the hint for this
417 // chunk.
418 if (curTL->surplus() <= 0) {
419 /* Use the hint to find a size with a surplus, and reset the hint. */
420 TreeList<Chunk>* hintTL = curTL;
421 while (hintTL->hint() != 0) {
422 assert(hintTL->hint() == 0 || hintTL->hint() > hintTL->size(),
423 "hint points in the wrong direction");
424 hintTL = findList(hintTL->hint());
425 assert(curTL != hintTL, "Infinite loop");
426 if (hintTL == NULL ||
427 hintTL == curTL /* Should not happen but protect against it */ ) {
428 // No useful hint. Set the hint to NULL and go on.
429 curTL->set_hint(0);
430 break;
431 }
432 assert(hintTL->size() > size, "hint is inconsistent");
433 if (hintTL->surplus() > 0) {
434 // The hint led to a list that has a surplus. Use it.
435 // Set the hint for the candidate to an overpopulated
436 // size.
437 curTL->set_hint(hintTL->size());
438 // Change the candidate.
439 curTL = hintTL;
440 break;
441 }
442 // The evm code reset the hint of the candidate as
443 // at an interim point. Why? Seems like this leaves
444 // the hint pointing to a list that didn't work.
445 // curTL->set_hint(hintTL->size());
446 }
447 }
448 }
449 // don't waste time splaying if chunk's singleton
450 if (splay && curTL->head()->next() != NULL) {
451 semiSplayStep(curTL);
452 }
453 retTC = curTL->first_available();
454 assert((retTC != NULL) && (curTL->count() > 0),
455 "A list in the binary tree should not be NULL");
456 assert(retTC->size() >= size,
457 "A chunk of the wrong size was found");
458 removeChunkFromTree(retTC);
459 assert(retTC->isFree(), "Header is not marked correctly");
460 }
461
462 if (FLSVerifyDictionary) {
463 verify();
464 }
465 return retTC;
466 }
467
468 template <class Chunk>
469 TreeList<Chunk>* BinaryTreeDictionary<Chunk>::findList(size_t size) const {
470 TreeList<Chunk>* curTL;
471 for (curTL = root(); curTL != NULL;) {
472 if (curTL->size() == size) { // exact match
473 break;
474 }
475
476 if (curTL->size() < size) { // proceed to right sub-tree
477 curTL = curTL->right();
478 } else { // proceed to left sub-tree
479 assert(curTL->size() > size, "size inconsistency");
480 curTL = curTL->left();
481 }
482 }
483 return curTL;
484 }
485
486
487 template <class Chunk>
488 bool BinaryTreeDictionary<Chunk>::verifyChunkInFreeLists(Chunk* tc) const {
489 size_t size = tc->size();
490 TreeList<Chunk>* tl = findList(size);
491 if (tl == NULL) {
492 return false;
493 } else {
494 return tl->verifyChunkInFreeLists(tc);
495 }
496 }
497
498 template <class Chunk>
499 Chunk* BinaryTreeDictionary<Chunk>::findLargestDict() const {
500 TreeList<Chunk> *curTL = root();
501 if (curTL != NULL) {
502 while(curTL->right() != NULL) curTL = curTL->right();
503 return curTL->largest_address();
504 } else {
505 return NULL;
506 }
507 }
508
509 // Remove the current chunk from the tree. If it is not the last
510 // chunk in a list on a tree node, just unlink it.
511 // If it is the last chunk in the list (the next link is NULL),
512 // remove the node and repair the tree.
513 template <class Chunk>
514 TreeChunk<Chunk>*
515 BinaryTreeDictionary<Chunk>::removeChunkFromTree(TreeChunk<Chunk>* tc) {
516 assert(tc != NULL, "Should not call with a NULL chunk");
517 assert(tc->isFree(), "Header is not marked correctly");
518
519 TreeList<Chunk> *newTL, *parentTL;
520 TreeChunk<Chunk>* retTC;
521 TreeList<Chunk>* tl = tc->list();
522 debug_only(
523 bool removing_only_chunk = false;
524 if (tl == _root) {
525 if ((_root->left() == NULL) && (_root->right() == NULL)) {
526 if (_root->count() == 1) {
527 assert(_root->head() == tc, "Should only be this one chunk");
528 removing_only_chunk = true;
529 }
530 }
531 }
532 )
533 assert(tl != NULL, "List should be set");
534 assert(tl->parent() == NULL || tl == tl->parent()->left() ||
535 tl == tl->parent()->right(), "list is inconsistent");
536
537 bool complicatedSplice = false;
538
539 retTC = tc;
540 // Removing this chunk can have the side effect of changing the node
541 // (TreeList<Chunk>*) in the tree. If the node is the root, update it.
542 TreeList<Chunk>* replacementTL = tl->removeChunkReplaceIfNeeded(tc);
543 assert(tc->isFree(), "Chunk should still be free");
544 assert(replacementTL->parent() == NULL ||
545 replacementTL == replacementTL->parent()->left() ||
546 replacementTL == replacementTL->parent()->right(),
547 "list is inconsistent");
548 if (tl == root()) {
549 assert(replacementTL->parent() == NULL, "Incorrectly replacing root");
550 set_root(replacementTL);
551 }
552 debug_only(
553 if (tl != replacementTL) {
554 assert(replacementTL->head() != NULL,
555 "If the tree list was replaced, it should not be a NULL list");
556 TreeList<Chunk>* rhl = replacementTL->head_as_TreeChunk()->list();
557 TreeList<Chunk>* rtl = TreeChunk<Chunk>::as_TreeChunk(replacementTL->tail())->list();
558 assert(rhl == replacementTL, "Broken head");
559 assert(rtl == replacementTL, "Broken tail");
560 assert(replacementTL->size() == tc->size(), "Broken size");
561 }
562 )
563
564 // Does the tree need to be repaired?
565 if (replacementTL->count() == 0) {
566 assert(replacementTL->head() == NULL &&
567 replacementTL->tail() == NULL, "list count is incorrect");
568 // Find the replacement node for the (soon to be empty) node being removed.
569 // if we have a single (or no) child, splice child in our stead
570 if (replacementTL->left() == NULL) {
571 // left is NULL so pick right. right may also be NULL.
572 newTL = replacementTL->right();
573 debug_only(replacementTL->clearRight();)
574 } else if (replacementTL->right() == NULL) {
575 // right is NULL
576 newTL = replacementTL->left();
577 debug_only(replacementTL->clearLeft();)
578 } else { // we have both children, so, by patriarchal convention,
579 // my replacement is least node in right sub-tree
580 complicatedSplice = true;
581 newTL = removeTreeMinimum(replacementTL->right());
582 assert(newTL != NULL && newTL->left() == NULL &&
583 newTL->right() == NULL, "sub-tree minimum exists");
584 }
585 // newTL is the replacement for the (soon to be empty) node.
586 // newTL may be NULL.
587 // should verify; we just cleanly excised our replacement
588 if (FLSVerifyDictionary) {
589 verifyTree();
590 }
591 // first make newTL my parent's child
592 if ((parentTL = replacementTL->parent()) == NULL) {
593 // newTL should be root
594 assert(tl == root(), "Incorrectly replacing root");
595 set_root(newTL);
596 if (newTL != NULL) {
597 newTL->clearParent();
598 }
599 } else if (parentTL->right() == replacementTL) {
600 // replacementTL is a right child
601 parentTL->setRight(newTL);
602 } else { // replacementTL is a left child
603 assert(parentTL->left() == replacementTL, "should be left child");
604 parentTL->setLeft(newTL);
605 }
606 debug_only(replacementTL->clearParent();)
607 if (complicatedSplice) { // we need newTL to get replacementTL's
608 // two children
609 assert(newTL != NULL &&
610 newTL->left() == NULL && newTL->right() == NULL,
611 "newTL should not have encumbrances from the past");
612 // we'd like to assert as below:
613 // assert(replacementTL->left() != NULL && replacementTL->right() != NULL,
614 // "else !complicatedSplice");
615 // ... however, the above assertion is too strong because we aren't
616 // guaranteed that replacementTL->right() is still NULL.
617 // Recall that we removed
618 // the right sub-tree minimum from replacementTL.
619 // That may well have been its right
620 // child! So we'll just assert half of the above:
621 assert(replacementTL->left() != NULL, "else !complicatedSplice");
622 newTL->setLeft(replacementTL->left());
623 newTL->setRight(replacementTL->right());
624 debug_only(
625 replacementTL->clearRight();
626 replacementTL->clearLeft();
627 )
628 }
629 assert(replacementTL->right() == NULL &&
630 replacementTL->left() == NULL &&
631 replacementTL->parent() == NULL,
632 "delete without encumbrances");
633 }
634
635 assert(totalSize() >= retTC->size(), "Incorrect total size");
636 dec_totalSize(retTC->size()); // size book-keeping
637 assert(totalFreeBlocks() > 0, "Incorrect total count");
638 set_totalFreeBlocks(totalFreeBlocks() - 1);
639
640 assert(retTC != NULL, "null chunk?");
641 assert(retTC->prev() == NULL && retTC->next() == NULL,
642 "should return without encumbrances");
643 if (FLSVerifyDictionary) {
644 verifyTree();
645 }
646 assert(!removing_only_chunk || _root == NULL, "root should be NULL");
647 return TreeChunk<Chunk>::as_TreeChunk(retTC);
648 }
649
650 // Remove the leftmost node (lm) in the tree and return it.
651 // If lm has a right child, link it to the left node of
652 // the parent of lm.
653 template <class Chunk>
654 TreeList<Chunk>* BinaryTreeDictionary<Chunk>::removeTreeMinimum(TreeList<Chunk>* tl) {
655 assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree");
656 // locate the subtree minimum by walking down left branches
657 TreeList<Chunk>* curTL = tl;
658 for (; curTL->left() != NULL; curTL = curTL->left());
659 // obviously curTL now has at most one child, a right child
660 if (curTL != root()) { // Should this test just be removed?
661 TreeList<Chunk>* parentTL = curTL->parent();
662 if (parentTL->left() == curTL) { // curTL is a left child
663 parentTL->setLeft(curTL->right());
664 } else {
665 // If the list tl has no left child, then curTL may be
666 // the right child of parentTL.
667 assert(parentTL->right() == curTL, "should be a right child");
668 parentTL->setRight(curTL->right());
669 }
670 } else {
671 // The only use of this method would not pass the root of the
672 // tree (as indicated by the assertion above that the tree list
673 // has a parent) but the specification does not explicitly exclude the
674 // passing of the root so accomodate it.
675 set_root(NULL);
676 }
677 debug_only(
678 curTL->clearParent(); // Test if this needs to be cleared
679 curTL->clearRight(); // recall, above, left child is already null
680 )
681 // we just excised a (non-root) node, we should still verify all tree invariants
682 if (FLSVerifyDictionary) {
683 verifyTree();
684 }
685 return curTL;
686 }
687
688 // Based on a simplification of the algorithm by Sleator and Tarjan (JACM 1985).
689 // The simplifications are the following:
690 // . we splay only when we delete (not when we insert)
691 // . we apply a single spay step per deletion/access
692 // By doing such partial splaying, we reduce the amount of restructuring,
693 // while getting a reasonably efficient search tree (we think).
694 // [Measurements will be needed to (in)validate this expectation.]
695
696 template <class Chunk>
697 void BinaryTreeDictionary<Chunk>::semiSplayStep(TreeList<Chunk>* tc) {
698 // apply a semi-splay step at the given node:
699 // . if root, norting needs to be done
700 // . if child of root, splay once
701 // . else zig-zig or sig-zag depending on path from grandparent
702 if (root() == tc) return;
703 warning("*** Splaying not yet implemented; "
704 "tree operations may be inefficient ***");
705 }
706
707 template <class Chunk>
708 void BinaryTreeDictionary<Chunk>::insertChunkInTree(Chunk* fc) {
709 TreeList<Chunk> *curTL, *prevTL;
710 size_t size = fc->size();
711
712 assert(size >= BinaryTreeDictionary<Chunk>::min_tree_chunk_size, "too small to be a TreeList<Chunk>");
713 if (FLSVerifyDictionary) {
714 verifyTree();
715 }
716
717 fc->clearNext();
718 fc->linkPrev(NULL);
719
720 // work down from the _root, looking for insertion point
721 for (prevTL = curTL = root(); curTL != NULL;) {
722 if (curTL->size() == size) // exact match
723 break;
724 prevTL = curTL;
725 if (curTL->size() > size) { // follow left branch
726 curTL = curTL->left();
727 } else { // follow right branch
728 assert(curTL->size() < size, "size inconsistency");
729 curTL = curTL->right();
730 }
731 }
732 TreeChunk<Chunk>* tc = TreeChunk<Chunk>::as_TreeChunk(fc);
733 // This chunk is being returned to the binary tree. Its embedded
734 // TreeList<Chunk> should be unused at this point.
735 tc->initialize();
736 if (curTL != NULL) { // exact match
737 tc->set_list(curTL);
738 curTL->returnChunkAtTail(tc);
739 } else { // need a new node in tree
740 tc->clearNext();
741 tc->linkPrev(NULL);
742 TreeList<Chunk>* newTL = TreeList<Chunk>::as_TreeList(tc);
743 assert(((TreeChunk<Chunk>*)tc)->list() == newTL,
744 "List was not initialized correctly");
745 if (prevTL == NULL) { // we are the only tree node
746 assert(root() == NULL, "control point invariant");
747 set_root(newTL);
748 } else { // insert under prevTL ...
749 if (prevTL->size() < size) { // am right child
750 assert(prevTL->right() == NULL, "control point invariant");
751 prevTL->setRight(newTL);
752 } else { // am left child
753 assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv");
754 prevTL->setLeft(newTL);
755 }
756 }
757 }
758 assert(tc->list() != NULL, "Tree list should be set");
759
760 inc_totalSize(size);
761 // Method 'totalSizeInTree' walks through the every block in the
762 // tree, so it can cause significant performance loss if there are
763 // many blocks in the tree
764 assert(!FLSVerifyDictionary || totalSizeInTree(root()) == totalSize(), "_totalSize inconsistency");
765 set_totalFreeBlocks(totalFreeBlocks() + 1);
766 if (FLSVerifyDictionary) {
767 verifyTree();
768 }
769 }
770
771 template <class Chunk>
772 size_t BinaryTreeDictionary<Chunk>::maxChunkSize() const {
773 FreeBlockDictionary<Chunk>::verify_par_locked();
774 TreeList<Chunk>* tc = root();
775 if (tc == NULL) return 0;
776 for (; tc->right() != NULL; tc = tc->right());
777 return tc->size();
778 }
779
780 template <class Chunk>
781 size_t BinaryTreeDictionary<Chunk>::totalListLength(TreeList<Chunk>* tl) const {
782 size_t res;
783 res = tl->count();
784 #ifdef ASSERT
785 size_t cnt;
786 Chunk* tc = tl->head();
787 for (cnt = 0; tc != NULL; tc = tc->next(), cnt++);
788 assert(res == cnt, "The count is not being maintained correctly");
789 #endif
790 return res;
791 }
792
793 template <class Chunk>
794 size_t BinaryTreeDictionary<Chunk>::totalSizeInTree(TreeList<Chunk>* tl) const {
795 if (tl == NULL)
796 return 0;
797 return (tl->size() * totalListLength(tl)) +
798 totalSizeInTree(tl->left()) +
799 totalSizeInTree(tl->right());
800 }
801
802 template <class Chunk>
803 double BinaryTreeDictionary<Chunk>::sum_of_squared_block_sizes(TreeList<Chunk>* const tl) const {
804 if (tl == NULL) {
805 return 0.0;
806 }
807 double size = (double)(tl->size());
808 double curr = size * size * totalListLength(tl);
809 curr += sum_of_squared_block_sizes(tl->left());
810 curr += sum_of_squared_block_sizes(tl->right());
811 return curr;
812 }
813
814 template <class Chunk>
815 size_t BinaryTreeDictionary<Chunk>::totalFreeBlocksInTree(TreeList<Chunk>* tl) const {
816 if (tl == NULL)
817 return 0;
818 return totalListLength(tl) +
819 totalFreeBlocksInTree(tl->left()) +
820 totalFreeBlocksInTree(tl->right());
821 }
822
823 template <class Chunk>
824 size_t BinaryTreeDictionary<Chunk>::numFreeBlocks() const {
825 assert(totalFreeBlocksInTree(root()) == totalFreeBlocks(),
826 "_totalFreeBlocks inconsistency");
827 return totalFreeBlocks();
828 }
829
830 template <class Chunk>
831 size_t BinaryTreeDictionary<Chunk>::treeHeightHelper(TreeList<Chunk>* tl) const {
832 if (tl == NULL)
833 return 0;
834 return 1 + MAX2(treeHeightHelper(tl->left()),
835 treeHeightHelper(tl->right()));
836 }
837
838 template <class Chunk>
839 size_t BinaryTreeDictionary<Chunk>::treeHeight() const {
840 return treeHeightHelper(root());
841 }
842
843 template <class Chunk>
844 size_t BinaryTreeDictionary<Chunk>::totalNodesHelper(TreeList<Chunk>* tl) const {
845 if (tl == NULL) {
846 return 0;
847 }
848 return 1 + totalNodesHelper(tl->left()) +
849 totalNodesHelper(tl->right());
850 }
851
852 template <class Chunk>
853 size_t BinaryTreeDictionary<Chunk>::totalNodesInTree(TreeList<Chunk>* tl) const {
854 return totalNodesHelper(root());
855 }
856
857 template <class Chunk>
858 void BinaryTreeDictionary<Chunk>::dictCensusUpdate(size_t size, bool split, bool birth){
859 TreeList<Chunk>* nd = findList(size);
860 if (nd) {
861 if (split) {
862 if (birth) {
863 nd->increment_splitBirths();
864 nd->increment_surplus();
865 } else {
866 nd->increment_splitDeaths();
867 nd->decrement_surplus();
868 }
869 } else {
870 if (birth) {
871 nd->increment_coalBirths();
872 nd->increment_surplus();
873 } else {
874 nd->increment_coalDeaths();
875 nd->decrement_surplus();
876 }
877 }
878 }
879 // A list for this size may not be found (nd == 0) if
880 // This is a death where the appropriate list is now
881 // empty and has been removed from the list.
882 // This is a birth associated with a LinAB. The chunk
883 // for the LinAB is not in the dictionary.
884 }
885
886 template <class Chunk>
887 bool BinaryTreeDictionary<Chunk>::coalDictOverPopulated(size_t size) {
888 if (FLSAlwaysCoalesceLarge) return true;
889
890 TreeList<Chunk>* list_of_size = findList(size);
891 // None of requested size implies overpopulated.
892 return list_of_size == NULL || list_of_size->coalDesired() <= 0 ||
893 list_of_size->count() > list_of_size->coalDesired();
894 }
895
896 // Closures for walking the binary tree.
897 // do_list() walks the free list in a node applying the closure
898 // to each free chunk in the list
899 // do_tree() walks the nodes in the binary tree applying do_list()
900 // to each list at each node.
901
902 template <class Chunk>
903 class TreeCensusClosure : public StackObj {
904 protected:
905 virtual void do_list(FreeList<Chunk>* fl) = 0;
906 public:
907 virtual void do_tree(TreeList<Chunk>* tl) = 0;
908 };
909
910 template <class Chunk>
911 class AscendTreeCensusClosure : public TreeCensusClosure<Chunk> {
912 public:
913 void do_tree(TreeList<Chunk>* tl) {
914 if (tl != NULL) {
915 do_tree(tl->left());
916 do_list(tl);
917 do_tree(tl->right());
918 }
919 }
920 };
921
922 template <class Chunk>
923 class DescendTreeCensusClosure : public TreeCensusClosure<Chunk> {
924 public:
925 void do_tree(TreeList<Chunk>* tl) {
926 if (tl != NULL) {
927 do_tree(tl->right());
928 do_list(tl);
929 do_tree(tl->left());
930 }
931 }
932 };
933
934 // For each list in the tree, calculate the desired, desired
935 // coalesce, count before sweep, and surplus before sweep.
936 template <class Chunk>
937 class BeginSweepClosure : public AscendTreeCensusClosure<Chunk> {
938 double _percentage;
939 float _inter_sweep_current;
940 float _inter_sweep_estimate;
941 float _intra_sweep_estimate;
942
943 public:
944 BeginSweepClosure(double p, float inter_sweep_current,
945 float inter_sweep_estimate,
946 float intra_sweep_estimate) :
947 _percentage(p),
948 _inter_sweep_current(inter_sweep_current),
949 _inter_sweep_estimate(inter_sweep_estimate),
950 _intra_sweep_estimate(intra_sweep_estimate) { }
951
952 void do_list(FreeList<Chunk>* fl) {
953 double coalSurplusPercent = _percentage;
954 fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate, _intra_sweep_estimate);
955 fl->set_coalDesired((ssize_t)((double)fl->desired() * coalSurplusPercent));
956 fl->set_beforeSweep(fl->count());
957 fl->set_bfrSurp(fl->surplus());
958 }
959 };
960
961 // Used to search the tree until a condition is met.
962 // Similar to TreeCensusClosure but searches the
963 // tree and returns promptly when found.
964
965 template <class Chunk>
966 class TreeSearchClosure : public StackObj {
967 protected:
968 virtual bool do_list(FreeList<Chunk>* fl) = 0;
969 public:
970 virtual bool do_tree(TreeList<Chunk>* tl) = 0;
971 };
972
973 #if 0 // Don't need this yet but here for symmetry.
974 template <class Chunk>
975 class AscendTreeSearchClosure : public TreeSearchClosure {
976 public:
977 bool do_tree(TreeList<Chunk>* tl) {
978 if (tl != NULL) {
979 if (do_tree(tl->left())) return true;
980 if (do_list(tl)) return true;
981 if (do_tree(tl->right())) return true;
982 }
983 return false;
984 }
985 };
986 #endif
987
988 template <class Chunk>
989 class DescendTreeSearchClosure : public TreeSearchClosure<Chunk> {
990 public:
991 bool do_tree(TreeList<Chunk>* tl) {
992 if (tl != NULL) {
993 if (do_tree(tl->right())) return true;
994 if (do_list(tl)) return true;
995 if (do_tree(tl->left())) return true;
996 }
997 return false;
998 }
999 };
1000
1001 // Searches the tree for a chunk that ends at the
1002 // specified address.
1003 template <class Chunk>
1004 class EndTreeSearchClosure : public DescendTreeSearchClosure<Chunk> {
1005 HeapWord* _target;
1006 Chunk* _found;
1007
1008 public:
1009 EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {}
1010 bool do_list(FreeList<Chunk>* fl) {
1011 Chunk* item = fl->head();
1012 while (item != NULL) {
1013 if (item->end() == _target) {
1014 _found = item;
1015 return true;
1016 }
1017 item = item->next();
1018 }
1019 return false;
1020 }
1021 Chunk* found() { return _found; }
1022 };
1023
1024 template <class Chunk>
1025 Chunk* BinaryTreeDictionary<Chunk>::find_chunk_ends_at(HeapWord* target) const {
1026 EndTreeSearchClosure<Chunk> etsc(target);
1027 bool found_target = etsc.do_tree(root());
1028 assert(found_target || etsc.found() == NULL, "Consistency check");
1029 assert(!found_target || etsc.found() != NULL, "Consistency check");
1030 return etsc.found();
1031 }
1032
1033 template <class Chunk>
1034 void BinaryTreeDictionary<Chunk>::beginSweepDictCensus(double coalSurplusPercent,
1035 float inter_sweep_current, float inter_sweep_estimate, float intra_sweep_estimate) {
1036 BeginSweepClosure<Chunk> bsc(coalSurplusPercent, inter_sweep_current,
1037 inter_sweep_estimate,
1038 intra_sweep_estimate);
1039 bsc.do_tree(root());
1040 }
1041
1042 // Closures and methods for calculating total bytes returned to the
1043 // free lists in the tree.
1044 #ifndef PRODUCT
1045 template <class Chunk>
1046 class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure<Chunk> {
1047 public:
1048 void do_list(FreeList<Chunk>* fl) {
1049 fl->set_returnedBytes(0);
1050 }
1051 };
1052
1053 template <class Chunk>
1054 void BinaryTreeDictionary<Chunk>::initializeDictReturnedBytes() {
1055 InitializeDictReturnedBytesClosure<Chunk> idrb;
1056 idrb.do_tree(root());
1057 }
1058
1059 template <class Chunk>
1060 class ReturnedBytesClosure : public AscendTreeCensusClosure<Chunk> {
1061 size_t _dictReturnedBytes;
1062 public:
1063 ReturnedBytesClosure() { _dictReturnedBytes = 0; }
1064 void do_list(FreeList<Chunk>* fl) {
1065 _dictReturnedBytes += fl->returnedBytes();
1066 }
1067 size_t dictReturnedBytes() { return _dictReturnedBytes; }
1068 };
1069
1070 template <class Chunk>
1071 size_t BinaryTreeDictionary<Chunk>::sumDictReturnedBytes() {
1072 ReturnedBytesClosure<Chunk> rbc;
1073 rbc.do_tree(root());
1074
1075 return rbc.dictReturnedBytes();
1076 }
1077
1078 // Count the number of entries in the tree.
1079 template <class Chunk>
1080 class treeCountClosure : public DescendTreeCensusClosure<Chunk> {
1081 public:
1082 uint count;
1083 treeCountClosure(uint c) { count = c; }
1084 void do_list(FreeList<Chunk>* fl) {
1085 count++;
1086 }
1087 };
1088
1089 template <class Chunk>
1090 size_t BinaryTreeDictionary<Chunk>::totalCount() {
1091 treeCountClosure<Chunk> ctc(0);
1092 ctc.do_tree(root());
1093 return ctc.count;
1094 }
1095 #endif // PRODUCT
1096
1097 // Calculate surpluses for the lists in the tree.
1098 template <class Chunk>
1099 class setTreeSurplusClosure : public AscendTreeCensusClosure<Chunk> {
1100 double percentage;
1101 public:
1102 setTreeSurplusClosure(double v) { percentage = v; }
1103 void do_list(FreeList<Chunk>* fl) {
1104 double splitSurplusPercent = percentage;
1105 fl->set_surplus(fl->count() -
1106 (ssize_t)((double)fl->desired() * splitSurplusPercent));
1107 }
1108 };
1109
1110 template <class Chunk>
1111 void BinaryTreeDictionary<Chunk>::setTreeSurplus(double splitSurplusPercent) {
1112 setTreeSurplusClosure<Chunk> sts(splitSurplusPercent);
1113 sts.do_tree(root());
1114 }
1115
1116 // Set hints for the lists in the tree.
1117 template <class Chunk>
1118 class setTreeHintsClosure : public DescendTreeCensusClosure<Chunk> {
1119 size_t hint;
1120 public:
1121 setTreeHintsClosure(size_t v) { hint = v; }
1122 void do_list(FreeList<Chunk>* fl) {
1123 fl->set_hint(hint);
1124 assert(fl->hint() == 0 || fl->hint() > fl->size(),
1125 "Current hint is inconsistent");
1126 if (fl->surplus() > 0) {
1127 hint = fl->size();
1128 }
1129 }
1130 };
1131
1132 template <class Chunk>
1133 void BinaryTreeDictionary<Chunk>::setTreeHints(void) {
1134 setTreeHintsClosure<Chunk> sth(0);
1135 sth.do_tree(root());
1136 }
1137
1138 // Save count before previous sweep and splits and coalesces.
1139 template <class Chunk>
1140 class clearTreeCensusClosure : public AscendTreeCensusClosure<Chunk> {
1141 void do_list(FreeList<Chunk>* fl) {
1142 fl->set_prevSweep(fl->count());
1143 fl->set_coalBirths(0);
1144 fl->set_coalDeaths(0);
1145 fl->set_splitBirths(0);
1146 fl->set_splitDeaths(0);
1147 }
1148 };
1149
1150 template <class Chunk>
1151 void BinaryTreeDictionary<Chunk>::clearTreeCensus(void) {
1152 clearTreeCensusClosure<Chunk> ctc;
1153 ctc.do_tree(root());
1154 }
1155
1156 // Do reporting and post sweep clean up.
1157 template <class Chunk>
1158 void BinaryTreeDictionary<Chunk>::endSweepDictCensus(double splitSurplusPercent) {
1159 // Does walking the tree 3 times hurt?
1160 setTreeSurplus(splitSurplusPercent);
1161 setTreeHints();
1162 if (PrintGC && Verbose) {
1163 reportStatistics();
1164 }
1165 clearTreeCensus();
1166 }
1167
1168 // Print summary statistics
1169 template <class Chunk>
1170 void BinaryTreeDictionary<Chunk>::reportStatistics() const {
1171 FreeBlockDictionary<Chunk>::verify_par_locked();
1172 gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n"
1173 "------------------------------------\n");
1174 size_t totalSize = totalChunkSize(debug_only(NULL));
1175 size_t freeBlocks = numFreeBlocks();
1176 gclog_or_tty->print("Total Free Space: %d\n", totalSize);
1177 gclog_or_tty->print("Max Chunk Size: %d\n", maxChunkSize());
1178 gclog_or_tty->print("Number of Blocks: %d\n", freeBlocks);
1179 if (freeBlocks > 0) {
1180 gclog_or_tty->print("Av. Block Size: %d\n", totalSize/freeBlocks);
1181 }
1182 gclog_or_tty->print("Tree Height: %d\n", treeHeight());
1183 }
1184
1185 // Print census information - counts, births, deaths, etc.
1186 // for each list in the tree. Also print some summary
1187 // information.
1188 template <class Chunk>
1189 class PrintTreeCensusClosure : public AscendTreeCensusClosure<Chunk> {
1190 int _print_line;
1191 size_t _totalFree;
1192 FreeList<Chunk> _total;
1193
1194 public:
1195 PrintTreeCensusClosure() {
1196 _print_line = 0;
1197 _totalFree = 0;
1198 }
1199 FreeList<Chunk>* total() { return &_total; }
1200 size_t totalFree() { return _totalFree; }
1201 void do_list(FreeList<Chunk>* fl) {
1202 if (++_print_line >= 40) {
1203 FreeList<Chunk>::print_labels_on(gclog_or_tty, "size");
1204 _print_line = 0;
1205 }
1206 fl->print_on(gclog_or_tty);
1207 _totalFree += fl->count() * fl->size() ;
1208 total()->set_count( total()->count() + fl->count() );
1209 total()->set_bfrSurp( total()->bfrSurp() + fl->bfrSurp() );
1210 total()->set_surplus( total()->splitDeaths() + fl->surplus() );
1211 total()->set_desired( total()->desired() + fl->desired() );
1212 total()->set_prevSweep( total()->prevSweep() + fl->prevSweep() );
1213 total()->set_beforeSweep(total()->beforeSweep() + fl->beforeSweep());
1214 total()->set_coalBirths( total()->coalBirths() + fl->coalBirths() );
1215 total()->set_coalDeaths( total()->coalDeaths() + fl->coalDeaths() );
1216 total()->set_splitBirths(total()->splitBirths() + fl->splitBirths());
1217 total()->set_splitDeaths(total()->splitDeaths() + fl->splitDeaths());
1218 }
1219 };
1220
1221 template <class Chunk>
1222 void BinaryTreeDictionary<Chunk>::printDictCensus(void) const {
1223
1224 gclog_or_tty->print("\nBinaryTree\n");
1225 FreeList<Chunk>::print_labels_on(gclog_or_tty, "size");
1226 PrintTreeCensusClosure<Chunk> ptc;
1227 ptc.do_tree(root());
1228
1229 FreeList<Chunk>* total = ptc.total();
1230 FreeList<Chunk>::print_labels_on(gclog_or_tty, " ");
1231 total->print_on(gclog_or_tty, "TOTAL\t");
1232 gclog_or_tty->print(
1233 "totalFree(words): " SIZE_FORMAT_W(16)
1234 " growth: %8.5f deficit: %8.5f\n",
1235 ptc.totalFree(),
1236 (double)(total->splitBirths() + total->coalBirths()
1237 - total->splitDeaths() - total->coalDeaths())
1238 /(total->prevSweep() != 0 ? (double)total->prevSweep() : 1.0),
1239 (double)(total->desired() - total->count())
1240 /(total->desired() != 0 ? (double)total->desired() : 1.0));
1241 }
1242
1243 template <class Chunk>
1244 class PrintFreeListsClosure : public AscendTreeCensusClosure<Chunk> {
1245 outputStream* _st;
1246 int _print_line;
1247
1248 public:
1249 PrintFreeListsClosure(outputStream* st) {
1250 _st = st;
1251 _print_line = 0;
1252 }
1253 void do_list(FreeList<Chunk>* fl) {
1254 if (++_print_line >= 40) {
1255 FreeList<Chunk>::print_labels_on(_st, "size");
1256 _print_line = 0;
1257 }
1258 fl->print_on(gclog_or_tty);
1259 size_t sz = fl->size();
1260 for (Chunk* fc = fl->head(); fc != NULL;
1261 fc = fc->next()) {
1262 _st->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ") %s",
1263 fc, (HeapWord*)fc + sz,
1264 fc->cantCoalesce() ? "\t CC" : "");
1265 }
1266 }
1267 };
1268
1269 template <class Chunk>
1270 void BinaryTreeDictionary<Chunk>::print_free_lists(outputStream* st) const {
1271
1272 FreeList<Chunk>::print_labels_on(st, "size");
1273 PrintFreeListsClosure<Chunk> pflc(st);
1274 pflc.do_tree(root());
1275 }
1276
1277 // Verify the following tree invariants:
1278 // . _root has no parent
1279 // . parent and child point to each other
1280 // . each node's key correctly related to that of its child(ren)
1281 template <class Chunk>
1282 void BinaryTreeDictionary<Chunk>::verifyTree() const {
1283 guarantee(root() == NULL || totalFreeBlocks() == 0 ||
1284 totalSize() != 0, "_totalSize should't be 0?");
1285 guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent");
1286 verifyTreeHelper(root());
1287 }
1288
1289 template <class Chunk>
1290 size_t BinaryTreeDictionary<Chunk>::verifyPrevFreePtrs(TreeList<Chunk>* tl) {
1291 size_t ct = 0;
1292 for (Chunk* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) {
1293 ct++;
1294 assert(curFC->prev() == NULL || curFC->prev()->isFree(),
1295 "Chunk should be free");
1296 }
1297 return ct;
1298 }
1299
1300 // Note: this helper is recursive rather than iterative, so use with
1301 // caution on very deep trees; and watch out for stack overflow errors;
1302 // In general, to be used only for debugging.
1303 template <class Chunk>
1304 void BinaryTreeDictionary<Chunk>::verifyTreeHelper(TreeList<Chunk>* tl) const {
1305 if (tl == NULL)
1306 return;
1307 guarantee(tl->size() != 0, "A list must has a size");
1308 guarantee(tl->left() == NULL || tl->left()->parent() == tl,
1309 "parent<-/->left");
1310 guarantee(tl->right() == NULL || tl->right()->parent() == tl,
1311 "parent<-/->right");;
1312 guarantee(tl->left() == NULL || tl->left()->size() < tl->size(),
1313 "parent !> left");
1314 guarantee(tl->right() == NULL || tl->right()->size() > tl->size(),
1315 "parent !< left");
1316 guarantee(tl->head() == NULL || tl->head()->isFree(), "!Free");
1317 guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl,
1318 "list inconsistency");
1319 guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL),
1320 "list count is inconsistent");
1321 guarantee(tl->count() > 1 || tl->head() == tl->tail(),
1322 "list is incorrectly constructed");
1323 size_t count = verifyPrevFreePtrs(tl);
1324 guarantee(count == (size_t)tl->count(), "Node count is incorrect");
1325 if (tl->head() != NULL) {
1326 tl->head_as_TreeChunk()->verifyTreeChunkList();
1327 }
1328 verifyTreeHelper(tl->left());
1329 verifyTreeHelper(tl->right());
1330 }
1331
1332 template <class Chunk>
1333 void BinaryTreeDictionary<Chunk>::verify() const {
1334 verifyTree();
1335 guarantee(totalSize() == totalSizeInTree(root()), "Total Size inconsistency");
1336 }
1337
1338 #ifndef SERIALGC
1339 // Explicitly instantiate these types for FreeChunk.
1340 template class BinaryTreeDictionary<FreeChunk>;
1341 template class TreeChunk<FreeChunk>;
1342 template class TreeList<FreeChunk>;
1343 #endif // SERIALGC