1 /*
   2  * Copyright (c) 2012, 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 #ifndef SHARE_VM_MEMORY_METACHUNK_HPP
  25 #define SHARE_VM_MEMORY_METACHUNK_HPP
  26 
  27 #include "memory/allocation.hpp"
  28 #include "utilities/debug.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 class VirtualSpaceNode;
  32 
  33 // Super class of Metablock and Metachunk to allow them to
  34 // be put on the FreeList and in the BinaryTreeDictionary.
  35 template <class T>
  36 class Metabase {
  37   size_t _word_size;
  38   T*     _next;
  39   T*     _prev;
  40 
  41  protected:
  42   Metabase(size_t word_size) : _word_size(word_size), _next(NULL), _prev(NULL) {}
  43 
  44  public:
  45   T* next() const         { return _next; }
  46   T* prev() const         { return _prev; }
  47   void set_next(T* v)     { _next = v; assert(v != this, "Boom");}
  48   void set_prev(T* v)     { _prev = v; assert(v != this, "Boom");}
  49   void clear_next()       { set_next(NULL); }
  50   void clear_prev()       { set_prev(NULL); }
  51 
  52   size_t size() const volatile { return _word_size; }
  53   void set_size(size_t v) { _word_size = v; }
  54 
  55   void link_next(T* ptr)  { set_next(ptr); }
  56   void link_prev(T* ptr)  { set_prev(ptr); }
  57   void link_after(T* ptr) {
  58     link_next(ptr);
  59     if (ptr != NULL) ptr->link_prev((T*)this);
  60   }
  61 
  62   uintptr_t* end() const        { return ((uintptr_t*) this) + size(); }
  63 
  64   bool cantCoalesce() const     { return false; }
  65 
  66   // Debug support
  67 #ifdef ASSERT
  68   void* prev_addr() const { return (void*)&_prev; }
  69   void* next_addr() const { return (void*)&_next; }
  70   void* size_addr() const { return (void*)&_word_size; }
  71 #endif
  72   bool verify_chunk_in_free_list(T* tc) const { return true; }
  73   bool verify_par_locked() { return true; }
  74 
  75   void assert_is_mangled() const {/* Don't check "\*/}
  76 
  77   bool is_free()                 { return true; }
  78 };
  79 
  80 //  Metachunk - Quantum of allocation from a Virtualspace
  81 //    Metachunks are reused (when freed are put on a global freelist) and
  82 //    have no permanent association to a SpaceManager.
  83 
  84 //            +--------------+ <- end    --+       --+
  85 //            |              |             |         |
  86 //            |              |             | free    |
  87 //            |              |             |         |
  88 //            |              |             |         | size | capacity
  89 //            |              |             |         |
  90 //            |              | <- top   -- +         |
  91 //            |              |             |         |
  92 //            |              |             | used    |
  93 //            |              |             |         |
  94 //            |              |             |         |
  95 //            +--------------+ <- bottom --+       --+
  96 
  97 // ChunkIndex defines the type of chunk.
  98 // Chunk types differ by size: specialized < small < medium, chunks
  99 // larger than medium are humongous chunks of varying size.
 100 enum ChunkIndex {
 101   ZeroIndex = 0,
 102   SpecializedIndex = ZeroIndex,
 103   SmallIndex = SpecializedIndex + 1,
 104   MediumIndex = SmallIndex + 1,
 105   HumongousIndex = MediumIndex + 1,
 106   NumberOfFreeLists = 3,
 107   NumberOfInUseLists = 4
 108 };
 109 
 110 // Utility functions.
 111 size_t get_size_for_nonhumongous_chunktype(ChunkIndex chunk_type, bool is_class);
 112 ChunkIndex get_chunk_type_by_size(size_t size, bool is_class);
 113 
 114 // Returns a descriptive name for a chunk type.
 115 const char* chunk_size_name(ChunkIndex index);
 116 
 117 // Verify chunk type.
 118 inline bool is_valid_chunktype(ChunkIndex index) {
 119   return index == SpecializedIndex || index == SmallIndex ||
 120          index == MediumIndex || index == HumongousIndex;
 121 }
 122 
 123 inline bool is_valid_nonhumongous_chunktype(ChunkIndex index) {
 124   return is_valid_chunktype(index) && index != HumongousIndex;
 125 }
 126 
 127 enum ChunkOrigin {
 128   // Chunk normally born (via take_from_committed)
 129   origin_normal = 1,
 130   // Chunk was born as padding chunk
 131   origin_pad = 2,
 132   // Chunk was born as leftover chunk in VirtualSpaceNode::retire
 133   origin_leftover = 3,
 134   // Chunk was born as result of a merge of smaller chunks
 135   origin_merge = 4,
 136   // Chunk was born as result of a split of a larger chunk
 137   origin_split = 5,
 138 
 139   origin_minimum = origin_normal,
 140   origin_maximum = origin_split,
 141   origins_count = origin_maximum + 1
 142 };
 143 
 144 inline bool is_valid_chunkorigin(ChunkOrigin origin) {
 145   return origin == origin_normal ||
 146     origin == origin_pad ||
 147     origin == origin_leftover ||
 148     origin == origin_merge ||
 149     origin == origin_split;
 150 }
 151 
 152 class Metachunk : public Metabase<Metachunk> {
 153   friend class MetachunkTest;
 154   // The VirtualSpaceNode containing this chunk.
 155   VirtualSpaceNode* const _container;
 156 
 157   // Current allocation top.
 158   MetaWord* _top;
 159 
 160   // A 32bit sentinel for debugging purposes.
 161   enum { CHUNK_SENTINEL = 0x4d4554EF,  // "MET"
 162          CHUNK_SENTINEL_INVALID = 0xFEEEEEEF
 163   };
 164 
 165   uint32_t _sentinel;
 166 
 167   const ChunkIndex _chunk_type;
 168   const bool _is_class;
 169   // Whether the chunk is free (in freelist) or in use by some class loader.
 170   bool _is_tagged_free;
 171 
 172   ChunkOrigin _origin;
 173   int _use_count;
 174 
 175   MetaWord* initial_top() const { return (MetaWord*)this + overhead(); }
 176   MetaWord* top() const         { return _top; }
 177 
 178  public:
 179   // Metachunks are allocated out of a MetadataVirtualSpace and
 180   // and use some of its space to describe itself (plus alignment
 181   // considerations).  Metadata is allocated in the rest of the chunk.
 182   // This size is the overhead of maintaining the Metachunk within
 183   // the space.
 184 
 185   // Alignment of each allocation in the chunks.
 186   static size_t object_alignment();
 187 
 188   // Size of the Metachunk header, including alignment.
 189   static size_t overhead();
 190 
 191   Metachunk(ChunkIndex chunktype, bool is_class, size_t word_size, VirtualSpaceNode* container);
 192 
 193   MetaWord* allocate(size_t word_size);
 194 
 195   VirtualSpaceNode* container() const { return _container; }
 196 
 197   MetaWord* bottom() const { return (MetaWord*) this; }
 198 
 199   // Reset top to bottom so chunk can be reused.
 200   void reset_empty() { _top = initial_top(); clear_next(); clear_prev(); }
 201   bool is_empty() { return _top == initial_top(); }
 202 
 203   // used (has been allocated)
 204   // free (available for future allocations)
 205   size_t word_size() const { return size(); }
 206   size_t used_word_size() const;
 207   size_t free_word_size() const;
 208 
 209   bool is_tagged_free() { return _is_tagged_free; }
 210   void set_is_tagged_free(bool v) { _is_tagged_free = v; }
 211 
 212   bool contains(const void* ptr) { return bottom() <= ptr && ptr < _top; }
 213 
 214   void print_on(outputStream* st) const;
 215 
 216   bool is_valid_sentinel() const        { return _sentinel == CHUNK_SENTINEL; }
 217   void remove_sentinel()                { _sentinel = CHUNK_SENTINEL_INVALID; }
 218 
 219   int get_use_count() const             { return _use_count; }
 220   void inc_use_count()                  { _use_count ++; }
 221 
 222   ChunkOrigin get_origin() const        { return _origin; }
 223   void set_origin(ChunkOrigin orig)     { _origin = orig; }
 224 
 225   ChunkIndex get_chunk_type() const     { return _chunk_type; }
 226   bool is_class() const                 { return _is_class; }
 227 
 228   DEBUG_ONLY(void mangle(juint word_value);)
 229   DEBUG_ONLY(void verify();)
 230 
 231 };
 232 
 233 // Metablock is the unit of allocation from a Chunk.
 234 //
 235 // A Metablock may be reused by its SpaceManager but are never moved between
 236 // SpaceManagers.  There is no explicit link to the Metachunk
 237 // from which it was allocated.  Metablock may be deallocated and
 238 // put on a freelist but the space is never freed, rather
 239 // the Metachunk it is a part of will be deallocated when it's
 240 // associated class loader is collected.
 241 
 242 class Metablock : public Metabase<Metablock> {
 243   friend class VMStructs;
 244  public:
 245   Metablock(size_t word_size) : Metabase<Metablock>(word_size) {}
 246 };
 247 
 248 #endif  // SHARE_VM_MEMORY_METACHUNK_HPP