1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)constantPoolKlass.cpp        1.105 07/05/29 09:44:18 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 # include "incls/_precompiled.incl"
  29 # include "incls/_constantPoolKlass.cpp.incl"
  30 
  31 constantPoolOop constantPoolKlass::allocate(int length, TRAPS) {
  32   int size = constantPoolOopDesc::object_size(length);
  33   KlassHandle klass (THREAD, as_klassOop());
  34   constantPoolOop c = 
  35     (constantPoolOop)CollectedHeap::permanent_array_allocate(klass, size, length, CHECK_NULL);
  36 
  37   c->set_tags(NULL);
  38   c->set_cache(NULL);
  39   c->set_pool_holder(NULL);
  40   // only set to non-zero if constant pool is merged by RedefineClasses
  41   c->set_orig_length(0);
  42   // all fields are initialized; needed for GC
  43 
  44   // initialize tag array
  45   // Note: cannot introduce constant pool handle before since it is not
  46   //       completely initialized (no class) -> would cause assertion failure
  47   constantPoolHandle pool (THREAD, c);
  48   typeArrayOop t_oop = oopFactory::new_permanent_byteArray(length, CHECK_NULL);
  49   typeArrayHandle tags (THREAD, t_oop);
  50   for (int index = 0; index < length; index++) {
  51     tags()->byte_at_put(index, JVM_CONSTANT_Invalid);
  52   }
  53   pool->set_tags(tags());
  54 
  55   return pool();
  56 }
  57 
  58 klassOop constantPoolKlass::create_klass(TRAPS) {
  59   constantPoolKlass o;
  60   KlassHandle klassklass(THREAD, Universe::arrayKlassKlassObj());  
  61   arrayKlassHandle k = base_create_array_klass(o.vtbl_value(), header_size(), klassklass, CHECK_NULL);
  62   arrayKlassHandle super (THREAD, k->super());
  63   complete_create_array_klass(k, super, CHECK_NULL);
  64   return k();
  65 }
  66 
  67 
  68 int constantPoolKlass::oop_size(oop obj) const { 
  69   assert(obj->is_constantPool(), "must be constantPool");
  70   return constantPoolOop(obj)->object_size();
  71 }
  72 
  73 
  74 void constantPoolKlass::oop_follow_contents(oop obj) {
  75   assert (obj->is_constantPool(), "obj must be constant pool");
  76   constantPoolOop cp = (constantPoolOop) obj;
  77   // Performance tweak: We skip iterating over the klass pointer since we 
  78   // know that Universe::constantPoolKlassObj never moves.
  79 
  80   // If the tags array is null we are in the middle of allocating this constant pool
  81   if (cp->tags() != NULL) {
  82     // gc of constant pool contents
  83     oop* base = (oop*)cp->base();
  84     for (int i = 0; i < cp->length(); i++) {
  85       if (cp->is_pointer_entry(i)) {
  86         if (*base != NULL) MarkSweep::mark_and_push(base); 
  87       } 
  88       base++;
  89     }
  90     // gc of constant pool instance variables
  91     MarkSweep::mark_and_push(cp->tags_addr());
  92     MarkSweep::mark_and_push(cp->cache_addr());
  93     MarkSweep::mark_and_push(cp->pool_holder_addr());
  94   }
  95 }
  96 
  97 #ifndef SERIALGC
  98 void constantPoolKlass::oop_follow_contents(ParCompactionManager* cm,
  99                                             oop obj) {
 100   assert (obj->is_constantPool(), "obj must be constant pool");
 101   constantPoolOop cp = (constantPoolOop) obj;
 102   // Performance tweak: We skip iterating over the klass pointer since we 
 103   // know that Universe::constantPoolKlassObj never moves.
 104 
 105   // If the tags array is null we are in the middle of allocating this constant
 106   // pool.
 107   if (cp->tags() != NULL) {
 108     // gc of constant pool contents
 109     oop* base = (oop*)cp->base();
 110     for (int i = 0; i < cp->length(); i++) {
 111       if (cp->is_pointer_entry(i)) {
 112         if (*base != NULL) PSParallelCompact::mark_and_push(cm, base); 
 113       } 
 114       base++;
 115     }
 116     // gc of constant pool instance variables
 117     PSParallelCompact::mark_and_push(cm, cp->tags_addr());
 118     PSParallelCompact::mark_and_push(cm, cp->cache_addr());
 119     PSParallelCompact::mark_and_push(cm, cp->pool_holder_addr());
 120   }
 121 }
 122 #endif // SERIALGC
 123 
 124 
 125 int constantPoolKlass::oop_adjust_pointers(oop obj) {
 126   assert (obj->is_constantPool(), "obj must be constant pool");
 127   constantPoolOop cp = (constantPoolOop) obj;
 128   // Get size before changing pointers. 
 129   // Don't call size() or oop_size() since that is a virtual call.
 130   int size = cp->object_size();
 131   // Performance tweak: We skip iterating over the klass pointer since we 
 132   // know that Universe::constantPoolKlassObj never moves.
 133 
 134   // If the tags array is null we are in the middle of allocating this constant
 135   // pool.
 136   if (cp->tags() != NULL) {
 137     oop* base = (oop*)cp->base();
 138     for (int i = 0; i< cp->length();  i++) {
 139       if (cp->is_pointer_entry(i)) {
 140         MarkSweep::adjust_pointer(base); 
 141       } 
 142       base++;
 143     }
 144   }
 145   MarkSweep::adjust_pointer(cp->tags_addr());
 146   MarkSweep::adjust_pointer(cp->cache_addr());
 147   MarkSweep::adjust_pointer(cp->pool_holder_addr());
 148   return size;
 149 }
 150 
 151 
 152 int constantPoolKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
 153   assert (obj->is_constantPool(), "obj must be constant pool");
 154   // Performance tweak: We skip iterating over the klass pointer since we 
 155   // know that Universe::constantPoolKlassObj never moves.
 156   constantPoolOop cp = (constantPoolOop) obj;
 157   // Get size before changing pointers. 
 158   // Don't call size() or oop_size() since that is a virtual call.
 159   int size = cp->object_size();
 160 
 161   // If the tags array is null we are in the middle of allocating this constant
 162   // pool.
 163   if (cp->tags() != NULL) {
 164     oop* base = (oop*)cp->base();
 165     for (int i = 0; i < cp->length(); i++) {
 166       if (cp->is_pointer_entry(i)) {
 167         blk->do_oop(base); 
 168       } 
 169       base++;
 170     }
 171   }
 172   blk->do_oop(cp->tags_addr());
 173   blk->do_oop(cp->cache_addr());
 174   blk->do_oop(cp->pool_holder_addr());
 175   return size;
 176 }
 177 
 178 
 179 int constantPoolKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
 180   assert (obj->is_constantPool(), "obj must be constant pool");
 181   // Performance tweak: We skip iterating over the klass pointer since we 
 182   // know that Universe::constantPoolKlassObj never moves.
 183   constantPoolOop cp = (constantPoolOop) obj;
 184   // Get size before changing pointers. 
 185   // Don't call size() or oop_size() since that is a virtual call.
 186   int size = cp->object_size();
 187 
 188   // If the tags array is null we are in the middle of allocating this constant
 189   // pool.
 190   if (cp->tags() != NULL) {
 191     oop* base = (oop*)cp->base();
 192     for (int i = 0; i < cp->length(); i++) {
 193       if (mr.contains(base)) {
 194         if (cp->is_pointer_entry(i)) {
 195           blk->do_oop(base); 
 196         }
 197       }
 198       base++;
 199     }
 200   }
 201   oop* addr;
 202   addr = cp->tags_addr();
 203   blk->do_oop(addr);
 204   addr = cp->cache_addr();
 205   blk->do_oop(addr);
 206   addr = cp->pool_holder_addr();
 207   blk->do_oop(addr);
 208   return size;
 209 }
 210 
 211 #ifndef SERIALGC
 212 int constantPoolKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
 213   assert (obj->is_constantPool(), "obj must be constant pool");
 214   constantPoolOop cp = (constantPoolOop) obj;
 215 
 216   // If the tags array is null we are in the middle of allocating this constant
 217   // pool.
 218   if (cp->tags() != NULL) {
 219     oop* base = (oop*)cp->base();
 220     for (int i = 0; i < cp->length(); ++i, ++base) {
 221       if (cp->is_pointer_entry(i)) {
 222         PSParallelCompact::adjust_pointer(base); 
 223       }
 224     }
 225   }
 226   PSParallelCompact::adjust_pointer(cp->tags_addr());
 227   PSParallelCompact::adjust_pointer(cp->cache_addr());
 228   PSParallelCompact::adjust_pointer(cp->pool_holder_addr());
 229   return cp->object_size();
 230 }
 231 
 232 int
 233 constantPoolKlass::oop_update_pointers(ParCompactionManager* cm, oop obj,
 234                                        HeapWord* beg_addr, HeapWord* end_addr) {
 235   assert (obj->is_constantPool(), "obj must be constant pool");
 236   constantPoolOop cp = (constantPoolOop) obj;
 237 
 238   // If the tags array is null we are in the middle of allocating this constant
 239   // pool.
 240   if (cp->tags() != NULL) {
 241     oop* base = (oop*)cp->base();
 242     oop* const beg_oop = MAX2((oop*)beg_addr, base);
 243     oop* const end_oop = MIN2((oop*)end_addr, base + cp->length());
 244     const size_t beg_idx = pointer_delta(beg_oop, base, sizeof(oop*));
 245     const size_t end_idx = pointer_delta(end_oop, base, sizeof(oop*));
 246     for (size_t cur_idx = beg_idx; cur_idx < end_idx; ++cur_idx, ++base) {
 247       if (cp->is_pointer_entry(int(cur_idx))) {
 248         PSParallelCompact::adjust_pointer(base);
 249       }
 250     }
 251   }
 252 
 253   oop* p;
 254   p = cp->tags_addr();
 255   PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
 256   p = cp->cache_addr();
 257   PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
 258   p = cp->pool_holder_addr();
 259   PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
 260 
 261   return cp->object_size();
 262 }
 263 
 264 void constantPoolKlass::oop_copy_contents(PSPromotionManager* pm, oop obj) {
 265   assert(obj->is_constantPool(), "should be constant pool");
 266 }
 267 
 268 void constantPoolKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
 269   assert(obj->is_constantPool(), "should be constant pool");
 270 }
 271 #endif // SERIALGC
 272 
 273 #ifndef PRODUCT
 274 
 275 // Printing
 276 
 277 void constantPoolKlass::oop_print_on(oop obj, outputStream* st) {
 278   EXCEPTION_MARK;
 279   oop anObj;
 280   assert(obj->is_constantPool(), "must be constantPool");
 281   arrayKlass::oop_print_on(obj, st);
 282   constantPoolOop cp = constantPoolOop(obj);  
 283 
 284   // Temp. remove cache so we can do lookups with original indicies.
 285   constantPoolCacheHandle cache (THREAD, cp->cache());
 286   cp->set_cache(NULL);
 287 
 288   for (int index = 1; index < cp->length(); index++) {      // Index 0 is unused
 289     st->print(" - %3d : ", index);
 290     cp->tag_at(index).print_on(st);
 291     st->print(" : ");
 292     switch (cp->tag_at(index).value()) {
 293       case JVM_CONSTANT_Class :
 294         { anObj = cp->klass_at(index, CATCH);
 295           anObj->print_value_on(st);
 296           st->print(" {0x%lx}", (address)anObj);
 297         }
 298         break;
 299       case JVM_CONSTANT_Fieldref :
 300       case JVM_CONSTANT_Methodref :
 301       case JVM_CONSTANT_InterfaceMethodref :        
 302         st->print("klass_index=%d", cp->klass_ref_index_at(index));
 303         st->print(" name_and_type_index=%d", cp->name_and_type_ref_index_at(index));        
 304         break;
 305       case JVM_CONSTANT_UnresolvedString :
 306       case JVM_CONSTANT_String :
 307         anObj = cp->string_at(index, CATCH);
 308         anObj->print_value_on(st);
 309         st->print(" {0x%lx}", (address)anObj);
 310         break;
 311       case JVM_CONSTANT_Integer :
 312         st->print("%d", cp->int_at(index));
 313         break;
 314       case JVM_CONSTANT_Float :
 315         st->print("%f", cp->float_at(index));
 316         break;
 317       case JVM_CONSTANT_Long :
 318         st->print_jlong(cp->long_at(index));
 319         index++;   // Skip entry following eigth-byte constant
 320         break;
 321       case JVM_CONSTANT_Double :
 322         st->print("%lf", cp->double_at(index));
 323         index++;   // Skip entry following eigth-byte constant
 324         break;
 325       case JVM_CONSTANT_NameAndType :        
 326         st->print("name_index=%d", cp->name_ref_index_at(index));
 327         st->print(" signature_index=%d", cp->signature_ref_index_at(index));
 328         break;
 329       case JVM_CONSTANT_Utf8 :
 330         cp->symbol_at(index)->print_value_on(st);
 331         break;
 332       case JVM_CONSTANT_UnresolvedClass :               // fall-through
 333       case JVM_CONSTANT_UnresolvedClassInError: {
 334         // unresolved_klass_at requires lock or safe world.
 335         oop entry = *cp->obj_at_addr(index);
 336         entry->print_value_on(st);
 337         }
 338         break;
 339       default:
 340         ShouldNotReachHere();
 341         break;
 342     }
 343     st->cr();
 344   }
 345   st->cr();
 346 
 347   // Restore cache
 348   cp->set_cache(cache());
 349 }
 350 
 351 
 352 #endif
 353 
 354 const char* constantPoolKlass::internal_name() const {
 355   return "{constant pool}";
 356 }
 357 
 358 // Verification
 359 
 360 void constantPoolKlass::oop_verify_on(oop obj, outputStream* st) {
 361   Klass::oop_verify_on(obj, st);
 362   guarantee(obj->is_constantPool(), "object must be constant pool");
 363   constantPoolOop cp = constantPoolOop(obj);  
 364   guarantee(cp->is_perm(), "should be in permspace");
 365   if (!cp->partially_loaded()) {
 366     oop* base = (oop*)cp->base();
 367     for (int i = 0; i< cp->length();  i++) {
 368       if (cp->tag_at(i).is_klass()) {
 369         guarantee((*base)->is_perm(),     "should be in permspace");
 370         guarantee((*base)->is_klass(),    "should be klass");
 371       }
 372       if (cp->tag_at(i).is_unresolved_klass()) {
 373         guarantee((*base)->is_perm(),     "should be in permspace");
 374         guarantee((*base)->is_symbol() || (*base)->is_klass(),
 375                   "should be symbol or klass");
 376       }
 377       if (cp->tag_at(i).is_symbol()) {
 378         guarantee((*base)->is_perm(),     "should be in permspace");
 379         guarantee((*base)->is_symbol(),   "should be symbol");
 380       }
 381       if (cp->tag_at(i).is_unresolved_string()) {
 382         guarantee((*base)->is_perm(),     "should be in permspace");
 383         guarantee((*base)->is_symbol() || (*base)->is_instance(),
 384                   "should be symbol or instance");
 385       }
 386       if (cp->tag_at(i).is_string()) {
 387         guarantee((*base)->is_perm(),     "should be in permspace");
 388         guarantee((*base)->is_instance(), "should be instance");
 389       }
 390       base++;
 391     }
 392     guarantee(cp->tags()->is_perm(),         "should be in permspace");
 393     guarantee(cp->tags()->is_typeArray(),    "should be type array");
 394     if (cp->cache() != NULL) {
 395       // Note: cache() can be NULL before a class is completely setup or
 396       // in temporary constant pools used during constant pool merging
 397       guarantee(cp->cache()->is_perm(),              "should be in permspace");
 398       guarantee(cp->cache()->is_constantPoolCache(), "should be constant pool cache");
 399     }
 400     if (cp->pool_holder() != NULL) {
 401       // Note: pool_holder() can be NULL in temporary constant pools
 402       // used during constant pool merging
 403       guarantee(cp->pool_holder()->is_perm(),  "should be in permspace");
 404       guarantee(cp->pool_holder()->is_klass(), "should be klass");
 405     }
 406   }
 407 }
 408 
 409 bool constantPoolKlass::oop_partially_loaded(oop obj) const {
 410   assert(obj->is_constantPool(), "object must be constant pool");
 411   constantPoolOop cp = constantPoolOop(obj);
 412   return cp->tags() == NULL || cp->pool_holder() == (klassOop) cp;   // Check whether pool holder points to self
 413 }
 414 
 415 
 416 void constantPoolKlass::oop_set_partially_loaded(oop obj) {
 417   assert(obj->is_constantPool(), "object must be constant pool");
 418   constantPoolOop cp = constantPoolOop(obj);
 419   assert(cp->pool_holder() == NULL, "just checking");
 420   cp->set_pool_holder((klassOop) cp);   // Temporarily set pool holder to point to self
 421 }
 422 
 423 #ifndef PRODUCT
 424 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
 425 void constantPoolKlass::preload_and_initialize_all_classes(oop obj, TRAPS) {
 426   guarantee(obj->is_constantPool(), "object must be constant pool");
 427   constantPoolHandle cp(THREAD, (constantPoolOop)obj);  
 428   guarantee(!cp->partially_loaded(), "must be fully loaded");
 429     
 430   for (int i = 0; i< cp->length();  i++) {    
 431     if (cp->tag_at(i).is_unresolved_klass()) {
 432       // This will force loading of the class
 433       klassOop klass = cp->klass_at(i, CHECK);
 434       if (klass->is_instance()) {
 435         // Force initialization of class
 436         instanceKlass::cast(klass)->initialize(CHECK);
 437       } 
 438     }    
 439   }
 440 }
 441 
 442 #endif