1 /*
   2  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "gc_implementation/shared/markSweep.inline.hpp"
  28 #include "gc_interface/collectedHeap.hpp"
  29 #include "interpreter/bytecodes.hpp"
  30 #include "memory/genOopClosures.inline.hpp"
  31 #include "memory/permGen.hpp"
  32 #include "oops/constantPoolOop.hpp"
  33 #include "oops/cpCacheKlass.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/handles.inline.hpp"
  36 #ifndef SERIALGC
  37 #include "gc_implementation/parNew/parOopClosures.inline.hpp"
  38 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
  39 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
  40 #include "memory/cardTableRS.hpp"
  41 #include "oops/oop.pcgc.inline.hpp"
  42 #endif
  43 
  44 
  45 int constantPoolCacheKlass::oop_size(oop obj) const {
  46   assert(obj->is_constantPoolCache(), "must be constantPool");
  47   return constantPoolCacheOop(obj)->object_size();
  48 }
  49 
  50 
  51 constantPoolCacheOop constantPoolCacheKlass::allocate(int length,
  52                                                       bool is_conc_safe,
  53                                                       TRAPS) {
  54   // allocate memory
  55   int size = constantPoolCacheOopDesc::object_size(length);
  56 
  57   KlassHandle klass (THREAD, as_klassOop());
  58 
  59   // This is the original code.  The code from permanent_obj_allocate()
  60   // was in-lined to allow the setting of is_conc_safe before the klass
  61   // is installed.
  62   // constantPoolCacheOop cache = (constantPoolCacheOop)
  63   //   CollectedHeap::permanent_obj_allocate(klass, size, CHECK_NULL);
  64 
  65   oop obj = CollectedHeap::permanent_obj_allocate_no_klass_install(klass, size, CHECK_NULL);
  66   constantPoolCacheOop cache = (constantPoolCacheOop) obj;
  67   cache->set_is_conc_safe(is_conc_safe);
  68   // The store to is_conc_safe must be visible before the klass
  69   // is set.  This should be done safely because _is_conc_safe has
  70   // been declared volatile.  If there are any problems, consider adding
  71   // OrderAccess::storestore();
  72   CollectedHeap::post_allocation_install_obj_klass(klass, obj, size);
  73   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value((HeapWord*) obj,
  74                                                               size));
  75 
  76   // The length field affects the size of the object.  The allocation
  77   // above allocates the correct size (see calculation of "size") but
  78   // the size() method of the constant pool cache oop will not reflect
  79   // that size until the correct length is set.
  80   cache->set_length(length);
  81 
  82   // The store of the length must be visible before is_conc_safe is
  83   // set to a safe state.
  84   // This should be done safely because _is_conc_safe has
  85   // been declared volatile.  If there are any problems, consider adding
  86   // OrderAccess::storestore();
  87   cache->set_is_conc_safe(methodOopDesc::IsSafeConc);
  88   cache->set_constant_pool(NULL);
  89   return cache;
  90 }
  91 
  92 klassOop constantPoolCacheKlass::create_klass(TRAPS) {
  93   constantPoolCacheKlass o;
  94   KlassHandle h_this_klass(THREAD, Universe::klassKlassObj());
  95   KlassHandle k = base_create_klass(h_this_klass, header_size(), o.vtbl_value(), CHECK_NULL);
  96   // Make sure size calculation is right
  97   assert(k()->size() == align_object_size(header_size()), "wrong size for object");
  98   java_lang_Class::create_mirror(k, CHECK_NULL); // Allocate mirror
  99   return k();
 100 }
 101 
 102 
 103 void constantPoolCacheKlass::oop_follow_contents(oop obj) {
 104   assert(obj->is_constantPoolCache(), "obj must be constant pool cache");
 105   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 106   // Performance tweak: We skip iterating over the klass pointer since we
 107   // know that Universe::constantPoolCacheKlassObj never moves.
 108   // gc of constant pool cache instance variables
 109   MarkSweep::mark_and_push((oop*)cache->constant_pool_addr());
 110   // gc of constant pool cache entries
 111   int i = cache->length();
 112   while (i-- > 0) cache->entry_at(i)->follow_contents();
 113 }
 114 
 115 #ifndef SERIALGC
 116 void constantPoolCacheKlass::oop_follow_contents(ParCompactionManager* cm,
 117                                                  oop obj) {
 118   assert(obj->is_constantPoolCache(), "obj must be constant pool cache");
 119   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 120   // Performance tweak: We skip iterating over the klass pointer since we
 121   // know that Universe::constantPoolCacheKlassObj never moves.
 122   // gc of constant pool cache instance variables
 123   PSParallelCompact::mark_and_push(cm, (oop*)cache->constant_pool_addr());
 124   // gc of constant pool cache entries
 125   int i = cache->length();
 126   while (i-- > 0) cache->entry_at(i)->follow_contents(cm);
 127 }
 128 #endif // SERIALGC
 129 
 130 
 131 int constantPoolCacheKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
 132   assert(obj->is_constantPoolCache(), "obj must be constant pool cache");
 133   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 134   // Get size before changing pointers.
 135   // Don't call size() or oop_size() since that is a virtual call.
 136   int size = cache->object_size();
 137   // Performance tweak: We skip iterating over the klass pointer since we
 138   // know that Universe::constantPoolCacheKlassObj never moves.
 139   // iteration over constant pool cache instance variables
 140   blk->do_oop((oop*)cache->constant_pool_addr());
 141   // iteration over constant pool cache entries
 142   for (int i = 0; i < cache->length(); i++) cache->entry_at(i)->oop_iterate(blk);
 143   return size;
 144 }
 145 
 146 
 147 int constantPoolCacheKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
 148   assert(obj->is_constantPoolCache(), "obj must be constant pool cache");
 149   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 150   // Get size before changing pointers.
 151   // Don't call size() or oop_size() since that is a virtual call.
 152   int size = cache->object_size();
 153   // Performance tweak: We skip iterating over the klass pointer since we
 154   // know that Universe::constantPoolCacheKlassObj never moves.
 155   // iteration over constant pool cache instance variables
 156   oop* addr = (oop*)cache->constant_pool_addr();
 157   if (mr.contains(addr)) blk->do_oop(addr);
 158   // iteration over constant pool cache entries
 159   for (int i = 0; i < cache->length(); i++) cache->entry_at(i)->oop_iterate_m(blk, mr);
 160   return size;
 161 }
 162 
 163 int constantPoolCacheKlass::oop_adjust_pointers(oop obj) {
 164   assert(obj->is_constantPoolCache(), "obj must be constant pool cache");
 165   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 166   // Get size before changing pointers.
 167   // Don't call size() or oop_size() since that is a virtual call.
 168   int size = cache->object_size();
 169   // Performance tweak: We skip iterating over the klass pointer since we
 170   // know that Universe::constantPoolCacheKlassObj never moves.
 171   // Iteration over constant pool cache instance variables
 172   MarkSweep::adjust_pointer((oop*)cache->constant_pool_addr());
 173   // iteration over constant pool cache entries
 174   for (int i = 0; i < cache->length(); i++)
 175     cache->entry_at(i)->adjust_pointers();
 176   return size;
 177 }
 178 
 179 bool constantPoolCacheKlass::oop_is_conc_safe(oop obj) const {
 180   assert(obj->is_constantPoolCache(), "should be constant pool");
 181   return constantPoolCacheOop(obj)->is_conc_safe();
 182 }
 183 
 184 #ifndef SERIALGC
 185 void constantPoolCacheKlass::oop_push_contents(PSPromotionManager* pm,
 186                                                oop obj) {
 187   assert(obj->is_constantPoolCache(), "should be constant pool");
 188   if (EnableInvokeDynamic) {
 189     constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 190     // during a scavenge, it is safe to inspect my pool, since it is perm
 191     constantPoolOop pool = cache->constant_pool();
 192     assert(pool->is_constantPool(), "should be constant pool");
 193     if (pool->has_invokedynamic()) {
 194       for (int i = 0; i < cache->length(); i++) {
 195         ConstantPoolCacheEntry* e = cache->entry_at(i);
 196         oop* p = (oop*)&e->_f1;
 197         if (e->is_secondary_entry()) {
 198           if (PSScavenge::should_scavenge(p))
 199             pm->claim_or_forward_depth(p);
 200           assert(!(e->is_vfinal() && PSScavenge::should_scavenge((oop*)&e->_f2)),
 201                  "no live oops here");
 202         }
 203       }
 204     }
 205   }
 206 }
 207 
 208 int
 209 constantPoolCacheKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
 210   assert(obj->is_constantPoolCache(), "obj must be constant pool cache");
 211   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 212 
 213   // Iteration over constant pool cache instance variables
 214   PSParallelCompact::adjust_pointer((oop*)cache->constant_pool_addr());
 215 
 216   // iteration over constant pool cache entries
 217   for (int i = 0; i < cache->length(); ++i) {
 218     cache->entry_at(i)->update_pointers();
 219   }
 220 
 221   return cache->object_size();
 222 }
 223 
 224 int
 225 constantPoolCacheKlass::oop_update_pointers(ParCompactionManager* cm, oop obj,
 226                                             HeapWord* beg_addr,
 227                                             HeapWord* end_addr) {
 228   assert(obj->is_constantPoolCache(), "obj must be constant pool cache");
 229   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 230 
 231   // Iteration over constant pool cache instance variables
 232   oop* p;
 233   p = (oop*)cache->constant_pool_addr();
 234   PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
 235 
 236   // Iteration over constant pool cache entries
 237   for (int i = 0; i < cache->length(); ++i) {
 238     cache->entry_at(i)->update_pointers(beg_addr, end_addr);
 239   }
 240   return cache->object_size();
 241 }
 242 #endif // SERIALGC
 243 
 244 void constantPoolCacheKlass::oop_print_on(oop obj, outputStream* st) {
 245   assert(obj->is_constantPoolCache(), "obj must be constant pool cache");
 246   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 247   // super print
 248   Klass::oop_print_on(obj, st);
 249   // print constant pool cache entries
 250   for (int i = 0; i < cache->length(); i++) cache->entry_at(i)->print(st, i);
 251 }
 252 
 253 void constantPoolCacheKlass::oop_print_value_on(oop obj, outputStream* st) {
 254   assert(obj->is_constantPoolCache(), "obj must be constant pool cache");
 255   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 256   st->print("cache [%d]", cache->length());
 257   cache->print_address_on(st);
 258   st->print(" for ");
 259   cache->constant_pool()->print_value_on(st);
 260 }
 261 
 262 void constantPoolCacheKlass::oop_verify_on(oop obj, outputStream* st) {
 263   guarantee(obj->is_constantPoolCache(), "obj must be constant pool cache");
 264   constantPoolCacheOop cache = (constantPoolCacheOop)obj;
 265   // super verify
 266   Klass::oop_verify_on(obj, st);
 267   // print constant pool cache entries
 268   for (int i = 0; i < cache->length(); i++) cache->entry_at(i)->verify(st);
 269 }
 270 
 271 
 272 const char* constantPoolCacheKlass::internal_name() const {
 273   return "{constant pool cache}";
 274 }