1 /*
  2  * Copyright (c) 2011, 2021, 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 at an address suitable for en/decoding narrow
 78   // Klass pointers (see: CompressedClassPointers::is_valid_base()).
 79   // The returned address shall both be suitable as a compressed class pointers
 80   //  base, and aligned to Metaspace::reserve_alignment (which is equal to or a
 81   //  multiple of allocation granularity).
 82   // On error, returns an unreserved space.
 83   static ReservedSpace reserve_address_space_for_compressed_classes(size_t size);
 84 
 85   // Given a prereserved space, use that to set up the compressed class space list.
 86   static void initialize_class_space(ReservedSpace rs);
 87 
 88   // Returns true if class space has been setup (initialize_class_space).
 89   static bool class_space_is_initialized();
 90 
 91 #endif
 92 
 93  public:
 94 
 95   static void ergo_initialize();
 96   static void global_initialize();
 97   static void post_initialize();
 98 
 99   // Alignment, in bytes, of metaspace mappings
100   static size_t reserve_alignment()       { return reserve_alignment_words() * BytesPerWord; }
101   // Alignment, in words, of metaspace mappings
102   static size_t reserve_alignment_words();
103 
104   // The granularity at which Metaspace is committed and uncommitted.
105   // (Todo: Why does this have to be exposed?)
106   static size_t commit_alignment()        { return commit_alignment_words() * BytesPerWord; }
107   static size_t commit_alignment_words();
108 
109   // The largest possible single allocation
110   static size_t max_allocation_word_size();
111 
112   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
113                             MetaspaceObj::Type type, TRAPS);
114 
115   // Non-TRAPS version of allocate which can be called by a non-Java thread, that returns
116   // NULL on failure.
117   static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
118                             MetaspaceObj::Type type);
119 
120   static bool contains(const void* ptr);
121   static bool contains_non_shared(const void* ptr);
122 
123   // Free empty virtualspaces
124   static void purge();
125 
126   static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
127                                    MetaspaceObj::Type type, MetadataType mdtype, TRAPS);
128 
129   static const char* metadata_type_name(Metaspace::MetadataType mdtype);
130 
131   static void print_compressed_class_space(outputStream* st) NOT_LP64({});
132 
133   // Return TRUE only if UseCompressedClassPointers is True.
134   static bool using_class_space() {
135     return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers);
136   }
137 
138   static bool is_class_space_allocation(MetadataType mdType) {
139     return mdType == ClassType && using_class_space();
140   }
141 
142   static bool initialized();
143 
144 };
145 
146 
147 #endif // SHARE_MEMORY_METASPACE_HPP