Tue, 24 Jan 2012 14:48:22 +0100
7130476: Remove use of #ifdef TRACE_DEFINE_KLASS_TRACE_ID from klass.hpp
Reviewed-by: kamg, phh, dsamersoff
Contributed-by: Rickard Backman <rickard.backman@oracle.com>
1 /*
2 * Copyright (c) 1997, 2011, 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 */
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "gc_interface/collectedHeap.inline.hpp"
29 #include "memory/oopFactory.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "oops/instanceKlass.hpp"
32 #include "oops/klass.inline.hpp"
33 #include "oops/klassOop.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "oops/oop.inline2.hpp"
36 #include "runtime/atomic.hpp"
38 void Klass::set_name(Symbol* n) {
39 _name = n;
40 if (_name != NULL) _name->increment_refcount();
41 }
43 bool Klass::is_subclass_of(klassOop k) const {
44 // Run up the super chain and check
45 klassOop t = as_klassOop();
47 if (t == k) return true;
48 t = Klass::cast(t)->super();
50 while (t != NULL) {
51 if (t == k) return true;
52 t = Klass::cast(t)->super();
53 }
54 return false;
55 }
57 bool Klass::search_secondary_supers(klassOop k) const {
58 // Put some extra logic here out-of-line, before the search proper.
59 // This cuts down the size of the inline method.
61 // This is necessary, since I am never in my own secondary_super list.
62 if (this->as_klassOop() == k)
63 return true;
64 // Scan the array-of-objects for a match
65 int cnt = secondary_supers()->length();
66 for (int i = 0; i < cnt; i++) {
67 if (secondary_supers()->obj_at(i) == k) {
68 ((Klass*)this)->set_secondary_super_cache(k);
69 return true;
70 }
71 }
72 return false;
73 }
75 // Return self, except for abstract classes with exactly 1
76 // implementor. Then return the 1 concrete implementation.
77 Klass *Klass::up_cast_abstract() {
78 Klass *r = this;
79 while( r->is_abstract() ) { // Receiver is abstract?
80 Klass *s = r->subklass(); // Check for exactly 1 subklass
81 if( !s || s->next_sibling() ) // Oops; wrong count; give up
82 return this; // Return 'this' as a no-progress flag
83 r = s; // Loop till find concrete class
84 }
85 return r; // Return the 1 concrete class
86 }
88 // Find LCA in class hierarchy
89 Klass *Klass::LCA( Klass *k2 ) {
90 Klass *k1 = this;
91 while( 1 ) {
92 if( k1->is_subtype_of(k2->as_klassOop()) ) return k2;
93 if( k2->is_subtype_of(k1->as_klassOop()) ) return k1;
94 k1 = k1->super()->klass_part();
95 k2 = k2->super()->klass_part();
96 }
97 }
100 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) {
101 ResourceMark rm(THREAD);
102 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
103 : vmSymbols::java_lang_InstantiationException(), external_name());
104 }
107 void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
108 THROW(vmSymbols::java_lang_ArrayStoreException());
109 }
112 void Klass::initialize(TRAPS) {
113 ShouldNotReachHere();
114 }
116 bool Klass::compute_is_subtype_of(klassOop k) {
117 assert(k->is_klass(), "argument must be a class");
118 return is_subclass_of(k);
119 }
122 methodOop Klass::uncached_lookup_method(Symbol* name, Symbol* signature) const {
123 #ifdef ASSERT
124 tty->print_cr("Error: uncached_lookup_method called on a klass oop."
125 " Likely error: reflection method does not correctly"
126 " wrap return value in a mirror object.");
127 #endif
128 ShouldNotReachHere();
129 return NULL;
130 }
132 klassOop Klass::base_create_klass_oop(KlassHandle& klass, int size,
133 const Klass_vtbl& vtbl, TRAPS) {
134 size = align_object_size(size);
135 // allocate and initialize vtable
136 Klass* kl = (Klass*) vtbl.allocate_permanent(klass, size, CHECK_NULL);
137 klassOop k = kl->as_klassOop();
139 { // Preinitialize supertype information.
140 // A later call to initialize_supers() may update these settings:
141 kl->set_super(NULL);
142 for (juint i = 0; i < Klass::primary_super_limit(); i++) {
143 kl->_primary_supers[i] = NULL;
144 }
145 kl->set_secondary_supers(NULL);
146 oop_store_without_check((oop*) &kl->_primary_supers[0], k);
147 kl->set_super_check_offset(in_bytes(primary_supers_offset()));
148 }
150 kl->set_java_mirror(NULL);
151 kl->set_modifier_flags(0);
152 kl->set_layout_helper(Klass::_lh_neutral_value);
153 kl->set_name(NULL);
154 AccessFlags af;
155 af.set_flags(0);
156 kl->set_access_flags(af);
157 kl->set_subklass(NULL);
158 kl->set_next_sibling(NULL);
159 kl->set_alloc_count(0);
160 kl->set_alloc_size(0);
161 TRACE_SET_KLASS_TRACE_ID(kl, 0);
163 kl->set_prototype_header(markOopDesc::prototype());
164 kl->set_biased_lock_revocation_count(0);
165 kl->set_last_biased_lock_bulk_revocation_time(0);
167 return k;
168 }
170 KlassHandle Klass::base_create_klass(KlassHandle& klass, int size,
171 const Klass_vtbl& vtbl, TRAPS) {
172 klassOop ek = base_create_klass_oop(klass, size, vtbl, THREAD);
173 return KlassHandle(THREAD, ek);
174 }
176 void Klass_vtbl::post_new_init_klass(KlassHandle& klass,
177 klassOop new_klass,
178 int size) const {
179 assert(!new_klass->klass_part()->null_vtbl(), "Not a complete klass");
180 CollectedHeap::post_allocation_install_obj_klass(klass, new_klass, size);
181 }
183 void* Klass_vtbl::operator new(size_t ignored, KlassHandle& klass,
184 int size, TRAPS) {
185 // The vtable pointer is installed during the execution of
186 // constructors in the call to permanent_obj_allocate(). Delay
187 // the installation of the klass pointer into the new klass "k"
188 // until after the vtable pointer has been installed (i.e., until
189 // after the return of permanent_obj_allocate().
190 klassOop k =
191 (klassOop) CollectedHeap::permanent_obj_allocate_no_klass_install(klass,
192 size, CHECK_NULL);
193 return k->klass_part();
194 }
196 jint Klass::array_layout_helper(BasicType etype) {
197 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
198 // Note that T_ARRAY is not allowed here.
199 int hsize = arrayOopDesc::base_offset_in_bytes(etype);
200 int esize = type2aelembytes(etype);
201 bool isobj = (etype == T_OBJECT);
202 int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value;
203 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
205 assert(lh < (int)_lh_neutral_value, "must look like an array layout");
206 assert(layout_helper_is_javaArray(lh), "correct kind");
207 assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
208 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
209 assert(layout_helper_header_size(lh) == hsize, "correct decode");
210 assert(layout_helper_element_type(lh) == etype, "correct decode");
211 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");
213 return lh;
214 }
216 bool Klass::can_be_primary_super_slow() const {
217 if (super() == NULL)
218 return true;
219 else if (super()->klass_part()->super_depth() >= primary_super_limit()-1)
220 return false;
221 else
222 return true;
223 }
225 void Klass::initialize_supers(klassOop k, TRAPS) {
226 if (FastSuperclassLimit == 0) {
227 // None of the other machinery matters.
228 set_super(k);
229 return;
230 }
231 if (k == NULL) {
232 set_super(NULL);
233 oop_store_without_check((oop*) &_primary_supers[0], (oop) this->as_klassOop());
234 assert(super_depth() == 0, "Object must already be initialized properly");
235 } else if (k != super() || k == SystemDictionary::Object_klass()) {
236 assert(super() == NULL || super() == SystemDictionary::Object_klass(),
237 "initialize this only once to a non-trivial value");
238 set_super(k);
239 Klass* sup = k->klass_part();
240 int sup_depth = sup->super_depth();
241 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit());
242 if (!can_be_primary_super_slow())
243 my_depth = primary_super_limit();
244 for (juint i = 0; i < my_depth; i++) {
245 oop_store_without_check((oop*) &_primary_supers[i], (oop) sup->_primary_supers[i]);
246 }
247 klassOop *super_check_cell;
248 if (my_depth < primary_super_limit()) {
249 oop_store_without_check((oop*) &_primary_supers[my_depth], (oop) this->as_klassOop());
250 super_check_cell = &_primary_supers[my_depth];
251 } else {
252 // Overflow of the primary_supers array forces me to be secondary.
253 super_check_cell = &_secondary_super_cache;
254 }
255 set_super_check_offset((address)super_check_cell - (address) this->as_klassOop());
257 #ifdef ASSERT
258 {
259 juint j = super_depth();
260 assert(j == my_depth, "computed accessor gets right answer");
261 klassOop t = as_klassOop();
262 while (!Klass::cast(t)->can_be_primary_super()) {
263 t = Klass::cast(t)->super();
264 j = Klass::cast(t)->super_depth();
265 }
266 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) {
267 assert(primary_super_of_depth(j1) == NULL, "super list padding");
268 }
269 while (t != NULL) {
270 assert(primary_super_of_depth(j) == t, "super list initialization");
271 t = Klass::cast(t)->super();
272 --j;
273 }
274 assert(j == (juint)-1, "correct depth count");
275 }
276 #endif
277 }
279 if (secondary_supers() == NULL) {
280 KlassHandle this_kh (THREAD, this);
282 // Now compute the list of secondary supertypes.
283 // Secondaries can occasionally be on the super chain,
284 // if the inline "_primary_supers" array overflows.
285 int extras = 0;
286 klassOop p;
287 for (p = super(); !(p == NULL || p->klass_part()->can_be_primary_super()); p = p->klass_part()->super()) {
288 ++extras;
289 }
291 // Compute the "real" non-extra secondaries.
292 objArrayOop secondary_oops = compute_secondary_supers(extras, CHECK);
293 objArrayHandle secondaries (THREAD, secondary_oops);
295 // Store the extra secondaries in the first array positions:
296 int fillp = extras;
297 for (p = this_kh->super(); !(p == NULL || p->klass_part()->can_be_primary_super()); p = p->klass_part()->super()) {
298 int i; // Scan for overflow primaries being duplicates of 2nd'arys
300 // This happens frequently for very deeply nested arrays: the
301 // primary superclass chain overflows into the secondary. The
302 // secondary list contains the element_klass's secondaries with
303 // an extra array dimension added. If the element_klass's
304 // secondary list already contains some primary overflows, they
305 // (with the extra level of array-ness) will collide with the
306 // normal primary superclass overflows.
307 for( i = extras; i < secondaries->length(); i++ )
308 if( secondaries->obj_at(i) == p )
309 break;
310 if( i < secondaries->length() )
311 continue; // It's a dup, don't put it in
312 secondaries->obj_at_put(--fillp, p);
313 }
314 // See if we had some dup's, so the array has holes in it.
315 if( fillp > 0 ) {
316 // Pack the array. Drop the old secondaries array on the floor
317 // and let GC reclaim it.
318 objArrayOop s2 = oopFactory::new_system_objArray(secondaries->length() - fillp, CHECK);
319 for( int i = 0; i < s2->length(); i++ )
320 s2->obj_at_put( i, secondaries->obj_at(i+fillp) );
321 secondaries = objArrayHandle(THREAD, s2);
322 }
324 #ifdef ASSERT
325 if (secondaries() != Universe::the_array_interfaces_array()) {
326 // We must not copy any NULL placeholders left over from bootstrap.
327 for (int j = 0; j < secondaries->length(); j++) {
328 assert(secondaries->obj_at(j) != NULL, "correct bootstrapping order");
329 }
330 }
331 #endif
333 this_kh->set_secondary_supers(secondaries());
334 }
335 }
337 objArrayOop Klass::compute_secondary_supers(int num_extra_slots, TRAPS) {
338 assert(num_extra_slots == 0, "override for complex klasses");
339 return Universe::the_empty_system_obj_array();
340 }
343 Klass* Klass::subklass() const {
344 return _subklass == NULL ? NULL : Klass::cast(_subklass);
345 }
347 instanceKlass* Klass::superklass() const {
348 assert(super() == NULL || super()->klass_part()->oop_is_instance(), "must be instance klass");
349 return _super == NULL ? NULL : instanceKlass::cast(_super);
350 }
352 Klass* Klass::next_sibling() const {
353 return _next_sibling == NULL ? NULL : Klass::cast(_next_sibling);
354 }
356 void Klass::set_subklass(klassOop s) {
357 assert(s != as_klassOop(), "sanity check");
358 oop_store_without_check((oop*)&_subklass, s);
359 }
361 void Klass::set_next_sibling(klassOop s) {
362 assert(s != as_klassOop(), "sanity check");
363 oop_store_without_check((oop*)&_next_sibling, s);
364 }
366 void Klass::append_to_sibling_list() {
367 debug_only(if (!SharedSkipVerify) as_klassOop()->verify();)
368 // add ourselves to superklass' subklass list
369 instanceKlass* super = superklass();
370 if (super == NULL) return; // special case: class Object
371 assert(SharedSkipVerify ||
372 (!super->is_interface() // interfaces cannot be supers
373 && (super->superklass() == NULL || !is_interface())),
374 "an interface can only be a subklass of Object");
375 klassOop prev_first_subklass = super->subklass_oop();
376 if (prev_first_subklass != NULL) {
377 // set our sibling to be the superklass' previous first subklass
378 set_next_sibling(prev_first_subklass);
379 }
380 // make ourselves the superklass' first subklass
381 super->set_subklass(as_klassOop());
382 debug_only(if (!SharedSkipVerify) as_klassOop()->verify();)
383 }
385 void Klass::remove_from_sibling_list() {
386 // remove receiver from sibling list
387 instanceKlass* super = superklass();
388 assert(super != NULL || as_klassOop() == SystemDictionary::Object_klass(), "should have super");
389 if (super == NULL) return; // special case: class Object
390 if (super->subklass() == this) {
391 // first subklass
392 super->set_subklass(_next_sibling);
393 } else {
394 Klass* sib = super->subklass();
395 while (sib->next_sibling() != this) {
396 sib = sib->next_sibling();
397 };
398 sib->set_next_sibling(_next_sibling);
399 }
400 }
402 void Klass::follow_weak_klass_links( BoolObjectClosure* is_alive, OopClosure* keep_alive) {
403 // This klass is alive but the subklass and siblings are not followed/updated.
404 // We update the subklass link and the subklass' sibling links here.
405 // Our own sibling link will be updated by our superclass (which must be alive
406 // since we are).
407 assert(is_alive->do_object_b(as_klassOop()), "just checking, this should be live");
408 if (ClassUnloading) {
409 klassOop sub = subklass_oop();
410 if (sub != NULL && !is_alive->do_object_b(sub)) {
411 // first subklass not alive, find first one alive
412 do {
413 #ifndef PRODUCT
414 if (TraceClassUnloading && WizardMode) {
415 ResourceMark rm;
416 tty->print_cr("[Unlinking class (subclass) %s]", sub->klass_part()->external_name());
417 }
418 #endif
419 sub = sub->klass_part()->next_sibling_oop();
420 } while (sub != NULL && !is_alive->do_object_b(sub));
421 set_subklass(sub);
422 }
423 // now update the subklass' sibling list
424 while (sub != NULL) {
425 klassOop next = sub->klass_part()->next_sibling_oop();
426 if (next != NULL && !is_alive->do_object_b(next)) {
427 // first sibling not alive, find first one alive
428 do {
429 #ifndef PRODUCT
430 if (TraceClassUnloading && WizardMode) {
431 ResourceMark rm;
432 tty->print_cr("[Unlinking class (sibling) %s]", next->klass_part()->external_name());
433 }
434 #endif
435 next = next->klass_part()->next_sibling_oop();
436 } while (next != NULL && !is_alive->do_object_b(next));
437 sub->klass_part()->set_next_sibling(next);
438 }
439 sub = next;
440 }
441 } else {
442 // Always follow subklass and sibling link. This will prevent any klasses from
443 // being unloaded (all classes are transitively linked from java.lang.Object).
444 keep_alive->do_oop(adr_subklass());
445 keep_alive->do_oop(adr_next_sibling());
446 }
447 }
450 void Klass::remove_unshareable_info() {
451 if (oop_is_instance()) {
452 instanceKlass* ik = (instanceKlass*)this;
453 if (ik->is_linked()) {
454 ik->unlink_class();
455 }
456 }
457 // Clear the Java vtable if the oop has one.
458 // The vtable isn't shareable because it's in the wrong order wrt the methods
459 // once the method names get moved and resorted.
460 klassVtable* vt = vtable();
461 if (vt != NULL) {
462 assert(oop_is_instance() || oop_is_array(), "nothing else has vtable");
463 vt->clear_vtable();
464 }
465 set_subklass(NULL);
466 set_next_sibling(NULL);
467 }
470 void Klass::shared_symbols_iterate(SymbolClosure* closure) {
471 closure->do_symbol(&_name);
472 }
475 klassOop Klass::array_klass_or_null(int rank) {
476 EXCEPTION_MARK;
477 // No exception can be thrown by array_klass_impl when called with or_null == true.
478 // (In anycase, the execption mark will fail if it do so)
479 return array_klass_impl(true, rank, THREAD);
480 }
483 klassOop Klass::array_klass_or_null() {
484 EXCEPTION_MARK;
485 // No exception can be thrown by array_klass_impl when called with or_null == true.
486 // (In anycase, the execption mark will fail if it do so)
487 return array_klass_impl(true, THREAD);
488 }
491 klassOop Klass::array_klass_impl(bool or_null, int rank, TRAPS) {
492 fatal("array_klass should be dispatched to instanceKlass, objArrayKlass or typeArrayKlass");
493 return NULL;
494 }
497 klassOop Klass::array_klass_impl(bool or_null, TRAPS) {
498 fatal("array_klass should be dispatched to instanceKlass, objArrayKlass or typeArrayKlass");
499 return NULL;
500 }
503 void Klass::with_array_klasses_do(void f(klassOop k)) {
504 f(as_klassOop());
505 }
508 const char* Klass::external_name() const {
509 if (oop_is_instance()) {
510 instanceKlass* ik = (instanceKlass*) this;
511 if (ik->is_anonymous()) {
512 assert(EnableInvokeDynamic, "");
513 intptr_t hash = ik->java_mirror()->identity_hash();
514 char hash_buf[40];
515 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash);
516 size_t hash_len = strlen(hash_buf);
518 size_t result_len = name()->utf8_length();
519 char* result = NEW_RESOURCE_ARRAY(char, result_len + hash_len + 1);
520 name()->as_klass_external_name(result, (int) result_len + 1);
521 assert(strlen(result) == result_len, "");
522 strcpy(result + result_len, hash_buf);
523 assert(strlen(result) == result_len + hash_len, "");
524 return result;
525 }
526 }
527 if (name() == NULL) return "<unknown>";
528 return name()->as_klass_external_name();
529 }
532 const char* Klass::signature_name() const {
533 if (name() == NULL) return "<unknown>";
534 return name()->as_C_string();
535 }
537 // Unless overridden, modifier_flags is 0.
538 jint Klass::compute_modifier_flags(TRAPS) const {
539 return 0;
540 }
542 int Klass::atomic_incr_biased_lock_revocation_count() {
543 return (int) Atomic::add(1, &_biased_lock_revocation_count);
544 }
546 // Unless overridden, jvmti_class_status has no flags set.
547 jint Klass::jvmti_class_status() const {
548 return 0;
549 }
551 // Printing
553 void Klass::oop_print_on(oop obj, outputStream* st) {
554 ResourceMark rm;
555 // print title
556 st->print_cr("%s ", internal_name());
557 obj->print_address_on(st);
559 if (WizardMode) {
560 // print header
561 obj->mark()->print_on(st);
562 }
564 // print class
565 st->print(" - klass: ");
566 obj->klass()->print_value_on(st);
567 st->cr();
568 }
570 void Klass::oop_print_value_on(oop obj, outputStream* st) {
571 // print title
572 ResourceMark rm; // Cannot print in debug mode without this
573 st->print("%s", internal_name());
574 obj->print_address_on(st);
575 }
577 // Verification
579 void Klass::oop_verify_on(oop obj, outputStream* st) {
580 guarantee(obj->is_oop(), "should be oop");
581 guarantee(obj->klass()->is_perm(), "should be in permspace");
582 guarantee(obj->klass()->is_klass(), "klass field is not a klass");
583 }
586 void Klass::oop_verify_old_oop(oop obj, oop* p, bool allow_dirty) {
587 /* $$$ I think this functionality should be handled by verification of
588 RememberedSet::verify_old_oop(obj, p, allow_dirty, false);
589 the card table. */
590 }
591 void Klass::oop_verify_old_oop(oop obj, narrowOop* p, bool allow_dirty) { }
593 #ifndef PRODUCT
595 void Klass::verify_vtable_index(int i) {
596 assert(oop_is_instance() || oop_is_array(), "only instanceKlass and arrayKlass have vtables");
597 if (oop_is_instance()) {
598 assert(i>=0 && i<((instanceKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds");
599 } else {
600 assert(i>=0 && i<((arrayKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds");
601 }
602 }
604 #endif