src/share/vm/oops/klass.cpp

Thu, 24 May 2018 17:06:56 +0800

author
aoqi
date
Thu, 24 May 2018 17:06:56 +0800
changeset 8604
04d83ba48607
parent 8185
5cece4584b8e
parent 7535
7ae4e26cb1e0
child 8856
ac27a9c85bea
permissions
-rw-r--r--

Merge

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

mercurial