1 /*
   2  * Copyright (c) 2018, 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/virtualSpaceNode.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 
  32 
  33 namespace metaspace {
  34 namespace internals {
  35 
  36 class Metachunk;
  37 class ChunkManager;
  38 
  39 // List of VirtualSpaces for metadata allocation.
  40 class VirtualSpaceList : public CHeapObj<mtClass> {
  41   friend class VirtualSpaceNode;
  42 
  43   enum VirtualSpaceSizes {
  44     VirtualSpaceSize = 256 * K
  45   };
  46 
  47   // Head of the list
  48   VirtualSpaceNode* _virtual_space_list;
  49   // virtual space currently being used for allocations
  50   VirtualSpaceNode* _current_virtual_space;
  51 
  52   // Is this VirtualSpaceList used for the compressed class space
  53   bool _is_class;
  54 
  55   // Sum of reserved and committed memory in the virtual spaces
  56   size_t _reserved_words;
  57   size_t _committed_words;
  58 
  59   // Number of virtual spaces
  60   size_t _virtual_space_count;
  61 
  62   ~VirtualSpaceList();
  63 
  64   VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
  65 
  66   void set_virtual_space_list(VirtualSpaceNode* v) {
  67     _virtual_space_list = v;
  68   }
  69   void set_current_virtual_space(VirtualSpaceNode* v) {
  70     _current_virtual_space = v;
  71   }
  72 
  73   void link_vs(VirtualSpaceNode* new_entry);
  74 
  75   // Get another virtual space and add it to the list.  This
  76   // is typically prompted by a failed attempt to allocate a chunk
  77   // and is typically followed by the allocation of a chunk.
  78   bool create_new_virtual_space(size_t vs_word_size);
  79 
  80   // Chunk up the unused committed space in the current
  81   // virtual space and add the chunks to the free list.
  82   void retire_current_virtual_space();
  83 
  84  public:
  85   VirtualSpaceList(size_t word_size);
  86   VirtualSpaceList(ReservedSpace rs);
  87 
  88   size_t free_bytes();
  89 
  90   Metachunk* get_new_chunk(size_t chunk_word_size,
  91                            size_t suggested_commit_granularity);
  92 
  93   bool expand_node_by(VirtualSpaceNode* node,
  94                       size_t min_words,
  95                       size_t preferred_words);
  96 
  97   bool expand_by(size_t min_words,
  98                  size_t preferred_words);
  99 
 100   VirtualSpaceNode* current_virtual_space() {
 101     return _current_virtual_space;
 102   }
 103 
 104   bool is_class() const { return _is_class; }
 105 
 106   bool initialization_succeeded() { return _virtual_space_list != NULL; }
 107 
 108   size_t reserved_words()  { return _reserved_words; }
 109   size_t reserved_bytes()  { return reserved_words() * BytesPerWord; }
 110   size_t committed_words() { return _committed_words; }
 111   size_t committed_bytes() { return committed_words() * BytesPerWord; }
 112 
 113   void inc_reserved_words(size_t v);
 114   void dec_reserved_words(size_t v);
 115   void inc_committed_words(size_t v);
 116   void dec_committed_words(size_t v);
 117   void inc_virtual_space_count();
 118   void dec_virtual_space_count();
 119 
 120   bool contains(const void* ptr);
 121 
 122   // Unlink empty VirtualSpaceNodes and free it.
 123   void purge(ChunkManager* chunk_manager);
 124 
 125   void print_on(outputStream* st) const                 { print_on(st, K); }
 126   void print_on(outputStream* st, size_t scale) const;
 127   void print_map(outputStream* st) const;
 128 
 129   class VirtualSpaceListIterator : public StackObj {
 130     VirtualSpaceNode* _virtual_spaces;
 131    public:
 132     VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
 133       _virtual_spaces(virtual_spaces) {}
 134 
 135     bool repeat() {
 136       return _virtual_spaces != NULL;
 137     }
 138 
 139     VirtualSpaceNode* get_next() {
 140       VirtualSpaceNode* result = _virtual_spaces;
 141       if (_virtual_spaces != NULL) {
 142         _virtual_spaces = _virtual_spaces->next();
 143       }
 144       return result;
 145     }
 146   };
 147 };
 148 
 149 } // namespace metaspace
 150 } // namespace internals
 151 
 152 #endif /* SHARE_MEMORY_METASPACE_VIRTUALSPACELIST_HPP_ */