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_VIRTUALSPACENODE_HPP_
  26 #define SHARE_MEMORY_METASPACE_VIRTUALSPACENODE_HPP_
  27 
  28 #include "memory/virtualspace.hpp"
  29 #include "memory/memRegion.hpp"
  30 #include "utilities/debug.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 class outputStream;
  34 
  35 namespace metaspace {
  36 
  37 class Metachunk;
  38 class ChunkManager;
  39 class OccupancyMap;
  40 
  41 // A VirtualSpaceList node.
  42 class VirtualSpaceNode : public CHeapObj<mtClass> {
  43   friend class VirtualSpaceList;
  44 
  45   // Link to next VirtualSpaceNode
  46   VirtualSpaceNode* _next;
  47 
  48   // Whether this node is contained in class or metaspace.
  49   const bool _is_class;
  50 
  51   // total in the VirtualSpace
  52   MemRegion _reserved;
  53   ReservedSpace _rs;
  54   VirtualSpace _virtual_space;
  55   MetaWord* _top;
  56   // count of chunks contained in this VirtualSpace
  57   uintx _container_count;
  58 
  59   OccupancyMap* _occupancy_map;
  60 
  61   // Convenience functions to access the _virtual_space
  62   char* low()  const { return virtual_space()->low(); }
  63   char* high() const { return virtual_space()->high(); }
  64 
  65   // The first Metachunk will be allocated at the bottom of the
  66   // VirtualSpace
  67   Metachunk* first_chunk() { return (Metachunk*) bottom(); }
  68 
  69   // Committed but unused space in the virtual space
  70   size_t free_words_in_vs() const;
  71 
  72   // True if this node belongs to class metaspace.
  73   bool is_class() const { return _is_class; }
  74 
  75   // Helper function for take_from_committed: allocate padding chunks
  76   // until top is at the given address.
  77   void allocate_padding_chunks_until_top_is_at(MetaWord* target_top);
  78 
  79  public:
  80 
  81   VirtualSpaceNode(bool is_class, size_t byte_size);
  82   VirtualSpaceNode(bool is_class, ReservedSpace rs) :
  83     _is_class(is_class), _top(NULL), _next(NULL), _rs(rs), _container_count(0), _occupancy_map(NULL) {}
  84   ~VirtualSpaceNode();
  85 
  86   // Convenience functions for logical bottom and end
  87   MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
  88   MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
  89 
  90   const OccupancyMap* occupancy_map() const { return _occupancy_map; }
  91   OccupancyMap* occupancy_map() { return _occupancy_map; }
  92 
  93   bool contains(const void* ptr) { return ptr >= low() && ptr < high(); }
  94 
  95   size_t reserved_words() const  { return _virtual_space.reserved_size() / BytesPerWord; }
  96   size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; }
  97 
  98   bool is_pre_committed() const { return _virtual_space.special(); }
  99 
 100   // address of next available space in _virtual_space;
 101   // Accessors
 102   VirtualSpaceNode* next() { return _next; }
 103   void set_next(VirtualSpaceNode* v) { _next = v; }
 104 
 105   void set_reserved(MemRegion const v) { _reserved = v; }
 106   void set_top(MetaWord* v) { _top = v; }
 107 
 108   // Accessors
 109   MemRegion* reserved() { return &_reserved; }
 110   VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
 111 
 112   // Returns true if "word_size" is available in the VirtualSpace
 113   bool is_available(size_t word_size) { return word_size <= pointer_delta(end(), _top, sizeof(MetaWord)); }
 114 
 115   MetaWord* top() const { return _top; }
 116   void inc_top(size_t word_size) { _top += word_size; }
 117 
 118   uintx container_count() { return _container_count; }
 119   void inc_container_count();
 120   void dec_container_count();
 121 #ifdef ASSERT
 122   uintx container_count_slow();
 123   void verify_container_count();
 124 #endif
 125 
 126   // used and capacity in this single entry in the list
 127   size_t used_words_in_vs() const;
 128   size_t capacity_words_in_vs() const;
 129 
 130   bool initialize();
 131 
 132   // get space from the virtual space
 133   Metachunk* take_from_committed(size_t chunk_word_size);
 134 
 135   // Allocate a chunk from the virtual space and return it.
 136   Metachunk* get_chunk_vs(size_t chunk_word_size);
 137 
 138   // Expands/shrinks the committed space in a virtual space.  Delegates
 139   // to Virtualspace
 140   bool expand_by(size_t min_words, size_t preferred_words);
 141 
 142   // In preparation for deleting this node, remove all the chunks
 143   // in the node from any freelist.
 144   void purge(ChunkManager* chunk_manager);
 145 
 146   // If an allocation doesn't fit in the current node a new node is created.
 147   // Allocate chunks out of the remaining committed space in this node
 148   // to avoid wasting that memory.
 149   // This always adds up because all the chunk sizes are multiples of
 150   // the smallest chunk size.
 151   void retire(ChunkManager* chunk_manager);
 152 
 153   void print_on(outputStream* st) const                 { print_on(st, K); }
 154   void print_on(outputStream* st, size_t scale) const;
 155   void print_map(outputStream* st, bool is_class) const;
 156 
 157   // Debug support
 158   DEBUG_ONLY(void mangle();)
 159   // Verify counters, all chunks in this list node and the occupancy map.
 160   DEBUG_ONLY(void verify();)
 161   // Verify that all free chunks in this node are ideally merged
 162   // (there not should be multiple small chunks where a large chunk could exist.)
 163   DEBUG_ONLY(void verify_free_chunks_are_ideally_merged();)
 164 
 165 };
 166 
 167 } // namespace metaspace
 168 
 169 #endif /* SHARE_MEMORY_METASPACE_VIRTUALSPACENODE_HPP_ */