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_VIRTUALSPACENODE_HPP 26 #define SHARE_MEMORY_METASPACE_VIRTUALSPACENODE_HPP 27 28 #include "memory/metaspace/constants.hpp" 29 #include "memory/metaspace/chunkTree.hpp" 30 #include "memory/metaspace/commitMask.hpp" 31 #include "memory/virtualspace.hpp" 32 #include "memory/memRegion.hpp" 33 #include "utilities/debug.hpp" 34 #include "utilities/bitMap.hpp" 35 #include "utilities/globalDefinitions.hpp" 36 37 class outputStream; 38 39 namespace metaspace { 40 41 // VirtualSpaceNode manage a single address range of the Metaspace. 42 // 43 // That address range may contain interleaved committed and uncommitted 44 // regions. It keeps track of which regions have committed and offers 45 // functions to commit and uncommit regions. 46 // 47 // It allocates and hands out memory ranges, starting at the bottom. 48 // 49 // Address range must be aligned to root chunk size. 50 // 51 class VirtualSpaceNode : public CHeapObj<mtClass> { 52 53 // Link to next VirtualSpaceNode 54 VirtualSpaceNode* _next; 55 56 ReservedSpace _rs; 57 58 // Start pointer of the area. 59 MetaWord* const _base; 60 61 // Size, in words, of the whole node 62 const size_t _word_size; 63 64 // Size, in words, of the used portion of the node 65 size_t _used_words; 66 67 // The bitmap describing the commit state of the region: 68 // Each bit covers a region of 64K (see constants::commit_granule_size). 69 CommitMask _commit_mask; 70 71 // An array of chunk trees. Each one describes fragmentation inside the associated root chunk. 72 ChunkTreeArray _chunk_tree_array; 73 74 /// committing, uncommitting /// 75 76 // Given a pointer into this node, calculate the start of the commit granule 77 // the pointer points into. 78 MetaWord* calc_start_of_granule(MetaWord* p) const { 79 DEBUG_ONLY(check_pointer(p)); 80 return align_down(p, constants::commit_granule_bytes); 81 } 82 83 // Given an address range (which has to be aligned to commit granule size): 84 // - commit it 85 // - mark it as committed in the commit mask 86 // May fail and return NULL if we hit a global commit limit (GC threshold, MaxMetaspaceSize). 87 // Will assert if we run out of memory. 88 bool commit_range(MetaWord* p, size_t word_size); 89 90 //// creation //// 91 92 // Create a new empty node spanning the given reserved space. 93 VirtualSpaceNode(ReservedSpace rs); 94 95 public: 96 97 // Create a node of a given size 98 static VirtualSpaceNode* create_node(size_t word_size); 99 100 // Create a node over an existing space 101 static VirtualSpaceNode* create_node(ReservedSpace rs); 102 103 ~VirtualSpaceNode(); 104 105 //// Chunk allocation, splitting, merging ///// 106 107 // Allocate a root chunk from this node. Will fail and return NULL 108 // if the node is full. 109 // Note that the chunk memory may or may not be committed. Commit its payload 110 // area before using it. 111 Metachunk* allocate_root_chunk(); 112 113 // Given a chunk c, split it recursively until you get a chunk of the given target_level. 114 // Returns pointer to the result chunk; returns split off chunks in p_splinters as linked list. 115 // Returns NULL if chunk cannot be split at least once. 116 Metachunk* split(chklvl_t target_level, Metachunk* c, Metachunk** p_splinters); 117 118 // Given a chunk, attempt to merge it recursively with its neighboring chunks. 119 // If successful (merged at least once), returns address of 120 // the merged chunk; NULL otherwise. 121 // 122 // !!! Please note that if this method returns a non-NULL value, the 123 // original chunk will be invalid and should not be accessed anymore! !!! 124 Metachunk* merge(Metachunk* c); 125 126 127 //// Committing/uncommitting memory ///// 128 129 // Given an address range (which has to be aligned to commit granule size), ensure 130 // it is committed. 131 // - commit it 132 // - mark it as committed in the commit mask 133 // May fail and return NULL if we hit a global commit limit (GC threshold, MaxMetaspaceSize). 134 // Will assert if we run out of memory. 135 bool ensure_range_is_committed(MetaWord* p, size_t word_size); 136 137 // Given an address range (which has to be aligned to commit granule size): 138 // - uncommit it 139 // - mark it as uncommitted in the commit mask 140 bool uncommit_range(MetaWord* p, size_t word_size); 141 142 143 /// Debug stuff //// 144 145 // Verify counters and basic structure. Slow mode: verify all chunks in depth 146 bool contains(const MetaWord* p) const { 147 return p >= _base && p < _base + _used_words; 148 } 149 150 #ifdef ASSERT 151 void check_pointer(const MetaWord* p) const { 152 assert(contains(p), "invalid pointer"); 153 } 154 void verify(bool slow); 155 #endif 156 157 }; 158 159 160 } // namespace metaspace 161 162 #endif // SHARE_MEMORY_METASPACE_VIRTUALSPACENODE_HPP