1 /*
 2  * Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef SHARE_MEMORY_METADATAFACTORY_HPP
26 #define SHARE_MEMORY_METADATAFACTORY_HPP
27 
28 #include "classfile/classLoaderData.hpp"
29 #include "memory/classLoaderMetaspace.hpp"
30 #include "oops/array.inline.hpp"
31 #include "utilities/exceptions.hpp"
32 #include "utilities/globalDefinitions.hpp"
33 #include <type_traits>
34 
35 class MetadataFactory : AllStatic {
36  public:
37   template <typename T>
38   static Array<T>* new_array(ClassLoaderData* loader_data, int length, TRAPS) {
39     return new (loader_data, length, THREAD) Array<T>(length);
40   }
41 
42   template <typename T>
43   static Array<T>* new_array(ClassLoaderData* loader_data, int length, T value, TRAPS) {
44     Array<T>* array = new_array<T>(loader_data, length, CHECK_NULL);
45     for (int i = 0; i < length; i++) {
46       array->at_put(i, value);
47     }
48     return array;
49   }
50 
51   // Work-around -- see JDK-8331086.
52   // This API should be used for TrainingData only.
53   template <typename T>
54   static Array<T>* new_array_from_c_heap(int length, MEMFLAGS flags) {
55     return new (length, flags) Array<T>(length);
56   }
57 
58   template <typename T>
59   static void free_array(ClassLoaderData* loader_data, Array<T>* data) {
60     if (data != nullptr) {
61       assert(loader_data != nullptr, "shouldn't pass null");
62       assert(!data->is_shared(), "cannot deallocate array in shared spaces");
63       int size = data->size();
64       loader_data->metaspace_non_null()->deallocate((MetaWord*)data, size, false);
65     }
66   }
67 
68   // Deallocation method for metadata
69   template <class T>
70   static void free_metadata(ClassLoaderData* loader_data, T* md) {
71     if (md != nullptr) {
72       assert(loader_data != nullptr, "shouldn't pass null");
73       int size = md->size();
74       // Call metadata's deallocate function which will deallocate fields and release_C_heap_structures
75       assert(!md->on_stack(), "can't deallocate things on stack");
76       assert(!md->is_shared(), "cannot deallocate if in shared spaces");
77       md->deallocate_contents(loader_data);
78       bool is_klass = md->is_klass();
79       // Call the destructor. This is currently used for MethodData which has a member
80       // that needs to be destructed to release resources. Most Metadata derived classes have noop
81       // destructors and/or cleanup using deallocate_contents.
82       // T is a potentially const or volatile qualified pointer. Remove any const
83       // or volatile so we can call the destructor of the type T points to.
84       using U = std::remove_cv_t<T>;
85       md->~U();
86       loader_data->metaspace_non_null()->deallocate((MetaWord*)md, size, is_klass);
87     }
88   }
89 };
90 
91 #endif // SHARE_MEMORY_METADATAFACTORY_HPP