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/metaspaceCommon.hpp" 34 35 class outputStream; 36 class Mutex; 37 38 namespace metaspace { 39 40 41 // The SpaceManager: 42 // - keeps a list of chunks-in-use by the class loader, as well as a current chunk used 43 // to allocate from 44 // - keeps a dictionary of free MetaBlocks. Those can be remnants of a retired chunk or 45 // allocations which were not needed anymore for some reason (e.g. releasing half-allocated 46 // structures when class loading fails) 47 48 class SpaceManager : public CHeapObj<mtClass> { 49 50 // Lock handed down from the associated ClassLoaderData. 51 // Protects allocations from this space. 52 Mutex* const _lock; 53 54 // The chunk manager to allocate chunks from. 55 ChunkManager* const _chunk_manager; 56 57 // The chunk allocation strategy to use. 58 const ChunkAllocSequence* const _chunk_alloc_sequence; 59 60 // List of chunks in use by this SpaceManager. Allocations 61 // are done from the current chunk. The list is used for deallocating 62 // chunks when the SpaceManager is freed. 63 Metachunk* _first_chunk; 64 Metachunk* _current_chunk; 65 66 // Prematurely released metablocks. 67 BlockFreelist* _block_freelist; 68 69 70 // Statistics 71 72 // Running counters. 73 // Note: capacity = used + free + waste + overhead. We do not keep running counters for 74 // free and waste. Their sum can be deduced from the three other values. 75 size_t _overhead_words; 76 size_t _capacity_words; 77 size_t _used_words; 78 uint32_t _num_chunks_by_type[NUM_CHUNK_LEVELS]; 79 80 81 Mutex* lock() const { return _lock; } 82 ChunkManager* chunk_manager() const { return _chunk_manager; } 83 const ChunkAllocSequence* chunk_alloc_sequence() const { return _chunk_alloc_sequence; } 84 85 Metachunk* first_chunk() { return _first_chunk; } 86 BlockFreelist* block_freelist(); // created on demand only 87 88 // The current chunk is unable to service a request. The remainder of the chunk is 89 // chopped into blocks and fed into the _block_freelists, in the hope of later reuse. 90 void retire_current_chunk(); 91 92 public: 93 94 SpaceManager(ChunkManager* chunk_manager, const ChunkAllocSequence* alloc_sequence, Mutex* lock); 95 96 ~SpaceManager(); 97 98 // Allocate memory from Metaspace. Will attempt to allocate from the _block_freelists, 99 // failing that, from the current chunk; failing that, attempt to get a new chunk from 100 // the associated ChunkManager. 101 MetaWord* allocate(size_t word_size); 102 103 // Prematurely returns a metaspace allocation to the _block_freelists because it is not 104 // needed anymore. 105 void deallocate(MetaWord* p, size_t word_size); 106 107 // Run verifications. slow=true: verify chunk-internal integrity too. 108 DEBUG_ONLY(void locked_verify(bool slow) const;) 109 110 }; 111 112 } // namespace metaspace 113 114 #endif // SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP