Path: blob/master/src/hotspot/share/memory/metaspace/chunkHeaderPool.hpp
40957 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#ifndef SHARE_MEMORY_METASPACE_CHUNKHEADERPOOL_HPP26#define SHARE_MEMORY_METASPACE_CHUNKHEADERPOOL_HPP2728#include "memory/allocation.hpp"29#include "memory/metaspace/counters.hpp"30#include "memory/metaspace/metachunk.hpp"31#include "memory/metaspace/metachunkList.hpp"32#include "utilities/debug.hpp"33#include "utilities/globalDefinitions.hpp"3435namespace metaspace {3637// Chunk headers (Metachunk objects) are separate entities from their payload.38// Since they are allocated and released frequently in the course of buddy allocation39// (splitting, merging chunks happens often) we want allocation of them fast. Therefore40// we keep them in a simple pool (somewhat like a primitive slab allocator).4142class ChunkHeaderPool : public CHeapObj<mtMetaspace> {4344static const int SlabCapacity = 128;4546struct Slab : public CHeapObj<mtMetaspace> {47Slab* _next;48int _top;49Metachunk _elems [SlabCapacity];50Slab() : _next(NULL), _top(0) {51for (int i = 0; i < SlabCapacity; i++) {52_elems[i].clear();53}54}55};5657IntCounter _num_slabs;58Slab* _first_slab;59Slab* _current_slab;6061IntCounter _num_handed_out;6263MetachunkList _freelist;6465void allocate_new_slab();6667static ChunkHeaderPool* _chunkHeaderPool;6869public:7071ChunkHeaderPool();7273~ChunkHeaderPool();7475// Allocates a Metachunk structure. The structure is uninitialized.76Metachunk* allocate_chunk_header() {77DEBUG_ONLY(verify());7879Metachunk* c = NULL;80c = _freelist.remove_first();81assert(c == NULL || c->is_dead(), "Not a freelist chunk header?");82if (c == NULL) {83if (_current_slab == NULL ||84_current_slab->_top == SlabCapacity) {85allocate_new_slab();86assert(_current_slab->_top < SlabCapacity, "Sanity");87}88c = _current_slab->_elems + _current_slab->_top;89_current_slab->_top++;90}91_num_handed_out.increment();92// By contract, the returned structure is uninitialized.93// Zap to make this clear.94DEBUG_ONLY(c->zap_header(0xBB);)9596return c;97}9899void return_chunk_header(Metachunk* c) {100// We only ever should return free chunks, since returning chunks101// happens only on merging and merging only works with free chunks.102assert(c != NULL && c->is_free(), "Sanity");103#ifdef ASSERT104// In debug, fill dead header with pattern.105c->zap_header(0xCC);106c->set_next(NULL);107c->set_prev(NULL);108#endif109c->set_dead();110_freelist.add(c);111_num_handed_out.decrement();112}113114// Returns number of allocated elements.115int used() const { return _num_handed_out.get(); }116117// Returns number of elements in free list.118int freelist_size() const { return _freelist.count(); }119120// Returns size of memory used.121size_t memory_footprint_words() const;122123DEBUG_ONLY(void verify() const;)124125static void initialize();126127// Returns reference to the one global chunk header pool.128static ChunkHeaderPool* pool() { return _chunkHeaderPool; }129130};131132} // namespace metaspace133134#endif // SHARE_MEMORY_METASPACE_CHUNKHEADERPOOL_HPP135136137