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   // Cutoff point: blocks larger than this size are kept in the
 77   // tree, blocks smaller than or equal to this size in the bin list.
 78   const size_t MaxSmallBlocksWordSize = BinList32::MaxWordSize;
 79 
 80 public:
 81 
 82   // Smallest blocks we can keep in this structure.
 83   const static size_t MinWordSize = BinList32::MinWordSize;
 84 
 85   // Add a block to the deallocation management.
 86   void add_block(MetaWord* p, size_t word_size);
 87 
 88   // Retrieve a block of at least requested_word_size.
 89   MetaWord* remove_block(size_t requested_word_size);
 90 
 91 #ifdef ASSERT
 92   void verify() const {
 93     _tree.verify();
 94     _small_blocks.verify();
 95   };
 96 #endif
 97 
 98   // Returns number of blocks.
 99   int count() const {
100     return _small_blocks.count() + _tree.count();
101   }
102 
103   // Returns total size, in words, of all elements.
104   size_t total_size() const {
105     return _small_blocks.total_size() + _tree.total_size();
106   }
107 
108   // Returns true if empty.
109   bool is_empty() const {
110     return _small_blocks.is_empty() && _tree.is_empty();
111   }
112 
113 };
114 
115 } // namespace metaspace
116 
117 #endif // SHARE_MEMORY_METASPACE_FREEBLOCKS_HPP