hotspot/src/share/vm/gc_implementation/concurrentMarkSweep/freeList.cpp
author xlu
Mon, 06 Apr 2009 15:47:39 -0700
changeset 2526 39a58a50be35
parent 670 ddf3e9583f2f
child 4574 b2d5b0975515
permissions -rw-r--r--
6699669: Hotspot server leaves synchronized block with monitor in bad state Summary: Remove usage of _highest_lock field in Thread so that is_lock_owned won't depend on the correct update of that field. Reviewed-by: never, dice, acorn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
670
ddf3e9583f2f 6719955: Update copyright year
xdono
parents: 185
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  All Rights Reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_freeList.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// Free list.  A FreeList is used to access a linked list of chunks
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// of space in the heap.  The head and tail are maintained so that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// items can be (as in the current implementation) added at the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// at the tail of the list and removed from the head of the list to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// maintain a FIFO queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
FreeList::FreeList() :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  _head(NULL), _tail(NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  , _protecting_lock(NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  _size         = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  _count        = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  _hint         = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  init_statistics();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
FreeList::FreeList(FreeChunk* fc) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  _head(fc), _tail(fc)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  , _protecting_lock(NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  _size         = fc->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  _count        = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  _hint         = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  init_statistics();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  _allocation_stats.set_returnedBytes(size() * HeapWordSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
FreeList::FreeList(HeapWord* addr, size_t size) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  _head((FreeChunk*) addr), _tail((FreeChunk*) addr)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  , _protecting_lock(NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  assert(size > sizeof(FreeChunk), "size is too small");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  head()->setSize(size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  _size         = size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  _count        = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  init_statistics();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  _allocation_stats.set_returnedBytes(_size * HeapWordSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
void FreeList::reset(size_t hint) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  set_count(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  set_head(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  set_tail(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  set_hint(hint);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
void FreeList::init_statistics() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  _allocation_stats.initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
FreeChunk* FreeList::getChunkAtHead() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  assert_proper_lock_protection();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  FreeChunk* fc = head();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  if (fc != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    FreeChunk* nextFC = fc->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    if (nextFC != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      // The chunk fc being removed has a "next".  Set the "next" to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      // "prev" of fc.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      nextFC->linkPrev(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    } else { // removed tail of list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
      link_tail(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    link_head(nextFC);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    decrement_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  return fc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
void FreeList::getFirstNChunksFromList(size_t n, FreeList* fl) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  assert_proper_lock_protection();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  assert(fl->count() == 0, "Precondition");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  if (count() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    int k = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    fl->set_head(head()); n--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    FreeChunk* tl = head();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    while (tl->next() != NULL && n > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      tl = tl->next(); n--; k++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    assert(tl != NULL, "Loop Inv.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    // First, fix up the list we took from.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    FreeChunk* new_head = tl->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    set_head(new_head);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    set_count(count() - k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    if (new_head == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
      set_tail(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
      new_head->linkPrev(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    // Now we can fix up the tail.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    tl->linkNext(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    // And return the result.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    fl->set_tail(tl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    fl->set_count(k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
// Remove this chunk from the list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
void FreeList::removeChunk(FreeChunk*fc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
   assert_proper_lock_protection();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
   assert(head() != NULL, "Remove from empty list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
   assert(fc != NULL, "Remove a NULL chunk");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
   assert(size() == fc->size(), "Wrong list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
   assert(head() == NULL || head()->prev() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
   FreeChunk* prevFC = fc->prev();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
   FreeChunk* nextFC = fc->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
   if (nextFC != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
     // The chunk fc being removed has a "next".  Set the "next" to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
     // "prev" of fc.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
     nextFC->linkPrev(prevFC);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
   } else { // removed tail of list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
     link_tail(prevFC);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
   if (prevFC == NULL) { // removed head of list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
     link_head(nextFC);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
     assert(nextFC == NULL || nextFC->prev() == NULL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
       "Prev of head should be NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
   } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
     prevFC->linkNext(nextFC);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
     assert(tail() != prevFC || prevFC->next() == NULL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
       "Next of tail should be NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
   decrement_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
#define TRAP_CODE 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
#if TRAP_CODE
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
   if (head() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
     guarantee(tail() == NULL, "INVARIANT");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
     guarantee(count() == 0, "INVARIANT");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
   // clear next and prev fields of fc, debug only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
   NOT_PRODUCT(
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
     fc->linkPrev(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
     fc->linkNext(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
   )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
   assert(fc->isFree(), "Should still be a free chunk");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
   assert(head() == NULL || head()->prev() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
   assert(head() == NULL || head()->size() == size(), "wrong item on list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
   assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
// Add this chunk at the head of the list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
void FreeList::returnChunkAtHead(FreeChunk* chunk, bool record_return) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  assert_proper_lock_protection();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  assert(chunk != NULL, "insert a NULL chunk");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  assert(size() == chunk->size(), "Wrong size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  FreeChunk* oldHead = head();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  assert(chunk != oldHead, "double insertion");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  chunk->linkAfter(oldHead);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  link_head(chunk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  if (oldHead == NULL) { // only chunk in list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    assert(tail() == NULL, "inconsistent FreeList");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    link_tail(chunk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  increment_count(); // of # of chunks in list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  DEBUG_ONLY(
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    if (record_return) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
      increment_returnedBytes_by(size()*HeapWordSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  assert(head() == NULL || head()->size() == size(), "wrong item on list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
void FreeList::returnChunkAtHead(FreeChunk* chunk) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  assert_proper_lock_protection();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  returnChunkAtHead(chunk, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
// Add this chunk at the tail of the list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
void FreeList::returnChunkAtTail(FreeChunk* chunk, bool record_return) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  assert_proper_lock_protection();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  assert(chunk != NULL, "insert a NULL chunk");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  assert(size() == chunk->size(), "wrong size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  FreeChunk* oldTail = tail();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  assert(chunk != oldTail, "double insertion");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  if (oldTail != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    oldTail->linkAfter(chunk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  } else { // only chunk in list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    assert(head() == NULL, "inconsistent FreeList");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    link_head(chunk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  link_tail(chunk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  increment_count();  // of # of chunks in list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  DEBUG_ONLY(
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    if (record_return) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      increment_returnedBytes_by(size()*HeapWordSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  assert(head() == NULL || head()->size() == size(), "wrong item on list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
void FreeList::returnChunkAtTail(FreeChunk* chunk) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  returnChunkAtTail(chunk, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
void FreeList::prepend(FreeList* fl) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  assert_proper_lock_protection();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  if (fl->count() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
    if (count() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
      set_head(fl->head());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
      set_tail(fl->tail());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
      set_count(fl->count());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
      // Both are non-empty.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
      FreeChunk* fl_tail = fl->tail();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
      FreeChunk* this_head = head();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
      assert(fl_tail->next() == NULL, "Well-formedness of fl");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
      fl_tail->linkNext(this_head);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
      this_head->linkPrev(fl_tail);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
      set_head(fl->head());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
      set_count(count() + fl->count());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    fl->set_head(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
    fl->set_tail(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
    fl->set_count(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
// verifyChunkInFreeLists() is used to verify that an item is in this free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
// It is used as a debugging aid.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
bool FreeList::verifyChunkInFreeLists(FreeChunk* fc) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  // This is an internal consistency check, not part of the check that the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  // chunk is in the free lists.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  guarantee(fc->size() == size(), "Wrong list is being searched");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  FreeChunk* curFC = head();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  while (curFC) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
    // This is an internal consistency check.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    guarantee(size() == curFC->size(), "Chunk is in wrong list.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    if (fc == curFC) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    curFC = curFC->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
void FreeList::assert_proper_lock_protection_work() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  if (_protecting_lock != NULL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
      SharedHeap::heap()->n_par_threads() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    // Should become an assert.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    guarantee(_protecting_lock->owned_by_self(), "FreeList RACE DETECTED");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
#endif
185
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   305
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   306
// Print the "label line" for free list stats.
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   307
void FreeList::print_labels_on(outputStream* st, const char* c) {
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   308
  st->print("%16s\t", c);
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   309
  st->print("%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   310
            "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "\n",
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   311
            "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep",
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   312
            "count",   "cBirths", "cDeaths", "sBirths", "sDeaths");
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   313
}
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   314
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   315
// Print the AllocationStats for the given free list. If the second argument
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   316
// to the call is a non-null string, it is printed in the first column;
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   317
// otherwise, if the argument is null (the default), then the size of the
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   318
// (free list) block is printed in the first column.
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   319
void FreeList::print_on(outputStream* st, const char* c) const {
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   320
  if (c != NULL) {
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   321
    st->print("%16s", c);
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   322
  } else {
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   323
    st->print(SIZE_FORMAT_W(16), size());
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   324
  }
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   325
  st->print("\t"
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   326
           SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t"
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   327
           SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\n",
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   328
           bfrSurp(),             surplus(),             desired(),             prevSweep(),           beforeSweep(),
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   329
           count(),               coalBirths(),          coalDeaths(),          splitBirths(),         splitDeaths());
cda2a1eb4be5 6668743: CMS: Consolidate block statistics reporting code
ysr
parents: 1
diff changeset
   330
}