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