hotspot/src/share/vm/oops/constantPool.cpp
changeset 46810 7dad333205cd
parent 46746 ea379ebb9447
child 46816 f64eb2bfb250
equal deleted inserted replaced
46809:057f21a10f5f 46810:7dad333205cd
    31 #include "classfile/vmSymbols.hpp"
    31 #include "classfile/vmSymbols.hpp"
    32 #include "interpreter/linkResolver.hpp"
    32 #include "interpreter/linkResolver.hpp"
    33 #include "memory/heapInspection.hpp"
    33 #include "memory/heapInspection.hpp"
    34 #include "memory/metadataFactory.hpp"
    34 #include "memory/metadataFactory.hpp"
    35 #include "memory/metaspaceClosure.hpp"
    35 #include "memory/metaspaceClosure.hpp"
       
    36 #include "memory/metaspaceShared.hpp"
    36 #include "memory/oopFactory.hpp"
    37 #include "memory/oopFactory.hpp"
    37 #include "memory/resourceArea.hpp"
    38 #include "memory/resourceArea.hpp"
    38 #include "oops/constantPool.hpp"
    39 #include "oops/constantPool.hpp"
    39 #include "oops/instanceKlass.hpp"
    40 #include "oops/instanceKlass.hpp"
    40 #include "oops/objArrayKlass.hpp"
    41 #include "oops/objArrayKlass.hpp"
    45 #include "runtime/init.hpp"
    46 #include "runtime/init.hpp"
    46 #include "runtime/javaCalls.hpp"
    47 #include "runtime/javaCalls.hpp"
    47 #include "runtime/signature.hpp"
    48 #include "runtime/signature.hpp"
    48 #include "runtime/vframe.hpp"
    49 #include "runtime/vframe.hpp"
    49 #include "utilities/copy.hpp"
    50 #include "utilities/copy.hpp"
       
    51 #if INCLUDE_ALL_GCS
       
    52 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
       
    53 #endif // INCLUDE_ALL_GCS
    50 
    54 
    51 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
    55 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
    52   Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
    56   Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
    53   int size = ConstantPool::size(length);
    57   int size = ConstantPool::size(length);
    54   return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
    58   return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
   236   // The interpreter assumes when the tag is stored, the klass is resolved
   240   // The interpreter assumes when the tag is stored, the klass is resolved
   237   // and the Klass* non-NULL, so we need hardware store ordering here.
   241   // and the Klass* non-NULL, so we need hardware store ordering here.
   238   release_tag_at_put(class_index, JVM_CONSTANT_Class);
   242   release_tag_at_put(class_index, JVM_CONSTANT_Class);
   239 }
   243 }
   240 
   244 
       
   245 #if INCLUDE_CDS_JAVA_HEAP
       
   246 // Archive the resolved references
       
   247 void ConstantPool::archive_resolved_references(Thread* THREAD) {
       
   248   if (_cache == NULL) {
       
   249     return; // nothing to do
       
   250   }
       
   251 
       
   252   InstanceKlass *ik = pool_holder();
       
   253   if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
       
   254         ik->is_shared_app_class())) {
       
   255     // Archiving resolved references for classes from non-builtin loaders
       
   256     // is not yet supported.
       
   257     set_resolved_references(NULL);
       
   258     return;
       
   259   }
       
   260 
       
   261   objArrayOop rr = resolved_references();
       
   262   if (rr != NULL) {
       
   263     for (int i = 0; i < rr->length(); i++) {
       
   264       oop p = rr->obj_at(i);
       
   265       if (p != NULL) {
       
   266         int index = object_to_cp_index(i);
       
   267         if (tag_at(index).is_string()) {
       
   268           oop op = StringTable::create_archived_string(p, THREAD);
       
   269           // If the String object is not archived (possibly too large),
       
   270           // NULL is returned. Also set it in the array, so we won't
       
   271           // have a 'bad' reference in the archived resolved_reference
       
   272           // array.
       
   273           rr->obj_at_put(i, op);
       
   274         } else {
       
   275           rr->obj_at_put(i, NULL);
       
   276         }
       
   277       }
       
   278     }
       
   279     oop archived = MetaspaceShared::archive_heap_object(rr, THREAD);
       
   280     _cache->set_archived_references(archived);
       
   281     set_resolved_references(NULL);
       
   282   }
       
   283 }
       
   284 #endif
       
   285 
   241 // CDS support. Create a new resolved_references array.
   286 // CDS support. Create a new resolved_references array.
   242 void ConstantPool::restore_unshareable_info(TRAPS) {
   287 void ConstantPool::restore_unshareable_info(TRAPS) {
   243   assert(is_constantPool(), "ensure C++ vtable is restored");
   288   assert(is_constantPool(), "ensure C++ vtable is restored");
   244   assert(on_stack(), "should always be set for shared constant pools");
   289   assert(on_stack(), "should always be set for shared constant pools");
   245   assert(is_shared(), "should always be set for shared constant pools");
   290   assert(is_shared(), "should always be set for shared constant pools");
       
   291   assert(_cache != NULL, "constant pool _cache should not be NULL");
   246 
   292 
   247   // Only create the new resolved references array if it hasn't been attempted before
   293   // Only create the new resolved references array if it hasn't been attempted before
   248   if (resolved_references() != NULL) return;
   294   if (resolved_references() != NULL) return;
   249 
   295 
   250   // restore the C++ vtable from the shared archive
   296   // restore the C++ vtable from the shared archive
   251   restore_vtable();
   297   restore_vtable();
   252 
   298 
   253   if (SystemDictionary::Object_klass_loaded()) {
   299   if (SystemDictionary::Object_klass_loaded()) {
   254     // Recreate the object array and add to ClassLoaderData.
   300     ClassLoaderData* loader_data = pool_holder()->class_loader_data();
   255     int map_length = resolved_reference_length();
   301 #if INCLUDE_CDS_JAVA_HEAP
   256     if (map_length > 0) {
   302     if (MetaspaceShared::open_archive_heap_region_mapped() &&
   257       objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
   303         _cache->archived_references() != NULL) {
   258       Handle refs_handle (THREAD, (oop)stom);  // must handleize.
   304       oop archived = _cache->archived_references();
   259 
   305       // Make sure GC knows the cached object is now live. This is necessary after
   260       ClassLoaderData* loader_data = pool_holder()->class_loader_data();
   306       // initial GC marking and during concurrent marking as strong roots are only
       
   307       // scanned during initial marking (at the start of the GC marking).
       
   308       assert(UseG1GC, "Requires G1 GC");
       
   309       G1SATBCardTableModRefBS::enqueue(archived);
       
   310       // Create handle for the archived resolved reference array object
       
   311       Handle refs_handle(THREAD, (oop)archived);
   261       set_resolved_references(loader_data->add_handle(refs_handle));
   312       set_resolved_references(loader_data->add_handle(refs_handle));
       
   313     } else
       
   314 #endif
       
   315     {
       
   316       // No mapped archived resolved reference array
       
   317       // Recreate the object array and add to ClassLoaderData.
       
   318       int map_length = resolved_reference_length();
       
   319       if (map_length > 0) {
       
   320         objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
       
   321         Handle refs_handle(THREAD, (oop)stom);  // must handleize.
       
   322         set_resolved_references(loader_data->add_handle(refs_handle));
       
   323       }
   262     }
   324     }
   263   }
   325   }
   264 }
   326 }
   265 
   327 
   266 void ConstantPool::remove_unshareable_info() {
   328 void ConstantPool::remove_unshareable_info() {
   267   // Resolved references are not in the shared archive.
   329   // Resolved references are not in the shared archive.
   268   // Save the length for restoration.  It is not necessarily the same length
   330   // Save the length for restoration.  It is not necessarily the same length
   269   // as reference_map.length() if invokedynamic is saved.
   331   // as reference_map.length() if invokedynamic is saved. It is needed when
       
   332   // re-creating the resolved reference array if archived heap data cannot be map
       
   333   // at runtime.
   270   set_resolved_reference_length(
   334   set_resolved_reference_length(
   271     resolved_references() != NULL ? resolved_references()->length() : 0);
   335     resolved_references() != NULL ? resolved_references()->length() : 0);
   272   set_resolved_references(NULL);
   336 
       
   337   // If archiving heap objects is not allowed, clear the resolved references.
       
   338   // Otherwise, it is cleared after the resolved references array is cached
       
   339   // (see archive_resolved_references()).
       
   340   if (!MetaspaceShared::is_heap_object_archiving_allowed()) {
       
   341     set_resolved_references(NULL);
       
   342   }
   273 
   343 
   274   // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
   344   // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
   275   // The _on_stack flag is used to prevent ConstantPools from deallocation during
   345   // The _on_stack flag is used to prevent ConstantPools from deallocation during
   276   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
   346   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
   277   // we always set _on_stack to true to avoid having to change _flags during runtime.
   347   // we always set _on_stack to true to avoid having to change _flags during runtime.
   617       this_cp->string_at(index, CHECK);
   687       this_cp->string_at(index, CHECK);
   618     }
   688     }
   619   }
   689   }
   620 }
   690 }
   621 
   691 
   622 // Resolve all the classes in the constant pool.  If they are all resolved,
       
   623 // the constant pool is read-only.  Enhancement: allocate cp entries to
       
   624 // another metaspace, and copy to read-only or read-write space if this
       
   625 // bit is set.
       
   626 bool ConstantPool::resolve_class_constants(TRAPS) {
   692 bool ConstantPool::resolve_class_constants(TRAPS) {
   627   constantPoolHandle cp(THREAD, this);
   693   constantPoolHandle cp(THREAD, this);
   628   for (int index = 1; index < length(); index++) { // Index 0 is unused
   694   for (int index = 1; index < length(); index++) { // Index 0 is unused
   629     if (tag_at(index).is_unresolved_klass() &&
   695     if (tag_at(index).is_string()) {
   630         klass_at_if_loaded(cp, index) == NULL) {
   696       Symbol* sym = cp->unresolved_string_at(index);
   631       return false;
   697       // Look up only. Only resolve references to already interned strings.
   632   }
   698       oop str = StringTable::lookup(sym);
   633   }
   699       if (str != NULL) {
   634   // set_preresolution(); or some bit for future use
   700         int cache_index = cp->cp_to_object_index(index);
       
   701         cp->string_at_put(index, cache_index, str);
       
   702       }
       
   703     }
       
   704   }
   635   return true;
   705   return true;
   636 }
   706 }
   637 
   707 
   638 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
   708 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
   639   // Dig out the detailed message to reuse if possible
   709   // Dig out the detailed message to reuse if possible