comparison src/share/vm/oops/klass.cpp @ 6725:da91efe96a93

6964458: Reimplement class meta-data storage to use native memory Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>
author coleenp
date Sat, 01 Sep 2012 13:25:18 -0400
parents b632e80fc9dc
children d8ce2825b193
comparison
equal deleted inserted replaced
6724:36d1d483d5d6 6725:da91efe96a93
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 kl->set_modifier_flags(0); 159 set_modifier_flags(0);
152 kl->set_layout_helper(Klass::_lh_neutral_value); 160 set_layout_helper(Klass::_lh_neutral_value);
153 kl->set_name(NULL); 161 set_name(NULL);
154 AccessFlags af; 162 AccessFlags af;
155 af.set_flags(0); 163 af.set_flags(0);
156 kl->set_access_flags(af); 164 set_access_flags(af);
157 kl->set_subklass(NULL); 165 set_subklass(NULL);
158 kl->set_next_sibling(NULL); 166 set_next_sibling(NULL);
159 kl->set_alloc_count(0); 167 set_next_link(NULL);
160 kl->set_alloc_size(0); 168 set_alloc_count(0);
161 TRACE_SET_KLASS_TRACE_ID(kl, 0); 169 TRACE_SET_KLASS_TRACE_ID(this, 0);
162 170
163 kl->set_prototype_header(markOopDesc::prototype()); 171 set_prototype_header(markOopDesc::prototype());
164 kl->set_biased_lock_revocation_count(0); 172 set_biased_lock_revocation_count(0);
165 kl->set_last_biased_lock_bulk_revocation_time(0); 173 set_last_biased_lock_bulk_revocation_time(0);
166 174
167 return k; 175 // The klass doesn't have any references at this point.
168 } 176 clear_modified_oops();
169 177 clear_accumulated_modified_oops();
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 }
175
176 void Klass_vtbl::post_new_init_klass(KlassHandle& klass,
177 klassOop new_klass) const {
178 assert(!new_klass->klass_part()->null_vtbl(), "Not a complete klass");
179 CollectedHeap::post_allocation_install_obj_klass(klass, new_klass);
180 }
181
182 void* Klass_vtbl::operator new(size_t ignored, KlassHandle& klass,
183 int size, TRAPS) {
184 // The vtable pointer is installed during the execution of
185 // constructors in the call to permanent_obj_allocate(). Delay
186 // the installation of the klass pointer into the new klass "k"
187 // until after the vtable pointer has been installed (i.e., until
188 // after the return of permanent_obj_allocate().
189 klassOop k =
190 (klassOop) CollectedHeap::permanent_obj_allocate_no_klass_install(klass,
191 size, CHECK_NULL);
192 return k->klass_part();
193 } 178 }
194 179
195 jint Klass::array_layout_helper(BasicType etype) { 180 jint Klass::array_layout_helper(BasicType etype) {
196 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype"); 181 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
197 // Note that T_ARRAY is not allowed here. 182 // Note that T_ARRAY is not allowed here.
200 bool isobj = (etype == T_OBJECT); 185 bool isobj = (etype == T_OBJECT);
201 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;
202 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize)); 187 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
203 188
204 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");
205 assert(layout_helper_is_javaArray(lh), "correct kind"); 190 assert(layout_helper_is_array(lh), "correct kind");
206 assert(layout_helper_is_objArray(lh) == isobj, "correct kind"); 191 assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
207 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind"); 192 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
208 assert(layout_helper_header_size(lh) == hsize, "correct decode"); 193 assert(layout_helper_header_size(lh) == hsize, "correct decode");
209 assert(layout_helper_element_type(lh) == etype, "correct decode"); 194 assert(layout_helper_element_type(lh) == etype, "correct decode");
210 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode"); 195 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");
213 } 198 }
214 199
215 bool Klass::can_be_primary_super_slow() const { 200 bool Klass::can_be_primary_super_slow() const {
216 if (super() == NULL) 201 if (super() == NULL)
217 return true; 202 return true;
218 else if (super()->klass_part()->super_depth() >= primary_super_limit()-1) 203 else if (super()->super_depth() >= primary_super_limit()-1)
219 return false; 204 return false;
220 else 205 else
221 return true; 206 return true;
222 } 207 }
223 208
224 void Klass::initialize_supers(klassOop k, TRAPS) { 209 void Klass::initialize_supers(Klass* k, TRAPS) {
225 if (FastSuperclassLimit == 0) { 210 if (FastSuperclassLimit == 0) {
226 // None of the other machinery matters. 211 // None of the other machinery matters.
227 set_super(k); 212 set_super(k);
228 return; 213 return;
229 } 214 }
230 if (k == NULL) { 215 if (k == NULL) {
231 set_super(NULL); 216 set_super(NULL);
232 oop_store_without_check((oop*) &_primary_supers[0], (oop) this->as_klassOop()); 217 _primary_supers[0] = this;
233 assert(super_depth() == 0, "Object must already be initialized properly"); 218 assert(super_depth() == 0, "Object must already be initialized properly");
234 } else if (k != super() || k == SystemDictionary::Object_klass()) { 219 } else if (k != super() || k == SystemDictionary::Object_klass()) {
235 assert(super() == NULL || super() == SystemDictionary::Object_klass(), 220 assert(super() == NULL || super() == SystemDictionary::Object_klass(),
236 "initialize this only once to a non-trivial value"); 221 "initialize this only once to a non-trivial value");
237 set_super(k); 222 set_super(k);
238 Klass* sup = k->klass_part(); 223 Klass* sup = k;
239 int sup_depth = sup->super_depth(); 224 int sup_depth = sup->super_depth();
240 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit()); 225 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit());
241 if (!can_be_primary_super_slow()) 226 if (!can_be_primary_super_slow())
242 my_depth = primary_super_limit(); 227 my_depth = primary_super_limit();
243 for (juint i = 0; i < my_depth; i++) { 228 for (juint i = 0; i < my_depth; i++) {
244 oop_store_without_check((oop*) &_primary_supers[i], (oop) sup->_primary_supers[i]); 229 _primary_supers[i] = sup->_primary_supers[i];
245 } 230 }
246 klassOop *super_check_cell; 231 Klass* *super_check_cell;
247 if (my_depth < primary_super_limit()) { 232 if (my_depth < primary_super_limit()) {
248 oop_store_without_check((oop*) &_primary_supers[my_depth], (oop) this->as_klassOop()); 233 _primary_supers[my_depth] = this;
249 super_check_cell = &_primary_supers[my_depth]; 234 super_check_cell = &_primary_supers[my_depth];
250 } else { 235 } else {
251 // Overflow of the primary_supers array forces me to be secondary. 236 // Overflow of the primary_supers array forces me to be secondary.
252 super_check_cell = &_secondary_super_cache; 237 super_check_cell = &_secondary_super_cache;
253 } 238 }
254 set_super_check_offset((address)super_check_cell - (address) this->as_klassOop()); 239 set_super_check_offset((address)super_check_cell - (address) this);
255 240
256 #ifdef ASSERT 241 #ifdef ASSERT
257 { 242 {
258 juint j = super_depth(); 243 juint j = super_depth();
259 assert(j == my_depth, "computed accessor gets right answer"); 244 assert(j == my_depth, "computed accessor gets right answer");
260 klassOop t = as_klassOop(); 245 Klass* t = this;
261 while (!Klass::cast(t)->can_be_primary_super()) { 246 while (!Klass::cast(t)->can_be_primary_super()) {
262 t = Klass::cast(t)->super(); 247 t = Klass::cast(t)->super();
263 j = Klass::cast(t)->super_depth(); 248 j = Klass::cast(t)->super_depth();
264 } 249 }
265 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) { 250 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) {
280 265
281 // Now compute the list of secondary supertypes. 266 // Now compute the list of secondary supertypes.
282 // Secondaries can occasionally be on the super chain, 267 // Secondaries can occasionally be on the super chain,
283 // if the inline "_primary_supers" array overflows. 268 // if the inline "_primary_supers" array overflows.
284 int extras = 0; 269 int extras = 0;
285 klassOop p; 270 Klass* p;
286 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()) {
287 ++extras; 272 ++extras;
288 } 273 }
289 274
275 ResourceMark rm(THREAD); // need to reclaim GrowableArrays allocated below
276
290 // Compute the "real" non-extra secondaries. 277 // Compute the "real" non-extra secondaries.
291 objArrayOop secondary_oops = compute_secondary_supers(extras, CHECK); 278 GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras);
292 objArrayHandle secondaries (THREAD, secondary_oops); 279 if (secondaries == NULL) {
293 280 // secondary_supers set by compute_secondary_supers
294 // Store the extra secondaries in the first array positions: 281 return;
295 int fillp = extras; 282 }
296 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()) {
297 int i; // Scan for overflow primaries being duplicates of 2nd'arys 287 int i; // Scan for overflow primaries being duplicates of 2nd'arys
298 288
299 // This happens frequently for very deeply nested arrays: the 289 // This happens frequently for very deeply nested arrays: the
300 // primary superclass chain overflows into the secondary. The 290 // primary superclass chain overflows into the secondary. The
301 // secondary list contains the element_klass's secondaries with 291 // secondary list contains the element_klass's secondaries with
302 // an extra array dimension added. If the element_klass's 292 // an extra array dimension added. If the element_klass's
303 // secondary list already contains some primary overflows, they 293 // secondary list already contains some primary overflows, they
304 // (with the extra level of array-ness) will collide with the 294 // (with the extra level of array-ness) will collide with the
305 // normal primary superclass overflows. 295 // normal primary superclass overflows.
306 for( i = extras; i < secondaries->length(); i++ ) 296 for( i = 0; i < secondaries->length(); i++ ) {
307 if( secondaries->obj_at(i) == p ) 297 if( secondaries->at(i) == p )
308 break; 298 break;
299 }
309 if( i < secondaries->length() ) 300 if( i < secondaries->length() )
310 continue; // It's a dup, don't put it in 301 continue; // It's a dup, don't put it in
311 secondaries->obj_at_put(--fillp, p); 302 primaries->push(p);
312 } 303 }
313 // 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.
314 if( fillp > 0 ) { 305 // The primaries are added in the reverse order, then the secondaries.
315 // Pack the array. Drop the old secondaries array on the floor 306 int new_length = primaries->length() + secondaries->length();
316 // and let GC reclaim it. 307 Array<Klass*>* s2 = MetadataFactory::new_array<Klass*>(
317 objArrayOop s2 = oopFactory::new_system_objArray(secondaries->length() - fillp, CHECK); 308 class_loader_data(), new_length, CHECK);
318 for( int i = 0; i < s2->length(); i++ ) 309 int fill_p = primaries->length();
319 s2->obj_at_put( i, secondaries->obj_at(i+fillp) ); 310 for (int j = 0; j < fill_p; j++) {
320 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.
321 } 315 }
322 316
323 #ifdef ASSERT 317 #ifdef ASSERT
324 if (secondaries() != Universe::the_array_interfaces_array()) {
325 // We must not copy any NULL placeholders left over from bootstrap. 318 // We must not copy any NULL placeholders left over from bootstrap.
326 for (int j = 0; j < secondaries->length(); j++) { 319 for (int j = 0; j < s2->length(); j++) {
327 assert(secondaries->obj_at(j) != NULL, "correct bootstrapping order"); 320 assert(s2->at(j) != NULL, "correct bootstrapping order");
328 }
329 } 321 }
330 #endif 322 #endif
331 323
332 this_kh->set_secondary_supers(secondaries()); 324 this_kh->set_secondary_supers(s2);
333 } 325 }
334 } 326 }
335 327
336 objArrayOop Klass::compute_secondary_supers(int num_extra_slots, TRAPS) { 328 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots) {
337 assert(num_extra_slots == 0, "override for complex klasses"); 329 assert(num_extra_slots == 0, "override for complex klasses");
338 return Universe::the_empty_system_obj_array(); 330 set_secondary_supers(Universe::the_empty_klass_array());
331 return NULL;
339 } 332 }
340 333
341 334
342 Klass* Klass::subklass() const { 335 Klass* Klass::subklass() const {
343 return _subklass == NULL ? NULL : Klass::cast(_subklass); 336 return _subklass == NULL ? NULL : Klass::cast(_subklass);
344 } 337 }
345 338
346 instanceKlass* Klass::superklass() const { 339 InstanceKlass* Klass::superklass() const {
347 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");
348 return _super == NULL ? NULL : instanceKlass::cast(_super); 341 return _super == NULL ? NULL : InstanceKlass::cast(_super);
349 } 342 }
350 343
351 Klass* Klass::next_sibling() const { 344 Klass* Klass::next_sibling() const {
352 return _next_sibling == NULL ? NULL : Klass::cast(_next_sibling); 345 return _next_sibling == NULL ? NULL : Klass::cast(_next_sibling);
353 } 346 }
354 347
355 void Klass::set_subklass(klassOop s) { 348 void Klass::set_subklass(Klass* s) {
356 assert(s != as_klassOop(), "sanity check"); 349 assert(s != this, "sanity check");
357 oop_store_without_check((oop*)&_subklass, s); 350 _subklass = s;
358 } 351 }
359 352
360 void Klass::set_next_sibling(klassOop s) { 353 void Klass::set_next_sibling(Klass* s) {
361 assert(s != as_klassOop(), "sanity check"); 354 assert(s != this, "sanity check");
362 oop_store_without_check((oop*)&_next_sibling, s); 355 _next_sibling = s;
363 } 356 }
364 357
365 void Klass::append_to_sibling_list() { 358 void Klass::append_to_sibling_list() {
366 debug_only(if (!SharedSkipVerify) as_klassOop()->verify();) 359 debug_only(if (!SharedSkipVerify) verify();)
367 // add ourselves to superklass' subklass list 360 // add ourselves to superklass' subklass list
368 instanceKlass* super = superklass(); 361 InstanceKlass* super = superklass();
369 if (super == NULL) return; // special case: class Object 362 if (super == NULL) return; // special case: class Object
370 assert(SharedSkipVerify || 363 assert(SharedSkipVerify ||
371 (!super->is_interface() // interfaces cannot be supers 364 (!super->is_interface() // interfaces cannot be supers
372 && (super->superklass() == NULL || !is_interface())), 365 && (super->superklass() == NULL || !is_interface())),
373 "an interface can only be a subklass of Object"); 366 "an interface can only be a subklass of Object");
374 klassOop prev_first_subklass = super->subklass_oop(); 367 Klass* prev_first_subklass = super->subklass_oop();
375 if (prev_first_subklass != NULL) { 368 if (prev_first_subklass != NULL) {
376 // set our sibling to be the superklass' previous first subklass 369 // set our sibling to be the superklass' previous first subklass
377 set_next_sibling(prev_first_subklass); 370 set_next_sibling(prev_first_subklass);
378 } 371 }
379 // make ourselves the superklass' first subklass 372 // make ourselves the superklass' first subklass
380 super->set_subklass(as_klassOop()); 373 super->set_subklass(this);
381 debug_only(if (!SharedSkipVerify) as_klassOop()->verify();) 374 debug_only(if (!SharedSkipVerify) verify();)
382 } 375 }
383 376
384 void Klass::remove_from_sibling_list() { 377 void Klass::remove_from_sibling_list() {
385 // remove receiver from sibling list 378 // remove receiver from sibling list
386 instanceKlass* super = superklass(); 379 InstanceKlass* super = superklass();
387 assert(super != NULL || as_klassOop() == SystemDictionary::Object_klass(), "should have super"); 380 assert(super != NULL || this == SystemDictionary::Object_klass(), "should have super");
388 if (super == NULL) return; // special case: class Object 381 if (super == NULL) return; // special case: class Object
389 if (super->subklass() == this) { 382 if (super->subklass() == this) {
390 // first subklass 383 // first subklass
391 super->set_subklass(_next_sibling); 384 super->set_subklass(_next_sibling);
392 } else { 385 } else {
396 }; 389 };
397 sib->set_next_sibling(_next_sibling); 390 sib->set_next_sibling(_next_sibling);
398 } 391 }
399 } 392 }
400 393
401 void Klass::follow_weak_klass_links( BoolObjectClosure* is_alive, OopClosure* keep_alive) { 394 bool Klass::is_loader_alive(BoolObjectClosure* is_alive) {
402 // This klass is alive but the subklass and siblings are not followed/updated. 395 assert(is_metadata(), "p is not meta-data");
403 // We update the subklass link and the subklass' sibling links here. 396 assert(ClassLoaderDataGraph::contains((address)this), "is in the metaspace");
404 // Our own sibling link will be updated by our superclass (which must be alive 397 // The class is alive iff the class loader is alive.
405 // since we are). 398 oop loader = class_loader();
406 assert(is_alive->do_object_b(as_klassOop()), "just checking, this should be live"); 399 return (loader == NULL) || is_alive->do_object_b(loader);
407 if (ClassUnloading) { 400 }
408 klassOop sub = subklass_oop(); 401
409 if (sub != NULL && !is_alive->do_object_b(sub)) { 402 void Klass::clean_weak_klass_links(BoolObjectClosure* is_alive) {
410 // first subklass not alive, find first one alive 403 if (!ClassUnloading) {
411 do { 404 return;
405 }
406
407 Klass* root = SystemDictionary::Object_klass();
408 Stack<Klass*, mtGC> stack;
409
410 stack.push(root);
411 while (!stack.is_empty()) {
412 Klass* current = stack.pop();
413
414 assert(current->is_loader_alive(is_alive), "just checking, this should be live");
415
416 // Find and set the first alive subklass
417 Klass* sub = current->subklass_oop();
418 while (sub != NULL && !sub->is_loader_alive(is_alive)) {
412 #ifndef PRODUCT 419 #ifndef PRODUCT
413 if (TraceClassUnloading && WizardMode) { 420 if (TraceClassUnloading && WizardMode) {
414 ResourceMark rm; 421 ResourceMark rm;
415 tty->print_cr("[Unlinking class (subclass) %s]", sub->klass_part()->external_name()); 422 tty->print_cr("[Unlinking class (subclass) %s]", sub->external_name());
416 } 423 }
417 #endif 424 #endif
418 sub = sub->klass_part()->next_sibling_oop(); 425 sub = sub->next_sibling_oop();
419 } while (sub != NULL && !is_alive->do_object_b(sub)); 426 }
420 set_subklass(sub); 427 current->set_subklass(sub);
421 } 428 if (sub != NULL) {
422 // now update the subklass' sibling list 429 stack.push(sub);
423 while (sub != NULL) { 430 }
424 klassOop next = sub->klass_part()->next_sibling_oop(); 431
425 if (next != NULL && !is_alive->do_object_b(next)) { 432 // Find and set the first alive sibling
426 // first sibling not alive, find first one alive 433 Klass* sibling = current->next_sibling_oop();
427 do { 434 while (sibling != NULL && !sibling->is_loader_alive(is_alive)) {
428 #ifndef PRODUCT
429 if (TraceClassUnloading && WizardMode) { 435 if (TraceClassUnloading && WizardMode) {
430 ResourceMark rm; 436 ResourceMark rm;
431 tty->print_cr("[Unlinking class (sibling) %s]", next->klass_part()->external_name()); 437 tty->print_cr("[Unlinking class (sibling) %s]", sibling->external_name());
432 } 438 }
433 #endif 439 sibling = sibling->next_sibling_oop();
434 next = next->klass_part()->next_sibling_oop();
435 } while (next != NULL && !is_alive->do_object_b(next));
436 sub->klass_part()->set_next_sibling(next);
437 } 440 }
438 sub = next; 441 current->set_next_sibling(sibling);
439 } 442 if (sibling != NULL) {
443 stack.push(sibling);
444 }
445
446 // Clean the implementors list and method data.
447 if (current->oop_is_instance()) {
448 InstanceKlass* ik = InstanceKlass::cast(current);
449 ik->clean_implementors_list(is_alive);
450 ik->clean_method_data(is_alive);
451 }
452 }
453 }
454
455 void Klass::klass_update_barrier_set(oop v) {
456 record_modified_oops();
457 }
458
459 void Klass::klass_update_barrier_set_pre(void* p, oop v) {
460 // This barrier used by G1, where it's used remember the old oop values,
461 // so that we don't forget any objects that were live at the snapshot at
462 // the beginning. This function is only used when we write oops into
463 // Klasses. Since the Klasses are used as roots in G1, we don't have to
464 // do anything here.
465 }
466
467 void Klass::klass_oop_store(oop* p, oop v) {
468 assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
469 assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
470
471 // do the store
472 if (always_do_update_barrier) {
473 klass_oop_store((volatile oop*)p, v);
440 } else { 474 } else {
441 // Always follow subklass and sibling link. This will prevent any klasses from 475 klass_update_barrier_set_pre((void*)p, v);
442 // being unloaded (all classes are transitively linked from java.lang.Object). 476 *p = v;
443 keep_alive->do_oop(adr_subklass()); 477 klass_update_barrier_set(v);
444 keep_alive->do_oop(adr_next_sibling()); 478 }
445 } 479 }
446 } 480
447 481 void Klass::klass_oop_store(volatile oop* p, oop v) {
482 assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
483 assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
484
485 klass_update_barrier_set_pre((void*)p, v);
486 OrderAccess::release_store_ptr(p, v);
487 klass_update_barrier_set(v);
488 }
489
490 void Klass::oops_do(OopClosure* cl) {
491 cl->do_oop(&_java_mirror);
492 }
448 493
449 void Klass::remove_unshareable_info() { 494 void Klass::remove_unshareable_info() {
450 if (oop_is_instance()) {
451 instanceKlass* ik = (instanceKlass*)this;
452 if (ik->is_linked()) {
453 ik->unlink_class();
454 }
455 }
456 // Clear the Java vtable if the oop has one.
457 // The vtable isn't shareable because it's in the wrong order wrt the methods
458 // once the method names get moved and resorted.
459 klassVtable* vt = vtable();
460 if (vt != NULL) {
461 assert(oop_is_instance() || oop_is_array(), "nothing else has vtable");
462 vt->clear_vtable();
463 }
464 set_subklass(NULL); 495 set_subklass(NULL);
465 set_next_sibling(NULL); 496 set_next_sibling(NULL);
466 } 497 // Clear the java mirror
467 498 set_java_mirror(NULL);
468 499 set_next_link(NULL);
469 void Klass::shared_symbols_iterate(SymbolClosure* closure) { 500
470 closure->do_symbol(&_name); 501 // Null out class_loader_data because we don't share that yet.
471 } 502 set_class_loader_data(NULL);
472 503 }
473 504
474 klassOop Klass::array_klass_or_null(int rank) { 505 void Klass::restore_unshareable_info(TRAPS) {
506 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
507 // Restore class_loader_data to the null class loader data
508 set_class_loader_data(loader_data);
509
510 // Add to null class loader list first before creating the mirror
511 // (same order as class file parsing)
512 loader_data->add_class(this);
513
514 // Recreate the class mirror
515 java_lang_Class::create_mirror(this, CHECK);
516 }
517
518 Klass* Klass::array_klass_or_null(int rank) {
475 EXCEPTION_MARK; 519 EXCEPTION_MARK;
476 // No exception can be thrown by array_klass_impl when called with or_null == true. 520 // No exception can be thrown by array_klass_impl when called with or_null == true.
477 // (In anycase, the execption mark will fail if it do so) 521 // (In anycase, the execption mark will fail if it do so)
478 return array_klass_impl(true, rank, THREAD); 522 return array_klass_impl(true, rank, THREAD);
479 } 523 }
480 524
481 525
482 klassOop Klass::array_klass_or_null() { 526 Klass* Klass::array_klass_or_null() {
483 EXCEPTION_MARK; 527 EXCEPTION_MARK;
484 // No exception can be thrown by array_klass_impl when called with or_null == true. 528 // No exception can be thrown by array_klass_impl when called with or_null == true.
485 // (In anycase, the execption mark will fail if it do so) 529 // (In anycase, the execption mark will fail if it do so)
486 return array_klass_impl(true, THREAD); 530 return array_klass_impl(true, THREAD);
487 } 531 }
488 532
489 533
490 klassOop Klass::array_klass_impl(bool or_null, int rank, TRAPS) { 534 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) {
491 fatal("array_klass should be dispatched to instanceKlass, objArrayKlass or typeArrayKlass"); 535 fatal("array_klass should be dispatched to InstanceKlass, objArrayKlass or typeArrayKlass");
492 return NULL; 536 return NULL;
493 } 537 }
494 538
495 539
496 klassOop Klass::array_klass_impl(bool or_null, TRAPS) { 540 Klass* Klass::array_klass_impl(bool or_null, TRAPS) {
497 fatal("array_klass should be dispatched to instanceKlass, objArrayKlass or typeArrayKlass"); 541 fatal("array_klass should be dispatched to InstanceKlass, objArrayKlass or typeArrayKlass");
498 return NULL; 542 return NULL;
499 } 543 }
500 544
501 545
502 void Klass::with_array_klasses_do(void f(klassOop k)) { 546 void Klass::with_array_klasses_do(void f(Klass* k)) {
503 f(as_klassOop()); 547 f(this);
504 } 548 }
505 549
550
551 oop Klass::class_loader() const { return class_loader_data()->class_loader(); }
506 552
507 const char* Klass::external_name() const { 553 const char* Klass::external_name() const {
508 if (oop_is_instance()) { 554 if (oop_is_instance()) {
509 instanceKlass* ik = (instanceKlass*) this; 555 InstanceKlass* ik = (InstanceKlass*) this;
510 if (ik->is_anonymous()) { 556 if (ik->is_anonymous()) {
511 assert(EnableInvokeDynamic, ""); 557 assert(EnableInvokeDynamic, "");
512 intptr_t hash = ik->java_mirror()->identity_hash(); 558 intptr_t hash = ik->java_mirror()->identity_hash();
513 char hash_buf[40]; 559 char hash_buf[40];
514 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash); 560 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash);
545 // Unless overridden, jvmti_class_status has no flags set. 591 // Unless overridden, jvmti_class_status has no flags set.
546 jint Klass::jvmti_class_status() const { 592 jint Klass::jvmti_class_status() const {
547 return 0; 593 return 0;
548 } 594 }
549 595
596
550 // Printing 597 // Printing
598
599 void Klass::print_on(outputStream* st) const {
600 ResourceMark rm;
601 // print title
602 st->print("%s", internal_name());
603 print_address_on(st);
604 st->cr();
605 }
551 606
552 void Klass::oop_print_on(oop obj, outputStream* st) { 607 void Klass::oop_print_on(oop obj, outputStream* st) {
553 ResourceMark rm; 608 ResourceMark rm;
554 // print title 609 // print title
555 st->print_cr("%s ", internal_name()); 610 st->print_cr("%s ", internal_name());
571 ResourceMark rm; // Cannot print in debug mode without this 626 ResourceMark rm; // Cannot print in debug mode without this
572 st->print("%s", internal_name()); 627 st->print("%s", internal_name());
573 obj->print_address_on(st); 628 obj->print_address_on(st);
574 } 629 }
575 630
631
576 // Verification 632 // Verification
633
634 void Klass::verify_on(outputStream* st) {
635 guarantee(!Universe::heap()->is_in_reserved(this), "Shouldn't be");
636 guarantee(this->is_metadata(), "should be in metaspace");
637
638 assert(ClassLoaderDataGraph::contains((address)this), "Should be");
639
640 guarantee(this->is_klass(),"should be klass");
641
642 if (super() != NULL) {
643 guarantee(super()->is_metadata(), "should be in metaspace");
644 guarantee(super()->is_klass(), "should be klass");
645 }
646 if (secondary_super_cache() != NULL) {
647 Klass* ko = secondary_super_cache();
648 guarantee(ko->is_metadata(), "should be in metaspace");
649 guarantee(ko->is_klass(), "should be klass");
650 }
651 for ( uint i = 0; i < primary_super_limit(); i++ ) {
652 Klass* ko = _primary_supers[i];
653 if (ko != NULL) {
654 guarantee(ko->is_metadata(), "should be in metaspace");
655 guarantee(ko->is_klass(), "should be klass");
656 }
657 }
658
659 if (java_mirror() != NULL) {
660 guarantee(java_mirror()->is_oop(), "should be instance");
661 }
662 }
577 663
578 void Klass::oop_verify_on(oop obj, outputStream* st) { 664 void Klass::oop_verify_on(oop obj, outputStream* st) {
579 guarantee(obj->is_oop(), "should be oop"); 665 guarantee(obj->is_oop(), "should be oop");
580 guarantee(obj->klass()->is_perm(), "should be in permspace"); 666 guarantee(obj->klass()->is_metadata(), "should not be in Java heap");
581 guarantee(obj->klass()->is_klass(), "klass field is not a klass"); 667 guarantee(obj->klass()->is_klass(), "klass field is not a klass");
582 } 668 }
583 669
584 #ifndef PRODUCT 670 #ifndef PRODUCT
585 671
586 void Klass::verify_vtable_index(int i) { 672 void Klass::verify_vtable_index(int i) {
587 assert(oop_is_instance() || oop_is_array(), "only instanceKlass and arrayKlass have vtables");
588 if (oop_is_instance()) { 673 if (oop_is_instance()) {
589 assert(i>=0 && i<((instanceKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds"); 674 assert(i>=0 && i<((InstanceKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds");
590 } else { 675 } else {
676 assert(oop_is_array(), "Must be");
591 assert(i>=0 && i<((arrayKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds"); 677 assert(i>=0 && i<((arrayKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds");
592 } 678 }
593 } 679 }
594 680
595 #endif 681 #endif