1 /*
   2  * Copyright (c) 2001, 2017, 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 #ifndef SHARE_MEMORY_FREELIST_INLINE_HPP
  26 #define SHARE_MEMORY_FREELIST_INLINE_HPP
  27 
  28 #include "gc/shared/collectedHeap.hpp"
  29 #include "memory/freeList.hpp"
  30 #include "memory/metachunk.hpp"
  31 #include "runtime/globals.hpp"
  32 #include "runtime/mutex.hpp"
  33 #include "runtime/vmThread.hpp"
  34 #include "utilities/macros.hpp"
  35 
  36 // Free list.  A FreeList is used to access a linked list of chunks
  37 // of space in the heap.  The head and tail are maintained so that
  38 // items can be (as in the current implementation) added at the
  39 // at the tail of the list and removed from the head of the list to
  40 // maintain a FIFO queue.
  41 
  42 template <class Chunk>
  43 FreeList<Chunk>::FreeList() :
  44   _head(NULL), _tail(NULL)
  45 #ifdef ASSERT
  46   , _protecting_lock(NULL)
  47 #endif
  48 {
  49   _size         = 0;
  50   _count        = 0;
  51 }
  52 
  53 template <class Chunk>
  54 void FreeList<Chunk>::link_head(Chunk* v) {
  55   assert_proper_lock_protection();
  56   set_head(v);
  57   // If this method is not used (just set the head instead),
  58   // this check can be avoided.
  59   if (v != NULL) {
  60     v->link_prev(NULL);
  61   }
  62 }
  63 
  64 
  65 
  66 template <class Chunk>
  67 void FreeList<Chunk>::reset() {
  68   // Don't set the _size to 0 because this method is
  69   // used with a existing list that has a size but which has
  70   // been emptied.
  71   // Don't clear the _protecting_lock of an existing list.
  72   set_count(0);
  73   set_head(NULL);
  74   set_tail(NULL);
  75 }
  76 
  77 template <class Chunk>
  78 void FreeList<Chunk>::initialize() {
  79 #ifdef ASSERT
  80   // Needed early because it might be checked in other initializing code.
  81   set_protecting_lock(NULL);
  82 #endif
  83   reset();
  84   set_size(0);
  85 }
  86 
  87 template <class Chunk_t>
  88 Chunk_t* FreeList<Chunk_t>::get_chunk_at_head() {
  89   assert_proper_lock_protection();
  90   assert(head() == NULL || head()->prev() == NULL, "list invariant");
  91   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
  92   Chunk_t* fc = head();
  93   if (fc != NULL) {
  94     Chunk_t* nextFC = fc->next();
  95     if (nextFC != NULL) {
  96       // The chunk fc being removed has a "next".  Set the "next" to the
  97       // "prev" of fc.
  98       nextFC->link_prev(NULL);
  99     } else { // removed tail of list
 100       link_tail(NULL);
 101     }
 102     link_head(nextFC);
 103     decrement_count();
 104   }
 105   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 106   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 107   return fc;
 108 }
 109 
 110 
 111 template <class Chunk>
 112 void FreeList<Chunk>::getFirstNChunksFromList(size_t n, FreeList<Chunk>* fl) {
 113   assert_proper_lock_protection();
 114   assert(fl->count() == 0, "Precondition");
 115   if (count() > 0) {
 116     int k = 1;
 117     fl->set_head(head()); n--;
 118     Chunk* tl = head();
 119     while (tl->next() != NULL && n > 0) {
 120       tl = tl->next(); n--; k++;
 121     }
 122     assert(tl != NULL, "Loop Inv.");
 123 
 124     // First, fix up the list we took from.
 125     Chunk* new_head = tl->next();
 126     set_head(new_head);
 127     set_count(count() - k);
 128     if (new_head == NULL) {
 129       set_tail(NULL);
 130     } else {
 131       new_head->link_prev(NULL);
 132     }
 133     // Now we can fix up the tail.
 134     tl->link_next(NULL);
 135     // And return the result.
 136     fl->set_tail(tl);
 137     fl->set_count(k);
 138   }
 139 }
 140 
 141 // Remove this chunk from the list
 142 template <class Chunk>
 143 void FreeList<Chunk>::remove_chunk(Chunk*fc) {
 144    assert_proper_lock_protection();
 145    assert(head() != NULL, "Remove from empty list");
 146    assert(fc != NULL, "Remove a NULL chunk");
 147    assert(size() == fc->size(), "Wrong list");
 148    assert(head() == NULL || head()->prev() == NULL, "list invariant");
 149    assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 150 
 151    Chunk* prevFC = fc->prev();
 152    Chunk* nextFC = fc->next();
 153    if (nextFC != NULL) {
 154      // The chunk fc being removed has a "next".  Set the "next" to the
 155      // "prev" of fc.
 156      nextFC->link_prev(prevFC);
 157    } else { // removed tail of list
 158      link_tail(prevFC);
 159    }
 160    if (prevFC == NULL) { // removed head of list
 161      link_head(nextFC);
 162      assert(nextFC == NULL || nextFC->prev() == NULL,
 163        "Prev of head should be NULL");
 164    } else {
 165      prevFC->link_next(nextFC);
 166      assert(tail() != prevFC || prevFC->next() == NULL,
 167        "Next of tail should be NULL");
 168    }
 169    decrement_count();
 170    assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0,
 171           "H/T/C Inconsistency");
 172    // clear next and prev fields of fc, debug only
 173    NOT_PRODUCT(
 174      fc->link_prev(NULL);
 175      fc->link_next(NULL);
 176    )
 177    assert(fc->is_free(), "Should still be a free chunk");
 178    assert(head() == NULL || head()->prev() == NULL, "list invariant");
 179    assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 180    assert(head() == NULL || head()->size() == size(), "wrong item on list");
 181    assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
 182 }
 183 
 184 // Add this chunk at the head of the list.
 185 template <class Chunk>
 186 void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk, bool record_return) {
 187   assert_proper_lock_protection();
 188   assert(chunk != NULL, "insert a NULL chunk");
 189   assert(size() == chunk->size(), "Wrong size");
 190   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 191   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 192 
 193   Chunk* oldHead = head();
 194   assert(chunk != oldHead, "double insertion");
 195   chunk->link_after(oldHead);
 196   link_head(chunk);
 197   if (oldHead == NULL) { // only chunk in list
 198     assert(tail() == NULL, "inconsistent FreeList");
 199     link_tail(chunk);
 200   }
 201   increment_count(); // of # of chunks in list
 202   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 203   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 204   assert(head() == NULL || head()->size() == size(), "wrong item on list");
 205   assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
 206 }
 207 
 208 template <class Chunk>
 209 void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk) {
 210   assert_proper_lock_protection();
 211   return_chunk_at_head(chunk, true);
 212 }
 213 
 214 // Add this chunk at the tail of the list.
 215 template <class Chunk>
 216 void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk, bool record_return) {
 217   assert_proper_lock_protection();
 218   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 219   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 220   assert(chunk != NULL, "insert a NULL chunk");
 221   assert(size() == chunk->size(), "wrong size");
 222 
 223   Chunk* oldTail = tail();
 224   assert(chunk != oldTail, "double insertion");
 225   if (oldTail != NULL) {
 226     oldTail->link_after(chunk);
 227   } else { // only chunk in list
 228     assert(head() == NULL, "inconsistent FreeList");
 229     link_head(chunk);
 230   }
 231   link_tail(chunk);
 232   increment_count();  // of # of chunks in list
 233   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 234   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 235   assert(head() == NULL || head()->size() == size(), "wrong item on list");
 236   assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
 237 }
 238 
 239 template <class Chunk>
 240 void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk) {
 241   return_chunk_at_tail(chunk, true);
 242 }
 243 
 244 template <class Chunk>
 245 void FreeList<Chunk>::prepend(FreeList<Chunk>* fl) {
 246   assert_proper_lock_protection();
 247   if (fl->count() > 0) {
 248     if (count() == 0) {
 249       set_head(fl->head());
 250       set_tail(fl->tail());
 251       set_count(fl->count());
 252     } else {
 253       // Both are non-empty.
 254       Chunk* fl_tail = fl->tail();
 255       Chunk* this_head = head();
 256       assert(fl_tail->next() == NULL, "Well-formedness of fl");
 257       fl_tail->link_next(this_head);
 258       this_head->link_prev(fl_tail);
 259       set_head(fl->head());
 260       set_count(count() + fl->count());
 261     }
 262     fl->set_head(NULL);
 263     fl->set_tail(NULL);
 264     fl->set_count(0);
 265   }
 266 }
 267 
 268 // verify_chunk_in_free_lists() is used to verify that an item is in this free list.
 269 // It is used as a debugging aid.
 270 template <class Chunk>
 271 bool FreeList<Chunk>::verify_chunk_in_free_list(Chunk* fc) const {
 272   // This is an internal consistency check, not part of the check that the
 273   // chunk is in the free lists.
 274   guarantee(fc->size() == size(), "Wrong list is being searched");
 275   Chunk* curFC = head();
 276   while (curFC) {
 277     // This is an internal consistency check.
 278     guarantee(size() == curFC->size(), "Chunk is in wrong list.");
 279     if (fc == curFC) {
 280       return true;
 281     }
 282     curFC = curFC->next();
 283   }
 284   return false;
 285 }
 286 
 287 #ifdef ASSERT
 288 template <class Chunk>
 289 void FreeList<Chunk>::assert_proper_lock_protection_work() const {
 290   // Nothing to do if the list has no assigned protecting lock
 291   if (protecting_lock() == NULL) {
 292     return;
 293   }
 294 
 295   Thread* thr = Thread::current();
 296   if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) {
 297     // assert that we are holding the freelist lock
 298   } else if (thr->is_GC_task_thread()) {
 299     assert(protecting_lock()->owned_by_self(), "FreeList RACE DETECTED");
 300   } else if (thr->is_Java_thread()) {
 301     assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing");
 302   } else {
 303     ShouldNotReachHere();  // unaccounted thread type?
 304   }
 305 }
 306 #endif
 307 
 308 // Print the "label line" for free list stats.
 309 template <class Chunk>
 310 void FreeList<Chunk>::print_labels_on(outputStream* st, const char* c) {
 311   st->print("%16s\t", c);
 312   st->print("%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"
 313             "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "\n",
 314             "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep",
 315             "count",   "cBirths", "cDeaths", "sBirths", "sDeaths");
 316 }
 317 
 318 // Print the AllocationStats for the given free list. If the second argument
 319 // to the call is a non-null string, it is printed in the first column;
 320 // otherwise, if the argument is null (the default), then the size of the
 321 // (free list) block is printed in the first column.
 322 template <class Chunk_t>
 323 void FreeList<Chunk_t>::print_on(outputStream* st, const char* c) const {
 324   if (c != NULL) {
 325     st->print("%16s", c);
 326   } else {
 327     st->print(SIZE_FORMAT_W(16), size());
 328   }
 329 }
 330 
 331 #endif // SHARE_MEMORY_FREELIST_INLINE_HPP