comparison src/share/vm/oops/klass.cpp @ 6948:e522a00b91aa

Merge with http://hg.openjdk.java.net/hsx/hsx25/hotspot/ after NPG - C++ build works
author Doug Simon <doug.simon@oracle.com>
date Mon, 12 Nov 2012 23:14:12 +0100
parents 957c266d8bc5 bdb5f8c9978b
children 2cb439954abf
comparison
equal deleted inserted replaced
6711:ae13cc658b80 6948:e522a00b91aa
21 * questions. 21 * questions.
22 * 22 *
23 */ 23 */
24 24
25 #include "precompiled.hpp" 25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "classfile/dictionary.hpp"
26 #include "classfile/systemDictionary.hpp" 28 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp" 29 #include "classfile/vmSymbols.hpp"
30 #include "gc_implementation/shared/markSweep.inline.hpp"
28 #include "gc_interface/collectedHeap.inline.hpp" 31 #include "gc_interface/collectedHeap.inline.hpp"
32 #include "memory/metadataFactory.hpp"
29 #include "memory/oopFactory.hpp" 33 #include "memory/oopFactory.hpp"
30 #include "memory/resourceArea.hpp" 34 #include "memory/resourceArea.hpp"
31 #include "oops/instanceKlass.hpp" 35 #include "oops/instanceKlass.hpp"
32 #include "oops/klass.inline.hpp" 36 #include "oops/klass.inline.hpp"
33 #include "oops/klassOop.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "oops/oop.inline2.hpp" 37 #include "oops/oop.inline2.hpp"
36 #include "runtime/atomic.hpp" 38 #include "runtime/atomic.hpp"
39 #include "utilities/stack.hpp"
40 #ifndef SERIALGC
41 #include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
42 #include "gc_implementation/parallelScavenge/psPromotionManager.hpp"
43 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
44 #endif
37 45
38 void Klass::set_name(Symbol* n) { 46 void Klass::set_name(Symbol* n) {
39 _name = n; 47 _name = n;
40 if (_name != NULL) _name->increment_refcount(); 48 if (_name != NULL) _name->increment_refcount();
41 } 49 }
42 50
43 bool Klass::is_subclass_of(klassOop k) const { 51 bool Klass::is_subclass_of(Klass* k) const {
44 // Run up the super chain and check 52 // Run up the super chain and check
45 klassOop t = as_klassOop(); 53 if (this == k) return true;
46 54
47 if (t == k) return true; 55 Klass* t = const_cast<Klass*>(this)->super();
48 t = Klass::cast(t)->super();
49 56
50 while (t != NULL) { 57 while (t != NULL) {
51 if (t == k) return true; 58 if (t == k) return true;
52 t = Klass::cast(t)->super(); 59 t = Klass::cast(t)->super();
53 } 60 }
54 return false; 61 return false;
55 } 62 }
56 63
57 bool Klass::search_secondary_supers(klassOop k) const { 64 bool Klass::search_secondary_supers(Klass* k) const {
58 // Put some extra logic here out-of-line, before the search proper. 65 // Put some extra logic here out-of-line, before the search proper.
59 // This cuts down the size of the inline method. 66 // This cuts down the size of the inline method.
60 67
61 // This is necessary, since I am never in my own secondary_super list. 68 // This is necessary, since I am never in my own secondary_super list.
62 if (this->as_klassOop() == k) 69 if (this == k)
63 return true; 70 return true;
64 // Scan the array-of-objects for a match 71 // Scan the array-of-objects for a match
65 int cnt = secondary_supers()->length(); 72 int cnt = secondary_supers()->length();
66 for (int i = 0; i < cnt; i++) { 73 for (int i = 0; i < cnt; i++) {
67 if (secondary_supers()->obj_at(i) == k) { 74 if (secondary_supers()->at(i) == k) {
68 ((Klass*)this)->set_secondary_super_cache(k); 75 ((Klass*)this)->set_secondary_super_cache(k);
69 return true; 76 return true;
70 } 77 }
71 } 78 }
72 return false; 79 return false;
87 94
88 // Find LCA in class hierarchy 95 // Find LCA in class hierarchy
89 Klass *Klass::LCA( Klass *k2 ) { 96 Klass *Klass::LCA( Klass *k2 ) {
90 Klass *k1 = this; 97 Klass *k1 = this;
91 while( 1 ) { 98 while( 1 ) {
92 if( k1->is_subtype_of(k2->as_klassOop()) ) return k2; 99 if( k1->is_subtype_of(k2) ) return k2;
93 if( k2->is_subtype_of(k1->as_klassOop()) ) return k1; 100 if( k2->is_subtype_of(k1) ) return k1;
94 k1 = k1->super()->klass_part(); 101 k1 = k1->super();
95 k2 = k2->super()->klass_part(); 102 k2 = k2->super();
96 } 103 }
97 } 104 }
98 105
99 106
100 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) { 107 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) {
111 118
112 void Klass::initialize(TRAPS) { 119 void Klass::initialize(TRAPS) {
113 ShouldNotReachHere(); 120 ShouldNotReachHere();
114 } 121 }
115 122
116 bool Klass::compute_is_subtype_of(klassOop k) { 123 bool Klass::compute_is_subtype_of(Klass* k) {
117 assert(k->is_klass(), "argument must be a class"); 124 assert(k->is_klass(), "argument must be a class");
118 return is_subclass_of(k); 125 return is_subclass_of(k);
119 } 126 }
120 127
121 128
122 methodOop Klass::uncached_lookup_method(Symbol* name, Symbol* signature) const { 129 Method* Klass::uncached_lookup_method(Symbol* name, Symbol* signature) const {
123 #ifdef ASSERT 130 #ifdef ASSERT
124 tty->print_cr("Error: uncached_lookup_method called on a klass oop." 131 tty->print_cr("Error: uncached_lookup_method called on a klass oop."
125 " Likely error: reflection method does not correctly" 132 " Likely error: reflection method does not correctly"
126 " wrap return value in a mirror object."); 133 " wrap return value in a mirror object.");
127 #endif 134 #endif
128 ShouldNotReachHere(); 135 ShouldNotReachHere();
129 return NULL; 136 return NULL;
130 } 137 }
131 138
132 klassOop Klass::base_create_klass_oop(KlassHandle& klass, int size, 139 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) {
133 const Klass_vtbl& vtbl, TRAPS) { 140 return Metaspace::allocate(loader_data, word_size, /*read_only*/false,
134 size = align_object_size(size); 141 Metaspace::ClassType, CHECK_NULL);
135 // allocate and initialize vtable 142 }
136 Klass* kl = (Klass*) vtbl.allocate_permanent(klass, size, CHECK_NULL); 143
137 klassOop k = kl->as_klassOop(); 144 Klass::Klass() {
145 Klass* k = this;
138 146
139 { // Preinitialize supertype information. 147 { // Preinitialize supertype information.
140 // A later call to initialize_supers() may update these settings: 148 // A later call to initialize_supers() may update these settings:
141 kl->set_super(NULL); 149 set_super(NULL);
142 for (juint i = 0; i < Klass::primary_super_limit(); i++) { 150 for (juint i = 0; i < Klass::primary_super_limit(); i++) {
143 kl->_primary_supers[i] = NULL; 151 _primary_supers[i] = NULL;
144 } 152 }
145 kl->set_secondary_supers(NULL); 153 set_secondary_supers(NULL);
146 oop_store_without_check((oop*) &kl->_primary_supers[0], k); 154 _primary_supers[0] = k;
147 kl->set_super_check_offset(in_bytes(primary_supers_offset())); 155 set_super_check_offset(in_bytes(primary_supers_offset()));
148 } 156 }
149 157
150 kl->set_java_mirror(NULL); 158 set_java_mirror(NULL);
151 #ifdef GRAAL 159 set_modifier_flags(0);
152 kl->set_graal_mirror(NULL); 160 set_layout_helper(Klass::_lh_neutral_value);
153 #endif 161 set_name(NULL);
154 kl->set_modifier_flags(0);
155 kl->set_layout_helper(Klass::_lh_neutral_value);
156 kl->set_name(NULL);
157 AccessFlags af; 162 AccessFlags af;
158 af.set_flags(0); 163 af.set_flags(0);
159 kl->set_access_flags(af); 164 set_access_flags(af);
160 kl->set_subklass(NULL); 165 set_subklass(NULL);
161 kl->set_next_sibling(NULL); 166 set_next_sibling(NULL);
162 kl->set_alloc_count(0); 167 set_next_link(NULL);
163 kl->set_alloc_size(0); 168 set_alloc_count(0);
164 TRACE_SET_KLASS_TRACE_ID(kl, 0); 169 TRACE_SET_KLASS_TRACE_ID(this, 0);
165 170
166 kl->set_prototype_header(markOopDesc::prototype()); 171 set_prototype_header(markOopDesc::prototype());
167 kl->set_biased_lock_revocation_count(0); 172 set_biased_lock_revocation_count(0);
168 kl->set_last_biased_lock_bulk_revocation_time(0); 173 set_last_biased_lock_bulk_revocation_time(0);
169 174
170 return k; 175 // The klass doesn't have any references at this point.
171 } 176 clear_modified_oops();
172 177 clear_accumulated_modified_oops();
173 KlassHandle Klass::base_create_klass(KlassHandle& klass, int size,
174 const Klass_vtbl& vtbl, TRAPS) {
175 klassOop ek = base_create_klass_oop(klass, size, vtbl, THREAD);
176 return KlassHandle(THREAD, ek);
177 }
178
179 void Klass_vtbl::post_new_init_klass(KlassHandle& klass,
180 klassOop new_klass) const {
181 assert(!new_klass->klass_part()->null_vtbl(), "Not a complete klass");
182 CollectedHeap::post_allocation_install_obj_klass(klass, new_klass);
183 }
184
185 void* Klass_vtbl::operator new(size_t ignored, KlassHandle& klass,
186 int size, TRAPS) {
187 // The vtable pointer is installed during the execution of
188 // constructors in the call to permanent_obj_allocate(). Delay
189 // the installation of the klass pointer into the new klass "k"
190 // until after the vtable pointer has been installed (i.e., until
191 // after the return of permanent_obj_allocate().
192 klassOop k =
193 (klassOop) CollectedHeap::permanent_obj_allocate_no_klass_install(klass,
194 size, CHECK_NULL);
195 return k->klass_part();
196 } 178 }
197 179
198 jint Klass::array_layout_helper(BasicType etype) { 180 jint Klass::array_layout_helper(BasicType etype) {
199 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype"); 181 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
200 // Note that T_ARRAY is not allowed here. 182 // Note that T_ARRAY is not allowed here.
203 bool isobj = (etype == T_OBJECT); 185 bool isobj = (etype == T_OBJECT);
204 int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value; 186 int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value;
205 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize)); 187 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
206 188
207 assert(lh < (int)_lh_neutral_value, "must look like an array layout"); 189 assert(lh < (int)_lh_neutral_value, "must look like an array layout");
208 assert(layout_helper_is_javaArray(lh), "correct kind"); 190 assert(layout_helper_is_array(lh), "correct kind");
209 assert(layout_helper_is_objArray(lh) == isobj, "correct kind"); 191 assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
210 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind"); 192 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
211 assert(layout_helper_header_size(lh) == hsize, "correct decode"); 193 assert(layout_helper_header_size(lh) == hsize, "correct decode");
212 assert(layout_helper_element_type(lh) == etype, "correct decode"); 194 assert(layout_helper_element_type(lh) == etype, "correct decode");
213 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode"); 195 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");
216 } 198 }
217 199
218 bool Klass::can_be_primary_super_slow() const { 200 bool Klass::can_be_primary_super_slow() const {
219 if (super() == NULL) 201 if (super() == NULL)
220 return true; 202 return true;
221 else if (super()->klass_part()->super_depth() >= primary_super_limit()-1) 203 else if (super()->super_depth() >= primary_super_limit()-1)
222 return false; 204 return false;
223 else 205 else
224 return true; 206 return true;
225 } 207 }
226 208
227 void Klass::initialize_supers(klassOop k, TRAPS) { 209 void Klass::initialize_supers(Klass* k, TRAPS) {
228 if (FastSuperclassLimit == 0) { 210 if (FastSuperclassLimit == 0) {
229 // None of the other machinery matters. 211 // None of the other machinery matters.
230 set_super(k); 212 set_super(k);
231 return; 213 return;
232 } 214 }
233 if (k == NULL) { 215 if (k == NULL) {
234 set_super(NULL); 216 set_super(NULL);
235 oop_store_without_check((oop*) &_primary_supers[0], (oop) this->as_klassOop()); 217 _primary_supers[0] = this;
236 assert(super_depth() == 0, "Object must already be initialized properly"); 218 assert(super_depth() == 0, "Object must already be initialized properly");
237 } else if (k != super() || k == SystemDictionary::Object_klass()) { 219 } else if (k != super() || k == SystemDictionary::Object_klass()) {
238 assert(super() == NULL || super() == SystemDictionary::Object_klass(), 220 assert(super() == NULL || super() == SystemDictionary::Object_klass(),
239 "initialize this only once to a non-trivial value"); 221 "initialize this only once to a non-trivial value");
240 set_super(k); 222 set_super(k);
241 Klass* sup = k->klass_part(); 223 Klass* sup = k;
242 int sup_depth = sup->super_depth(); 224 int sup_depth = sup->super_depth();
243 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit()); 225 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit());
244 if (!can_be_primary_super_slow()) 226 if (!can_be_primary_super_slow())
245 my_depth = primary_super_limit(); 227 my_depth = primary_super_limit();
246 for (juint i = 0; i < my_depth; i++) { 228 for (juint i = 0; i < my_depth; i++) {
247 oop_store_without_check((oop*) &_primary_supers[i], (oop) sup->_primary_supers[i]); 229 _primary_supers[i] = sup->_primary_supers[i];
248 } 230 }
249 klassOop *super_check_cell; 231 Klass* *super_check_cell;
250 if (my_depth < primary_super_limit()) { 232 if (my_depth < primary_super_limit()) {
251 oop_store_without_check((oop*) &_primary_supers[my_depth], (oop) this->as_klassOop()); 233 _primary_supers[my_depth] = this;
252 super_check_cell = &_primary_supers[my_depth]; 234 super_check_cell = &_primary_supers[my_depth];
253 } else { 235 } else {
254 // Overflow of the primary_supers array forces me to be secondary. 236 // Overflow of the primary_supers array forces me to be secondary.
255 super_check_cell = &_secondary_super_cache; 237 super_check_cell = &_secondary_super_cache;
256 } 238 }
257 set_super_check_offset((address)super_check_cell - (address) this->as_klassOop()); 239 set_super_check_offset((address)super_check_cell - (address) this);
258 240
259 #ifdef ASSERT 241 #ifdef ASSERT
260 { 242 {
261 juint j = super_depth(); 243 juint j = super_depth();
262 assert(j == my_depth, "computed accessor gets right answer"); 244 assert(j == my_depth, "computed accessor gets right answer");
263 klassOop t = as_klassOop(); 245 Klass* t = this;
264 while (!Klass::cast(t)->can_be_primary_super()) { 246 while (!Klass::cast(t)->can_be_primary_super()) {
265 t = Klass::cast(t)->super(); 247 t = Klass::cast(t)->super();
266 j = Klass::cast(t)->super_depth(); 248 j = Klass::cast(t)->super_depth();
267 } 249 }
268 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) { 250 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) {
283 265
284 // Now compute the list of secondary supertypes. 266 // Now compute the list of secondary supertypes.
285 // Secondaries can occasionally be on the super chain, 267 // Secondaries can occasionally be on the super chain,
286 // if the inline "_primary_supers" array overflows. 268 // if the inline "_primary_supers" array overflows.
287 int extras = 0; 269 int extras = 0;
288 klassOop p; 270 Klass* p;
289 for (p = super(); !(p == NULL || p->klass_part()->can_be_primary_super()); p = p->klass_part()->super()) { 271 for (p = super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
290 ++extras; 272 ++extras;
291 } 273 }
292 274
275 ResourceMark rm(THREAD); // need to reclaim GrowableArrays allocated below
276
293 // Compute the "real" non-extra secondaries. 277 // Compute the "real" non-extra secondaries.
294 objArrayOop secondary_oops = compute_secondary_supers(extras, CHECK); 278 GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras);
295 objArrayHandle secondaries (THREAD, secondary_oops); 279 if (secondaries == NULL) {
296 280 // secondary_supers set by compute_secondary_supers
297 // Store the extra secondaries in the first array positions: 281 return;
298 int fillp = extras; 282 }
299 for (p = this_kh->super(); !(p == NULL || p->klass_part()->can_be_primary_super()); p = p->klass_part()->super()) { 283
284 GrowableArray<Klass*>* primaries = new GrowableArray<Klass*>(extras);
285
286 for (p = this_kh->super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
300 int i; // Scan for overflow primaries being duplicates of 2nd'arys 287 int i; // Scan for overflow primaries being duplicates of 2nd'arys
301 288
302 // This happens frequently for very deeply nested arrays: the 289 // This happens frequently for very deeply nested arrays: the
303 // primary superclass chain overflows into the secondary. The 290 // primary superclass chain overflows into the secondary. The
304 // secondary list contains the element_klass's secondaries with 291 // secondary list contains the element_klass's secondaries with
305 // an extra array dimension added. If the element_klass's 292 // an extra array dimension added. If the element_klass's
306 // secondary list already contains some primary overflows, they 293 // secondary list already contains some primary overflows, they
307 // (with the extra level of array-ness) will collide with the 294 // (with the extra level of array-ness) will collide with the
308 // normal primary superclass overflows. 295 // normal primary superclass overflows.
309 for( i = extras; i < secondaries->length(); i++ ) 296 for( i = 0; i < secondaries->length(); i++ ) {
310 if( secondaries->obj_at(i) == p ) 297 if( secondaries->at(i) == p )
311 break; 298 break;
299 }
312 if( i < secondaries->length() ) 300 if( i < secondaries->length() )
313 continue; // It's a dup, don't put it in 301 continue; // It's a dup, don't put it in
314 secondaries->obj_at_put(--fillp, p); 302 primaries->push(p);
315 } 303 }
316 // See if we had some dup's, so the array has holes in it. 304 // Combine the two arrays into a metadata object to pack the array.
317 if( fillp > 0 ) { 305 // The primaries are added in the reverse order, then the secondaries.
318 // Pack the array. Drop the old secondaries array on the floor 306 int new_length = primaries->length() + secondaries->length();
319 // and let GC reclaim it. 307 Array<Klass*>* s2 = MetadataFactory::new_array<Klass*>(
320 objArrayOop s2 = oopFactory::new_system_objArray(secondaries->length() - fillp, CHECK); 308 class_loader_data(), new_length, CHECK);
321 for( int i = 0; i < s2->length(); i++ ) 309 int fill_p = primaries->length();
322 s2->obj_at_put( i, secondaries->obj_at(i+fillp) ); 310 for (int j = 0; j < fill_p; j++) {
323 secondaries = objArrayHandle(THREAD, s2); 311 s2->at_put(j, primaries->pop()); // add primaries in reverse order.
312 }
313 for( int j = 0; j < secondaries->length(); j++ ) {
314 s2->at_put(j+fill_p, secondaries->at(j)); // add secondaries on the end.
324 } 315 }
325 316
326 #ifdef ASSERT 317 #ifdef ASSERT
327 if (secondaries() != Universe::the_array_interfaces_array()) {
328 // We must not copy any NULL placeholders left over from bootstrap. 318 // We must not copy any NULL placeholders left over from bootstrap.
329 for (int j = 0; j < secondaries->length(); j++) { 319 for (int j = 0; j < s2->length(); j++) {
330 assert(secondaries->obj_at(j) != NULL, "correct bootstrapping order"); 320 assert(s2->at(j) != NULL, "correct bootstrapping order");
331 }
332 } 321 }
333 #endif 322 #endif
334 323
335 this_kh->set_secondary_supers(secondaries()); 324 this_kh->set_secondary_supers(s2);
336 } 325 }
337 } 326 }
338 327
339 objArrayOop Klass::compute_secondary_supers(int num_extra_slots, TRAPS) { 328 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots) {
340 assert(num_extra_slots == 0, "override for complex klasses"); 329 assert(num_extra_slots == 0, "override for complex klasses");
341 return Universe::the_empty_system_obj_array(); 330 set_secondary_supers(Universe::the_empty_klass_array());
331 return NULL;
342 } 332 }
343 333
344 334
345 Klass* Klass::subklass() const { 335 Klass* Klass::subklass() const {
346 return _subklass == NULL ? NULL : Klass::cast(_subklass); 336 return _subklass == NULL ? NULL : Klass::cast(_subklass);
347 } 337 }
348 338
349 instanceKlass* Klass::superklass() const { 339 InstanceKlass* Klass::superklass() const {
350 assert(super() == NULL || super()->klass_part()->oop_is_instance(), "must be instance klass"); 340 assert(super() == NULL || super()->oop_is_instance(), "must be instance klass");
351 return _super == NULL ? NULL : instanceKlass::cast(_super); 341 return _super == NULL ? NULL : InstanceKlass::cast(_super);
352 } 342 }
353 343
354 Klass* Klass::next_sibling() const { 344 Klass* Klass::next_sibling() const {
355 return _next_sibling == NULL ? NULL : Klass::cast(_next_sibling); 345 return _next_sibling == NULL ? NULL : Klass::cast(_next_sibling);
356 } 346 }
357 347
358 void Klass::set_subklass(klassOop s) { 348 void Klass::set_subklass(Klass* s) {
359 assert(s != as_klassOop(), "sanity check"); 349 assert(s != this, "sanity check");
360 oop_store_without_check((oop*)&_subklass, s); 350 _subklass = s;
361 } 351 }
362 352
363 void Klass::set_next_sibling(klassOop s) { 353 void Klass::set_next_sibling(Klass* s) {
364 assert(s != as_klassOop(), "sanity check"); 354 assert(s != this, "sanity check");
365 oop_store_without_check((oop*)&_next_sibling, s); 355 _next_sibling = s;
366 } 356 }
367 357
368 void Klass::append_to_sibling_list() { 358 void Klass::append_to_sibling_list() {
369 debug_only(if (!SharedSkipVerify) as_klassOop()->verify();) 359 debug_only(verify();)
370 // add ourselves to superklass' subklass list 360 // add ourselves to superklass' subklass list
371 instanceKlass* super = superklass(); 361 InstanceKlass* super = superklass();
372 if (super == NULL) return; // special case: class Object 362 if (super == NULL) return; // special case: class Object
373 assert(SharedSkipVerify || 363 assert((!super->is_interface() // interfaces cannot be supers
374 (!super->is_interface() // interfaces cannot be supers
375 && (super->superklass() == NULL || !is_interface())), 364 && (super->superklass() == NULL || !is_interface())),
376 "an interface can only be a subklass of Object"); 365 "an interface can only be a subklass of Object");
377 klassOop prev_first_subklass = super->subklass_oop(); 366 Klass* prev_first_subklass = super->subklass_oop();
378 if (prev_first_subklass != NULL) { 367 if (prev_first_subklass != NULL) {
379 // set our sibling to be the superklass' previous first subklass 368 // set our sibling to be the superklass' previous first subklass
380 set_next_sibling(prev_first_subklass); 369 set_next_sibling(prev_first_subklass);
381 } 370 }
382 // make ourselves the superklass' first subklass 371 // make ourselves the superklass' first subklass
383 super->set_subklass(as_klassOop()); 372 super->set_subklass(this);
384 debug_only(if (!SharedSkipVerify) as_klassOop()->verify();) 373 debug_only(verify();)
385 } 374 }
386 375
387 void Klass::remove_from_sibling_list() { 376 void Klass::remove_from_sibling_list() {
388 // remove receiver from sibling list 377 // remove receiver from sibling list
389 instanceKlass* super = superklass(); 378 InstanceKlass* super = superklass();
390 assert(super != NULL || as_klassOop() == SystemDictionary::Object_klass(), "should have super"); 379 assert(super != NULL || this == SystemDictionary::Object_klass(), "should have super");
391 if (super == NULL) return; // special case: class Object 380 if (super == NULL) return; // special case: class Object
392 if (super->subklass() == this) { 381 if (super->subklass() == this) {
393 // first subklass 382 // first subklass
394 super->set_subklass(_next_sibling); 383 super->set_subklass(_next_sibling);
395 } else { 384 } else {
399 }; 388 };
400 sib->set_next_sibling(_next_sibling); 389 sib->set_next_sibling(_next_sibling);
401 } 390 }
402 } 391 }
403 392
404 void Klass::follow_weak_klass_links( BoolObjectClosure* is_alive, OopClosure* keep_alive) { 393 bool Klass::is_loader_alive(BoolObjectClosure* is_alive) {
405 // This klass is alive but the subklass and siblings are not followed/updated. 394 assert(is_metadata(), "p is not meta-data");
406 // We update the subklass link and the subklass' sibling links here. 395 assert(ClassLoaderDataGraph::contains((address)this), "is in the metaspace");
407 // Our own sibling link will be updated by our superclass (which must be alive 396 // The class is alive iff the class loader is alive.
408 // since we are). 397 oop loader = class_loader();
409 assert(is_alive->do_object_b(as_klassOop()), "just checking, this should be live"); 398 return (loader == NULL) || is_alive->do_object_b(loader);
410 if (ClassUnloading) { 399 }
411 klassOop sub = subklass_oop(); 400
412 if (sub != NULL && !is_alive->do_object_b(sub)) { 401 void Klass::clean_weak_klass_links(BoolObjectClosure* is_alive) {
413 // first subklass not alive, find first one alive 402 if (!ClassUnloading) {
414 do { 403 return;
404 }
405
406 Klass* root = SystemDictionary::Object_klass();
407 Stack<Klass*, mtGC> stack;
408
409 stack.push(root);
410 while (!stack.is_empty()) {
411 Klass* current = stack.pop();
412
413 assert(current->is_loader_alive(is_alive), "just checking, this should be live");
414
415 // Find and set the first alive subklass
416 Klass* sub = current->subklass_oop();
417 while (sub != NULL && !sub->is_loader_alive(is_alive)) {
415 #ifndef PRODUCT 418 #ifndef PRODUCT
416 if (TraceClassUnloading && WizardMode) { 419 if (TraceClassUnloading && WizardMode) {
417 ResourceMark rm; 420 ResourceMark rm;
418 tty->print_cr("[Unlinking class (subclass) %s]", sub->klass_part()->external_name()); 421 tty->print_cr("[Unlinking class (subclass) %s]", sub->external_name());
419 } 422 }
420 #endif 423 #endif
421 sub = sub->klass_part()->next_sibling_oop(); 424 sub = sub->next_sibling_oop();
422 } while (sub != NULL && !is_alive->do_object_b(sub)); 425 }
423 set_subklass(sub); 426 current->set_subklass(sub);
424 } 427 if (sub != NULL) {
425 // now update the subklass' sibling list 428 stack.push(sub);
426 while (sub != NULL) { 429 }
427 klassOop next = sub->klass_part()->next_sibling_oop(); 430
428 if (next != NULL && !is_alive->do_object_b(next)) { 431 // Find and set the first alive sibling
429 // first sibling not alive, find first one alive 432 Klass* sibling = current->next_sibling_oop();
430 do { 433 while (sibling != NULL && !sibling->is_loader_alive(is_alive)) {
431 #ifndef PRODUCT
432 if (TraceClassUnloading && WizardMode) { 434 if (TraceClassUnloading && WizardMode) {
433 ResourceMark rm; 435 ResourceMark rm;
434 tty->print_cr("[Unlinking class (sibling) %s]", next->klass_part()->external_name()); 436 tty->print_cr("[Unlinking class (sibling) %s]", sibling->external_name());
435 } 437 }
436 #endif 438 sibling = sibling->next_sibling_oop();
437 next = next->klass_part()->next_sibling_oop();
438 } while (next != NULL && !is_alive->do_object_b(next));
439 sub->klass_part()->set_next_sibling(next);
440 } 439 }
441 sub = next; 440 current->set_next_sibling(sibling);
442 } 441 if (sibling != NULL) {
442 stack.push(sibling);
443 }
444
445 // Clean the implementors list and method data.
446 if (current->oop_is_instance()) {
447 InstanceKlass* ik = InstanceKlass::cast(current);
448 ik->clean_implementors_list(is_alive);
449 ik->clean_method_data(is_alive);
450 }
451 }
452 }
453
454 void Klass::klass_update_barrier_set(oop v) {
455 record_modified_oops();
456 }
457
458 void Klass::klass_update_barrier_set_pre(void* p, oop v) {
459 // This barrier used by G1, where it's used remember the old oop values,
460 // so that we don't forget any objects that were live at the snapshot at
461 // the beginning. This function is only used when we write oops into
462 // Klasses. Since the Klasses are used as roots in G1, we don't have to
463 // do anything here.
464 }
465
466 void Klass::klass_oop_store(oop* p, oop v) {
467 assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
468 assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
469
470 // do the store
471 if (always_do_update_barrier) {
472 klass_oop_store((volatile oop*)p, v);
443 } else { 473 } else {
444 // Always follow subklass and sibling link. This will prevent any klasses from 474 klass_update_barrier_set_pre((void*)p, v);
445 // being unloaded (all classes are transitively linked from java.lang.Object). 475 *p = v;
446 keep_alive->do_oop(adr_subklass()); 476 klass_update_barrier_set(v);
447 keep_alive->do_oop(adr_next_sibling()); 477 }
448 } 478 }
449 } 479
450 480 void Klass::klass_oop_store(volatile oop* p, oop v) {
481 assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
482 assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
483
484 klass_update_barrier_set_pre((void*)p, v);
485 OrderAccess::release_store_ptr(p, v);
486 klass_update_barrier_set(v);
487 }
488
489 void Klass::oops_do(OopClosure* cl) {
490 cl->do_oop(&_java_mirror);
491 }
451 492
452 void Klass::remove_unshareable_info() { 493 void Klass::remove_unshareable_info() {
453 if (oop_is_instance()) {
454 instanceKlass* ik = (instanceKlass*)this;
455 if (ik->is_linked()) {
456 ik->unlink_class();
457 }
458 }
459 // Clear the Java vtable if the oop has one.
460 // The vtable isn't shareable because it's in the wrong order wrt the methods
461 // once the method names get moved and resorted.
462 klassVtable* vt = vtable();
463 if (vt != NULL) {
464 assert(oop_is_instance() || oop_is_array(), "nothing else has vtable");
465 vt->clear_vtable();
466 }
467 set_subklass(NULL); 494 set_subklass(NULL);
468 set_next_sibling(NULL); 495 set_next_sibling(NULL);
469 } 496 // Clear the java mirror
470 497 set_java_mirror(NULL);
471 498 set_next_link(NULL);
472 void Klass::shared_symbols_iterate(SymbolClosure* closure) { 499
473 closure->do_symbol(&_name); 500 // Null out class_loader_data because we don't share that yet.
474 } 501 set_class_loader_data(NULL);
475 502 }
476 503
477 klassOop Klass::array_klass_or_null(int rank) { 504 void Klass::restore_unshareable_info(TRAPS) {
505 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
506 // Restore class_loader_data to the null class loader data
507 set_class_loader_data(loader_data);
508
509 // Add to null class loader list first before creating the mirror
510 // (same order as class file parsing)
511 loader_data->add_class(this);
512
513 // Recreate the class mirror
514 java_lang_Class::create_mirror(this, CHECK);
515 }
516
517 Klass* Klass::array_klass_or_null(int rank) {
478 EXCEPTION_MARK; 518 EXCEPTION_MARK;
479 // No exception can be thrown by array_klass_impl when called with or_null == true. 519 // No exception can be thrown by array_klass_impl when called with or_null == true.
480 // (In anycase, the execption mark will fail if it do so) 520 // (In anycase, the execption mark will fail if it do so)
481 return array_klass_impl(true, rank, THREAD); 521 return array_klass_impl(true, rank, THREAD);
482 } 522 }
483 523
484 524
485 klassOop Klass::array_klass_or_null() { 525 Klass* Klass::array_klass_or_null() {
486 EXCEPTION_MARK; 526 EXCEPTION_MARK;
487 // No exception can be thrown by array_klass_impl when called with or_null == true. 527 // No exception can be thrown by array_klass_impl when called with or_null == true.
488 // (In anycase, the execption mark will fail if it do so) 528 // (In anycase, the execption mark will fail if it do so)
489 return array_klass_impl(true, THREAD); 529 return array_klass_impl(true, THREAD);
490 } 530 }
491 531
492 532
493 klassOop Klass::array_klass_impl(bool or_null, int rank, TRAPS) { 533 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) {
494 fatal("array_klass should be dispatched to instanceKlass, objArrayKlass or typeArrayKlass"); 534 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
495 return NULL; 535 return NULL;
496 } 536 }
497 537
498 538
499 klassOop Klass::array_klass_impl(bool or_null, TRAPS) { 539 Klass* Klass::array_klass_impl(bool or_null, TRAPS) {
500 fatal("array_klass should be dispatched to instanceKlass, objArrayKlass or typeArrayKlass"); 540 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
501 return NULL; 541 return NULL;
502 } 542 }
503 543
504 544
505 void Klass::with_array_klasses_do(void f(klassOop k)) { 545 void Klass::with_array_klasses_do(void f(Klass* k)) {
506 f(as_klassOop()); 546 f(this);
507 } 547 }
508 548
549
550 oop Klass::class_loader() const { return class_loader_data()->class_loader(); }
509 551
510 const char* Klass::external_name() const { 552 const char* Klass::external_name() const {
511 if (oop_is_instance()) { 553 if (oop_is_instance()) {
512 instanceKlass* ik = (instanceKlass*) this; 554 InstanceKlass* ik = (InstanceKlass*) this;
513 if (ik->is_anonymous()) { 555 if (ik->is_anonymous()) {
514 assert(EnableInvokeDynamic, ""); 556 assert(EnableInvokeDynamic, "");
515 intptr_t hash = ik->java_mirror()->identity_hash(); 557 intptr_t hash = ik->java_mirror()->identity_hash();
516 char hash_buf[40]; 558 char hash_buf[40];
517 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash); 559 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash);
548 // Unless overridden, jvmti_class_status has no flags set. 590 // Unless overridden, jvmti_class_status has no flags set.
549 jint Klass::jvmti_class_status() const { 591 jint Klass::jvmti_class_status() const {
550 return 0; 592 return 0;
551 } 593 }
552 594
595
553 // Printing 596 // Printing
597
598 void Klass::print_on(outputStream* st) const {
599 ResourceMark rm;
600 // print title
601 st->print("%s", internal_name());
602 print_address_on(st);
603 st->cr();
604 }
554 605
555 void Klass::oop_print_on(oop obj, outputStream* st) { 606 void Klass::oop_print_on(oop obj, outputStream* st) {
556 ResourceMark rm; 607 ResourceMark rm;
557 // print title 608 // print title
558 st->print_cr("%s ", internal_name()); 609 st->print_cr("%s ", internal_name());
574 ResourceMark rm; // Cannot print in debug mode without this 625 ResourceMark rm; // Cannot print in debug mode without this
575 st->print("%s", internal_name()); 626 st->print("%s", internal_name());
576 obj->print_address_on(st); 627 obj->print_address_on(st);
577 } 628 }
578 629
630
579 // Verification 631 // Verification
632
633 void Klass::verify_on(outputStream* st) {
634 guarantee(!Universe::heap()->is_in_reserved(this), "Shouldn't be");
635 guarantee(this->is_metadata(), "should be in metaspace");
636
637 assert(ClassLoaderDataGraph::contains((address)this), "Should be");
638
639 guarantee(this->is_klass(),"should be klass");
640
641 if (super() != NULL) {
642 guarantee(super()->is_metadata(), "should be in metaspace");
643 guarantee(super()->is_klass(), "should be klass");
644 }
645 if (secondary_super_cache() != NULL) {
646 Klass* ko = secondary_super_cache();
647 guarantee(ko->is_metadata(), "should be in metaspace");
648 guarantee(ko->is_klass(), "should be klass");
649 }
650 for ( uint i = 0; i < primary_super_limit(); i++ ) {
651 Klass* ko = _primary_supers[i];
652 if (ko != NULL) {
653 guarantee(ko->is_metadata(), "should be in metaspace");
654 guarantee(ko->is_klass(), "should be klass");
655 }
656 }
657
658 if (java_mirror() != NULL) {
659 guarantee(java_mirror()->is_oop(), "should be instance");
660 }
661 }
580 662
581 void Klass::oop_verify_on(oop obj, outputStream* st) { 663 void Klass::oop_verify_on(oop obj, outputStream* st) {
582 guarantee(obj->is_oop(), "should be oop"); 664 guarantee(obj->is_oop(), "should be oop");
583 guarantee(obj->klass()->is_perm(), "should be in permspace"); 665 guarantee(obj->klass()->is_metadata(), "should not be in Java heap");
584 guarantee(obj->klass()->is_klass(), "klass field is not a klass"); 666 guarantee(obj->klass()->is_klass(), "klass field is not a klass");
585 } 667 }
586 668
587 #ifndef PRODUCT 669 #ifndef PRODUCT
588 670
589 void Klass::verify_vtable_index(int i) { 671 void Klass::verify_vtable_index(int i) {
590 assert(oop_is_instance() || oop_is_array(), "only instanceKlass and arrayKlass have vtables");
591 if (oop_is_instance()) { 672 if (oop_is_instance()) {
592 assert(i>=0 && i<((instanceKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds"); 673 assert(i>=0 && i<((InstanceKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds");
593 } else { 674 } else {
594 assert(i>=0 && i<((arrayKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds"); 675 assert(oop_is_array(), "Must be");
676 assert(i>=0 && i<((ArrayKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds");
595 } 677 }
596 } 678 }
597 679
598 #endif 680 #endif