1 /*
   2  * Copyright (c) 2018, 2019, 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 
  26 #include "precompiled.hpp"
  27 #include "memory/metaspace.hpp"
  28 #include "memory/metaspace/chunkManager.hpp"
  29 #include "memory/metaspace/counter.hpp"
  30 #include "memory/metaspace/commitLimiter.hpp"
  31 #include "memory/metaspace/counter.hpp"
  32 #include "memory/metaspace/virtualSpaceList.hpp"
  33 #include "memory/metaspace/virtualSpaceNode.hpp"
  34 #include "runtime/mutexLocker.hpp"
  35 
  36 
  37 namespace metaspace {
  38 
  39 // Create a new, empty, expandable list.
  40 VirtualSpaceList::VirtualSpaceList(const char* name, CommitLimiter* commit_limiter)
  41   : _first_node(NULL),
  42     _current_node(NULL),
  43     _can_expand(true),
  44     _name(name),
  45     _commit_limiter(commit_limiter),
  46     _reserved_words_counter(),
  47     _committed_words_counter()
  48 {
  49   // Create the first node right now. Nothing gets committed yet though.
  50   create_new_node();
  51 }
  52 
  53 // Create a new list. The list will contain one node only, which uses the given ReservedSpace.
  54 // It will be not expandable beyond that first node.
  55 VirtualSpaceList::VirtualSpaceList(const char* name, ReservedSpace rs, CommitLimiter* commit_limiter)
  56 : _first_node(NULL),
  57   _current_node(NULL),
  58   _can_expand(false),
  59   _name(name),
  60   _commit_limiter(commit_limiter),
  61   _reserved_words_counter(),
  62   _committed_words_counter()
  63 {
  64   // Create the first node spanning the existing ReservedSpace. This will be the only node created
  65   // for this list since we cannot expand.
  66   VirtualSpaceNode* vsn = VirtualSpaceNode::create_node(rs, _commit_limiter,
  67                                                         &_reserved_words_counter, &_committed_words_counter);
  68   assert(vsn != NULL, "node creation failed");
  69   _first_node = _current_node = vsn;
  70   _current_node->set_next(NULL);
  71 }
  72 
  73 VirtualSpaceList::~VirtualSpaceList() {
  74   // Note: normally, there is no reason ever to delete a vslist since they are
  75   // global objects, but for gtests it makes sense to allow this.
  76   VirtualSpaceNode* vsn = _first_node;
  77   VirtualSpaceNode* vsn2 = vsn;
  78   while (vsn != NULL) {
  79     vsn2 = vsn->next();
  80     delete vsn;
  81     vsn = vsn2;
  82   }
  83 }
  84 
  85 // Create a new node and append it to the list. After
  86 // this function, _current_node shall point to a new empty node.
  87 // List must be expandable for this to work.
  88 void VirtualSpaceList::create_new_node() {
  89   assert(_can_expand, "List is not expandable");
  90   VirtualSpaceNode* vsn = VirtualSpaceNode::create_node(constants::virtual_space_node_default_size,
  91                                                         _commit_limiter,
  92                                                         &_reserved_words_counter, &_committed_words_counter);
  93   assert(vsn != NULL, "node creation failed");
  94   vsn->set_next(_first_node);
  95   _first_node = _current_node = vsn;
  96 }
  97 
  98 // Allocate a root chunk from this list.
  99 // Note: this just returns a chunk whose memory is reserved; no memory is committed yet.
 100 // Hence, before using this chunk, it must be committed.
 101 // Also, no limits are checked, since no committing takes place.
 102 Metachunk*  VirtualSpaceList::allocate_root_chunk() {
 103 
 104   assert(_current_node != NULL, "Sanity");
 105 
 106   Metachunk* c = _current_node->allocate_root_chunk();
 107 
 108   if (c == NULL) {
 109 
 110     // The current node is fully used up.
 111 
 112     // Since all allocations from a VirtualSpaceNode happen in root-chunk-size units,
 113     // we should never have som
 114     assert(_current_node->used_words() == _current_node->word_size(), "Sanity");
 115 
 116     if (_can_expand) {
 117       create_new_node();
 118     } else {
 119       return NULL; // We cannot expand this list.
 120     }
 121   }
 122 
 123   Metachunk* c = _current_node->allocate_root_chunk();
 124 
 125   assert(c != NULL, "This should have worked");
 126 
 127   return c;
 128 
 129 }
 130 
 131 // Print all nodes in this space list.
 132 void VirtualSpaceList::print_on(outputStream* st, size_t scale) const {
 133   const VirtualSpaceNode* vsn = _first_node;
 134   while (vsn != NULL) {
 135     vsn->print_on(st, scale);
 136     st->cr();
 137     vsn = vsn->next();
 138   }
 139 }
 140 
 141 #ifdef ASSERT
 142 void VirtualSpaceList::verify(bool slow) const {
 143   assert(_current_node != NULL && _first_node != NULL && _name != NULL, "Sanity");
 144 
 145   size_t total_reserved_words = 0;
 146   size_t total_committed_words = 0;
 147   const VirtualSpaceNode* vsn = _first_node;
 148   while (vsn != NULL) {
 149     vsn->verify(slow);
 150     total_reserved_words += vsn->word_size();
 151     total_committed_words += vsn->committed_words();
 152     vsn = vsn->next();
 153   }
 154 
 155   _reserved_words_counter.check(total_reserved_words);
 156   _committed_words_counter.check(total_committed_words);
 157 
 158 }
 159 #endif
 160 
 161 // Returns true if this pointer is contained in one of our nodes.
 162 bool VirtualSpaceList::contains(const MetaWord* p) const {
 163   const VirtualSpaceNode* vsn = _first_node;
 164   while (vsn != NULL && !vsn->contains(p)) {
 165     vsn = vsn->next();
 166   }
 167 }
 168 
 169 VirtualSpaceList* VirtualSpaceList::_vslist_class = NULL;
 170 VirtualSpaceList* VirtualSpaceList::_vslist_nonclass = NULL;
 171 
 172 void VirtualSpaceList::set_vslist_class(VirtualSpaceList* vsl) {
 173   assert(_vslist_class == NULL, "Sanity");
 174   _vslist_class = vsl;
 175 }
 176 
 177 void VirtualSpaceList::set_vslist_nonclass(VirtualSpaceList* vsl) {
 178   assert(_vslist_nonclass == NULL, "Sanity");
 179   _vslist_nonclass = vsl;
 180 }
 181 
 182 } // namespace metaspace
 183