/* * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #ifndef SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP #define SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP #include "memory/allocation.hpp" #include "memory/metaspace.hpp" #include "memory/metaspace/blockFreelist.hpp" #include "memory/metaspace/chunkAllocSequence.hpp" #include "memory/metaspace/chunkManager.hpp" #include "memory/metaspace/metachunk.hpp" #include "memory/metaspace/metaspaceCommon.hpp" class outputStream; class Mutex; namespace metaspace { class SpaceManagerStatistics; // The SpaceManager: // - keeps a list of chunks-in-use by the class loader, as well as a current chunk used // to allocate from // - keeps a dictionary of free MetaBlocks. Those can be remnants of a retired chunk or // allocations which were not needed anymore for some reason (e.g. releasing half-allocated // structures when class loading fails) class SpaceManager : public CHeapObj { // Lock handed down from the associated ClassLoaderData. // Protects allocations from this space. Mutex* const _lock; // The chunk manager to allocate chunks from. ChunkManager* const _chunk_manager; // The chunk allocation strategy to use. const ChunkAllocSequence* const _chunk_alloc_sequence; // List of chunks in use by this SpaceManager. Allocations // are done from the current chunk. The list is used for deallocating // chunks when the SpaceManager is freed. MetachunkList _chunks; Metachunk* _current_chunk; // Prematurely released metablocks. BlockFreelist* _block_freelist; // Statistics // Running counters. // Note: capacity = used + free + waste + overhead. We do not keep running counters for // free and waste. Their sum can be deduced from the three other values. // size_t _overhead_words; // size_t _capacity_words; // size_t _used_words; uintx _num_chunks_by_level[chklvl::NUM_CHUNK_LEVELS]; uintx _num_chunks_total; Mutex* lock() const { return _lock; } ChunkManager* chunk_manager() const { return _chunk_manager; } const ChunkAllocSequence* chunk_alloc_sequence() const { return _chunk_alloc_sequence; } BlockFreelist* block_freelist() const { return _block_freelist; } void create_block_freelist(); // The current chunk is unable to service a request. The remainder of the chunk is // chopped into blocks and fed into the _block_freelists, in the hope of later reuse. void retire_current_chunk(); // Given a requested word size, will allocate a chunk large enough to at least fit that // size, but may be larger according to the rules in the ChunkAllocSequence. // Updates counters and adds the chunk to the head of the chunk list. Metachunk* allocate_chunk_to_fit(size_t requested_word_size); public: SpaceManager(ChunkManager* chunk_manager, const ChunkAllocSequence* alloc_sequence, Mutex* lock); ~SpaceManager(); // Allocate memory from Metaspace. Will attempt to allocate from the _block_freelists, // failing that, from the current chunk; failing that, attempt to get a new chunk from // the associated ChunkManager. MetaWord* allocate(size_t word_size); // Prematurely returns a metaspace allocation to the _block_freelists because it is not // needed anymore. void deallocate(MetaWord* p, size_t word_size); // Update statistics. This walks all in-use chunks. void add_to_statistics(SpaceManagerStatistics* out) const; // Run verifications. slow=true: verify chunk-internal integrity too. DEBUG_ONLY(void locked_verify(bool slow) const;) }; } // namespace metaspace #endif // SHARE_MEMORY_METASPACE_SPACEMANAGER_HPP