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 "memory/virtualspace.hpp"
 30 #include "runtime/globals.hpp"
 31 #include "utilities/exceptions.hpp"
 32 #include "utilities/globalDefinitions.hpp"
 33 
 34 class ClassLoaderData;
 35 class MetaspaceShared;
 36 class MetaspaceTracer;
 37 class Mutex;
 38 class outputStream;
 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   // For quick pointer testing: extent of class space; nullptr if no class space.
 68   static const void* _class_space_start;
 69   static const void* _class_space_end;
 70 
 71 public:
 72 
 73   static const MetaspaceTracer* tracer() { return _tracer; }
 74 
 75  private:
 76 
 77 #ifdef _LP64
 78 
 79   // Reserve a range of memory that is to contain narrow Klass IDs. If "try_in_low_address_ranges"
 80   // is true, we will attempt to reserve memory suitable for zero-based encoding.
 81   static ReservedSpace reserve_address_space_for_compressed_classes(size_t size, bool optimize_for_zero_base);
 82 
 83   // Given a prereserved space, use that to set up the compressed class space list.
 84   static void initialize_class_space(ReservedSpace rs);
 85 
 86   // Returns true if class space has been setup (initialize_class_space).
 87   static bool class_space_is_initialized();
 88 
 89 #endif
 90 
 91  public:
 92 
 93   static void ergo_initialize();
 94   static void global_initialize();
 95   static void post_initialize();
 96 
 97   // Alignment, in bytes, of metaspace mappings
 98   static size_t reserve_alignment()       { return reserve_alignment_words() * BytesPerWord; }
 99   // Alignment, in words, of metaspace mappings
100   static size_t reserve_alignment_words();
101 
102   // The granularity at which Metaspace is committed and uncommitted.
103   // (Todo: Why does this have to be exposed?)
104   static size_t commit_alignment()        { return commit_alignment_words() * BytesPerWord; }
105   static size_t commit_alignment_words();
106 
107   // The largest possible single allocation
108   static size_t max_allocation_word_size();
109 
110   // Minimum allocation alignment, in bytes. All MetaData shall be aligned correclty
111   // to be able to hold 64-bit data types. Unlike malloc, we don't care for larger
112   // data types.
113   static constexpr size_t min_allocation_alignment_bytes = sizeof(uint64_t);
114 
115   // Minimum allocation alignment, in words, Metaspace observes.
116   static constexpr size_t min_allocation_alignment_words = min_allocation_alignment_bytes / BytesPerWord;
117 
118   // Every allocation will get rounded up to the minimum word size.
119   static constexpr size_t min_allocation_word_size = min_allocation_alignment_words;
120 
121   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
122                             MetaspaceObj::Type type, TRAPS);
123 
124   // Non-TRAPS version of allocate which can be called by a non-Java thread, that returns
125   // null on failure.
126   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
127                             MetaspaceObj::Type type);
128 
129   static bool contains(const void* ptr) {
130     return is_in_shared_metaspace(ptr) || // in cds
131            is_in_class_space(ptr) ||      // in class space
132            is_in_nonclass_metaspace(ptr); // in one of the non-class regions?
133   }
134 
135   // kept for now for backward compat reasons, but lets test if callers really need this
136   static bool contains_non_shared(const void* ptr) {
137     return is_in_class_space(ptr) ||      // in class space
138            is_in_nonclass_metaspace(ptr); // in one of the non-class regions?
139   }
140 
141   // Returns true if pointer points into one of the metaspace regions, or
142   // into the class space.
143   static bool is_in_shared_metaspace(const void* ptr);
144 
145   // Returns true if pointer points into one of the non-class-space metaspace regions.
146   static bool is_in_nonclass_metaspace(const void* ptr);
147 
148   // Returns true if ptr points into class space, false if it doesn't or if
149   // there is no class space.
150   static inline bool is_in_class_space(const void* ptr) {
151     return ptr < _class_space_end && ptr >= _class_space_start;
152   }
153 
154   // Free empty virtualspaces
155   static void purge(bool classes_unloaded);
156 
157   static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
158                                    MetaspaceObj::Type type, MetadataType mdtype, TRAPS);
159 
160   static const char* metadata_type_name(Metaspace::MetadataType mdtype);
161 
162   static void print_compressed_class_space(outputStream* st) NOT_LP64({});
163 
164   // Return TRUE only if UseCompressedClassPointers is True.
165   static bool using_class_space() {
166     return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers);
167   }
168 
169   static bool is_class_space_allocation(MetadataType mdType) {
170     return mdType == ClassType && using_class_space();
171   }
172 
173   static bool initialized();
174 
175 };
176 
177 
178 #endif // SHARE_MEMORY_METASPACE_HPP