/* * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "precompiled.hpp" #include "memory/metaspace.hpp" #include "memory/metaspace/chunkManager.hpp" #include "memory/metaspace/counter.hpp" #include "memory/metaspace/commitLimiter.hpp" #include "memory/metaspace/counter.hpp" #include "memory/metaspace/virtualSpaceList.hpp" #include "memory/metaspace/virtualSpaceNode.hpp" #include "runtime/mutexLocker.hpp" namespace metaspace { // Create a new, empty, expandable list. VirtualSpaceList::VirtualSpaceList(const char* name, CommitLimiter* commit_limiter) : _name(name), _first_node(NULL), _current_node(NULL), _can_expand(true), _commit_limiter(commit_limiter), _reserved_words_counter(), _committed_words_counter() { // Create the first node right now. Nothing gets committed yet though. create_new_node(); } // Create a new list. The list will contain one node only, which uses the given ReservedSpace. // It will be not expandable beyond that first node. VirtualSpaceList::VirtualSpaceList(const char* name, ReservedSpace rs, CommitLimiter* commit_limiter) : _name(name), _first_node(NULL), _current_node(NULL), _can_expand(false), _commit_limiter(commit_limiter), _reserved_words_counter(), _committed_words_counter() { // Create the first node spanning the existing ReservedSpace. This will be the only node created // for this list since we cannot expand. VirtualSpaceNode* vsn = VirtualSpaceNode::create_node(rs, _commit_limiter, &_reserved_words_counter, &_committed_words_counter); assert(vsn != NULL, "node creation failed"); _first_node = _current_node = vsn; _current_node->set_next(NULL); } VirtualSpaceList::~VirtualSpaceList() { // Note: normally, there is no reason ever to delete a vslist since they are // global objects, but for gtests it makes sense to allow this. VirtualSpaceNode* vsn = _first_node; VirtualSpaceNode* vsn2 = vsn; while (vsn != NULL) { vsn2 = vsn->next(); delete vsn; vsn = vsn2; } } // Create a new node and append it to the list. After // this function, _current_node shall point to a new empty node. // List must be expandable for this to work. void VirtualSpaceList::create_new_node() { assert(_can_expand, "List is not expandable"); VirtualSpaceNode* vsn = VirtualSpaceNode::create_node(constants::virtual_space_node_default_size, _commit_limiter, &_reserved_words_counter, &_committed_words_counter); assert(vsn != NULL, "node creation failed"); vsn->set_next(_first_node); _first_node = _current_node = vsn; } // Allocate a root chunk from this list. // Note: this just returns a chunk whose memory is reserved; no memory is committed yet. // Hence, before using this chunk, it must be committed. // Also, no limits are checked, since no committing takes place. Metachunk* VirtualSpaceList::allocate_root_chunk() { assert(_current_node != NULL, "Sanity"); Metachunk* c = _current_node->allocate_root_chunk(); if (c == NULL) { // The current node is fully used up. // Since all allocations from a VirtualSpaceNode happen in root-chunk-size units, // we should never have remaining space. assert(_current_node->free_words() == 0, "Sanity"); if (_can_expand) { create_new_node(); } else { return NULL; // We cannot expand this list. } } c = _current_node->allocate_root_chunk(); assert(c != NULL, "This should have worked"); return c; } // Print all nodes in this space list. void VirtualSpaceList::print_on(outputStream* st, size_t scale) const { const VirtualSpaceNode* vsn = _first_node; while (vsn != NULL) { vsn->print_on(st, scale); st->cr(); vsn = vsn->next(); } } #ifdef ASSERT void VirtualSpaceList::verify(bool slow) const { assert(_current_node != NULL && _first_node != NULL && _name != NULL, "Sanity"); size_t total_reserved_words = 0; size_t total_committed_words = 0; const VirtualSpaceNode* vsn = _first_node; while (vsn != NULL) { vsn->verify(slow); total_reserved_words += vsn->word_size(); total_committed_words += vsn->committed_words(); vsn = vsn->next(); } _reserved_words_counter.check(total_reserved_words); _committed_words_counter.check(total_committed_words); } #endif // Returns true if this pointer is contained in one of our nodes. bool VirtualSpaceList::contains(const MetaWord* p) const { const VirtualSpaceNode* vsn = _first_node; while (vsn != NULL) { if (vsn->contains(p)) { return true; } vsn = vsn->next(); } return false; } VirtualSpaceList* VirtualSpaceList::_vslist_class = NULL; VirtualSpaceList* VirtualSpaceList::_vslist_nonclass = NULL; void VirtualSpaceList::set_vslist_class(VirtualSpaceList* vsl) { assert(_vslist_class == NULL, "Sanity"); _vslist_class = vsl; } void VirtualSpaceList::set_vslist_nonclass(VirtualSpaceList* vsl) { assert(_vslist_nonclass == NULL, "Sanity"); _vslist_nonclass = vsl; } } // namespace metaspace