Path: blob/master/src/hotspot/share/memory/metaspace/rootChunkArea.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_ROOTCHUNKAREA_HPP26#define SHARE_MEMORY_METASPACE_ROOTCHUNKAREA_HPP2728#include "memory/allocation.hpp"29#include "memory/metaspace/chunklevel.hpp"30#include "utilities/debug.hpp"31#include "utilities/globalDefinitions.hpp"3233class outputStream;3435namespace metaspace {3637class Metachunk;38class MetachunkClosure;39class FreeChunkListVector;40class VirtualSpaceNode;4142// RootChunkArea manages a memory area covering a single root chunk.43//44// Such an area may contain a single root chunk, or a number of chunks the45// root chunk was split into.46//47// RootChunkArea contains the functionality to merge and split chunks in48// buddy allocator fashion.49//5051class RootChunkArea {5253// The base address of this area.54// Todo: this may be somewhat superfluous since RootChunkArea only exist in the55// context of a series of chunks, so the address is somewhat implicit. Remove?56const MetaWord* const _base;5758// The first chunk in this area; if this area is maximally59// folded, this is the root chunk covering the whole area size.60Metachunk* _first_chunk;6162public:6364RootChunkArea(const MetaWord* base);65~RootChunkArea();6667// Initialize: allocate a root node and a root chunk header; return the68// root chunk header. It will be partly initialized.69// Note: this just allocates a memory-less header; memory itself is allocated inside VirtualSpaceNode.70Metachunk* alloc_root_chunk_header(VirtualSpaceNode* node);7172// Given a chunk c, split it recursively until you get a chunk of the given target_level.73//74// The resulting target chunk resides at the same address as the original chunk.75// The resulting splinters are added to freelists.76//77// Returns pointer to the result chunk; the splitted-off chunks are added as78// free chunks to the freelists.79void split(chunklevel_t target_level, Metachunk* c, FreeChunkListVector* freelists);8081// Given a chunk, attempt to merge it recursively with its neighboring chunks.82//83// If successful (merged at least once), returns address of84// the merged chunk; NULL otherwise.85//86// The merged chunks are removed from the freelists.87//88// !!! Please note that if this method returns a non-NULL value, the89// original chunk will be invalid and should not be accessed anymore! !!!90Metachunk* merge(Metachunk* c, FreeChunkListVector* freelists);9192// Given a chunk c, which must be "in use" and must not be a root chunk, attempt to93// enlarge it in place by claiming its trailing buddy.94//95// This will only work if c is the leader of the buddy pair and the trailing buddy is free.96//97// If successful, the follower chunk will be removed from the freelists, the leader chunk c will98// double in size (level decreased by one).99//100// On success, true is returned, false otherwise.101bool attempt_enlarge_chunk(Metachunk* c, FreeChunkListVector* freelists);102103/// range ///104105const MetaWord* base() const { return _base; }106size_t word_size() const { return chunklevel::MAX_CHUNK_WORD_SIZE; }107const MetaWord* end() const { return _base + word_size(); }108109// Direct access to the first chunk (use with care)110Metachunk* first_chunk() { return _first_chunk; }111const Metachunk* first_chunk() const { return _first_chunk; }112113// Returns true if this root chunk area is completely free:114// In that case, it should only contain one chunk (maximally merged, so a root chunk)115// and it should be free.116bool is_free() const;117118//// Debug stuff ////119120#ifdef ASSERT121void check_pointer(const MetaWord* p) const {122assert(p >= _base && p < _base + word_size(),123"pointer " PTR_FORMAT " oob for this root area [" PTR_FORMAT ".." PTR_FORMAT ")",124p2i(p), p2i(_base), p2i(_base + word_size()));125}126void verify() const;127128// This is a separate operation from verify(). We should be able to call verify()129// from almost anywhere, regardless of state, but verify_area_is_ideally_merged()130// can only be called outside split and merge ops.131void verify_area_is_ideally_merged() const;132#endif // ASSERT133134void print_on(outputStream* st) const;135136};137138// RootChunkAreaLUT (lookup table) manages a series of contiguous root chunk areas139// in memory (in the context of a VirtualSpaceNode). It allows finding the containing140// root chunk for any given memory address. It allows for easy iteration over all141// root chunks.142// Beyond that it is unexciting.143class RootChunkAreaLUT {144145// Base address of the whole area.146const MetaWord* const _base;147148// Number of root chunk areas.149const int _num;150151// Array of RootChunkArea objects.152RootChunkArea* _arr;153154#ifdef ASSERT155void check_pointer(const MetaWord* p) const {156assert(p >= base() && p < base() + word_size(), "Invalid pointer");157}158#endif159160// Given an address into this range, return the index into the area array for the161// area this address falls into.162int index_by_address(const MetaWord* p) const {163DEBUG_ONLY(check_pointer(p);)164int idx = (int)((p - base()) / chunklevel::MAX_CHUNK_WORD_SIZE);165assert(idx >= 0 && idx < _num, "Sanity");166return idx;167}168169public:170171RootChunkAreaLUT(const MetaWord* base, size_t word_size);172~RootChunkAreaLUT();173174// Given a memory address into the range this array covers, return the175// corresponding area object. If none existed at this position, create it176// on demand.177RootChunkArea* get_area_by_address(const MetaWord* p) const {178const int idx = index_by_address(p);179RootChunkArea* ra = _arr + idx;180DEBUG_ONLY(ra->check_pointer(p);)181return _arr + idx;182}183184// Access area by its index185int number_of_areas() const { return _num; }186RootChunkArea* get_area_by_index(int index) { assert(index >= 0 && index < _num, "oob"); return _arr + index; }187const RootChunkArea* get_area_by_index(int index) const { assert(index >= 0 && index < _num, "oob"); return _arr + index; }188189/// range ///190191const MetaWord* base() const { return _base; }192size_t word_size() const { return _num * chunklevel::MAX_CHUNK_WORD_SIZE; }193const MetaWord* end() const { return _base + word_size(); }194195// Returns true if all areas in this area table are free (only contain free chunks).196bool is_free() const;197198DEBUG_ONLY(void verify() const;)199200void print_on(outputStream* st) const;201202};203204} // namespace metaspace205206#endif // SHARE_MEMORY_METASPACE_ROOTCHUNKAREA_HPP207208209