binaryTreeDictionary.cpp revision 9056:dc9930a04ab0
1/*
2 * Copyright (c) 2001, 2015, 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/cms/allocationStats.hpp"
27#include "gc/shared/spaceDecorator.hpp"
28#include "memory/binaryTreeDictionary.hpp"
29#include "memory/freeBlockDictionary.hpp"
30#include "memory/freeList.hpp"
31#include "memory/metachunk.hpp"
32#include "runtime/globals.hpp"
33#include "utilities/macros.hpp"
34#include "utilities/ostream.hpp"
35#if INCLUDE_ALL_GCS
36#include "gc/cms/adaptiveFreeList.hpp"
37#include "gc/cms/freeChunk.hpp"
38#endif // INCLUDE_ALL_GCS
39
40////////////////////////////////////////////////////////////////////////////////
41// A binary tree based search structure for free blocks.
42// This is currently used in the Concurrent Mark&Sweep implementation.
43////////////////////////////////////////////////////////////////////////////////
44
45template <class Chunk_t, class FreeList_t>
46size_t TreeChunk<Chunk_t, FreeList_t>::_min_tree_chunk_size = sizeof(TreeChunk<Chunk_t,  FreeList_t>)/HeapWordSize;
47
48template <class Chunk_t, class FreeList_t>
49TreeChunk<Chunk_t, FreeList_t>* TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(Chunk_t* fc) {
50  // Do some assertion checking here.
51  return (TreeChunk<Chunk_t, FreeList_t>*) fc;
52}
53
54template <class Chunk_t, class FreeList_t>
55void TreeChunk<Chunk_t, FreeList_t>::verify_tree_chunk_list() const {
56  TreeChunk<Chunk_t, FreeList_t>* nextTC = (TreeChunk<Chunk_t, FreeList_t>*)next();
57  if (prev() != NULL) { // interior list node shouldn't have tree fields
58    guarantee(embedded_list()->parent() == NULL && embedded_list()->left() == NULL &&
59              embedded_list()->right()  == NULL, "should be clear");
60  }
61  if (nextTC != NULL) {
62    guarantee(as_TreeChunk(nextTC->prev()) == this, "broken chain");
63    guarantee(nextTC->size() == size(), "wrong size");
64    nextTC->verify_tree_chunk_list();
65  }
66}
67
68template <class Chunk_t, class FreeList_t>
69TreeList<Chunk_t, FreeList_t>::TreeList() : _parent(NULL),
70  _left(NULL), _right(NULL) {}
71
72template <class Chunk_t, class FreeList_t>
73TreeList<Chunk_t, FreeList_t>*
74TreeList<Chunk_t, FreeList_t>::as_TreeList(TreeChunk<Chunk_t,FreeList_t>* tc) {
75  // This first free chunk in the list will be the tree list.
76  assert((tc->size() >= (TreeChunk<Chunk_t, FreeList_t>::min_size())),
77    "Chunk is too small for a TreeChunk");
78  TreeList<Chunk_t, FreeList_t>* tl = tc->embedded_list();
79  tl->initialize();
80  tc->set_list(tl);
81  tl->set_size(tc->size());
82  tl->link_head(tc);
83  tl->link_tail(tc);
84  tl->set_count(1);
85  assert(tl->parent() == NULL, "Should be clear");
86  return tl;
87}
88
89template <class Chunk_t, class FreeList_t>
90TreeList<Chunk_t, FreeList_t>*
91TreeList<Chunk_t, FreeList_t>::as_TreeList(HeapWord* addr, size_t size) {
92  TreeChunk<Chunk_t, FreeList_t>* tc = (TreeChunk<Chunk_t, FreeList_t>*) addr;
93  assert((size >= TreeChunk<Chunk_t, FreeList_t>::min_size()),
94    "Chunk is too small for a TreeChunk");
95  // The space will have been mangled initially but
96  // is not remangled when a Chunk_t is returned to the free list
97  // (since it is used to maintain the chunk on the free list).
98  tc->assert_is_mangled();
99  tc->set_size(size);
100  tc->link_prev(NULL);
101  tc->link_next(NULL);
102  TreeList<Chunk_t, FreeList_t>* tl = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);
103  return tl;
104}
105
106
107#if INCLUDE_ALL_GCS
108// Specialize for AdaptiveFreeList which tries to avoid
109// splitting a chunk of a size that is under populated in favor of
110// an over populated size.  The general get_better_list() just returns
111// the current list.
112template <>
113TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >*
114TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >::get_better_list(
115  BinaryTreeDictionary<FreeChunk, ::AdaptiveFreeList<FreeChunk> >* dictionary) {
116  // A candidate chunk has been found.  If it is already under
117  // populated, get a chunk associated with the hint for this
118  // chunk.
119
120  TreeList<FreeChunk, ::AdaptiveFreeList<FreeChunk> >* curTL = this;
121  if (curTL->surplus() <= 0) {
122    /* Use the hint to find a size with a surplus, and reset the hint. */
123    TreeList<FreeChunk, ::AdaptiveFreeList<FreeChunk> >* hintTL = this;
124    while (hintTL->hint() != 0) {
125      assert(hintTL->hint() > hintTL->size(),
126        "hint points in the wrong direction");
127      hintTL = dictionary->find_list(hintTL->hint());
128      assert(curTL != hintTL, "Infinite loop");
129      if (hintTL == NULL ||
130          hintTL == curTL /* Should not happen but protect against it */ ) {
131        // No useful hint.  Set the hint to NULL and go on.
132        curTL->set_hint(0);
133        break;
134      }
135      assert(hintTL->size() > curTL->size(), "hint is inconsistent");
136      if (hintTL->surplus() > 0) {
137        // The hint led to a list that has a surplus.  Use it.
138        // Set the hint for the candidate to an overpopulated
139        // size.
140        curTL->set_hint(hintTL->size());
141        // Change the candidate.
142        curTL = hintTL;
143        break;
144      }
145    }
146  }
147  return curTL;
148}
149#endif // INCLUDE_ALL_GCS
150
151template <class Chunk_t, class FreeList_t>
152TreeList<Chunk_t, FreeList_t>*
153TreeList<Chunk_t, FreeList_t>::get_better_list(
154  BinaryTreeDictionary<Chunk_t, FreeList_t>* dictionary) {
155  return this;
156}
157
158template <class Chunk_t, class FreeList_t>
159TreeList<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::remove_chunk_replace_if_needed(TreeChunk<Chunk_t, FreeList_t>* tc) {
160
161  TreeList<Chunk_t, FreeList_t>* retTL = this;
162  Chunk_t* list = head();
163  assert(!list || list != list->next(), "Chunk on list twice");
164  assert(tc != NULL, "Chunk being removed is NULL");
165  assert(parent() == NULL || this == parent()->left() ||
166    this == parent()->right(), "list is inconsistent");
167  assert(tc->is_free(), "Header is not marked correctly");
168  assert(head() == NULL || head()->prev() == NULL, "list invariant");
169  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
170
171  Chunk_t* prevFC = tc->prev();
172  TreeChunk<Chunk_t, FreeList_t>* nextTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(tc->next());
173  assert(list != NULL, "should have at least the target chunk");
174
175  // Is this the first item on the list?
176  if (tc == list) {
177    // The "getChunk..." functions for a TreeList<Chunk_t, FreeList_t> will not return the
178    // first chunk in the list unless it is the last chunk in the list
179    // because the first chunk is also acting as the tree node.
180    // When coalescing happens, however, the first chunk in the a tree
181    // list can be the start of a free range.  Free ranges are removed
182    // from the free lists so that they are not available to be
183    // allocated when the sweeper yields (giving up the free list lock)
184    // to allow mutator activity.  If this chunk is the first in the
185    // list and is not the last in the list, do the work to copy the
186    // TreeList<Chunk_t, FreeList_t> from the first chunk to the next chunk and update all
187    // the TreeList<Chunk_t, FreeList_t> pointers in the chunks in the list.
188    if (nextTC == NULL) {
189      assert(prevFC == NULL, "Not last chunk in the list");
190      set_tail(NULL);
191      set_head(NULL);
192    } else {
193      // copy embedded list.
194      nextTC->set_embedded_list(tc->embedded_list());
195      retTL = nextTC->embedded_list();
196      // Fix the pointer to the list in each chunk in the list.
197      // This can be slow for a long list.  Consider having
198      // an option that does not allow the first chunk on the
199      // list to be coalesced.
200      for (TreeChunk<Chunk_t, FreeList_t>* curTC = nextTC; curTC != NULL;
201          curTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(curTC->next())) {
202        curTC->set_list(retTL);
203      }
204      // Fix the parent to point to the new TreeList<Chunk_t, FreeList_t>.
205      if (retTL->parent() != NULL) {
206        if (this == retTL->parent()->left()) {
207          retTL->parent()->set_left(retTL);
208        } else {
209          assert(this == retTL->parent()->right(), "Parent is incorrect");
210          retTL->parent()->set_right(retTL);
211        }
212      }
213      // Fix the children's parent pointers to point to the
214      // new list.
215      assert(right() == retTL->right(), "Should have been copied");
216      if (retTL->right() != NULL) {
217        retTL->right()->set_parent(retTL);
218      }
219      assert(left() == retTL->left(), "Should have been copied");
220      if (retTL->left() != NULL) {
221        retTL->left()->set_parent(retTL);
222      }
223      retTL->link_head(nextTC);
224      assert(nextTC->is_free(), "Should be a free chunk");
225    }
226  } else {
227    if (nextTC == NULL) {
228      // Removing chunk at tail of list
229      this->link_tail(prevFC);
230    }
231    // Chunk is interior to the list
232    prevFC->link_after(nextTC);
233  }
234
235  // Below this point the embedded TreeList<Chunk_t, FreeList_t> being used for the
236  // tree node may have changed. Don't use "this"
237  // TreeList<Chunk_t, FreeList_t>*.
238  // chunk should still be a free chunk (bit set in _prev)
239  assert(!retTL->head() || retTL->size() == retTL->head()->size(),
240    "Wrong sized chunk in list");
241  debug_only(
242    tc->link_prev(NULL);
243    tc->link_next(NULL);
244    tc->set_list(NULL);
245    bool prev_found = false;
246    bool next_found = false;
247    for (Chunk_t* curFC = retTL->head();
248         curFC != NULL; curFC = curFC->next()) {
249      assert(curFC != tc, "Chunk is still in list");
250      if (curFC == prevFC) {
251        prev_found = true;
252      }
253      if (curFC == nextTC) {
254        next_found = true;
255      }
256    }
257    assert(prevFC == NULL || prev_found, "Chunk was lost from list");
258    assert(nextTC == NULL || next_found, "Chunk was lost from list");
259    assert(retTL->parent() == NULL ||
260           retTL == retTL->parent()->left() ||
261           retTL == retTL->parent()->right(),
262           "list is inconsistent");
263  )
264  retTL->decrement_count();
265
266  assert(tc->is_free(), "Should still be a free chunk");
267  assert(retTL->head() == NULL || retTL->head()->prev() == NULL,
268    "list invariant");
269  assert(retTL->tail() == NULL || retTL->tail()->next() == NULL,
270    "list invariant");
271  return retTL;
272}
273
274template <class Chunk_t, class FreeList_t>
275void TreeList<Chunk_t, FreeList_t>::return_chunk_at_tail(TreeChunk<Chunk_t, FreeList_t>* chunk) {
276  assert(chunk != NULL, "returning NULL chunk");
277  assert(chunk->list() == this, "list should be set for chunk");
278  assert(tail() != NULL, "The tree list is embedded in the first chunk");
279  // which means that the list can never be empty.
280  assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
281  assert(head() == NULL || head()->prev() == NULL, "list invariant");
282  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
283
284  Chunk_t* fc = tail();
285  fc->link_after(chunk);
286  this->link_tail(chunk);
287
288  assert(!tail() || size() == tail()->size(), "Wrong sized chunk in list");
289  FreeList_t::increment_count();
290  debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
291  assert(head() == NULL || head()->prev() == NULL, "list invariant");
292  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
293}
294
295// Add this chunk at the head of the list.  "At the head of the list"
296// is defined to be after the chunk pointer to by head().  This is
297// because the TreeList<Chunk_t, FreeList_t> is embedded in the first TreeChunk<Chunk_t, FreeList_t> in the
298// list.  See the definition of TreeChunk<Chunk_t, FreeList_t>.
299template <class Chunk_t, class FreeList_t>
300void TreeList<Chunk_t, FreeList_t>::return_chunk_at_head(TreeChunk<Chunk_t, FreeList_t>* chunk) {
301  assert(chunk->list() == this, "list should be set for chunk");
302  assert(head() != NULL, "The tree list is embedded in the first chunk");
303  assert(chunk != NULL, "returning NULL chunk");
304  assert(!this->verify_chunk_in_free_list(chunk), "Double entry");
305  assert(head() == NULL || head()->prev() == NULL, "list invariant");
306  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
307
308  Chunk_t* fc = head()->next();
309  if (fc != NULL) {
310    chunk->link_after(fc);
311  } else {
312    assert(tail() == NULL, "List is inconsistent");
313    this->link_tail(chunk);
314  }
315  head()->link_after(chunk);
316  assert(!head() || size() == head()->size(), "Wrong sized chunk in list");
317  FreeList_t::increment_count();
318  debug_only(this->increment_returned_bytes_by(chunk->size()*sizeof(HeapWord));)
319  assert(head() == NULL || head()->prev() == NULL, "list invariant");
320  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
321}
322
323template <class Chunk_t, class FreeList_t>
324void TreeChunk<Chunk_t, FreeList_t>::assert_is_mangled() const {
325  assert((ZapUnusedHeapArea &&
326          SpaceMangler::is_mangled((HeapWord*) Chunk_t::size_addr()) &&
327          SpaceMangler::is_mangled((HeapWord*) Chunk_t::prev_addr()) &&
328          SpaceMangler::is_mangled((HeapWord*) Chunk_t::next_addr())) ||
329          (size() == 0 && prev() == NULL && next() == NULL),
330    "Space should be clear or mangled");
331}
332
333template <class Chunk_t, class FreeList_t>
334TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::head_as_TreeChunk() {
335  assert(head() == NULL || (TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head())->list() == this),
336    "Wrong type of chunk?");
337  return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(head());
338}
339
340template <class Chunk_t, class FreeList_t>
341TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::first_available() {
342  assert(head() != NULL, "The head of the list cannot be NULL");
343  Chunk_t* fc = head()->next();
344  TreeChunk<Chunk_t, FreeList_t>* retTC;
345  if (fc == NULL) {
346    retTC = head_as_TreeChunk();
347  } else {
348    retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);
349  }
350  assert(retTC->list() == this, "Wrong type of chunk.");
351  return retTC;
352}
353
354// Returns the block with the largest heap address amongst
355// those in the list for this size; potentially slow and expensive,
356// use with caution!
357template <class Chunk_t, class FreeList_t>
358TreeChunk<Chunk_t, FreeList_t>* TreeList<Chunk_t, FreeList_t>::largest_address() {
359  assert(head() != NULL, "The head of the list cannot be NULL");
360  Chunk_t* fc = head()->next();
361  TreeChunk<Chunk_t, FreeList_t>* retTC;
362  if (fc == NULL) {
363    retTC = head_as_TreeChunk();
364  } else {
365    // walk down the list and return the one with the highest
366    // heap address among chunks of this size.
367    Chunk_t* last = fc;
368    while (fc->next() != NULL) {
369      if ((HeapWord*)last < (HeapWord*)fc) {
370        last = fc;
371      }
372      fc = fc->next();
373    }
374    retTC = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(last);
375  }
376  assert(retTC->list() == this, "Wrong type of chunk.");
377  return retTC;
378}
379
380template <class Chunk_t, class FreeList_t>
381BinaryTreeDictionary<Chunk_t, FreeList_t>::BinaryTreeDictionary(MemRegion mr) {
382  assert((mr.byte_size() > min_size()), "minimum chunk size");
383
384  reset(mr);
385  assert(root()->left() == NULL, "reset check failed");
386  assert(root()->right() == NULL, "reset check failed");
387  assert(root()->head()->next() == NULL, "reset check failed");
388  assert(root()->head()->prev() == NULL, "reset check failed");
389  assert(total_size() == root()->size(), "reset check failed");
390  assert(total_free_blocks() == 1, "reset check failed");
391}
392
393template <class Chunk_t, class FreeList_t>
394void BinaryTreeDictionary<Chunk_t, FreeList_t>::inc_total_size(size_t inc) {
395  _total_size = _total_size + inc;
396}
397
398template <class Chunk_t, class FreeList_t>
399void BinaryTreeDictionary<Chunk_t, FreeList_t>::dec_total_size(size_t dec) {
400  _total_size = _total_size - dec;
401}
402
403template <class Chunk_t, class FreeList_t>
404void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(MemRegion mr) {
405  assert((mr.byte_size() > min_size()), "minimum chunk size");
406  set_root(TreeList<Chunk_t, FreeList_t>::as_TreeList(mr.start(), mr.word_size()));
407  set_total_size(mr.word_size());
408  set_total_free_blocks(1);
409}
410
411template <class Chunk_t, class FreeList_t>
412void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset(HeapWord* addr, size_t byte_size) {
413  MemRegion mr(addr, heap_word_size(byte_size));
414  reset(mr);
415}
416
417template <class Chunk_t, class FreeList_t>
418void BinaryTreeDictionary<Chunk_t, FreeList_t>::reset() {
419  set_root(NULL);
420  set_total_size(0);
421  set_total_free_blocks(0);
422}
423
424// Get a free block of size at least size from tree, or NULL.
425template <class Chunk_t, class FreeList_t>
426TreeChunk<Chunk_t, FreeList_t>*
427BinaryTreeDictionary<Chunk_t, FreeList_t>::get_chunk_from_tree(
428                              size_t size,
429                              enum FreeBlockDictionary<Chunk_t>::Dither dither)
430{
431  TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
432  TreeChunk<Chunk_t, FreeList_t>* retTC = NULL;
433
434  assert((size >= min_size()), "minimum chunk size");
435  if (FLSVerifyDictionary) {
436    verify_tree();
437  }
438  // starting at the root, work downwards trying to find match.
439  // Remember the last node of size too great or too small.
440  for (prevTL = curTL = root(); curTL != NULL;) {
441    if (curTL->size() == size) {        // exact match
442      break;
443    }
444    prevTL = curTL;
445    if (curTL->size() < size) {        // proceed to right sub-tree
446      curTL = curTL->right();
447    } else {                           // proceed to left sub-tree
448      assert(curTL->size() > size, "size inconsistency");
449      curTL = curTL->left();
450    }
451  }
452  if (curTL == NULL) { // couldn't find exact match
453
454    if (dither == FreeBlockDictionary<Chunk_t>::exactly) return NULL;
455
456    // try and find the next larger size by walking back up the search path
457    for (curTL = prevTL; curTL != NULL;) {
458      if (curTL->size() >= size) break;
459      else curTL = curTL->parent();
460    }
461    assert(curTL == NULL || curTL->count() > 0,
462      "An empty list should not be in the tree");
463  }
464  if (curTL != NULL) {
465    assert(curTL->size() >= size, "size inconsistency");
466
467    curTL = curTL->get_better_list(this);
468
469    retTC = curTL->first_available();
470    assert((retTC != NULL) && (curTL->count() > 0),
471      "A list in the binary tree should not be NULL");
472    assert(retTC->size() >= size,
473      "A chunk of the wrong size was found");
474    remove_chunk_from_tree(retTC);
475    assert(retTC->is_free(), "Header is not marked correctly");
476  }
477
478  if (FLSVerifyDictionary) {
479    verify();
480  }
481  return retTC;
482}
483
484template <class Chunk_t, class FreeList_t>
485TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_list(size_t size) const {
486  TreeList<Chunk_t, FreeList_t>* curTL;
487  for (curTL = root(); curTL != NULL;) {
488    if (curTL->size() == size) {        // exact match
489      break;
490    }
491
492    if (curTL->size() < size) {        // proceed to right sub-tree
493      curTL = curTL->right();
494    } else {                           // proceed to left sub-tree
495      assert(curTL->size() > size, "size inconsistency");
496      curTL = curTL->left();
497    }
498  }
499  return curTL;
500}
501
502
503template <class Chunk_t, class FreeList_t>
504bool BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_chunk_in_free_list(Chunk_t* tc) const {
505  size_t size = tc->size();
506  TreeList<Chunk_t, FreeList_t>* tl = find_list(size);
507  if (tl == NULL) {
508    return false;
509  } else {
510    return tl->verify_chunk_in_free_list(tc);
511  }
512}
513
514template <class Chunk_t, class FreeList_t>
515Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_largest_dict() const {
516  TreeList<Chunk_t, FreeList_t> *curTL = root();
517  if (curTL != NULL) {
518    while(curTL->right() != NULL) curTL = curTL->right();
519    return curTL->largest_address();
520  } else {
521    return NULL;
522  }
523}
524
525// Remove the current chunk from the tree.  If it is not the last
526// chunk in a list on a tree node, just unlink it.
527// If it is the last chunk in the list (the next link is NULL),
528// remove the node and repair the tree.
529template <class Chunk_t, class FreeList_t>
530TreeChunk<Chunk_t, FreeList_t>*
531BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_chunk_from_tree(TreeChunk<Chunk_t, FreeList_t>* tc) {
532  assert(tc != NULL, "Should not call with a NULL chunk");
533  assert(tc->is_free(), "Header is not marked correctly");
534
535  TreeList<Chunk_t, FreeList_t> *newTL, *parentTL;
536  TreeChunk<Chunk_t, FreeList_t>* retTC;
537  TreeList<Chunk_t, FreeList_t>* tl = tc->list();
538  debug_only(
539    bool removing_only_chunk = false;
540    if (tl == _root) {
541      if ((_root->left() == NULL) && (_root->right() == NULL)) {
542        if (_root->count() == 1) {
543          assert(_root->head() == tc, "Should only be this one chunk");
544          removing_only_chunk = true;
545        }
546      }
547    }
548  )
549  assert(tl != NULL, "List should be set");
550  assert(tl->parent() == NULL || tl == tl->parent()->left() ||
551         tl == tl->parent()->right(), "list is inconsistent");
552
553  bool complicated_splice = false;
554
555  retTC = tc;
556  // Removing this chunk can have the side effect of changing the node
557  // (TreeList<Chunk_t, FreeList_t>*) in the tree.  If the node is the root, update it.
558  TreeList<Chunk_t, FreeList_t>* replacementTL = tl->remove_chunk_replace_if_needed(tc);
559  assert(tc->is_free(), "Chunk should still be free");
560  assert(replacementTL->parent() == NULL ||
561         replacementTL == replacementTL->parent()->left() ||
562         replacementTL == replacementTL->parent()->right(),
563         "list is inconsistent");
564  if (tl == root()) {
565    assert(replacementTL->parent() == NULL, "Incorrectly replacing root");
566    set_root(replacementTL);
567  }
568#ifdef ASSERT
569    if (tl != replacementTL) {
570      assert(replacementTL->head() != NULL,
571        "If the tree list was replaced, it should not be a NULL list");
572      TreeList<Chunk_t, FreeList_t>* rhl = replacementTL->head_as_TreeChunk()->list();
573      TreeList<Chunk_t, FreeList_t>* rtl =
574        TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(replacementTL->tail())->list();
575      assert(rhl == replacementTL, "Broken head");
576      assert(rtl == replacementTL, "Broken tail");
577      assert(replacementTL->size() == tc->size(),  "Broken size");
578    }
579#endif
580
581  // Does the tree need to be repaired?
582  if (replacementTL->count() == 0) {
583    assert(replacementTL->head() == NULL &&
584           replacementTL->tail() == NULL, "list count is incorrect");
585    // Find the replacement node for the (soon to be empty) node being removed.
586    // if we have a single (or no) child, splice child in our stead
587    if (replacementTL->left() == NULL) {
588      // left is NULL so pick right.  right may also be NULL.
589      newTL = replacementTL->right();
590      debug_only(replacementTL->clear_right();)
591    } else if (replacementTL->right() == NULL) {
592      // right is NULL
593      newTL = replacementTL->left();
594      debug_only(replacementTL->clear_left();)
595    } else {  // we have both children, so, by patriarchal convention,
596              // my replacement is least node in right sub-tree
597      complicated_splice = true;
598      newTL = remove_tree_minimum(replacementTL->right());
599      assert(newTL != NULL && newTL->left() == NULL &&
600             newTL->right() == NULL, "sub-tree minimum exists");
601    }
602    // newTL is the replacement for the (soon to be empty) node.
603    // newTL may be NULL.
604    // should verify; we just cleanly excised our replacement
605    if (FLSVerifyDictionary) {
606      verify_tree();
607    }
608    // first make newTL my parent's child
609    if ((parentTL = replacementTL->parent()) == NULL) {
610      // newTL should be root
611      assert(tl == root(), "Incorrectly replacing root");
612      set_root(newTL);
613      if (newTL != NULL) {
614        newTL->clear_parent();
615      }
616    } else if (parentTL->right() == replacementTL) {
617      // replacementTL is a right child
618      parentTL->set_right(newTL);
619    } else {                                // replacementTL is a left child
620      assert(parentTL->left() == replacementTL, "should be left child");
621      parentTL->set_left(newTL);
622    }
623    debug_only(replacementTL->clear_parent();)
624    if (complicated_splice) {  // we need newTL to get replacementTL's
625                              // two children
626      assert(newTL != NULL &&
627             newTL->left() == NULL && newTL->right() == NULL,
628            "newTL should not have encumbrances from the past");
629      // we'd like to assert as below:
630      // assert(replacementTL->left() != NULL && replacementTL->right() != NULL,
631      //       "else !complicated_splice");
632      // ... however, the above assertion is too strong because we aren't
633      // guaranteed that replacementTL->right() is still NULL.
634      // Recall that we removed
635      // the right sub-tree minimum from replacementTL.
636      // That may well have been its right
637      // child! So we'll just assert half of the above:
638      assert(replacementTL->left() != NULL, "else !complicated_splice");
639      newTL->set_left(replacementTL->left());
640      newTL->set_right(replacementTL->right());
641      debug_only(
642        replacementTL->clear_right();
643        replacementTL->clear_left();
644      )
645    }
646    assert(replacementTL->right() == NULL &&
647           replacementTL->left() == NULL &&
648           replacementTL->parent() == NULL,
649        "delete without encumbrances");
650  }
651
652  assert(total_size() >= retTC->size(), "Incorrect total size");
653  dec_total_size(retTC->size());     // size book-keeping
654  assert(total_free_blocks() > 0, "Incorrect total count");
655  set_total_free_blocks(total_free_blocks() - 1);
656
657  assert(retTC != NULL, "null chunk?");
658  assert(retTC->prev() == NULL && retTC->next() == NULL,
659         "should return without encumbrances");
660  if (FLSVerifyDictionary) {
661    verify_tree();
662  }
663  assert(!removing_only_chunk || _root == NULL, "root should be NULL");
664  return TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(retTC);
665}
666
667// Remove the leftmost node (lm) in the tree and return it.
668// If lm has a right child, link it to the left node of
669// the parent of lm.
670template <class Chunk_t, class FreeList_t>
671TreeList<Chunk_t, FreeList_t>* BinaryTreeDictionary<Chunk_t, FreeList_t>::remove_tree_minimum(TreeList<Chunk_t, FreeList_t>* tl) {
672  assert(tl != NULL && tl->parent() != NULL, "really need a proper sub-tree");
673  // locate the subtree minimum by walking down left branches
674  TreeList<Chunk_t, FreeList_t>* curTL = tl;
675  for (; curTL->left() != NULL; curTL = curTL->left());
676  // obviously curTL now has at most one child, a right child
677  if (curTL != root()) {  // Should this test just be removed?
678    TreeList<Chunk_t, FreeList_t>* parentTL = curTL->parent();
679    if (parentTL->left() == curTL) { // curTL is a left child
680      parentTL->set_left(curTL->right());
681    } else {
682      // If the list tl has no left child, then curTL may be
683      // the right child of parentTL.
684      assert(parentTL->right() == curTL, "should be a right child");
685      parentTL->set_right(curTL->right());
686    }
687  } else {
688    // The only use of this method would not pass the root of the
689    // tree (as indicated by the assertion above that the tree list
690    // has a parent) but the specification does not explicitly exclude the
691    // passing of the root so accommodate it.
692    set_root(NULL);
693  }
694  debug_only(
695    curTL->clear_parent();  // Test if this needs to be cleared
696    curTL->clear_right();    // recall, above, left child is already null
697  )
698  // we just excised a (non-root) node, we should still verify all tree invariants
699  if (FLSVerifyDictionary) {
700    verify_tree();
701  }
702  return curTL;
703}
704
705template <class Chunk_t, class FreeList_t>
706void BinaryTreeDictionary<Chunk_t, FreeList_t>::insert_chunk_in_tree(Chunk_t* fc) {
707  TreeList<Chunk_t, FreeList_t> *curTL, *prevTL;
708  size_t size = fc->size();
709
710  assert((size >= min_size()),
711         SIZE_FORMAT " is too small to be a TreeChunk<Chunk_t, FreeList_t> " SIZE_FORMAT,
712         size, min_size());
713  if (FLSVerifyDictionary) {
714    verify_tree();
715  }
716
717  fc->clear_next();
718  fc->link_prev(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_t, FreeList_t>* tc = TreeChunk<Chunk_t, FreeList_t>::as_TreeChunk(fc);
733  // This chunk is being returned to the binary tree.  Its embedded
734  // TreeList<Chunk_t, FreeList_t> should be unused at this point.
735  tc->initialize();
736  if (curTL != NULL) {          // exact match
737    tc->set_list(curTL);
738    curTL->return_chunk_at_tail(tc);
739  } else {                     // need a new node in tree
740    tc->clear_next();
741    tc->link_prev(NULL);
742    TreeList<Chunk_t, FreeList_t>* newTL = TreeList<Chunk_t, FreeList_t>::as_TreeList(tc);
743    assert(((TreeChunk<Chunk_t, FreeList_t>*)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->set_right(newTL);
752      } else {                       // am left child
753        assert(prevTL->size() > size && prevTL->left() == NULL, "cpt pt inv");
754        prevTL->set_left(newTL);
755      }
756    }
757  }
758  assert(tc->list() != NULL, "Tree list should be set");
759
760  inc_total_size(size);
761  // Method 'total_size_in_tree' 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 || total_size_in_tree(root()) == total_size(), "_total_size inconsistency");
765  set_total_free_blocks(total_free_blocks() + 1);
766  if (FLSVerifyDictionary) {
767    verify_tree();
768  }
769}
770
771template <class Chunk_t, class FreeList_t>
772size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::max_chunk_size() const {
773  FreeBlockDictionary<Chunk_t>::verify_par_locked();
774  TreeList<Chunk_t, FreeList_t>* tc = root();
775  if (tc == NULL) return 0;
776  for (; tc->right() != NULL; tc = tc->right());
777  return tc->size();
778}
779
780template <class Chunk_t, class FreeList_t>
781size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_list_length(TreeList<Chunk_t, FreeList_t>* tl) const {
782  size_t res;
783  res = tl->count();
784#ifdef ASSERT
785  size_t cnt;
786  Chunk_t* 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
793template <class Chunk_t, class FreeList_t>
794size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_size_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
795  if (tl == NULL)
796    return 0;
797  return (tl->size() * total_list_length(tl)) +
798         total_size_in_tree(tl->left())    +
799         total_size_in_tree(tl->right());
800}
801
802template <class Chunk_t, class FreeList_t>
803double BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_of_squared_block_sizes(TreeList<Chunk_t, FreeList_t>* const tl) const {
804  if (tl == NULL) {
805    return 0.0;
806  }
807  double size = (double)(tl->size());
808  double curr = size * size * total_list_length(tl);
809  curr += sum_of_squared_block_sizes(tl->left());
810  curr += sum_of_squared_block_sizes(tl->right());
811  return curr;
812}
813
814template <class Chunk_t, class FreeList_t>
815size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_free_blocks_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
816  if (tl == NULL)
817    return 0;
818  return total_list_length(tl) +
819         total_free_blocks_in_tree(tl->left()) +
820         total_free_blocks_in_tree(tl->right());
821}
822
823template <class Chunk_t, class FreeList_t>
824size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::num_free_blocks() const {
825  assert(total_free_blocks_in_tree(root()) == total_free_blocks(),
826         "_total_free_blocks inconsistency");
827  return total_free_blocks();
828}
829
830template <class Chunk_t, class FreeList_t>
831size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
832  if (tl == NULL)
833    return 0;
834  return 1 + MAX2(tree_height_helper(tl->left()),
835                  tree_height_helper(tl->right()));
836}
837
838template <class Chunk_t, class FreeList_t>
839size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::tree_height() const {
840  return tree_height_helper(root());
841}
842
843template <class Chunk_t, class FreeList_t>
844size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
845  if (tl == NULL) {
846    return 0;
847  }
848  return 1 + total_nodes_helper(tl->left()) +
849    total_nodes_helper(tl->right());
850}
851
852template <class Chunk_t, class FreeList_t>
853size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_nodes_in_tree(TreeList<Chunk_t, FreeList_t>* tl) const {
854  return total_nodes_helper(root());
855}
856
857template <class Chunk_t, class FreeList_t>
858void BinaryTreeDictionary<Chunk_t, FreeList_t>::dict_census_update(size_t size, bool split, bool birth){}
859
860#if INCLUDE_ALL_GCS
861template <>
862void AFLBinaryTreeDictionary::dict_census_update(size_t size, bool split, bool birth) {
863  TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >* nd = find_list(size);
864  if (nd) {
865    if (split) {
866      if (birth) {
867        nd->increment_split_births();
868        nd->increment_surplus();
869      }  else {
870        nd->increment_split_deaths();
871        nd->decrement_surplus();
872      }
873    } else {
874      if (birth) {
875        nd->increment_coal_births();
876        nd->increment_surplus();
877      } else {
878        nd->increment_coal_deaths();
879        nd->decrement_surplus();
880      }
881    }
882  }
883  // A list for this size may not be found (nd == 0) if
884  //   This is a death where the appropriate list is now
885  //     empty and has been removed from the list.
886  //   This is a birth associated with a LinAB.  The chunk
887  //     for the LinAB is not in the dictionary.
888}
889#endif // INCLUDE_ALL_GCS
890
891template <class Chunk_t, class FreeList_t>
892bool BinaryTreeDictionary<Chunk_t, FreeList_t>::coal_dict_over_populated(size_t size) {
893  // For the general type of freelists, encourage coalescing by
894  // returning true.
895  return true;
896}
897
898#if INCLUDE_ALL_GCS
899template <>
900bool AFLBinaryTreeDictionary::coal_dict_over_populated(size_t size) {
901  if (FLSAlwaysCoalesceLarge) return true;
902
903  TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >* list_of_size = find_list(size);
904  // None of requested size implies overpopulated.
905  return list_of_size == NULL || list_of_size->coal_desired() <= 0 ||
906         list_of_size->count() > list_of_size->coal_desired();
907}
908#endif // INCLUDE_ALL_GCS
909
910// Closures for walking the binary tree.
911//   do_list() walks the free list in a node applying the closure
912//     to each free chunk in the list
913//   do_tree() walks the nodes in the binary tree applying do_list()
914//     to each list at each node.
915
916template <class Chunk_t, class FreeList_t>
917class TreeCensusClosure : public StackObj {
918 protected:
919  virtual void do_list(FreeList_t* fl) = 0;
920 public:
921  virtual void do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
922};
923
924template <class Chunk_t, class FreeList_t>
925class AscendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
926 public:
927  void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
928    if (tl != NULL) {
929      do_tree(tl->left());
930      this->do_list(tl);
931      do_tree(tl->right());
932    }
933  }
934};
935
936template <class Chunk_t, class FreeList_t>
937class DescendTreeCensusClosure : public TreeCensusClosure<Chunk_t, FreeList_t> {
938 public:
939  void do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
940    if (tl != NULL) {
941      do_tree(tl->right());
942      this->do_list(tl);
943      do_tree(tl->left());
944    }
945  }
946};
947
948// For each list in the tree, calculate the desired, desired
949// coalesce, count before sweep, and surplus before sweep.
950template <class Chunk_t, class FreeList_t>
951class BeginSweepClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
952  double _percentage;
953  float _inter_sweep_current;
954  float _inter_sweep_estimate;
955  float _intra_sweep_estimate;
956
957 public:
958  BeginSweepClosure(double p, float inter_sweep_current,
959                              float inter_sweep_estimate,
960                              float intra_sweep_estimate) :
961   _percentage(p),
962   _inter_sweep_current(inter_sweep_current),
963   _inter_sweep_estimate(inter_sweep_estimate),
964   _intra_sweep_estimate(intra_sweep_estimate) { }
965
966  void do_list(FreeList<Chunk_t>* fl) {}
967
968#if INCLUDE_ALL_GCS
969  void do_list(AdaptiveFreeList<Chunk_t>* fl) {
970    double coalSurplusPercent = _percentage;
971    fl->compute_desired(_inter_sweep_current, _inter_sweep_estimate, _intra_sweep_estimate);
972    fl->set_coal_desired((ssize_t)((double)fl->desired() * coalSurplusPercent));
973    fl->set_before_sweep(fl->count());
974    fl->set_bfr_surp(fl->surplus());
975  }
976#endif // INCLUDE_ALL_GCS
977};
978
979// Used to search the tree until a condition is met.
980// Similar to TreeCensusClosure but searches the
981// tree and returns promptly when found.
982
983template <class Chunk_t, class FreeList_t>
984class TreeSearchClosure : public StackObj {
985 protected:
986  virtual bool do_list(FreeList_t* fl) = 0;
987 public:
988  virtual bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) = 0;
989};
990
991#if 0 //  Don't need this yet but here for symmetry.
992template <class Chunk_t, class FreeList_t>
993class AscendTreeSearchClosure : public TreeSearchClosure<Chunk_t> {
994 public:
995  bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
996    if (tl != NULL) {
997      if (do_tree(tl->left())) return true;
998      if (do_list(tl)) return true;
999      if (do_tree(tl->right())) return true;
1000    }
1001    return false;
1002  }
1003};
1004#endif
1005
1006template <class Chunk_t, class FreeList_t>
1007class DescendTreeSearchClosure : public TreeSearchClosure<Chunk_t, FreeList_t> {
1008 public:
1009  bool do_tree(TreeList<Chunk_t, FreeList_t>* tl) {
1010    if (tl != NULL) {
1011      if (do_tree(tl->right())) return true;
1012      if (this->do_list(tl)) return true;
1013      if (do_tree(tl->left())) return true;
1014    }
1015    return false;
1016  }
1017};
1018
1019// Searches the tree for a chunk that ends at the
1020// specified address.
1021template <class Chunk_t, class FreeList_t>
1022class EndTreeSearchClosure : public DescendTreeSearchClosure<Chunk_t, FreeList_t> {
1023  HeapWord* _target;
1024  Chunk_t* _found;
1025
1026 public:
1027  EndTreeSearchClosure(HeapWord* target) : _target(target), _found(NULL) {}
1028  bool do_list(FreeList_t* fl) {
1029    Chunk_t* item = fl->head();
1030    while (item != NULL) {
1031      if (item->end() == (uintptr_t*) _target) {
1032        _found = item;
1033        return true;
1034      }
1035      item = item->next();
1036    }
1037    return false;
1038  }
1039  Chunk_t* found() { return _found; }
1040};
1041
1042template <class Chunk_t, class FreeList_t>
1043Chunk_t* BinaryTreeDictionary<Chunk_t, FreeList_t>::find_chunk_ends_at(HeapWord* target) const {
1044  EndTreeSearchClosure<Chunk_t, FreeList_t> etsc(target);
1045  bool found_target = etsc.do_tree(root());
1046  assert(found_target || etsc.found() == NULL, "Consistency check");
1047  assert(!found_target || etsc.found() != NULL, "Consistency check");
1048  return etsc.found();
1049}
1050
1051template <class Chunk_t, class FreeList_t>
1052void BinaryTreeDictionary<Chunk_t, FreeList_t>::begin_sweep_dict_census(double coalSurplusPercent,
1053  float inter_sweep_current, float inter_sweep_estimate, float intra_sweep_estimate) {
1054  BeginSweepClosure<Chunk_t, FreeList_t> bsc(coalSurplusPercent, inter_sweep_current,
1055                                            inter_sweep_estimate,
1056                                            intra_sweep_estimate);
1057  bsc.do_tree(root());
1058}
1059
1060// Closures and methods for calculating total bytes returned to the
1061// free lists in the tree.
1062#ifndef PRODUCT
1063template <class Chunk_t, class FreeList_t>
1064class InitializeDictReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1065   public:
1066  void do_list(FreeList_t* fl) {
1067    fl->set_returned_bytes(0);
1068  }
1069};
1070
1071template <class Chunk_t, class FreeList_t>
1072void BinaryTreeDictionary<Chunk_t, FreeList_t>::initialize_dict_returned_bytes() {
1073  InitializeDictReturnedBytesClosure<Chunk_t, FreeList_t> idrb;
1074  idrb.do_tree(root());
1075}
1076
1077template <class Chunk_t, class FreeList_t>
1078class ReturnedBytesClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1079  size_t _dict_returned_bytes;
1080 public:
1081  ReturnedBytesClosure() { _dict_returned_bytes = 0; }
1082  void do_list(FreeList_t* fl) {
1083    _dict_returned_bytes += fl->returned_bytes();
1084  }
1085  size_t dict_returned_bytes() { return _dict_returned_bytes; }
1086};
1087
1088template <class Chunk_t, class FreeList_t>
1089size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::sum_dict_returned_bytes() {
1090  ReturnedBytesClosure<Chunk_t, FreeList_t> rbc;
1091  rbc.do_tree(root());
1092
1093  return rbc.dict_returned_bytes();
1094}
1095
1096// Count the number of entries in the tree.
1097template <class Chunk_t, class FreeList_t>
1098class treeCountClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
1099 public:
1100  uint count;
1101  treeCountClosure(uint c) { count = c; }
1102  void do_list(FreeList_t* fl) {
1103    count++;
1104  }
1105};
1106
1107template <class Chunk_t, class FreeList_t>
1108size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::total_count() {
1109  treeCountClosure<Chunk_t, FreeList_t> ctc(0);
1110  ctc.do_tree(root());
1111  return ctc.count;
1112}
1113#endif // PRODUCT
1114
1115// Calculate surpluses for the lists in the tree.
1116template <class Chunk_t, class FreeList_t>
1117class setTreeSurplusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1118  double percentage;
1119 public:
1120  setTreeSurplusClosure(double v) { percentage = v; }
1121  void do_list(FreeList<Chunk_t>* fl) {}
1122
1123#if INCLUDE_ALL_GCS
1124  void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1125    double splitSurplusPercent = percentage;
1126    fl->set_surplus(fl->count() -
1127                   (ssize_t)((double)fl->desired() * splitSurplusPercent));
1128  }
1129#endif // INCLUDE_ALL_GCS
1130};
1131
1132template <class Chunk_t, class FreeList_t>
1133void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_surplus(double splitSurplusPercent) {
1134  setTreeSurplusClosure<Chunk_t, FreeList_t> sts(splitSurplusPercent);
1135  sts.do_tree(root());
1136}
1137
1138// Set hints for the lists in the tree.
1139template <class Chunk_t, class FreeList_t>
1140class setTreeHintsClosure : public DescendTreeCensusClosure<Chunk_t, FreeList_t> {
1141  size_t hint;
1142 public:
1143  setTreeHintsClosure(size_t v) { hint = v; }
1144  void do_list(FreeList<Chunk_t>* fl) {}
1145
1146#if INCLUDE_ALL_GCS
1147  void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1148    fl->set_hint(hint);
1149    assert(fl->hint() == 0 || fl->hint() > fl->size(),
1150      "Current hint is inconsistent");
1151    if (fl->surplus() > 0) {
1152      hint = fl->size();
1153    }
1154  }
1155#endif // INCLUDE_ALL_GCS
1156};
1157
1158template <class Chunk_t, class FreeList_t>
1159void BinaryTreeDictionary<Chunk_t, FreeList_t>::set_tree_hints(void) {
1160  setTreeHintsClosure<Chunk_t, FreeList_t> sth(0);
1161  sth.do_tree(root());
1162}
1163
1164// Save count before previous sweep and splits and coalesces.
1165template <class Chunk_t, class FreeList_t>
1166class clearTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1167  void do_list(FreeList<Chunk_t>* fl) {}
1168
1169#if INCLUDE_ALL_GCS
1170  void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1171    fl->set_prev_sweep(fl->count());
1172    fl->set_coal_births(0);
1173    fl->set_coal_deaths(0);
1174    fl->set_split_births(0);
1175    fl->set_split_deaths(0);
1176  }
1177#endif // INCLUDE_ALL_GCS
1178};
1179
1180template <class Chunk_t, class FreeList_t>
1181void BinaryTreeDictionary<Chunk_t, FreeList_t>::clear_tree_census(void) {
1182  clearTreeCensusClosure<Chunk_t, FreeList_t> ctc;
1183  ctc.do_tree(root());
1184}
1185
1186// Do reporting and post sweep clean up.
1187template <class Chunk_t, class FreeList_t>
1188void BinaryTreeDictionary<Chunk_t, FreeList_t>::end_sweep_dict_census(double splitSurplusPercent) {
1189  // Does walking the tree 3 times hurt?
1190  set_tree_surplus(splitSurplusPercent);
1191  set_tree_hints();
1192  if (PrintGC && Verbose) {
1193    report_statistics();
1194  }
1195  clear_tree_census();
1196}
1197
1198// Print summary statistics
1199template <class Chunk_t, class FreeList_t>
1200void BinaryTreeDictionary<Chunk_t, FreeList_t>::report_statistics() const {
1201  FreeBlockDictionary<Chunk_t>::verify_par_locked();
1202  gclog_or_tty->print("Statistics for BinaryTreeDictionary:\n"
1203         "------------------------------------\n");
1204  size_t total_size = total_chunk_size(debug_only(NULL));
1205  size_t    free_blocks = num_free_blocks();
1206  gclog_or_tty->print("Total Free Space: " SIZE_FORMAT "\n", total_size);
1207  gclog_or_tty->print("Max   Chunk Size: " SIZE_FORMAT "\n", max_chunk_size());
1208  gclog_or_tty->print("Number of Blocks: " SIZE_FORMAT "\n", free_blocks);
1209  if (free_blocks > 0) {
1210    gclog_or_tty->print("Av.  Block  Size: " SIZE_FORMAT "\n", total_size/free_blocks);
1211  }
1212  gclog_or_tty->print("Tree      Height: " SIZE_FORMAT "\n", tree_height());
1213}
1214
1215// Print census information - counts, births, deaths, etc.
1216// for each list in the tree.  Also print some summary
1217// information.
1218template <class Chunk_t, class FreeList_t>
1219class PrintTreeCensusClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1220  int _print_line;
1221  size_t _total_free;
1222  FreeList_t _total;
1223
1224 public:
1225  PrintTreeCensusClosure() {
1226    _print_line = 0;
1227    _total_free = 0;
1228  }
1229  FreeList_t* total() { return &_total; }
1230  size_t total_free() { return _total_free; }
1231  void do_list(FreeList<Chunk_t>* fl) {
1232    if (++_print_line >= 40) {
1233      FreeList_t::print_labels_on(gclog_or_tty, "size");
1234      _print_line = 0;
1235    }
1236    fl->print_on(gclog_or_tty);
1237    _total_free +=            fl->count()            * fl->size()        ;
1238    total()->set_count(      total()->count()       + fl->count()      );
1239  }
1240
1241#if INCLUDE_ALL_GCS
1242  void do_list(AdaptiveFreeList<Chunk_t>* fl) {
1243    if (++_print_line >= 40) {
1244      FreeList_t::print_labels_on(gclog_or_tty, "size");
1245      _print_line = 0;
1246    }
1247    fl->print_on(gclog_or_tty);
1248    _total_free +=           fl->count()             * fl->size()        ;
1249    total()->set_count(      total()->count()        + fl->count()      );
1250    total()->set_bfr_surp(   total()->bfr_surp()     + fl->bfr_surp()    );
1251    total()->set_surplus(    total()->split_deaths() + fl->surplus()    );
1252    total()->set_desired(    total()->desired()      + fl->desired()    );
1253    total()->set_prev_sweep(  total()->prev_sweep()   + fl->prev_sweep()  );
1254    total()->set_before_sweep(total()->before_sweep() + fl->before_sweep());
1255    total()->set_coal_births( total()->coal_births()  + fl->coal_births() );
1256    total()->set_coal_deaths( total()->coal_deaths()  + fl->coal_deaths() );
1257    total()->set_split_births(total()->split_births() + fl->split_births());
1258    total()->set_split_deaths(total()->split_deaths() + fl->split_deaths());
1259  }
1260#endif // INCLUDE_ALL_GCS
1261};
1262
1263template <class Chunk_t, class FreeList_t>
1264void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_dict_census(void) const {
1265
1266  gclog_or_tty->print("\nBinaryTree\n");
1267  FreeList_t::print_labels_on(gclog_or_tty, "size");
1268  PrintTreeCensusClosure<Chunk_t, FreeList_t> ptc;
1269  ptc.do_tree(root());
1270
1271  FreeList_t* total = ptc.total();
1272  FreeList_t::print_labels_on(gclog_or_tty, " ");
1273}
1274
1275#if INCLUDE_ALL_GCS
1276template <>
1277void AFLBinaryTreeDictionary::print_dict_census(void) const {
1278
1279  gclog_or_tty->print("\nBinaryTree\n");
1280  AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, "size");
1281  PrintTreeCensusClosure<FreeChunk, AdaptiveFreeList<FreeChunk> > ptc;
1282  ptc.do_tree(root());
1283
1284  AdaptiveFreeList<FreeChunk>* total = ptc.total();
1285  AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, " ");
1286  total->print_on(gclog_or_tty, "TOTAL\t");
1287  gclog_or_tty->print(
1288              "total_free(words): " SIZE_FORMAT_W(16)
1289              " growth: %8.5f  deficit: %8.5f\n",
1290              ptc.total_free(),
1291              (double)(total->split_births() + total->coal_births()
1292                     - total->split_deaths() - total->coal_deaths())
1293              /(total->prev_sweep() != 0 ? (double)total->prev_sweep() : 1.0),
1294             (double)(total->desired() - total->count())
1295             /(total->desired() != 0 ? (double)total->desired() : 1.0));
1296}
1297#endif // INCLUDE_ALL_GCS
1298
1299template <class Chunk_t, class FreeList_t>
1300class PrintFreeListsClosure : public AscendTreeCensusClosure<Chunk_t, FreeList_t> {
1301  outputStream* _st;
1302  int _print_line;
1303
1304 public:
1305  PrintFreeListsClosure(outputStream* st) {
1306    _st = st;
1307    _print_line = 0;
1308  }
1309  void do_list(FreeList_t* fl) {
1310    if (++_print_line >= 40) {
1311      FreeList_t::print_labels_on(_st, "size");
1312      _print_line = 0;
1313    }
1314    fl->print_on(gclog_or_tty);
1315    size_t sz = fl->size();
1316    for (Chunk_t* fc = fl->head(); fc != NULL;
1317         fc = fc->next()) {
1318      _st->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ")  %s",
1319                    p2i(fc), p2i((HeapWord*)fc + sz),
1320                    fc->cantCoalesce() ? "\t CC" : "");
1321    }
1322  }
1323};
1324
1325template <class Chunk_t, class FreeList_t>
1326void BinaryTreeDictionary<Chunk_t, FreeList_t>::print_free_lists(outputStream* st) const {
1327
1328  FreeList_t::print_labels_on(st, "size");
1329  PrintFreeListsClosure<Chunk_t, FreeList_t> pflc(st);
1330  pflc.do_tree(root());
1331}
1332
1333// Verify the following tree invariants:
1334// . _root has no parent
1335// . parent and child point to each other
1336// . each node's key correctly related to that of its child(ren)
1337template <class Chunk_t, class FreeList_t>
1338void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree() const {
1339  guarantee(root() == NULL || total_free_blocks() == 0 ||
1340    total_size() != 0, "_total_size shouldn't be 0?");
1341  guarantee(root() == NULL || root()->parent() == NULL, "_root shouldn't have parent");
1342  verify_tree_helper(root());
1343}
1344
1345template <class Chunk_t, class FreeList_t>
1346size_t BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_prev_free_ptrs(TreeList<Chunk_t, FreeList_t>* tl) {
1347  size_t ct = 0;
1348  for (Chunk_t* curFC = tl->head(); curFC != NULL; curFC = curFC->next()) {
1349    ct++;
1350    assert(curFC->prev() == NULL || curFC->prev()->is_free(),
1351      "Chunk should be free");
1352  }
1353  return ct;
1354}
1355
1356// Note: this helper is recursive rather than iterative, so use with
1357// caution on very deep trees; and watch out for stack overflow errors;
1358// In general, to be used only for debugging.
1359template <class Chunk_t, class FreeList_t>
1360void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify_tree_helper(TreeList<Chunk_t, FreeList_t>* tl) const {
1361  if (tl == NULL)
1362    return;
1363  guarantee(tl->size() != 0, "A list must has a size");
1364  guarantee(tl->left()  == NULL || tl->left()->parent()  == tl,
1365         "parent<-/->left");
1366  guarantee(tl->right() == NULL || tl->right()->parent() == tl,
1367         "parent<-/->right");;
1368  guarantee(tl->left() == NULL  || tl->left()->size()    <  tl->size(),
1369         "parent !> left");
1370  guarantee(tl->right() == NULL || tl->right()->size()   >  tl->size(),
1371         "parent !< left");
1372  guarantee(tl->head() == NULL || tl->head()->is_free(), "!Free");
1373  guarantee(tl->head() == NULL || tl->head_as_TreeChunk()->list() == tl,
1374    "list inconsistency");
1375  guarantee(tl->count() > 0 || (tl->head() == NULL && tl->tail() == NULL),
1376    "list count is inconsistent");
1377  guarantee(tl->count() > 1 || tl->head() == tl->tail(),
1378    "list is incorrectly constructed");
1379  size_t count = verify_prev_free_ptrs(tl);
1380  guarantee(count == (size_t)tl->count(), "Node count is incorrect");
1381  if (tl->head() != NULL) {
1382    tl->head_as_TreeChunk()->verify_tree_chunk_list();
1383  }
1384  verify_tree_helper(tl->left());
1385  verify_tree_helper(tl->right());
1386}
1387
1388template <class Chunk_t, class FreeList_t>
1389void BinaryTreeDictionary<Chunk_t, FreeList_t>::verify() const {
1390  verify_tree();
1391  guarantee(total_size() == total_size_in_tree(root()), "Total Size inconsistency");
1392}
1393
1394template class TreeList<Metablock, FreeList<Metablock> >;
1395template class BinaryTreeDictionary<Metablock, FreeList<Metablock> >;
1396template class TreeChunk<Metablock, FreeList<Metablock> >;
1397
1398template class TreeList<Metachunk, FreeList<Metachunk> >;
1399template class BinaryTreeDictionary<Metachunk, FreeList<Metachunk> >;
1400template class TreeChunk<Metachunk, FreeList<Metachunk> >;
1401
1402
1403#if INCLUDE_ALL_GCS
1404// Explicitly instantiate these types for FreeChunk.
1405template class TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >;
1406template class BinaryTreeDictionary<FreeChunk, AdaptiveFreeList<FreeChunk> >;
1407template class TreeChunk<FreeChunk, AdaptiveFreeList<FreeChunk> >;
1408
1409#endif // INCLUDE_ALL_GCS
1410