1 /*
   2  * Copyright (c) 2020, 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_MSCONTEXT_HPP
  27 #define SHARE_MEMORY_METASPACE_MSCONTEXT_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), _vslist(vslist), _cm(cm) {}
  67 
  68   static MetaspaceContext* _nonclass_space_context;
  69   static MetaspaceContext* _class_space_context;
  70 
  71 public:
  72 
  73   // Destroys the context: deletes chunkmanager and virtualspacelist.
  74   // If this is a non-expandable context over an existing space, that space remains
  75   // untouched, otherwise all memory is unmapped.
  76   ~MetaspaceContext();
  77 
  78   VirtualSpaceList* vslist() { return _vslist; }
  79   ChunkManager* cm() { return _cm; }
  80 
  81   // Create a new, empty, expandable metaspace context.
  82   static MetaspaceContext* create_expandable_context(const char* name, CommitLimiter* commit_limiter);
  83 
  84   // Create a new, empty, non-expandable metaspace context atop of an externally provided space.
  85   static MetaspaceContext* create_nonexpandable_context(const char* name, ReservedSpace rs, CommitLimiter* commit_limiter);
  86 
  87   void print_on(outputStream* st) const;
  88 
  89   DEBUG_ONLY(void verify() const;)
  90 
  91   static void initialize_class_space_context(ReservedSpace rs);
  92   static void initialize_nonclass_space_context();
  93 
  94   // Returns pointer to the global metaspace context.
  95   // If compressed class space is active, this contains the non-class-space allocations.
  96   // If compressed class space is inactive, this contains all metaspace allocations.
  97   static MetaspaceContext* context_nonclass()     { return _nonclass_space_context; }
  98 
  99   // Returns pointer to the global class space context, if compressed class space is active,
 100   // NULL otherwise.
 101   static MetaspaceContext* context_class()        { return _class_space_context; }
 102 
 103 };
 104 
 105 } // end namespace
 106 
 107 #endif // SHARE_MEMORY_METASPACE_MSCONTEXT_HPP
 108