< prev index next >

src/hotspot/share/oops/arrayKlass.cpp

Print this page

  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 #include "cds/aotMetaspace.hpp"
 26 #include "cds/cdsConfig.hpp"
 27 #include "classfile/javaClasses.hpp"
 28 #include "classfile/moduleEntry.hpp"

 29 #include "classfile/vmClasses.hpp"
 30 #include "classfile/vmSymbols.hpp"
 31 #include "gc/shared/collectedHeap.inline.hpp"
 32 #include "jvmtifiles/jvmti.h"
 33 #include "memory/metaspaceClosure.hpp"
 34 #include "memory/resourceArea.hpp"
 35 #include "memory/universe.hpp"
 36 #include "oops/arrayKlass.inline.hpp"
 37 #include "oops/arrayOop.hpp"
 38 #include "oops/instanceKlass.hpp"
 39 #include "oops/klass.inline.hpp"

 40 #include "oops/objArrayOop.hpp"
 41 #include "oops/oop.inline.hpp"

 42 #include "runtime/handles.inline.hpp"
 43 
 44 ArrayKlass::ArrayKlass() {
 45   assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
 46 }
 47 
 48 int ArrayKlass::static_size(int header_size) {
 49   // size of an array klass object
 50   assert(header_size <= InstanceKlass::header_size(), "bad header size");
 51   // If this assert fails, see comments in base_create_array_klass.
 52   header_size = InstanceKlass::header_size();
 53   int vtable_len = Universe::base_vtable_size();
 54   int size = header_size + vtable_len;
 55   return align_metadata_size(size);
 56 }
 57 
 58 
 59 InstanceKlass* ArrayKlass::java_super() const {
 60   if (super() == nullptr)  return nullptr;  // bootstrap case
 61   // Array klasses have primary supertypes which are not reported to Java.

 71 
 72 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
 73 Klass* ArrayKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
 74   // There are no fields in an array klass but look to the super class (Object)
 75   assert(super(), "super klass must be present");
 76   return super()->find_field(name, sig, fd);
 77 }
 78 
 79 Method* ArrayKlass::uncached_lookup_method(const Symbol* name,
 80                                            const Symbol* signature,
 81                                            OverpassLookupMode overpass_mode,
 82                                            PrivateLookupMode private_mode) const {
 83   // There are no methods in an array klass but the super class (Object) has some
 84   assert(super(), "super klass must be present");
 85   // Always ignore overpass methods in superclasses, although technically the
 86   // super klass of an array, (j.l.Object) should not have
 87   // any overpass methods present.
 88   return super()->uncached_lookup_method(name, signature, OverpassLookupMode::skip, private_mode);
 89 }
 90 
 91 ArrayKlass::ArrayKlass(Symbol* name, KlassKind kind) :
 92   Klass(kind),
 93   _dimension(1),

 94   _higher_dimension(nullptr),
 95   _lower_dimension(nullptr) {
 96   // Arrays don't add any new methods, so their vtable is the same size as
 97   // the vtable of klass Object.
 98   set_vtable_length(Universe::base_vtable_size());
 99   set_name(name);
100   set_super(Universe::is_bootstrapping() ? nullptr : vmClasses::Object_klass());
101   set_layout_helper(Klass::_lh_neutral_value);
102   set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
103   JFR_ONLY(INIT_ID(this);)
104   log_array_class_load(this);
105 }
106 



















107 
108 // Initialization of vtables and mirror object is done separately from base_create_array_klass,
109 // since a GC can happen. At this point all instance variables of the ArrayKlass must be setup.
110 void ArrayKlass::complete_create_array_klass(ArrayKlass* k, Klass* super_klass, ModuleEntry* module_entry, TRAPS) {
111   k->initialize_supers(super_klass, nullptr, CHECK);
112   k->vtable().initialize_vtable();
113 
114   // During bootstrapping, before java.base is defined, the module_entry may not be present yet.
115   // These classes will be put on a fixup list and their module fields will be patched once
116   // java.base is defined.
117   assert((module_entry != nullptr) || ((module_entry == nullptr) && !ModuleEntryTable::javabase_defined()),
118          "module entry not available post " JAVA_BASE_NAME " definition");
119   oop module_oop = (module_entry != nullptr) ? module_entry->module_oop() : (oop)nullptr;
120   java_lang_Class::create_mirror(k, Handle(THREAD, k->class_loader()), Handle(THREAD, module_oop), Handle(), Handle(), CHECK);









121 }
122 
123 ArrayKlass* ArrayKlass::array_klass(int n, TRAPS) {
124 
125   assert(dimension() <= n, "check order of chain");
126   int dim = dimension();
127   if (dim == n) return this;
128 
129   // lock-free read needs acquire semantics
130   if (higher_dimension_acquire() == nullptr) {
131 
132     // Ensure atomic creation of higher dimensions
133     RecursiveLocker rl(MultiArray_lock, THREAD);
134 
135     if (higher_dimension() == nullptr) {
136       // Create multi-dim klass object and link them together
137       ObjArrayKlass* ak =
138           ObjArrayKlass::allocate_objArray_klass(class_loader_data(), dim + 1, this, CHECK_NULL);
139       // use 'release' to pair with lock-free load
140       release_set_higher_dimension(ak);
141       assert(ak->lower_dimension() == this, "lower dimension mismatch");
142     }
143   }
144 
145   ObjArrayKlass* ak = higher_dimension();
146   assert(ak != nullptr, "should be set");
147   THREAD->check_possible_safepoint();
148   return ak->array_klass(n, THREAD);
149 }
150 
151 ArrayKlass* ArrayKlass::array_klass_or_null(int n) {
152 
153   assert(dimension() <= n, "check order of chain");
154   int dim = dimension();
155   if (dim == n) return this;
156 
157   // lock-free read needs acquire semantics
158   if (higher_dimension_acquire() == nullptr) {

166 ArrayKlass* ArrayKlass::array_klass(TRAPS) {
167   return array_klass(dimension() +  1, THREAD);
168 }
169 
170 ArrayKlass* ArrayKlass::array_klass_or_null() {
171   return array_klass_or_null(dimension() +  1);
172 }
173 
174 
175 GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots,
176                                                             Array<InstanceKlass*>* transitive_interfaces) {
177   // interfaces = { cloneable_klass, serializable_klass };
178   assert(num_extra_slots == 0, "sanity of primitive array type");
179   assert(transitive_interfaces == nullptr, "sanity");
180   // Must share this for correct bootstrapping!
181   set_secondary_supers(Universe::the_array_interfaces_array(),
182                        Universe::the_array_interfaces_bitmap());
183   return nullptr;
184 }
185 




186 // JVMTI support
187 
188 jint ArrayKlass::jvmti_class_status() const {
189   return JVMTI_CLASS_STATUS_ARRAY;
190 }
191 
192 void ArrayKlass::metaspace_pointers_do(MetaspaceClosure* it) {
193   Klass::metaspace_pointers_do(it);
194 
195   ResourceMark rm;
196   log_trace(aot)("Iter(ArrayKlass): %p (%s)", this, external_name());
197 
198   // need to cast away volatile
199   it->push((Klass**)&_higher_dimension);
200   it->push((Klass**)&_lower_dimension);
201 }
202 
203 #if INCLUDE_CDS
204 void ArrayKlass::remove_unshareable_info() {
205   Klass::remove_unshareable_info();
206   if (_higher_dimension != nullptr) {
207     ArrayKlass *ak = higher_dimension();
208     ak->remove_unshareable_info();
209   }
210 }
211 
212 void ArrayKlass::remove_java_mirror() {
213   Klass::remove_java_mirror();
214   if (_higher_dimension != nullptr) {
215     ArrayKlass *ak = higher_dimension();
216     ak->remove_java_mirror();
217   }
218 }
219 
220 void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
221   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
222   // Klass recreates the component mirror also
223 
224   if (_higher_dimension != nullptr) {
225     ArrayKlass *ak = higher_dimension();
226     log_array_class_load(ak);
227     ak->restore_unshareable_info(loader_data, protection_domain, CHECK);
228   }
229 }
230 
231 void ArrayKlass::cds_print_value_on(outputStream* st) const {
232   assert(is_klass(), "must be klass");
233   st->print("      - array: %s", internal_name());
234   if (_higher_dimension != nullptr) {
235     ArrayKlass* ak = higher_dimension();
236     st->cr();
237     ak->cds_print_value_on(st);
238   }
239 }
240 #endif // INCLUDE_CDS
241 
242 void ArrayKlass::log_array_class_load(Klass* k) {
243   LogTarget(Debug, class, load, array) lt;
244   if (lt.is_enabled()) {
245     LogStream ls(lt);

  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 #include "cds/aotMetaspace.hpp"
 26 #include "cds/cdsConfig.hpp"
 27 #include "classfile/javaClasses.hpp"
 28 #include "classfile/moduleEntry.hpp"
 29 #include "classfile/symbolTable.hpp"
 30 #include "classfile/vmClasses.hpp"
 31 #include "classfile/vmSymbols.hpp"
 32 #include "gc/shared/collectedHeap.inline.hpp"
 33 #include "jvmtifiles/jvmti.h"
 34 #include "memory/metaspaceClosure.hpp"
 35 #include "memory/resourceArea.hpp"
 36 #include "memory/universe.hpp"
 37 #include "oops/arrayKlass.inline.hpp"
 38 #include "oops/arrayOop.hpp"
 39 #include "oops/instanceKlass.hpp"
 40 #include "oops/klass.inline.hpp"
 41 #include "oops/objArrayKlass.hpp"
 42 #include "oops/objArrayOop.hpp"
 43 #include "oops/oop.inline.hpp"
 44 #include "oops/refArrayKlass.hpp"
 45 #include "runtime/handles.inline.hpp"
 46 
 47 ArrayKlass::ArrayKlass() {
 48   assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
 49 }
 50 
 51 int ArrayKlass::static_size(int header_size) {
 52   // size of an array klass object
 53   assert(header_size <= InstanceKlass::header_size(), "bad header size");
 54   // If this assert fails, see comments in base_create_array_klass.
 55   header_size = InstanceKlass::header_size();
 56   int vtable_len = Universe::base_vtable_size();
 57   int size = header_size + vtable_len;
 58   return align_metadata_size(size);
 59 }
 60 
 61 
 62 InstanceKlass* ArrayKlass::java_super() const {
 63   if (super() == nullptr)  return nullptr;  // bootstrap case
 64   // Array klasses have primary supertypes which are not reported to Java.

 74 
 75 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
 76 Klass* ArrayKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
 77   // There are no fields in an array klass but look to the super class (Object)
 78   assert(super(), "super klass must be present");
 79   return super()->find_field(name, sig, fd);
 80 }
 81 
 82 Method* ArrayKlass::uncached_lookup_method(const Symbol* name,
 83                                            const Symbol* signature,
 84                                            OverpassLookupMode overpass_mode,
 85                                            PrivateLookupMode private_mode) const {
 86   // There are no methods in an array klass but the super class (Object) has some
 87   assert(super(), "super klass must be present");
 88   // Always ignore overpass methods in superclasses, although technically the
 89   // super klass of an array, (j.l.Object) should not have
 90   // any overpass methods present.
 91   return super()->uncached_lookup_method(name, signature, OverpassLookupMode::skip, private_mode);
 92 }
 93 
 94 ArrayKlass::ArrayKlass(Symbol* name, KlassKind kind, ArrayProperties props, markWord prototype_header) :
 95 Klass(kind, prototype_header),
 96   _dimension(1),
 97   _properties(props),
 98   _higher_dimension(nullptr),
 99   _lower_dimension(nullptr) {
100   // Arrays don't add any new methods, so their vtable is the same size as
101   // the vtable of klass Object.
102   set_vtable_length(Universe::base_vtable_size());
103   set_name(name);
104   set_super(Universe::is_bootstrapping() ? nullptr : vmClasses::Object_klass());
105   set_layout_helper(Klass::_lh_neutral_value);
106   set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
107   JFR_ONLY(INIT_ID(this);)
108   log_array_class_load(this);
109 }
110 
111 Symbol* ArrayKlass::create_element_klass_array_name(Klass* element_klass, TRAPS) {
112   ResourceMark rm(THREAD);
113   Symbol* name = nullptr;
114   char *name_str = element_klass->name()->as_C_string();
115   int len = element_klass->name()->utf8_length();
116   char *new_str = NEW_RESOURCE_ARRAY(char, len + 4);
117   int idx = 0;
118   new_str[idx++] = JVM_SIGNATURE_ARRAY;
119   if (element_klass->is_instance_klass()) { // it could be an array or simple type
120     new_str[idx++] = JVM_SIGNATURE_CLASS;
121   }
122   memcpy(&new_str[idx], name_str, len * sizeof(char));
123   idx += len;
124   if (element_klass->is_instance_klass()) {
125     new_str[idx++] = JVM_SIGNATURE_ENDCLASS;
126   }
127   new_str[idx++] = '\0';
128   return SymbolTable::new_symbol(new_str);
129 }
130 
131 // Initialization of vtables and mirror object is done separately from base_create_array_klass,
132 // since a GC can happen. At this point all instance variables of the ArrayKlass must be setup.
133 void ArrayKlass::complete_create_array_klass(ArrayKlass* k, Klass* super_klass, ModuleEntry* module_entry, TRAPS) {
134   k->initialize_supers(super_klass, nullptr, CHECK);
135   k->vtable().initialize_vtable();
136 
137   // During bootstrapping, before java.base is defined, the module_entry may not be present yet.
138   // These classes will be put on a fixup list and their module fields will be patched once
139   // java.base is defined.
140   assert((module_entry != nullptr) || ((module_entry == nullptr) && !ModuleEntryTable::javabase_defined()),
141          "module entry not available post " JAVA_BASE_NAME " definition");
142   oop module_oop = (module_entry != nullptr) ? module_entry->module_oop() : (oop)nullptr;
143 
144   if (k->is_refArray_klass() || k->is_flatArray_klass()) {
145     assert(super_klass != nullptr, "Must be");
146     assert(k->super() != nullptr, "Must be");
147     assert(k->super() == super_klass, "Must be");
148     Handle mirror(THREAD, super_klass->java_mirror());
149     k->set_java_mirror(mirror);
150   } else {
151     java_lang_Class::create_mirror(k, Handle(THREAD, k->class_loader()), Handle(THREAD, module_oop), Handle(), Handle(), CHECK);
152   }
153 }
154 
155 ArrayKlass* ArrayKlass::array_klass(int n, TRAPS) {
156 
157   assert(dimension() <= n, "check order of chain");
158   int dim = dimension();
159   if (dim == n) return this;
160 
161   // lock-free read needs acquire semantics
162   if (higher_dimension_acquire() == nullptr) {
163 
164     // Ensure atomic creation of higher dimensions
165     RecursiveLocker rl(MultiArray_lock, THREAD);
166 
167     if (higher_dimension() == nullptr) {
168       // Create multi-dim klass object and link them together
169       ObjArrayKlass* ak = RefArrayKlass::allocate_objArray_klass(class_loader_data(), dim + 1, this, CHECK_NULL);

170       // use 'release' to pair with lock-free load
171       release_set_higher_dimension(ak);
172       assert(ak->lower_dimension() == this, "lower dimension mismatch");
173     }
174   }
175 
176   ObjArrayKlass* ak = higher_dimension();
177   assert(ak != nullptr, "should be set");
178   THREAD->check_possible_safepoint();
179   return ak->array_klass(n, THREAD);
180 }
181 
182 ArrayKlass* ArrayKlass::array_klass_or_null(int n) {
183 
184   assert(dimension() <= n, "check order of chain");
185   int dim = dimension();
186   if (dim == n) return this;
187 
188   // lock-free read needs acquire semantics
189   if (higher_dimension_acquire() == nullptr) {

197 ArrayKlass* ArrayKlass::array_klass(TRAPS) {
198   return array_klass(dimension() +  1, THREAD);
199 }
200 
201 ArrayKlass* ArrayKlass::array_klass_or_null() {
202   return array_klass_or_null(dimension() +  1);
203 }
204 
205 
206 GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots,
207                                                             Array<InstanceKlass*>* transitive_interfaces) {
208   // interfaces = { cloneable_klass, serializable_klass };
209   assert(num_extra_slots == 0, "sanity of primitive array type");
210   assert(transitive_interfaces == nullptr, "sanity");
211   // Must share this for correct bootstrapping!
212   set_secondary_supers(Universe::the_array_interfaces_array(),
213                        Universe::the_array_interfaces_bitmap());
214   return nullptr;
215 }
216 
217 oop ArrayKlass::component_mirror() const {
218   return java_lang_Class::component_mirror(java_mirror());
219 }
220 
221 // JVMTI support
222 
223 jint ArrayKlass::jvmti_class_status() const {
224   return JVMTI_CLASS_STATUS_ARRAY;
225 }
226 
227 void ArrayKlass::metaspace_pointers_do(MetaspaceClosure* it) {
228   Klass::metaspace_pointers_do(it);
229 
230   ResourceMark rm;
231   log_trace(aot)("Iter(ArrayKlass): %p (%s)", this, external_name());
232 
233   // need to cast away volatile
234   it->push((Klass**)&_higher_dimension);
235   it->push((Klass**)&_lower_dimension);
236 }
237 
238 #if INCLUDE_CDS
239 void ArrayKlass::remove_unshareable_info() {
240   Klass::remove_unshareable_info();
241   if (_higher_dimension != nullptr) {
242     ArrayKlass *ak = higher_dimension();
243     ak->remove_unshareable_info();
244   }
245 }
246 
247 void ArrayKlass::remove_java_mirror() {
248   Klass::remove_java_mirror();
249   if (_higher_dimension != nullptr) {
250     ArrayKlass *ak = higher_dimension();
251     ak->remove_java_mirror();
252   }
253 }
254 
255 void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
256   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
257   // Klass recreates the component mirror also
258 
259   if (_higher_dimension != nullptr) {
260     ObjArrayKlass *ak = higher_dimension();
261     log_array_class_load(ak);
262     ak->restore_unshareable_info(loader_data, protection_domain, CHECK);
263   }
264 }
265 
266 void ArrayKlass::cds_print_value_on(outputStream* st) const {
267   assert(is_klass(), "must be klass");
268   st->print("      - array: %s", internal_name());
269   if (_higher_dimension != nullptr) {
270     ArrayKlass* ak = higher_dimension();
271     st->cr();
272     ak->cds_print_value_on(st);
273   }
274 }
275 #endif // INCLUDE_CDS
276 
277 void ArrayKlass::log_array_class_load(Klass* k) {
278   LogTarget(Debug, class, load, array) lt;
279   if (lt.is_enabled()) {
280     LogStream ls(lt);
< prev index next >