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