1 /*
  2  * Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2017, 2021 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 #ifndef SHARE_MEMORY_METASPACE_HPP
 26 #define SHARE_MEMORY_METASPACE_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "runtime/globals.hpp"
 30 #include "utilities/exceptions.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 
 33 class ClassLoaderData;
 34 class MetaspaceShared;
 35 class MetaspaceTracer;
 36 class Mutex;
 37 class outputStream;
 38 class ReservedSpace;
 39 
 40 ////////////////// Metaspace ///////////////////////
 41 
 42 // Namespace for important central static functions
 43 // (auxiliary stuff goes into MetaspaceUtils)
 44 class Metaspace : public AllStatic {
 45 
 46   friend class MetaspaceShared;
 47 
 48 public:
 49   enum MetadataType {
 50     ClassType,
 51     NonClassType,
 52     MetadataTypeCount
 53   };
 54   enum MetaspaceType {
 55     ZeroMetaspaceType = 0,
 56     StandardMetaspaceType = ZeroMetaspaceType,
 57     BootMetaspaceType = StandardMetaspaceType + 1,
 58     ClassMirrorHolderMetaspaceType = BootMetaspaceType + 1,
 59     ReflectionMetaspaceType = ClassMirrorHolderMetaspaceType + 1,
 60     MetaspaceTypeCount
 61   };
 62 
 63 private:
 64 
 65   static const MetaspaceTracer* _tracer;
 66 
 67   static bool _initialized;
 68 
 69 public:
 70 
 71   static const MetaspaceTracer* tracer() { return _tracer; }
 72 
 73  private:
 74 
 75 #ifdef _LP64
 76 
 77   // Reserve a range of memory that is to contain narrow Klass IDs. If "try_in_low_address_ranges"
 78   // is true, we will attempt to reserve memory suitable for zero-based encoding.
 79   static ReservedSpace reserve_address_space_for_compressed_classes(size_t size, bool optimize_for_zero_base);
 80 
 81   // Given a prereserved space, use that to set up the compressed class space list.
 82   static void initialize_class_space(ReservedSpace rs);
 83 
 84   // Returns true if class space has been setup (initialize_class_space).
 85   static bool class_space_is_initialized();
 86 
 87 #endif
 88 
 89  public:
 90 
 91   static void ergo_initialize();
 92   static void global_initialize();
 93   static void post_initialize();
 94 
 95   // Alignment, in bytes, of metaspace mappings
 96   static size_t reserve_alignment()       { return reserve_alignment_words() * BytesPerWord; }
 97   // Alignment, in words, of metaspace mappings
 98   static size_t reserve_alignment_words();
 99 
100   // The granularity at which Metaspace is committed and uncommitted.
101   // (Todo: Why does this have to be exposed?)
102   static size_t commit_alignment()        { return commit_alignment_words() * BytesPerWord; }
103   static size_t commit_alignment_words();
104 
105   // The largest possible single allocation
106   static size_t max_allocation_word_size();
107 
108   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
109                             MetaspaceObj::Type type, TRAPS);
110 
111   // Non-TRAPS version of allocate which can be called by a non-Java thread, that returns
112   // null on failure.
113   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
114                             MetaspaceObj::Type type);
115 
116   static bool contains(const void* ptr);
117   static bool contains_non_shared(const void* ptr);
118 
119   // Free empty virtualspaces
120   static void purge(bool classes_unloaded);
121 
122   static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
123                                    MetaspaceObj::Type type, MetadataType mdtype, TRAPS);
124 
125   static const char* metadata_type_name(Metaspace::MetadataType mdtype);
126 
127   static void print_compressed_class_space(outputStream* st) NOT_LP64({});
128 
129   // Return TRUE only if UseCompressedClassPointers is True.
130   static bool using_class_space() {
131     return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers);
132   }
133 
134   static bool is_class_space_allocation(MetadataType mdType) {
135     return mdType == ClassType && using_class_space();
136   }
137 
138   static bool initialized();
139 
140 };
141 
142 
143 #endif // SHARE_MEMORY_METASPACE_HPP