1 /*
  2  * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2020 SAP SE. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_MEMORY_METASPACE_METASPACECONTEXT_HPP
 27 #define SHARE_MEMORY_METASPACE_METASPACECONTEXT_HPP
 28 
 29 #include "memory/allocation.hpp"
 30 #include "memory/virtualspace.hpp"
 31 #include "utilities/debug.hpp"
 32 
 33 class outputStream;
 34 
 35 namespace metaspace {
 36 
 37 class ChunkManager;
 38 class VirtualSpaceList;
 39 class CommitLimiter;
 40 
 41 // MetaspaceContext is a convenience bracket around:
 42 //
 43 // - a VirtualSpaceList managing a memory area used for Metaspace
 44 // - a ChunkManager sitting atop of that which manages chunk freelists
 45 //
 46 // In a normal VM only one or two of these contexts ever exist: one for the metaspace, and
 47 //  optionally another one for the compressed class space.
 48 //
 49 // For tests more contexts may be created, and this would also be a way to use Metaspace
 50 //  for things other than class metadata. We would have to work on the naming then.
 51 //
 52 // - (Future TODO): Context should own a lock to guard it. Currently this stuff is guarded
 53 //     by one global lock, the slightly misnamed Metaspace_expandlock, but that one
 54 //     should be split into one per context.
 55 // - (Future TODO): Context can/should have its own allocation alignment. That way we
 56 //     can have different alignment between class space and non-class metaspace. That could
 57 //     help optimize compressed class pointer encoding, see discussion for JDK-8244943).
 58 
 59 class MetaspaceContext : public CHeapObj<mtMetaspace> {
 60 
 61   const char* const _name;
 62   VirtualSpaceList* const _vslist;
 63   ChunkManager* const _cm;
 64 
 65   MetaspaceContext(const char* name, VirtualSpaceList* vslist, ChunkManager* cm) :
 66     _name(name),
 67     _vslist(vslist),
 68     _cm(cm)
 69   {}
 70 
 71   static MetaspaceContext* _nonclass_space_context;
 72   static MetaspaceContext* _class_space_context;
 73 
 74 public:
 75 
 76   // Destroys the context: deletes chunkmanager and virtualspacelist.
 77   // If this is a non-expandable context over an existing space, that space remains
 78   // untouched, otherwise all memory is unmapped.
 79   ~MetaspaceContext();
 80 
 81   VirtualSpaceList* vslist() { return _vslist; }
 82   ChunkManager* cm() { return _cm; }
 83 
 84   // Create a new, empty, expandable metaspace context.
 85   static MetaspaceContext* create_expandable_context(const char* name, CommitLimiter* commit_limiter);
 86 
 87   // Create a new, empty, non-expandable metaspace context atop of an externally provided space.
 88   static MetaspaceContext* create_nonexpandable_context(const char* name, ReservedSpace rs, CommitLimiter* commit_limiter);
 89 
 90   void print_on(outputStream* st) const;
 91 
 92   DEBUG_ONLY(void verify() const;)
 93 
 94   static void initialize_class_space_context(ReservedSpace rs);
 95   static void initialize_nonclass_space_context();
 96 
 97   // Returns pointer to the global metaspace context.
 98   // If compressed class space is active, this contains the non-class-space allocations.
 99   // If compressed class space is inactive, this contains all metaspace allocations.
100   static MetaspaceContext* context_nonclass()     { return _nonclass_space_context; }
101 
102   // Returns pointer to the global class space context, if compressed class space is active,
103   // null otherwise.
104   static MetaspaceContext* context_class()        { return _class_space_context; }
105 
106 };
107 
108 } // end namespace
109 
110 #endif // SHARE_MEMORY_METASPACE_METASPACECONTEXT_HPP
111