Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/freeList.cpp
32285 views
/*1* Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/freeBlockDictionary.hpp"26#include "memory/freeList.hpp"27#include "memory/metachunk.hpp"28#include "memory/sharedHeap.hpp"29#include "runtime/globals.hpp"30#include "runtime/mutex.hpp"31#include "runtime/vmThread.hpp"32#include "utilities/macros.hpp"3334#if INCLUDE_ALL_GCS35#include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"36#endif // INCLUDE_ALL_GCS3738// Free list. A FreeList is used to access a linked list of chunks39// of space in the heap. The head and tail are maintained so that40// items can be (as in the current implementation) added at the41// at the tail of the list and removed from the head of the list to42// maintain a FIFO queue.4344template <class Chunk>45FreeList<Chunk>::FreeList() :46_head(NULL), _tail(NULL)47#ifdef ASSERT48, _protecting_lock(NULL)49#endif50{51_size = 0;52_count = 0;53}5455template <class Chunk>56void FreeList<Chunk>::link_head(Chunk* v) {57assert_proper_lock_protection();58set_head(v);59// If this method is not used (just set the head instead),60// this check can be avoided.61if (v != NULL) {62v->link_prev(NULL);63}64}65666768template <class Chunk>69void FreeList<Chunk>::reset() {70// Don't set the _size to 0 because this method is71// used with a existing list that has a size but which has72// been emptied.73// Don't clear the _protecting_lock of an existing list.74set_count(0);75set_head(NULL);76set_tail(NULL);77}7879template <class Chunk>80void FreeList<Chunk>::initialize() {81#ifdef ASSERT82// Needed early because it might be checked in other initializing code.83set_protecting_lock(NULL);84#endif85reset();86set_size(0);87}8889template <class Chunk_t>90Chunk_t* FreeList<Chunk_t>::get_chunk_at_head() {91assert_proper_lock_protection();92assert(head() == NULL || head()->prev() == NULL, "list invariant");93assert(tail() == NULL || tail()->next() == NULL, "list invariant");94Chunk_t* fc = head();95if (fc != NULL) {96Chunk_t* nextFC = fc->next();97if (nextFC != NULL) {98// The chunk fc being removed has a "next". Set the "next" to the99// "prev" of fc.100nextFC->link_prev(NULL);101} else { // removed tail of list102link_tail(NULL);103}104link_head(nextFC);105decrement_count();106}107assert(head() == NULL || head()->prev() == NULL, "list invariant");108assert(tail() == NULL || tail()->next() == NULL, "list invariant");109return fc;110}111112113template <class Chunk>114void FreeList<Chunk>::getFirstNChunksFromList(size_t n, FreeList<Chunk>* fl) {115assert_proper_lock_protection();116assert(fl->count() == 0, "Precondition");117if (count() > 0) {118int k = 1;119fl->set_head(head()); n--;120Chunk* tl = head();121while (tl->next() != NULL && n > 0) {122tl = tl->next(); n--; k++;123}124assert(tl != NULL, "Loop Inv.");125126// First, fix up the list we took from.127Chunk* new_head = tl->next();128set_head(new_head);129set_count(count() - k);130if (new_head == NULL) {131set_tail(NULL);132} else {133new_head->link_prev(NULL);134}135// Now we can fix up the tail.136tl->link_next(NULL);137// And return the result.138fl->set_tail(tl);139fl->set_count(k);140}141}142143// Remove this chunk from the list144template <class Chunk>145void FreeList<Chunk>::remove_chunk(Chunk*fc) {146assert_proper_lock_protection();147assert(head() != NULL, "Remove from empty list");148assert(fc != NULL, "Remove a NULL chunk");149assert(size() == fc->size(), "Wrong list");150assert(head() == NULL || head()->prev() == NULL, "list invariant");151assert(tail() == NULL || tail()->next() == NULL, "list invariant");152153Chunk* prevFC = fc->prev();154Chunk* nextFC = fc->next();155if (nextFC != NULL) {156// The chunk fc being removed has a "next". Set the "next" to the157// "prev" of fc.158nextFC->link_prev(prevFC);159} else { // removed tail of list160link_tail(prevFC);161}162if (prevFC == NULL) { // removed head of list163link_head(nextFC);164assert(nextFC == NULL || nextFC->prev() == NULL,165"Prev of head should be NULL");166} else {167prevFC->link_next(nextFC);168assert(tail() != prevFC || prevFC->next() == NULL,169"Next of tail should be NULL");170}171decrement_count();172assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0,173"H/T/C Inconsistency");174// clear next and prev fields of fc, debug only175NOT_PRODUCT(176fc->link_prev(NULL);177fc->link_next(NULL);178)179assert(fc->is_free(), "Should still be a free chunk");180assert(head() == NULL || head()->prev() == NULL, "list invariant");181assert(tail() == NULL || tail()->next() == NULL, "list invariant");182assert(head() == NULL || head()->size() == size(), "wrong item on list");183assert(tail() == NULL || tail()->size() == size(), "wrong item on list");184}185186// Add this chunk at the head of the list.187template <class Chunk>188void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk, bool record_return) {189assert_proper_lock_protection();190assert(chunk != NULL, "insert a NULL chunk");191assert(size() == chunk->size(), "Wrong size");192assert(head() == NULL || head()->prev() == NULL, "list invariant");193assert(tail() == NULL || tail()->next() == NULL, "list invariant");194195Chunk* oldHead = head();196assert(chunk != oldHead, "double insertion");197chunk->link_after(oldHead);198link_head(chunk);199if (oldHead == NULL) { // only chunk in list200assert(tail() == NULL, "inconsistent FreeList");201link_tail(chunk);202}203increment_count(); // of # of chunks in list204assert(head() == NULL || head()->prev() == NULL, "list invariant");205assert(tail() == NULL || tail()->next() == NULL, "list invariant");206assert(head() == NULL || head()->size() == size(), "wrong item on list");207assert(tail() == NULL || tail()->size() == size(), "wrong item on list");208}209210template <class Chunk>211void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk) {212assert_proper_lock_protection();213return_chunk_at_head(chunk, true);214}215216// Add this chunk at the tail of the list.217template <class Chunk>218void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk, bool record_return) {219assert_proper_lock_protection();220assert(head() == NULL || head()->prev() == NULL, "list invariant");221assert(tail() == NULL || tail()->next() == NULL, "list invariant");222assert(chunk != NULL, "insert a NULL chunk");223assert(size() == chunk->size(), "wrong size");224225Chunk* oldTail = tail();226assert(chunk != oldTail, "double insertion");227if (oldTail != NULL) {228oldTail->link_after(chunk);229} else { // only chunk in list230assert(head() == NULL, "inconsistent FreeList");231link_head(chunk);232}233link_tail(chunk);234increment_count(); // of # of chunks in list235assert(head() == NULL || head()->prev() == NULL, "list invariant");236assert(tail() == NULL || tail()->next() == NULL, "list invariant");237assert(head() == NULL || head()->size() == size(), "wrong item on list");238assert(tail() == NULL || tail()->size() == size(), "wrong item on list");239}240241template <class Chunk>242void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk) {243return_chunk_at_tail(chunk, true);244}245246template <class Chunk>247void FreeList<Chunk>::prepend(FreeList<Chunk>* fl) {248assert_proper_lock_protection();249if (fl->count() > 0) {250if (count() == 0) {251set_head(fl->head());252set_tail(fl->tail());253set_count(fl->count());254} else {255// Both are non-empty.256Chunk* fl_tail = fl->tail();257Chunk* this_head = head();258assert(fl_tail->next() == NULL, "Well-formedness of fl");259fl_tail->link_next(this_head);260this_head->link_prev(fl_tail);261set_head(fl->head());262set_count(count() + fl->count());263}264fl->set_head(NULL);265fl->set_tail(NULL);266fl->set_count(0);267}268}269270// verify_chunk_in_free_lists() is used to verify that an item is in this free list.271// It is used as a debugging aid.272template <class Chunk>273bool FreeList<Chunk>::verify_chunk_in_free_list(Chunk* fc) const {274// This is an internal consistency check, not part of the check that the275// chunk is in the free lists.276guarantee(fc->size() == size(), "Wrong list is being searched");277Chunk* curFC = head();278while (curFC) {279// This is an internal consistency check.280guarantee(size() == curFC->size(), "Chunk is in wrong list.");281if (fc == curFC) {282return true;283}284curFC = curFC->next();285}286return false;287}288289#ifndef PRODUCT290template <class Chunk>291void FreeList<Chunk>::assert_proper_lock_protection_work() const {292assert(protecting_lock() != NULL, "Don't call this directly");293assert(ParallelGCThreads > 0, "Don't call this directly");294Thread* thr = Thread::current();295if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) {296// assert that we are holding the freelist lock297} else if (thr->is_GC_task_thread()) {298assert(protecting_lock()->owned_by_self(), "FreeList RACE DETECTED");299} else if (thr->is_Java_thread()) {300assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing");301} else {302ShouldNotReachHere(); // unaccounted thread type?303}304}305#endif306307// Print the "label line" for free list stats.308template <class Chunk>309void FreeList<Chunk>::print_labels_on(outputStream* st, const char* c) {310st->print("%16s\t", c);311st->print("%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t"312"%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" "\n",313"bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep",314"count", "cBirths", "cDeaths", "sBirths", "sDeaths");315}316317// Print the AllocationStats for the given free list. If the second argument318// to the call is a non-null string, it is printed in the first column;319// otherwise, if the argument is null (the default), then the size of the320// (free list) block is printed in the first column.321template <class Chunk_t>322void FreeList<Chunk_t>::print_on(outputStream* st, const char* c) const {323if (c != NULL) {324st->print("%16s", c);325} else {326st->print(SIZE_FORMAT_W(16), size());327}328}329330template class FreeList<Metablock>;331template class FreeList<Metachunk>;332#if INCLUDE_ALL_GCS333template class FreeList<FreeChunk>;334#endif // INCLUDE_ALL_GCS335336337