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_SPACEMANAGER_HPP 26 #define SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/metaspace.hpp" 30 #include "memory/metaspace/blockFreelist.hpp" 31 #include "memory/metaspace/chunkAllocSequence.hpp" 32 #include "memory/metaspace/chunkManager.hpp" 33 #include "memory/metaspace/metachunk.hpp" 34 #include "memory/metaspace/metaspaceCommon.hpp" 35 36 class outputStream; 37 class Mutex; 38 39 namespace metaspace { 40 41 class SpaceManagerStatistics; 42 43 // The SpaceManager: 44 // - keeps a list of chunks-in-use by the class loader, as well as a current chunk used 45 // to allocate from 46 // - keeps a dictionary of free MetaBlocks. Those can be remnants of a retired chunk or 47 // allocations which were not needed anymore for some reason (e.g. releasing half-allocated 48 // structures when class loading fails) 49 50 class SpaceManager : public CHeapObj<mtClass> { 51 52 // Lock handed down from the associated ClassLoaderData. 53 // Protects allocations from this space. 54 Mutex* const _lock; 55 56 // The chunk manager to allocate chunks from. 57 ChunkManager* const _chunk_manager; 58 59 // The chunk allocation strategy to use. 60 const ChunkAllocSequence* const _chunk_alloc_sequence; 61 62 // List of chunks in use by this SpaceManager. Allocations 63 // are done from the current chunk. The list is used for deallocating 64 // chunks when the SpaceManager is freed. 65 MetachunkList _chunks; 66 Metachunk* _current_chunk; 67 68 // Prematurely released metablocks. 69 BlockFreelist* _block_freelist; 70 71 Mutex* lock() const { return _lock; } 72 ChunkManager* chunk_manager() const { return _chunk_manager; } 73 const ChunkAllocSequence* chunk_alloc_sequence() const { return _chunk_alloc_sequence; } 74 75 BlockFreelist* block_freelist() const { return _block_freelist; } 76 void create_block_freelist(); 77 78 // The current chunk is unable to service a request. The remainder of the chunk is 79 // chopped into blocks and fed into the _block_freelists, in the hope of later reuse. 80 void retire_current_chunk(); 81 82 // Given a requested word size, will allocate a chunk large enough to at least fit that 83 // size, but may be larger according to the rules in the ChunkAllocSequence. 84 // Updates counters and adds the chunk to the head of the chunk list. 85 Metachunk* allocate_chunk_to_fit(size_t requested_word_size); 86 87 public: 88 89 SpaceManager(ChunkManager* chunk_manager, const ChunkAllocSequence* alloc_sequence, Mutex* lock); 90 91 ~SpaceManager(); 92 93 // Allocate memory from Metaspace. Will attempt to allocate from the _block_freelists, 94 // failing that, from the current chunk; failing that, attempt to get a new chunk from 95 // the associated ChunkManager. 96 MetaWord* allocate(size_t word_size); 97 98 // Prematurely returns a metaspace allocation to the _block_freelists because it is not 99 // needed anymore. 100 void deallocate(MetaWord* p, size_t word_size); 101 102 // Update statistics. This walks all in-use chunks. 103 void add_to_statistics(SpaceManagerStatistics* out) const; 104 105 // Run verifications. slow=true: verify chunk-internal integrity too. 106 DEBUG_ONLY(void verify(bool slow) const;) 107 108 }; 109 110 } // namespace metaspace 111 112 #endif // SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP