src/share/vm/oops/klass.cpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 9507
7e72702243a4
parent 9448
73d689add964
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
kbarrett@9099 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/javaClasses.hpp"
aoqi@0 27 #include "classfile/dictionary.hpp"
aoqi@0 28 #include "classfile/systemDictionary.hpp"
aoqi@0 29 #include "classfile/vmSymbols.hpp"
aoqi@0 30 #include "gc_implementation/shared/markSweep.inline.hpp"
aoqi@0 31 #include "gc_interface/collectedHeap.inline.hpp"
aoqi@0 32 #include "memory/heapInspection.hpp"
aoqi@0 33 #include "memory/metadataFactory.hpp"
aoqi@0 34 #include "memory/oopFactory.hpp"
aoqi@0 35 #include "memory/resourceArea.hpp"
aoqi@0 36 #include "oops/instanceKlass.hpp"
aoqi@0 37 #include "oops/klass.inline.hpp"
aoqi@0 38 #include "oops/oop.inline2.hpp"
goetz@6911 39 #include "runtime/atomic.inline.hpp"
goetz@6911 40 #include "runtime/orderAccess.inline.hpp"
aoqi@0 41 #include "trace/traceMacros.hpp"
aoqi@0 42 #include "utilities/stack.hpp"
aoqi@0 43 #include "utilities/macros.hpp"
aoqi@0 44 #if INCLUDE_ALL_GCS
stefank@6992 45 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
aoqi@0 46 #include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
aoqi@0 47 #include "gc_implementation/parallelScavenge/psPromotionManager.hpp"
aoqi@0 48 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
aoqi@0 49 #endif // INCLUDE_ALL_GCS
aoqi@0 50
kbarrett@9099 51 bool Klass::is_cloneable() const {
kbarrett@9099 52 return _access_flags.is_cloneable() ||
kbarrett@9099 53 is_subtype_of(SystemDictionary::Cloneable_klass());
kbarrett@9099 54 }
kbarrett@9099 55
kbarrett@9099 56 void Klass::set_is_cloneable() {
kbarrett@9099 57 if (oop_is_instance() && InstanceKlass::cast(this)->reference_type() != REF_NONE) {
kbarrett@9099 58 // Reference cloning should not be intrinsified and always happen in JVM_Clone.
kbarrett@9099 59 } else {
kbarrett@9099 60 _access_flags.set_is_cloneable();
kbarrett@9099 61 }
kbarrett@9099 62 }
kbarrett@9099 63
aoqi@0 64 void Klass::set_name(Symbol* n) {
aoqi@0 65 _name = n;
aoqi@0 66 if (_name != NULL) _name->increment_refcount();
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 bool Klass::is_subclass_of(const Klass* k) const {
aoqi@0 70 // Run up the super chain and check
aoqi@0 71 if (this == k) return true;
aoqi@0 72
aoqi@0 73 Klass* t = const_cast<Klass*>(this)->super();
aoqi@0 74
aoqi@0 75 while (t != NULL) {
aoqi@0 76 if (t == k) return true;
aoqi@0 77 t = t->super();
aoqi@0 78 }
aoqi@0 79 return false;
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 bool Klass::search_secondary_supers(Klass* k) const {
aoqi@0 83 // Put some extra logic here out-of-line, before the search proper.
aoqi@0 84 // This cuts down the size of the inline method.
aoqi@0 85
aoqi@0 86 // This is necessary, since I am never in my own secondary_super list.
aoqi@0 87 if (this == k)
aoqi@0 88 return true;
aoqi@0 89 // Scan the array-of-objects for a match
aoqi@0 90 int cnt = secondary_supers()->length();
aoqi@0 91 for (int i = 0; i < cnt; i++) {
aoqi@0 92 if (secondary_supers()->at(i) == k) {
aoqi@0 93 ((Klass*)this)->set_secondary_super_cache(k);
aoqi@0 94 return true;
aoqi@0 95 }
aoqi@0 96 }
aoqi@0 97 return false;
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 // Return self, except for abstract classes with exactly 1
aoqi@0 101 // implementor. Then return the 1 concrete implementation.
aoqi@0 102 Klass *Klass::up_cast_abstract() {
aoqi@0 103 Klass *r = this;
aoqi@0 104 while( r->is_abstract() ) { // Receiver is abstract?
aoqi@0 105 Klass *s = r->subklass(); // Check for exactly 1 subklass
aoqi@0 106 if( !s || s->next_sibling() ) // Oops; wrong count; give up
aoqi@0 107 return this; // Return 'this' as a no-progress flag
aoqi@0 108 r = s; // Loop till find concrete class
aoqi@0 109 }
aoqi@0 110 return r; // Return the 1 concrete class
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 // Find LCA in class hierarchy
aoqi@0 114 Klass *Klass::LCA( Klass *k2 ) {
aoqi@0 115 Klass *k1 = this;
aoqi@0 116 while( 1 ) {
aoqi@0 117 if( k1->is_subtype_of(k2) ) return k2;
aoqi@0 118 if( k2->is_subtype_of(k1) ) return k1;
aoqi@0 119 k1 = k1->super();
aoqi@0 120 k2 = k2->super();
aoqi@0 121 }
aoqi@0 122 }
aoqi@0 123
aoqi@0 124
aoqi@0 125 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) {
aoqi@0 126 ResourceMark rm(THREAD);
aoqi@0 127 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
aoqi@0 128 : vmSymbols::java_lang_InstantiationException(), external_name());
aoqi@0 129 }
aoqi@0 130
aoqi@0 131
aoqi@0 132 void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
aoqi@0 133 THROW(vmSymbols::java_lang_ArrayStoreException());
aoqi@0 134 }
aoqi@0 135
aoqi@0 136
aoqi@0 137 void Klass::initialize(TRAPS) {
aoqi@0 138 ShouldNotReachHere();
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 bool Klass::compute_is_subtype_of(Klass* k) {
aoqi@0 142 assert(k->is_klass(), "argument must be a class");
aoqi@0 143 return is_subclass_of(k);
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
aoqi@0 147 #ifdef ASSERT
aoqi@0 148 tty->print_cr("Error: find_field called on a klass oop."
aoqi@0 149 " Likely error: reflection method does not correctly"
aoqi@0 150 " wrap return value in a mirror object.");
aoqi@0 151 #endif
aoqi@0 152 ShouldNotReachHere();
aoqi@0 153 return NULL;
aoqi@0 154 }
aoqi@0 155
dbuck@8716 156 Method* Klass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const {
aoqi@0 157 #ifdef ASSERT
aoqi@0 158 tty->print_cr("Error: uncached_lookup_method called on a klass oop."
aoqi@0 159 " Likely error: reflection method does not correctly"
aoqi@0 160 " wrap return value in a mirror object.");
aoqi@0 161 #endif
aoqi@0 162 ShouldNotReachHere();
aoqi@0 163 return NULL;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() {
aoqi@0 167 return Metaspace::allocate(loader_data, word_size, /*read_only*/false,
phh@9507 168 MetaspaceObj::ClassType, THREAD);
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 Klass::Klass() {
aoqi@0 172 Klass* k = this;
aoqi@0 173
aoqi@0 174 // Preinitialize supertype information.
aoqi@0 175 // A later call to initialize_supers() may update these settings:
aoqi@0 176 set_super(NULL);
aoqi@0 177 for (juint i = 0; i < Klass::primary_super_limit(); i++) {
aoqi@0 178 _primary_supers[i] = NULL;
aoqi@0 179 }
aoqi@0 180 set_secondary_supers(NULL);
aoqi@0 181 set_secondary_super_cache(NULL);
aoqi@0 182 _primary_supers[0] = k;
aoqi@0 183 set_super_check_offset(in_bytes(primary_supers_offset()));
aoqi@0 184
stefank@6992 185 // The constructor is used from init_self_patching_vtbl_list,
stefank@6992 186 // which doesn't zero out the memory before calling the constructor.
stefank@6992 187 // Need to set the field explicitly to not hit an assert that the field
stefank@6992 188 // should be NULL before setting it.
stefank@6992 189 _java_mirror = NULL;
stefank@6992 190
aoqi@0 191 set_modifier_flags(0);
aoqi@0 192 set_layout_helper(Klass::_lh_neutral_value);
aoqi@0 193 set_name(NULL);
aoqi@0 194 AccessFlags af;
aoqi@0 195 af.set_flags(0);
aoqi@0 196 set_access_flags(af);
aoqi@0 197 set_subklass(NULL);
aoqi@0 198 set_next_sibling(NULL);
aoqi@0 199 set_next_link(NULL);
aoqi@0 200 TRACE_INIT_ID(this);
aoqi@0 201
aoqi@0 202 set_prototype_header(markOopDesc::prototype());
aoqi@0 203 set_biased_lock_revocation_count(0);
aoqi@0 204 set_last_biased_lock_bulk_revocation_time(0);
aoqi@0 205
aoqi@0 206 // The klass doesn't have any references at this point.
aoqi@0 207 clear_modified_oops();
aoqi@0 208 clear_accumulated_modified_oops();
iklam@7089 209 _shared_class_path_index = -1;
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 jint Klass::array_layout_helper(BasicType etype) {
aoqi@0 213 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
aoqi@0 214 // Note that T_ARRAY is not allowed here.
aoqi@0 215 int hsize = arrayOopDesc::base_offset_in_bytes(etype);
aoqi@0 216 int esize = type2aelembytes(etype);
aoqi@0 217 bool isobj = (etype == T_OBJECT);
aoqi@0 218 int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value;
aoqi@0 219 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
aoqi@0 220
aoqi@0 221 assert(lh < (int)_lh_neutral_value, "must look like an array layout");
aoqi@0 222 assert(layout_helper_is_array(lh), "correct kind");
aoqi@0 223 assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
aoqi@0 224 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
aoqi@0 225 assert(layout_helper_header_size(lh) == hsize, "correct decode");
aoqi@0 226 assert(layout_helper_element_type(lh) == etype, "correct decode");
aoqi@0 227 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");
aoqi@0 228
aoqi@0 229 return lh;
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 bool Klass::can_be_primary_super_slow() const {
aoqi@0 233 if (super() == NULL)
aoqi@0 234 return true;
aoqi@0 235 else if (super()->super_depth() >= primary_super_limit()-1)
aoqi@0 236 return false;
aoqi@0 237 else
aoqi@0 238 return true;
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 void Klass::initialize_supers(Klass* k, TRAPS) {
aoqi@0 242 if (FastSuperclassLimit == 0) {
aoqi@0 243 // None of the other machinery matters.
aoqi@0 244 set_super(k);
aoqi@0 245 return;
aoqi@0 246 }
aoqi@0 247 if (k == NULL) {
aoqi@0 248 set_super(NULL);
aoqi@0 249 _primary_supers[0] = this;
aoqi@0 250 assert(super_depth() == 0, "Object must already be initialized properly");
aoqi@0 251 } else if (k != super() || k == SystemDictionary::Object_klass()) {
aoqi@0 252 assert(super() == NULL || super() == SystemDictionary::Object_klass(),
aoqi@0 253 "initialize this only once to a non-trivial value");
aoqi@0 254 set_super(k);
aoqi@0 255 Klass* sup = k;
aoqi@0 256 int sup_depth = sup->super_depth();
aoqi@0 257 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit());
aoqi@0 258 if (!can_be_primary_super_slow())
aoqi@0 259 my_depth = primary_super_limit();
aoqi@0 260 for (juint i = 0; i < my_depth; i++) {
aoqi@0 261 _primary_supers[i] = sup->_primary_supers[i];
aoqi@0 262 }
aoqi@0 263 Klass* *super_check_cell;
aoqi@0 264 if (my_depth < primary_super_limit()) {
aoqi@0 265 _primary_supers[my_depth] = this;
aoqi@0 266 super_check_cell = &_primary_supers[my_depth];
aoqi@0 267 } else {
aoqi@0 268 // Overflow of the primary_supers array forces me to be secondary.
aoqi@0 269 super_check_cell = &_secondary_super_cache;
aoqi@0 270 }
aoqi@0 271 set_super_check_offset((address)super_check_cell - (address) this);
aoqi@0 272
aoqi@0 273 #ifdef ASSERT
aoqi@0 274 {
aoqi@0 275 juint j = super_depth();
aoqi@0 276 assert(j == my_depth, "computed accessor gets right answer");
aoqi@0 277 Klass* t = this;
aoqi@0 278 while (!t->can_be_primary_super()) {
aoqi@0 279 t = t->super();
aoqi@0 280 j = t->super_depth();
aoqi@0 281 }
aoqi@0 282 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) {
aoqi@0 283 assert(primary_super_of_depth(j1) == NULL, "super list padding");
aoqi@0 284 }
aoqi@0 285 while (t != NULL) {
aoqi@0 286 assert(primary_super_of_depth(j) == t, "super list initialization");
aoqi@0 287 t = t->super();
aoqi@0 288 --j;
aoqi@0 289 }
aoqi@0 290 assert(j == (juint)-1, "correct depth count");
aoqi@0 291 }
aoqi@0 292 #endif
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 if (secondary_supers() == NULL) {
aoqi@0 296 KlassHandle this_kh (THREAD, this);
aoqi@0 297
aoqi@0 298 // Now compute the list of secondary supertypes.
aoqi@0 299 // Secondaries can occasionally be on the super chain,
aoqi@0 300 // if the inline "_primary_supers" array overflows.
aoqi@0 301 int extras = 0;
aoqi@0 302 Klass* p;
aoqi@0 303 for (p = super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
aoqi@0 304 ++extras;
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 ResourceMark rm(THREAD); // need to reclaim GrowableArrays allocated below
aoqi@0 308
aoqi@0 309 // Compute the "real" non-extra secondaries.
aoqi@0 310 GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras);
aoqi@0 311 if (secondaries == NULL) {
aoqi@0 312 // secondary_supers set by compute_secondary_supers
aoqi@0 313 return;
aoqi@0 314 }
aoqi@0 315
aoqi@0 316 GrowableArray<Klass*>* primaries = new GrowableArray<Klass*>(extras);
aoqi@0 317
aoqi@0 318 for (p = this_kh->super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
aoqi@0 319 int i; // Scan for overflow primaries being duplicates of 2nd'arys
aoqi@0 320
aoqi@0 321 // This happens frequently for very deeply nested arrays: the
aoqi@0 322 // primary superclass chain overflows into the secondary. The
aoqi@0 323 // secondary list contains the element_klass's secondaries with
aoqi@0 324 // an extra array dimension added. If the element_klass's
aoqi@0 325 // secondary list already contains some primary overflows, they
aoqi@0 326 // (with the extra level of array-ness) will collide with the
aoqi@0 327 // normal primary superclass overflows.
aoqi@0 328 for( i = 0; i < secondaries->length(); i++ ) {
aoqi@0 329 if( secondaries->at(i) == p )
aoqi@0 330 break;
aoqi@0 331 }
aoqi@0 332 if( i < secondaries->length() )
aoqi@0 333 continue; // It's a dup, don't put it in
aoqi@0 334 primaries->push(p);
aoqi@0 335 }
aoqi@0 336 // Combine the two arrays into a metadata object to pack the array.
aoqi@0 337 // The primaries are added in the reverse order, then the secondaries.
aoqi@0 338 int new_length = primaries->length() + secondaries->length();
aoqi@0 339 Array<Klass*>* s2 = MetadataFactory::new_array<Klass*>(
aoqi@0 340 class_loader_data(), new_length, CHECK);
aoqi@0 341 int fill_p = primaries->length();
aoqi@0 342 for (int j = 0; j < fill_p; j++) {
aoqi@0 343 s2->at_put(j, primaries->pop()); // add primaries in reverse order.
aoqi@0 344 }
aoqi@0 345 for( int j = 0; j < secondaries->length(); j++ ) {
aoqi@0 346 s2->at_put(j+fill_p, secondaries->at(j)); // add secondaries on the end.
aoqi@0 347 }
aoqi@0 348
aoqi@0 349 #ifdef ASSERT
aoqi@0 350 // We must not copy any NULL placeholders left over from bootstrap.
aoqi@0 351 for (int j = 0; j < s2->length(); j++) {
aoqi@0 352 assert(s2->at(j) != NULL, "correct bootstrapping order");
aoqi@0 353 }
aoqi@0 354 #endif
aoqi@0 355
aoqi@0 356 this_kh->set_secondary_supers(s2);
aoqi@0 357 }
aoqi@0 358 }
aoqi@0 359
aoqi@0 360 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots) {
aoqi@0 361 assert(num_extra_slots == 0, "override for complex klasses");
aoqi@0 362 set_secondary_supers(Universe::the_empty_klass_array());
aoqi@0 363 return NULL;
aoqi@0 364 }
aoqi@0 365
aoqi@0 366
aoqi@0 367 Klass* Klass::subklass() const {
aoqi@0 368 return _subklass == NULL ? NULL : _subklass;
aoqi@0 369 }
aoqi@0 370
aoqi@0 371 InstanceKlass* Klass::superklass() const {
aoqi@0 372 assert(super() == NULL || super()->oop_is_instance(), "must be instance klass");
aoqi@0 373 return _super == NULL ? NULL : InstanceKlass::cast(_super);
aoqi@0 374 }
aoqi@0 375
aoqi@0 376 Klass* Klass::next_sibling() const {
aoqi@0 377 return _next_sibling == NULL ? NULL : _next_sibling;
aoqi@0 378 }
aoqi@0 379
aoqi@0 380 void Klass::set_subklass(Klass* s) {
aoqi@0 381 assert(s != this, "sanity check");
aoqi@0 382 _subklass = s;
aoqi@0 383 }
aoqi@0 384
aoqi@0 385 void Klass::set_next_sibling(Klass* s) {
aoqi@0 386 assert(s != this, "sanity check");
aoqi@0 387 _next_sibling = s;
aoqi@0 388 }
aoqi@0 389
aoqi@0 390 void Klass::append_to_sibling_list() {
aoqi@0 391 debug_only(verify();)
aoqi@0 392 // add ourselves to superklass' subklass list
aoqi@0 393 InstanceKlass* super = superklass();
aoqi@0 394 if (super == NULL) return; // special case: class Object
aoqi@0 395 assert((!super->is_interface() // interfaces cannot be supers
aoqi@0 396 && (super->superklass() == NULL || !is_interface())),
aoqi@0 397 "an interface can only be a subklass of Object");
aoqi@0 398 Klass* prev_first_subklass = super->subklass_oop();
aoqi@0 399 if (prev_first_subklass != NULL) {
aoqi@0 400 // set our sibling to be the superklass' previous first subklass
aoqi@0 401 set_next_sibling(prev_first_subklass);
aoqi@0 402 }
aoqi@0 403 // make ourselves the superklass' first subklass
aoqi@0 404 super->set_subklass(this);
aoqi@0 405 debug_only(verify();)
aoqi@0 406 }
aoqi@0 407
aoqi@0 408 bool Klass::is_loader_alive(BoolObjectClosure* is_alive) {
aoqi@0 409 #ifdef ASSERT
aoqi@0 410 // The class is alive iff the class loader is alive.
aoqi@0 411 oop loader = class_loader();
aoqi@0 412 bool loader_alive = (loader == NULL) || is_alive->do_object_b(loader);
aoqi@0 413 #endif // ASSERT
aoqi@0 414
aoqi@0 415 // The class is alive if it's mirror is alive (which should be marked if the
aoqi@0 416 // loader is alive) unless it's an anoymous class.
aoqi@0 417 bool mirror_alive = is_alive->do_object_b(java_mirror());
aoqi@0 418 assert(!mirror_alive || loader_alive, "loader must be alive if the mirror is"
aoqi@0 419 " but not the other way around with anonymous classes");
aoqi@0 420 return mirror_alive;
aoqi@0 421 }
aoqi@0 422
stefank@6992 423 void Klass::clean_weak_klass_links(BoolObjectClosure* is_alive, bool clean_alive_klasses) {
aoqi@0 424 if (!ClassUnloading) {
aoqi@0 425 return;
aoqi@0 426 }
aoqi@0 427
aoqi@0 428 Klass* root = SystemDictionary::Object_klass();
aoqi@0 429 Stack<Klass*, mtGC> stack;
aoqi@0 430
aoqi@0 431 stack.push(root);
aoqi@0 432 while (!stack.is_empty()) {
aoqi@0 433 Klass* current = stack.pop();
aoqi@0 434
aoqi@0 435 assert(current->is_loader_alive(is_alive), "just checking, this should be live");
aoqi@0 436
aoqi@0 437 // Find and set the first alive subklass
aoqi@0 438 Klass* sub = current->subklass_oop();
aoqi@0 439 while (sub != NULL && !sub->is_loader_alive(is_alive)) {
aoqi@0 440 #ifndef PRODUCT
aoqi@0 441 if (TraceClassUnloading && WizardMode) {
aoqi@0 442 ResourceMark rm;
aoqi@0 443 tty->print_cr("[Unlinking class (subclass) %s]", sub->external_name());
aoqi@0 444 }
aoqi@0 445 #endif
aoqi@0 446 sub = sub->next_sibling_oop();
aoqi@0 447 }
aoqi@0 448 current->set_subklass(sub);
aoqi@0 449 if (sub != NULL) {
aoqi@0 450 stack.push(sub);
aoqi@0 451 }
aoqi@0 452
aoqi@0 453 // Find and set the first alive sibling
aoqi@0 454 Klass* sibling = current->next_sibling_oop();
aoqi@0 455 while (sibling != NULL && !sibling->is_loader_alive(is_alive)) {
aoqi@0 456 if (TraceClassUnloading && WizardMode) {
aoqi@0 457 ResourceMark rm;
aoqi@0 458 tty->print_cr("[Unlinking class (sibling) %s]", sibling->external_name());
aoqi@0 459 }
aoqi@0 460 sibling = sibling->next_sibling_oop();
aoqi@0 461 }
aoqi@0 462 current->set_next_sibling(sibling);
aoqi@0 463 if (sibling != NULL) {
aoqi@0 464 stack.push(sibling);
aoqi@0 465 }
aoqi@0 466
aoqi@0 467 // Clean the implementors list and method data.
stefank@6992 468 if (clean_alive_klasses && current->oop_is_instance()) {
aoqi@0 469 InstanceKlass* ik = InstanceKlass::cast(current);
stefank@8185 470 ik->clean_weak_instanceklass_links(is_alive);
kevinw@9184 471
kevinw@9184 472 // JVMTI RedefineClasses creates previous versions that are not in
kevinw@9184 473 // the class hierarchy, so process them here.
kevinw@9184 474 while ((ik = ik->previous_versions()) != NULL) {
kevinw@9184 475 ik->clean_weak_instanceklass_links(is_alive);
kevinw@9184 476 }
aoqi@0 477 }
aoqi@0 478 }
aoqi@0 479 }
aoqi@0 480
aoqi@0 481 void Klass::klass_update_barrier_set(oop v) {
aoqi@0 482 record_modified_oops();
aoqi@0 483 }
aoqi@0 484
stefank@6992 485 // This barrier is used by G1 to remember the old oop values, so
stefank@6992 486 // that we don't forget any objects that were live at the snapshot at
stefank@6992 487 // the beginning. This function is only used when we write oops into Klasses.
stefank@6992 488 void Klass::klass_update_barrier_set_pre(oop* p, oop v) {
stefank@6992 489 #if INCLUDE_ALL_GCS
stefank@6992 490 if (UseG1GC) {
stefank@6992 491 oop obj = *p;
stefank@6992 492 if (obj != NULL) {
stefank@6992 493 G1SATBCardTableModRefBS::enqueue(obj);
stefank@6992 494 }
stefank@6992 495 }
stefank@6992 496 #endif
aoqi@0 497 }
aoqi@0 498
aoqi@0 499 void Klass::klass_oop_store(oop* p, oop v) {
aoqi@0 500 assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
aoqi@0 501 assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
aoqi@0 502
aoqi@0 503 // do the store
aoqi@0 504 if (always_do_update_barrier) {
aoqi@0 505 klass_oop_store((volatile oop*)p, v);
aoqi@0 506 } else {
stefank@6992 507 klass_update_barrier_set_pre(p, v);
aoqi@0 508 *p = v;
aoqi@0 509 klass_update_barrier_set(v);
aoqi@0 510 }
aoqi@0 511 }
aoqi@0 512
aoqi@0 513 void Klass::klass_oop_store(volatile oop* p, oop v) {
aoqi@0 514 assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
aoqi@0 515 assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
aoqi@0 516
stefank@6992 517 klass_update_barrier_set_pre((oop*)p, v); // Cast away volatile.
aoqi@0 518 OrderAccess::release_store_ptr(p, v);
aoqi@0 519 klass_update_barrier_set(v);
aoqi@0 520 }
aoqi@0 521
aoqi@0 522 void Klass::oops_do(OopClosure* cl) {
aoqi@0 523 cl->do_oop(&_java_mirror);
aoqi@0 524 }
aoqi@0 525
aoqi@0 526 void Klass::remove_unshareable_info() {
aoqi@0 527 assert (DumpSharedSpaces, "only called for DumpSharedSpaces");
aoqi@0 528
aoqi@0 529 set_subklass(NULL);
aoqi@0 530 set_next_sibling(NULL);
aoqi@0 531 // Clear the java mirror
aoqi@0 532 set_java_mirror(NULL);
aoqi@0 533 set_next_link(NULL);
aoqi@0 534
aoqi@0 535 // Null out class_loader_data because we don't share that yet.
aoqi@0 536 set_class_loader_data(NULL);
aoqi@0 537 }
aoqi@0 538
iklam@7089 539 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
aoqi@0 540 TRACE_INIT_ID(this);
aoqi@0 541 // If an exception happened during CDS restore, some of these fields may already be
aoqi@0 542 // set. We leave the class on the CLD list, even if incomplete so that we don't
aoqi@0 543 // modify the CLD list outside a safepoint.
aoqi@0 544 if (class_loader_data() == NULL) {
iklam@7089 545 // Restore class_loader_data
aoqi@0 546 set_class_loader_data(loader_data);
aoqi@0 547
iklam@7089 548 // Add to class loader list first before creating the mirror
aoqi@0 549 // (same order as class file parsing)
aoqi@0 550 loader_data->add_class(this);
aoqi@0 551 }
aoqi@0 552
iklam@7089 553 // Recreate the class mirror.
aoqi@0 554 // Only recreate it if not present. A previous attempt to restore may have
aoqi@0 555 // gotten an OOM later but keep the mirror if it was created.
aoqi@0 556 if (java_mirror() == NULL) {
coleenp@7129 557 java_lang_Class::create_mirror(this, class_loader(), protection_domain, CHECK);
aoqi@0 558 }
aoqi@0 559 }
aoqi@0 560
aoqi@0 561 Klass* Klass::array_klass_or_null(int rank) {
aoqi@0 562 EXCEPTION_MARK;
aoqi@0 563 // No exception can be thrown by array_klass_impl when called with or_null == true.
aoqi@0 564 // (In anycase, the execption mark will fail if it do so)
aoqi@0 565 return array_klass_impl(true, rank, THREAD);
aoqi@0 566 }
aoqi@0 567
aoqi@0 568
aoqi@0 569 Klass* Klass::array_klass_or_null() {
aoqi@0 570 EXCEPTION_MARK;
aoqi@0 571 // No exception can be thrown by array_klass_impl when called with or_null == true.
aoqi@0 572 // (In anycase, the execption mark will fail if it do so)
aoqi@0 573 return array_klass_impl(true, THREAD);
aoqi@0 574 }
aoqi@0 575
aoqi@0 576
aoqi@0 577 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) {
aoqi@0 578 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
aoqi@0 579 return NULL;
aoqi@0 580 }
aoqi@0 581
aoqi@0 582
aoqi@0 583 Klass* Klass::array_klass_impl(bool or_null, TRAPS) {
aoqi@0 584 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
aoqi@0 585 return NULL;
aoqi@0 586 }
aoqi@0 587
aoqi@0 588 oop Klass::class_loader() const { return class_loader_data()->class_loader(); }
aoqi@0 589
aoqi@0 590 const char* Klass::external_name() const {
aoqi@0 591 if (oop_is_instance()) {
aoqi@0 592 InstanceKlass* ik = (InstanceKlass*) this;
aoqi@0 593 if (ik->is_anonymous()) {
aoqi@0 594 assert(EnableInvokeDynamic, "");
aoqi@0 595 intptr_t hash = 0;
aoqi@0 596 if (ik->java_mirror() != NULL) {
aoqi@0 597 // java_mirror might not be created yet, return 0 as hash.
aoqi@0 598 hash = ik->java_mirror()->identity_hash();
aoqi@0 599 }
aoqi@0 600 char hash_buf[40];
aoqi@0 601 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash);
aoqi@0 602 size_t hash_len = strlen(hash_buf);
aoqi@0 603
aoqi@0 604 size_t result_len = name()->utf8_length();
aoqi@0 605 char* result = NEW_RESOURCE_ARRAY(char, result_len + hash_len + 1);
aoqi@0 606 name()->as_klass_external_name(result, (int) result_len + 1);
aoqi@0 607 assert(strlen(result) == result_len, "");
aoqi@0 608 strcpy(result + result_len, hash_buf);
aoqi@0 609 assert(strlen(result) == result_len + hash_len, "");
aoqi@0 610 return result;
aoqi@0 611 }
aoqi@0 612 }
aoqi@0 613 if (name() == NULL) return "<unknown>";
aoqi@0 614 return name()->as_klass_external_name();
aoqi@0 615 }
aoqi@0 616
aoqi@0 617
aoqi@0 618 const char* Klass::signature_name() const {
aoqi@0 619 if (name() == NULL) return "<unknown>";
aoqi@0 620 return name()->as_C_string();
aoqi@0 621 }
aoqi@0 622
aoqi@0 623 // Unless overridden, modifier_flags is 0.
aoqi@0 624 jint Klass::compute_modifier_flags(TRAPS) const {
aoqi@0 625 return 0;
aoqi@0 626 }
aoqi@0 627
aoqi@0 628 int Klass::atomic_incr_biased_lock_revocation_count() {
aoqi@0 629 return (int) Atomic::add(1, &_biased_lock_revocation_count);
aoqi@0 630 }
aoqi@0 631
aoqi@0 632 // Unless overridden, jvmti_class_status has no flags set.
aoqi@0 633 jint Klass::jvmti_class_status() const {
aoqi@0 634 return 0;
aoqi@0 635 }
aoqi@0 636
aoqi@0 637
aoqi@0 638 // Printing
aoqi@0 639
aoqi@0 640 void Klass::print_on(outputStream* st) const {
aoqi@0 641 ResourceMark rm;
aoqi@0 642 // print title
aoqi@0 643 st->print("%s", internal_name());
aoqi@0 644 print_address_on(st);
aoqi@0 645 st->cr();
aoqi@0 646 }
aoqi@0 647
aoqi@0 648 void Klass::oop_print_on(oop obj, outputStream* st) {
aoqi@0 649 ResourceMark rm;
aoqi@0 650 // print title
aoqi@0 651 st->print_cr("%s ", internal_name());
aoqi@0 652 obj->print_address_on(st);
aoqi@0 653
aoqi@0 654 if (WizardMode) {
aoqi@0 655 // print header
aoqi@0 656 obj->mark()->print_on(st);
aoqi@0 657 }
aoqi@0 658
aoqi@0 659 // print class
aoqi@0 660 st->print(" - klass: ");
aoqi@0 661 obj->klass()->print_value_on(st);
aoqi@0 662 st->cr();
aoqi@0 663 }
aoqi@0 664
aoqi@0 665 void Klass::oop_print_value_on(oop obj, outputStream* st) {
aoqi@0 666 // print title
aoqi@0 667 ResourceMark rm; // Cannot print in debug mode without this
aoqi@0 668 st->print("%s", internal_name());
aoqi@0 669 obj->print_address_on(st);
aoqi@0 670 }
aoqi@0 671
aoqi@0 672 #if INCLUDE_SERVICES
aoqi@0 673 // Size Statistics
aoqi@0 674 void Klass::collect_statistics(KlassSizeStats *sz) const {
aoqi@0 675 sz->_klass_bytes = sz->count(this);
aoqi@0 676 sz->_mirror_bytes = sz->count(java_mirror());
aoqi@0 677 sz->_secondary_supers_bytes = sz->count_array(secondary_supers());
aoqi@0 678
aoqi@0 679 sz->_ro_bytes += sz->_secondary_supers_bytes;
aoqi@0 680 sz->_rw_bytes += sz->_klass_bytes + sz->_mirror_bytes;
aoqi@0 681 }
aoqi@0 682 #endif // INCLUDE_SERVICES
aoqi@0 683
aoqi@0 684 // Verification
aoqi@0 685
aoqi@0 686 void Klass::verify_on(outputStream* st) {
aoqi@0 687
aoqi@0 688 // This can be expensive, but it is worth checking that this klass is actually
aoqi@0 689 // in the CLD graph but not in production.
aoqi@0 690 assert(Metaspace::contains((address)this), "Should be");
aoqi@0 691
aoqi@0 692 guarantee(this->is_klass(),"should be klass");
aoqi@0 693
aoqi@0 694 if (super() != NULL) {
aoqi@0 695 guarantee(super()->is_klass(), "should be klass");
aoqi@0 696 }
aoqi@0 697 if (secondary_super_cache() != NULL) {
aoqi@0 698 Klass* ko = secondary_super_cache();
aoqi@0 699 guarantee(ko->is_klass(), "should be klass");
aoqi@0 700 }
aoqi@0 701 for ( uint i = 0; i < primary_super_limit(); i++ ) {
aoqi@0 702 Klass* ko = _primary_supers[i];
aoqi@0 703 if (ko != NULL) {
aoqi@0 704 guarantee(ko->is_klass(), "should be klass");
aoqi@0 705 }
aoqi@0 706 }
aoqi@0 707
aoqi@0 708 if (java_mirror() != NULL) {
aoqi@0 709 guarantee(java_mirror()->is_oop(), "should be instance");
aoqi@0 710 }
aoqi@0 711 }
aoqi@0 712
aoqi@0 713 void Klass::oop_verify_on(oop obj, outputStream* st) {
aoqi@0 714 guarantee(obj->is_oop(), "should be oop");
aoqi@0 715 guarantee(obj->klass()->is_klass(), "klass field is not a klass");
aoqi@0 716 }
aoqi@0 717
aoqi@0 718 #ifndef PRODUCT
aoqi@0 719
aoqi@0 720 bool Klass::verify_vtable_index(int i) {
aoqi@0 721 if (oop_is_instance()) {
aoqi@0 722 int limit = ((InstanceKlass*)this)->vtable_length()/vtableEntry::size();
aoqi@0 723 assert(i >= 0 && i < limit, err_msg("index %d out of bounds %d", i, limit));
aoqi@0 724 } else {
aoqi@0 725 assert(oop_is_array(), "Must be");
aoqi@0 726 int limit = ((ArrayKlass*)this)->vtable_length()/vtableEntry::size();
aoqi@0 727 assert(i >= 0 && i < limit, err_msg("index %d out of bounds %d", i, limit));
aoqi@0 728 }
aoqi@0 729 return true;
aoqi@0 730 }
aoqi@0 731
aoqi@0 732 bool Klass::verify_itable_index(int i) {
aoqi@0 733 assert(oop_is_instance(), "");
aoqi@0 734 int method_count = klassItable::method_count_for_interface(this);
aoqi@0 735 assert(i >= 0 && i < method_count, "index out of bounds");
aoqi@0 736 return true;
aoqi@0 737 }
aoqi@0 738
aoqi@0 739 #endif
stefank@6976 740
stefank@6976 741 /////////////// Unit tests ///////////////
stefank@6976 742
stefank@6976 743 #ifndef PRODUCT
stefank@6976 744
stefank@6976 745 class TestKlass {
stefank@6976 746 public:
stefank@6976 747 static void test_oop_is_instanceClassLoader() {
stefank@6976 748 assert(SystemDictionary::ClassLoader_klass()->oop_is_instanceClassLoader(), "assert");
stefank@6976 749 assert(!SystemDictionary::String_klass()->oop_is_instanceClassLoader(), "assert");
stefank@6976 750 }
stefank@6976 751 };
stefank@6976 752
stefank@6976 753 void TestKlass_test() {
stefank@6976 754 TestKlass::test_oop_is_instanceClassLoader();
stefank@6976 755 }
stefank@6976 756
stefank@6976 757 #endif

mercurial