1 /* 2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2020 SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #ifndef SHARE_MEMORY_METASPACE_FREEBLOCKS_HPP 27 #define SHARE_MEMORY_METASPACE_FREEBLOCKS_HPP 28 29 #include "memory/allocation.hpp" 30 #include "memory/metaspace/binList.hpp" 31 #include "memory/metaspace/blockTree.hpp" 32 #include "memory/metaspace/counters.hpp" 33 #include "utilities/debug.hpp" 34 #include "utilities/globalDefinitions.hpp" 35 36 class outputStream; 37 38 namespace metaspace { 39 40 // Class FreeBlocks manages deallocated blocks in Metaspace. 41 // 42 // In Metaspace, allocated memory blocks may be release prematurely. This is 43 // uncommon (otherwise an arena-based allocation scheme would not make sense). 44 // It can happen e.g. when class loading fails or when bytecode gets rewritten. 45 // 46 // All these released blocks should be reused, so they are collected. Since these 47 // blocks are embedded into chunks which are still in use by a live arena, 48 // we cannot just give these blocks to anyone; only the owner of this arena can 49 // reuse these blocks. Therefore these blocks are kept at arena-level. 50 // 51 // The structure to manage these released blocks at arena level is class FreeBlocks. 52 // 53 // FreeBlocks is optimized toward the typical size and number of deallocated 54 // blocks. The vast majority of them (about 90%) are below 16 words in size, 55 // but there is a significant portion of memory blocks much larger than that, 56 // leftover space from retired chunks, see MetaspaceArena::retire_current_chunk(). 57 // 58 // Since the vast majority of blocks are small or very small, FreeBlocks consists 59 // internally of two separate structures to keep very small blocks and other blocks. 60 // Very small blocks are kept in a bin list (see binlist.hpp) and larger blocks in 61 // a BST (see blocktree.hpp). 62 63 class FreeBlocks : public CHeapObj<mtMetaspace> { 64 65 // _small_blocks takes care of small to very small blocks. 66 BinList32 _small_blocks; 67 68 // A BST for larger blocks, only for blocks which are too large 69 // to fit into _smallblocks. 70 BlockTree _tree; 71 72 // This verifies that blocks too large to go into the binlist can be 73 // kept in the blocktree. 74 STATIC_ASSERT(BinList32::MaxWordSize >= BlockTree::MinWordSize); 75 76 public: 77 78 // Smallest blocks we can keep in this structure. 79 const static size_t MinWordSize = BinList32::MinWordSize; 80 81 // Add a block to the deallocation management. 82 void add_block(MetaBlock bl); 83 84 // Retrieve a block of at least requested_word_size. May be larger. 85 MetaBlock remove_block(size_t requested_word_size); 86 87 #ifdef ASSERT 88 void verify() const { 89 _tree.verify(); 90 _small_blocks.verify(); 91 }; 92 #endif 93 94 // Returns number of blocks. 95 int count() const { 96 return _small_blocks.count() + _tree.count(); 97 } 98 99 // Returns total size, in words, of all elements. 100 size_t total_size() const { 101 return _small_blocks.total_size() + _tree.total_size(); 102 } 103 104 // Returns true if empty. 105 bool is_empty() const { 106 return _small_blocks.is_empty() && _tree.is_empty(); 107 } 108 109 }; 110 111 } // namespace metaspace 112 113 #endif // SHARE_MEMORY_METASPACE_FREEBLOCKS_HPP