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 #ifndef SHARE_MEMORY_METASPACE_VIRTUALSPACELIST_HPP
  26 #define SHARE_MEMORY_METASPACE_VIRTUALSPACELIST_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/metaspace/counter.hpp"
  30 #include "memory/metaspace/commitLimiter.hpp"
  31 #include "memory/metaspace/virtualSpaceNode.hpp"
  32 #include "memory/virtualspace.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 
  36 namespace metaspace {
  37 
  38 class Metachunk;
  39 
  40 class VirtualSpaceList : public public CHeapObj<mtClass> {
  41 
  42   // Name
  43   const char* const _name;
  44 
  45   // Head of the list.
  46   VirtualSpaceNode* _first_node;
  47 
  48   // Node currently being used for allocations.
  49   VirtualSpaceNode* _current_node;
  50 
  51   // Whether this list can expand by allocating new nodes.
  52   const bool _can_expand;
  53 
  54   // Used to check limits before committing memory.
  55   CommitLimiter* const _commit_limiter;
  56 
  57   // Statistics
  58 
  59   // Holds sum of reserved space, in words, over all list nodes.
  60   SizeCounter _reserved_words_counter;
  61 
  62   // Holds sum of committed space, in words, over all list nodes.
  63   SizeCounter _committed_words_counter;
  64 
  65   // Create a new node and append it to the list. After
  66   // this function, _current_node shall point to a new empty node.
  67   // List must be expandable for this to work.
  68   void create_new_node();
  69 
  70 public:
  71 
  72   // Create a new, empty, expandable list.
  73   VirtualSpaceList(const char* name, CommitLimiter* commit_limiter);
  74 
  75   // Create a new list. The list will contain one node only, which uses the given ReservedSpace.
  76   // It will be not expandable beyond that first node.
  77   VirtualSpaceList(const char* name, ReservedSpace rs, CommitLimiter* commit_limiter);
  78 
  79   virtual ~VirtualSpaceList();
  80 
  81   // Allocate a root chunk from this list.
  82   // Note: this just returns a chunk whose memory is reserved; no memory is committed yet.
  83   // Hence, before using this chunk, it must be committed.
  84   // Also, no limits are checked, since no committing takes place.
  85   Metachunk* allocate_root_chunk();
  86 
  87   DEBUG_ONLY(void verify(bool slow) const;)
  88 
  89   //// Statistics ////
  90 
  91   // Return sum of reserved words in all nodes.
  92   size_t reserved_words() const     { return _reserved_words_counter.get(); }
  93 
  94   // Return sum of committed words in all nodes.
  95   size_t committed_words() const    { return _committed_words_counter.get(); }
  96 
  97   //// Debug stuff ////
  98   DEBUG_ONLY(void verify(bool slow) const;)
  99 
 100 
 101 
 102 
 103 private:
 104 
 105   static VirtualSpaceList* _vslist_class;
 106   static VirtualSpaceList* _vslist_nonclass;
 107 
 108 public:
 109 
 110   static VirtualSpaceList* vslist_class()       { return _vslist_class; }
 111   static VirtualSpaceList* vslist_nonclass()    { return _vslist_nonclass; }
 112 
 113   static void initialize(VirtualSpaceList* vslist_class, VirtualSpaceList* vslist_nonclass);
 114 
 115 
 116 };
 117 
 118 } // namespace metaspace
 119 
 120 #endif // SHARE_MEMORY_METASPACE_VIRTUALSPACELIST_HPP