1 /*
   2  * Copyright (c) 1997, 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/classLoader.hpp"
  27 #include "classfile/javaClasses.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "code/codeCache.hpp"
  32 #include "code/dependencies.hpp"
  33 #include "gc_interface/collectedHeap.inline.hpp"
  34 #include "interpreter/interpreter.hpp"
  35 #include "memory/cardTableModRefBS.hpp"
  36 #include "memory/filemap.hpp"
  37 #include "memory/gcLocker.inline.hpp"
  38 #include "memory/genCollectedHeap.hpp"
  39 #include "memory/genRemSet.hpp"
  40 #include "memory/generation.hpp"
  41 #include "memory/oopFactory.hpp"
  42 #include "memory/permGen.hpp"
  43 #include "memory/space.hpp"
  44 #include "memory/universe.hpp"
  45 #include "memory/universe.inline.hpp"
  46 #include "oops/arrayKlassKlass.hpp"
  47 #include "oops/compiledICHolderKlass.hpp"
  48 #include "oops/constMethodKlass.hpp"
  49 #include "oops/constantPoolKlass.hpp"
  50 #include "oops/constantPoolOop.hpp"
  51 #include "oops/cpCacheKlass.hpp"
  52 #include "oops/cpCacheOop.hpp"
  53 #include "oops/instanceKlass.hpp"
  54 #include "oops/instanceKlassKlass.hpp"
  55 #include "oops/instanceRefKlass.hpp"
  56 #include "oops/klassKlass.hpp"
  57 #include "oops/klassOop.hpp"
  58 #include "oops/methodDataKlass.hpp"
  59 #include "oops/methodKlass.hpp"
  60 #include "oops/objArrayKlassKlass.hpp"
  61 #include "oops/oop.inline.hpp"
  62 #include "oops/symbolKlass.hpp"
  63 #include "oops/typeArrayKlass.hpp"
  64 #include "oops/typeArrayKlassKlass.hpp"
  65 #include "prims/jvmtiRedefineClassesTrace.hpp"
  66 #include "runtime/aprofiler.hpp"
  67 #include "runtime/arguments.hpp"
  68 #include "runtime/deoptimization.hpp"
  69 #include "runtime/fprofiler.hpp"
  70 #include "runtime/handles.inline.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/javaCalls.hpp"
  73 #include "runtime/sharedRuntime.hpp"
  74 #include "runtime/synchronizer.hpp"
  75 #include "runtime/timer.hpp"
  76 #include "runtime/vm_operations.hpp"
  77 #include "services/memoryService.hpp"
  78 #include "utilities/copy.hpp"
  79 #include "utilities/events.hpp"
  80 #include "utilities/hashtable.inline.hpp"
  81 #include "utilities/preserveException.hpp"
  82 #ifdef TARGET_OS_FAMILY_linux
  83 # include "thread_linux.inline.hpp"
  84 #endif
  85 #ifdef TARGET_OS_FAMILY_solaris
  86 # include "thread_solaris.inline.hpp"
  87 #endif
  88 #ifdef TARGET_OS_FAMILY_windows
  89 # include "thread_windows.inline.hpp"
  90 #endif
  91 #ifndef SERIALGC
  92 #include "gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.hpp"
  93 #include "gc_implementation/concurrentMarkSweep/cmsCollectorPolicy.hpp"
  94 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  95 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
  96 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
  97 #endif
  98 
  99 // Known objects
 100 klassOop Universe::_boolArrayKlassObj                 = NULL;
 101 klassOop Universe::_byteArrayKlassObj                 = NULL;
 102 klassOop Universe::_charArrayKlassObj                 = NULL;
 103 klassOop Universe::_intArrayKlassObj                  = NULL;
 104 klassOop Universe::_shortArrayKlassObj                = NULL;
 105 klassOop Universe::_longArrayKlassObj                 = NULL;
 106 klassOop Universe::_singleArrayKlassObj               = NULL;
 107 klassOop Universe::_doubleArrayKlassObj               = NULL;
 108 klassOop Universe::_typeArrayKlassObjs[T_VOID+1]      = { NULL /*, NULL...*/ };
 109 klassOop Universe::_objectArrayKlassObj               = NULL;
 110 klassOop Universe::_symbolKlassObj                    = NULL;
 111 klassOop Universe::_methodKlassObj                    = NULL;
 112 klassOop Universe::_constMethodKlassObj               = NULL;
 113 klassOop Universe::_methodDataKlassObj                = NULL;
 114 klassOop Universe::_klassKlassObj                     = NULL;
 115 klassOop Universe::_arrayKlassKlassObj                = NULL;
 116 klassOop Universe::_objArrayKlassKlassObj             = NULL;
 117 klassOop Universe::_typeArrayKlassKlassObj            = NULL;
 118 klassOop Universe::_instanceKlassKlassObj             = NULL;
 119 klassOop Universe::_constantPoolKlassObj              = NULL;
 120 klassOop Universe::_constantPoolCacheKlassObj         = NULL;
 121 klassOop Universe::_compiledICHolderKlassObj          = NULL;
 122 klassOop Universe::_systemObjArrayKlassObj            = NULL;
 123 oop Universe::_int_mirror                             = NULL;
 124 oop Universe::_float_mirror                           = NULL;
 125 oop Universe::_double_mirror                          = NULL;
 126 oop Universe::_byte_mirror                            = NULL;
 127 oop Universe::_bool_mirror                            = NULL;
 128 oop Universe::_char_mirror                            = NULL;
 129 oop Universe::_long_mirror                            = NULL;
 130 oop Universe::_short_mirror                           = NULL;
 131 oop Universe::_void_mirror                            = NULL;
 132 oop Universe::_mirrors[T_VOID+1]                      = { NULL /*, NULL...*/ };
 133 oop Universe::_main_thread_group                      = NULL;
 134 oop Universe::_system_thread_group                    = NULL;
 135 typeArrayOop Universe::_the_empty_byte_array          = NULL;
 136 typeArrayOop Universe::_the_empty_short_array         = NULL;
 137 typeArrayOop Universe::_the_empty_int_array           = NULL;
 138 objArrayOop Universe::_the_empty_system_obj_array     = NULL;
 139 objArrayOop Universe::_the_empty_class_klass_array    = NULL;
 140 objArrayOop Universe::_the_array_interfaces_array     = NULL;
 141 oop Universe::_the_null_string                        = NULL;
 142 oop Universe::_the_min_jint_string                   = NULL;
 143 LatestMethodOopCache* Universe::_finalizer_register_cache = NULL;
 144 LatestMethodOopCache* Universe::_loader_addClass_cache    = NULL;
 145 ActiveMethodOopsCache* Universe::_reflect_invoke_cache    = NULL;
 146 oop Universe::_out_of_memory_error_java_heap          = NULL;
 147 oop Universe::_out_of_memory_error_perm_gen           = NULL;
 148 oop Universe::_out_of_memory_error_array_size         = NULL;
 149 oop Universe::_out_of_memory_error_gc_overhead_limit  = NULL;
 150 objArrayOop Universe::_preallocated_out_of_memory_error_array = NULL;
 151 volatile jint Universe::_preallocated_out_of_memory_error_avail_count = 0;
 152 bool Universe::_verify_in_progress                    = false;
 153 oop Universe::_null_ptr_exception_instance            = NULL;
 154 oop Universe::_arithmetic_exception_instance          = NULL;
 155 oop Universe::_virtual_machine_error_instance         = NULL;
 156 oop Universe::_vm_exception                           = NULL;
 157 oop Universe::_emptySymbol                            = NULL;
 158 
 159 // These variables are guarded by FullGCALot_lock.
 160 debug_only(objArrayOop Universe::_fullgc_alot_dummy_array = NULL;)
 161 debug_only(int Universe::_fullgc_alot_dummy_next      = 0;)
 162 
 163 
 164 // Heap
 165 int             Universe::_verify_count = 0;
 166 
 167 int             Universe::_base_vtable_size = 0;
 168 bool            Universe::_bootstrapping = false;
 169 bool            Universe::_fully_initialized = false;
 170 
 171 size_t          Universe::_heap_capacity_at_last_gc;
 172 size_t          Universe::_heap_used_at_last_gc = 0;
 173 
 174 CollectedHeap*  Universe::_collectedHeap = NULL;
 175 
 176 NarrowOopStruct Universe::_narrow_oop = { NULL, 0, true };
 177 
 178 
 179 void Universe::basic_type_classes_do(void f(klassOop)) {
 180   f(boolArrayKlassObj());
 181   f(byteArrayKlassObj());
 182   f(charArrayKlassObj());
 183   f(intArrayKlassObj());
 184   f(shortArrayKlassObj());
 185   f(longArrayKlassObj());
 186   f(singleArrayKlassObj());
 187   f(doubleArrayKlassObj());
 188 }
 189 
 190 
 191 void Universe::system_classes_do(void f(klassOop)) {
 192   f(symbolKlassObj());
 193   f(methodKlassObj());
 194   f(constMethodKlassObj());
 195   f(methodDataKlassObj());
 196   f(klassKlassObj());
 197   f(arrayKlassKlassObj());
 198   f(objArrayKlassKlassObj());
 199   f(typeArrayKlassKlassObj());
 200   f(instanceKlassKlassObj());
 201   f(constantPoolKlassObj());
 202   f(systemObjArrayKlassObj());
 203 }
 204 
 205 void Universe::oops_do(OopClosure* f, bool do_all) {
 206 
 207   f->do_oop((oop*) &_int_mirror);
 208   f->do_oop((oop*) &_float_mirror);
 209   f->do_oop((oop*) &_double_mirror);
 210   f->do_oop((oop*) &_byte_mirror);
 211   f->do_oop((oop*) &_bool_mirror);
 212   f->do_oop((oop*) &_char_mirror);
 213   f->do_oop((oop*) &_long_mirror);
 214   f->do_oop((oop*) &_short_mirror);
 215   f->do_oop((oop*) &_void_mirror);
 216 
 217   // It's important to iterate over these guys even if they are null,
 218   // since that's how shared heaps are restored.
 219   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
 220     f->do_oop((oop*) &_mirrors[i]);
 221   }
 222   assert(_mirrors[0] == NULL && _mirrors[T_BOOLEAN - 1] == NULL, "checking");
 223 
 224   // %%% Consider moving those "shared oops" over here with the others.
 225   f->do_oop((oop*)&_boolArrayKlassObj);
 226   f->do_oop((oop*)&_byteArrayKlassObj);
 227   f->do_oop((oop*)&_charArrayKlassObj);
 228   f->do_oop((oop*)&_intArrayKlassObj);
 229   f->do_oop((oop*)&_shortArrayKlassObj);
 230   f->do_oop((oop*)&_longArrayKlassObj);
 231   f->do_oop((oop*)&_singleArrayKlassObj);
 232   f->do_oop((oop*)&_doubleArrayKlassObj);
 233   f->do_oop((oop*)&_objectArrayKlassObj);
 234   {
 235     for (int i = 0; i < T_VOID+1; i++) {
 236       if (_typeArrayKlassObjs[i] != NULL) {
 237         assert(i >= T_BOOLEAN, "checking");
 238         f->do_oop((oop*)&_typeArrayKlassObjs[i]);
 239       } else if (do_all) {
 240         f->do_oop((oop*)&_typeArrayKlassObjs[i]);
 241       }
 242     }
 243   }
 244   f->do_oop((oop*)&_symbolKlassObj);
 245   f->do_oop((oop*)&_methodKlassObj);
 246   f->do_oop((oop*)&_constMethodKlassObj);
 247   f->do_oop((oop*)&_methodDataKlassObj);
 248   f->do_oop((oop*)&_klassKlassObj);
 249   f->do_oop((oop*)&_arrayKlassKlassObj);
 250   f->do_oop((oop*)&_objArrayKlassKlassObj);
 251   f->do_oop((oop*)&_typeArrayKlassKlassObj);
 252   f->do_oop((oop*)&_instanceKlassKlassObj);
 253   f->do_oop((oop*)&_constantPoolKlassObj);
 254   f->do_oop((oop*)&_constantPoolCacheKlassObj);
 255   f->do_oop((oop*)&_compiledICHolderKlassObj);
 256   f->do_oop((oop*)&_systemObjArrayKlassObj);
 257   f->do_oop((oop*)&_the_empty_byte_array);
 258   f->do_oop((oop*)&_the_empty_short_array);
 259   f->do_oop((oop*)&_the_empty_int_array);
 260   f->do_oop((oop*)&_the_empty_system_obj_array);
 261   f->do_oop((oop*)&_the_empty_class_klass_array);
 262   f->do_oop((oop*)&_the_array_interfaces_array);
 263   f->do_oop((oop*)&_the_null_string);
 264   f->do_oop((oop*)&_the_min_jint_string);
 265   _finalizer_register_cache->oops_do(f);
 266   _loader_addClass_cache->oops_do(f);
 267   _reflect_invoke_cache->oops_do(f);
 268   f->do_oop((oop*)&_out_of_memory_error_java_heap);
 269   f->do_oop((oop*)&_out_of_memory_error_perm_gen);
 270   f->do_oop((oop*)&_out_of_memory_error_array_size);
 271   f->do_oop((oop*)&_out_of_memory_error_gc_overhead_limit);
 272   if (_preallocated_out_of_memory_error_array != (oop)NULL) {   // NULL when DumpSharedSpaces
 273     f->do_oop((oop*)&_preallocated_out_of_memory_error_array);
 274   }
 275   f->do_oop((oop*)&_null_ptr_exception_instance);
 276   f->do_oop((oop*)&_arithmetic_exception_instance);
 277   f->do_oop((oop*)&_virtual_machine_error_instance);
 278   f->do_oop((oop*)&_main_thread_group);
 279   f->do_oop((oop*)&_system_thread_group);
 280   f->do_oop((oop*)&_vm_exception);
 281   f->do_oop((oop*)&_emptySymbol);
 282   debug_only(f->do_oop((oop*)&_fullgc_alot_dummy_array);)
 283 }
 284 
 285 
 286 void Universe::check_alignment(uintx size, uintx alignment, const char* name) {
 287   if (size < alignment || size % alignment != 0) {
 288     ResourceMark rm;
 289     stringStream st;
 290     st.print("Size of %s (%ld bytes) must be aligned to %ld bytes", name, size, alignment);
 291     char* error = st.as_string();
 292     vm_exit_during_initialization(error);
 293   }
 294 }
 295 
 296 
 297 void Universe::genesis(TRAPS) {
 298   ResourceMark rm;
 299   { FlagSetting fs(_bootstrapping, true);
 300 
 301     { MutexLocker mc(Compile_lock);
 302 
 303       // determine base vtable size; without that we cannot create the array klasses
 304       compute_base_vtable_size();
 305 
 306       if (!UseSharedSpaces) {
 307         _klassKlassObj          = klassKlass::create_klass(CHECK);
 308         _arrayKlassKlassObj     = arrayKlassKlass::create_klass(CHECK);
 309 
 310         _objArrayKlassKlassObj  = objArrayKlassKlass::create_klass(CHECK);
 311         _instanceKlassKlassObj  = instanceKlassKlass::create_klass(CHECK);
 312         _typeArrayKlassKlassObj = typeArrayKlassKlass::create_klass(CHECK);
 313 
 314         _symbolKlassObj         = symbolKlass::create_klass(CHECK);
 315 
 316         _emptySymbol            = oopFactory::new_symbol("", CHECK);
 317 
 318         _boolArrayKlassObj      = typeArrayKlass::create_klass(T_BOOLEAN, sizeof(jboolean), CHECK);
 319         _charArrayKlassObj      = typeArrayKlass::create_klass(T_CHAR,    sizeof(jchar),    CHECK);
 320         _singleArrayKlassObj    = typeArrayKlass::create_klass(T_FLOAT,   sizeof(jfloat),   CHECK);
 321         _doubleArrayKlassObj    = typeArrayKlass::create_klass(T_DOUBLE,  sizeof(jdouble),  CHECK);
 322         _byteArrayKlassObj      = typeArrayKlass::create_klass(T_BYTE,    sizeof(jbyte),    CHECK);
 323         _shortArrayKlassObj     = typeArrayKlass::create_klass(T_SHORT,   sizeof(jshort),   CHECK);
 324         _intArrayKlassObj       = typeArrayKlass::create_klass(T_INT,     sizeof(jint),     CHECK);
 325         _longArrayKlassObj      = typeArrayKlass::create_klass(T_LONG,    sizeof(jlong),    CHECK);
 326 
 327         _typeArrayKlassObjs[T_BOOLEAN] = _boolArrayKlassObj;
 328         _typeArrayKlassObjs[T_CHAR]    = _charArrayKlassObj;
 329         _typeArrayKlassObjs[T_FLOAT]   = _singleArrayKlassObj;
 330         _typeArrayKlassObjs[T_DOUBLE]  = _doubleArrayKlassObj;
 331         _typeArrayKlassObjs[T_BYTE]    = _byteArrayKlassObj;
 332         _typeArrayKlassObjs[T_SHORT]   = _shortArrayKlassObj;
 333         _typeArrayKlassObjs[T_INT]     = _intArrayKlassObj;
 334         _typeArrayKlassObjs[T_LONG]    = _longArrayKlassObj;
 335 
 336         _methodKlassObj             = methodKlass::create_klass(CHECK);
 337         _constMethodKlassObj        = constMethodKlass::create_klass(CHECK);
 338         _methodDataKlassObj         = methodDataKlass::create_klass(CHECK);
 339         _constantPoolKlassObj       = constantPoolKlass::create_klass(CHECK);
 340         _constantPoolCacheKlassObj  = constantPoolCacheKlass::create_klass(CHECK);
 341 
 342         _compiledICHolderKlassObj   = compiledICHolderKlass::create_klass(CHECK);
 343         _systemObjArrayKlassObj     = objArrayKlassKlass::cast(objArrayKlassKlassObj())->allocate_system_objArray_klass(CHECK);
 344 
 345         _the_empty_byte_array       = oopFactory::new_permanent_byteArray(0, CHECK);
 346         _the_empty_short_array      = oopFactory::new_permanent_shortArray(0, CHECK);
 347         _the_empty_int_array        = oopFactory::new_permanent_intArray(0, CHECK);
 348         _the_empty_system_obj_array = oopFactory::new_system_objArray(0, CHECK);
 349 
 350         _the_array_interfaces_array = oopFactory::new_system_objArray(2, CHECK);
 351         _vm_exception               = oopFactory::new_symbol("vm exception holder", CHECK);
 352       } else {
 353         FileMapInfo *mapinfo = FileMapInfo::current_info();
 354         char* buffer = mapinfo->region_base(CompactingPermGenGen::md);
 355         void** vtbl_list = (void**)buffer;
 356         init_self_patching_vtbl_list(vtbl_list,
 357                                      CompactingPermGenGen::vtbl_list_size);
 358       }
 359     }
 360 
 361     vmSymbols::initialize(CHECK);
 362 
 363     SystemDictionary::initialize(CHECK);
 364 
 365     klassOop ok = SystemDictionary::Object_klass();
 366 
 367     _the_null_string            = StringTable::intern("null", CHECK);
 368     _the_min_jint_string       = StringTable::intern("-2147483648", CHECK);
 369 
 370     if (UseSharedSpaces) {
 371       // Verify shared interfaces array.
 372       assert(_the_array_interfaces_array->obj_at(0) ==
 373              SystemDictionary::Cloneable_klass(), "u3");
 374       assert(_the_array_interfaces_array->obj_at(1) ==
 375              SystemDictionary::Serializable_klass(), "u3");
 376 
 377       // Verify element klass for system obj array klass
 378       assert(objArrayKlass::cast(_systemObjArrayKlassObj)->element_klass() == ok, "u1");
 379       assert(objArrayKlass::cast(_systemObjArrayKlassObj)->bottom_klass() == ok, "u2");
 380 
 381       // Verify super class for the classes created above
 382       assert(Klass::cast(boolArrayKlassObj()     )->super() == ok, "u3");
 383       assert(Klass::cast(charArrayKlassObj()     )->super() == ok, "u3");
 384       assert(Klass::cast(singleArrayKlassObj()   )->super() == ok, "u3");
 385       assert(Klass::cast(doubleArrayKlassObj()   )->super() == ok, "u3");
 386       assert(Klass::cast(byteArrayKlassObj()     )->super() == ok, "u3");
 387       assert(Klass::cast(shortArrayKlassObj()    )->super() == ok, "u3");
 388       assert(Klass::cast(intArrayKlassObj()      )->super() == ok, "u3");
 389       assert(Klass::cast(longArrayKlassObj()     )->super() == ok, "u3");
 390       assert(Klass::cast(constantPoolKlassObj()  )->super() == ok, "u3");
 391       assert(Klass::cast(systemObjArrayKlassObj())->super() == ok, "u3");
 392     } else {
 393       // Set up shared interfaces array.  (Do this before supers are set up.)
 394       _the_array_interfaces_array->obj_at_put(0, SystemDictionary::Cloneable_klass());
 395       _the_array_interfaces_array->obj_at_put(1, SystemDictionary::Serializable_klass());
 396 
 397       // Set element klass for system obj array klass
 398       objArrayKlass::cast(_systemObjArrayKlassObj)->set_element_klass(ok);
 399       objArrayKlass::cast(_systemObjArrayKlassObj)->set_bottom_klass(ok);
 400 
 401       // Set super class for the classes created above
 402       Klass::cast(boolArrayKlassObj()     )->initialize_supers(ok, CHECK);
 403       Klass::cast(charArrayKlassObj()     )->initialize_supers(ok, CHECK);
 404       Klass::cast(singleArrayKlassObj()   )->initialize_supers(ok, CHECK);
 405       Klass::cast(doubleArrayKlassObj()   )->initialize_supers(ok, CHECK);
 406       Klass::cast(byteArrayKlassObj()     )->initialize_supers(ok, CHECK);
 407       Klass::cast(shortArrayKlassObj()    )->initialize_supers(ok, CHECK);
 408       Klass::cast(intArrayKlassObj()      )->initialize_supers(ok, CHECK);
 409       Klass::cast(longArrayKlassObj()     )->initialize_supers(ok, CHECK);
 410       Klass::cast(constantPoolKlassObj()  )->initialize_supers(ok, CHECK);
 411       Klass::cast(systemObjArrayKlassObj())->initialize_supers(ok, CHECK);
 412       Klass::cast(boolArrayKlassObj()     )->set_super(ok);
 413       Klass::cast(charArrayKlassObj()     )->set_super(ok);
 414       Klass::cast(singleArrayKlassObj()   )->set_super(ok);
 415       Klass::cast(doubleArrayKlassObj()   )->set_super(ok);
 416       Klass::cast(byteArrayKlassObj()     )->set_super(ok);
 417       Klass::cast(shortArrayKlassObj()    )->set_super(ok);
 418       Klass::cast(intArrayKlassObj()      )->set_super(ok);
 419       Klass::cast(longArrayKlassObj()     )->set_super(ok);
 420       Klass::cast(constantPoolKlassObj()  )->set_super(ok);
 421       Klass::cast(systemObjArrayKlassObj())->set_super(ok);
 422     }
 423 
 424     Klass::cast(boolArrayKlassObj()     )->append_to_sibling_list();
 425     Klass::cast(charArrayKlassObj()     )->append_to_sibling_list();
 426     Klass::cast(singleArrayKlassObj()   )->append_to_sibling_list();
 427     Klass::cast(doubleArrayKlassObj()   )->append_to_sibling_list();
 428     Klass::cast(byteArrayKlassObj()     )->append_to_sibling_list();
 429     Klass::cast(shortArrayKlassObj()    )->append_to_sibling_list();
 430     Klass::cast(intArrayKlassObj()      )->append_to_sibling_list();
 431     Klass::cast(longArrayKlassObj()     )->append_to_sibling_list();
 432     Klass::cast(constantPoolKlassObj()  )->append_to_sibling_list();
 433     Klass::cast(systemObjArrayKlassObj())->append_to_sibling_list();
 434   } // end of core bootstrapping
 435 
 436   // Initialize _objectArrayKlass after core bootstraping to make
 437   // sure the super class is set up properly for _objectArrayKlass.
 438   _objectArrayKlassObj = instanceKlass::
 439     cast(SystemDictionary::Object_klass())->array_klass(1, CHECK);
 440   // Add the class to the class hierarchy manually to make sure that
 441   // its vtable is initialized after core bootstrapping is completed.
 442   Klass::cast(_objectArrayKlassObj)->append_to_sibling_list();
 443 
 444   // Compute is_jdk version flags.
 445   // Only 1.3 or later has the java.lang.Shutdown class.
 446   // Only 1.4 or later has the java.lang.CharSequence interface.
 447   // Only 1.5 or later has the java.lang.management.MemoryUsage class.
 448   if (JDK_Version::is_partially_initialized()) {
 449     uint8_t jdk_version;
 450     klassOop k = SystemDictionary::resolve_or_null(
 451         vmSymbolHandles::java_lang_management_MemoryUsage(), THREAD);
 452     CLEAR_PENDING_EXCEPTION; // ignore exceptions
 453     if (k == NULL) {
 454       k = SystemDictionary::resolve_or_null(
 455           vmSymbolHandles::java_lang_CharSequence(), THREAD);
 456       CLEAR_PENDING_EXCEPTION; // ignore exceptions
 457       if (k == NULL) {
 458         k = SystemDictionary::resolve_or_null(
 459             vmSymbolHandles::java_lang_Shutdown(), THREAD);
 460         CLEAR_PENDING_EXCEPTION; // ignore exceptions
 461         if (k == NULL) {
 462           jdk_version = 2;
 463         } else {
 464           jdk_version = 3;
 465         }
 466       } else {
 467         jdk_version = 4;
 468       }
 469     } else {
 470       jdk_version = 5;
 471     }
 472     JDK_Version::fully_initialize(jdk_version);
 473   }
 474 
 475   #ifdef ASSERT
 476   if (FullGCALot) {
 477     // Allocate an array of dummy objects.
 478     // We'd like these to be at the bottom of the old generation,
 479     // so that when we free one and then collect,
 480     // (almost) the whole heap moves
 481     // and we find out if we actually update all the oops correctly.
 482     // But we can't allocate directly in the old generation,
 483     // so we allocate wherever, and hope that the first collection
 484     // moves these objects to the bottom of the old generation.
 485     // We can allocate directly in the permanent generation, so we do.
 486     int size;
 487     if (UseConcMarkSweepGC) {
 488       warning("Using +FullGCALot with concurrent mark sweep gc "
 489               "will not force all objects to relocate");
 490       size = FullGCALotDummies;
 491     } else {
 492       size = FullGCALotDummies * 2;
 493     }
 494     objArrayOop    naked_array = oopFactory::new_system_objArray(size, CHECK);
 495     objArrayHandle dummy_array(THREAD, naked_array);
 496     int i = 0;
 497     while (i < size) {
 498       if (!UseConcMarkSweepGC) {
 499         // Allocate dummy in old generation
 500         oop dummy = instanceKlass::cast(SystemDictionary::Object_klass())->allocate_instance(CHECK);
 501         dummy_array->obj_at_put(i++, dummy);
 502       }
 503       // Allocate dummy in permanent generation
 504       oop dummy = instanceKlass::cast(SystemDictionary::Object_klass())->allocate_permanent_instance(CHECK);
 505       dummy_array->obj_at_put(i++, dummy);
 506     }
 507     {
 508       // Only modify the global variable inside the mutex.
 509       // If we had a race to here, the other dummy_array instances
 510       // and their elements just get dropped on the floor, which is fine.
 511       MutexLocker ml(FullGCALot_lock);
 512       if (_fullgc_alot_dummy_array == NULL) {
 513         _fullgc_alot_dummy_array = dummy_array();
 514       }
 515     }
 516     assert(i == _fullgc_alot_dummy_array->length(), "just checking");
 517   }
 518   #endif
 519 }
 520 
 521 
 522 static inline void add_vtable(void** list, int* n, Klass* o, int count) {
 523   list[(*n)++] = *(void**)&o->vtbl_value();
 524   guarantee((*n) <= count, "vtable list too small.");
 525 }
 526 
 527 
 528 void Universe::init_self_patching_vtbl_list(void** list, int count) {
 529   int n = 0;
 530   { klassKlass o;             add_vtable(list, &n, &o, count); }
 531   { arrayKlassKlass o;        add_vtable(list, &n, &o, count); }
 532   { objArrayKlassKlass o;     add_vtable(list, &n, &o, count); }
 533   { instanceKlassKlass o;     add_vtable(list, &n, &o, count); }
 534   { instanceKlass o;          add_vtable(list, &n, &o, count); }
 535   { instanceRefKlass o;       add_vtable(list, &n, &o, count); }
 536   { typeArrayKlassKlass o;    add_vtable(list, &n, &o, count); }
 537   { symbolKlass o;            add_vtable(list, &n, &o, count); }
 538   { typeArrayKlass o;         add_vtable(list, &n, &o, count); }
 539   { methodKlass o;            add_vtable(list, &n, &o, count); }
 540   { constMethodKlass o;       add_vtable(list, &n, &o, count); }
 541   { constantPoolKlass o;      add_vtable(list, &n, &o, count); }
 542   { constantPoolCacheKlass o; add_vtable(list, &n, &o, count); }
 543   { objArrayKlass o;          add_vtable(list, &n, &o, count); }
 544   { methodDataKlass o;        add_vtable(list, &n, &o, count); }
 545   { compiledICHolderKlass o;  add_vtable(list, &n, &o, count); }
 546 }
 547 
 548 
 549 class FixupMirrorClosure: public ObjectClosure {
 550  public:
 551   virtual void do_object(oop obj) {
 552     if (obj->is_klass()) {
 553       EXCEPTION_MARK;
 554       KlassHandle k(THREAD, klassOop(obj));
 555       // We will never reach the CATCH below since Exceptions::_throw will cause
 556       // the VM to exit if an exception is thrown during initialization
 557       java_lang_Class::create_mirror(k, CATCH);
 558       // This call unconditionally creates a new mirror for k,
 559       // and links in k's component_mirror field if k is an array.
 560       // If k is an objArray, k's element type must already have
 561       // a mirror.  In other words, this closure must process
 562       // the component type of an objArray k before it processes k.
 563       // This works because the permgen iterator presents arrays
 564       // and their component types in order of creation.
 565     }
 566   }
 567 };
 568 
 569 void Universe::initialize_basic_type_mirrors(TRAPS) {
 570   if (UseSharedSpaces) {
 571     assert(_int_mirror != NULL, "already loaded");
 572     assert(_void_mirror == _mirrors[T_VOID], "consistently loaded");
 573   } else {
 574 
 575     assert(_int_mirror==NULL, "basic type mirrors already initialized");
 576     _int_mirror     =
 577       java_lang_Class::create_basic_type_mirror("int",    T_INT, CHECK);
 578     _float_mirror   =
 579       java_lang_Class::create_basic_type_mirror("float",  T_FLOAT,   CHECK);
 580     _double_mirror  =
 581       java_lang_Class::create_basic_type_mirror("double", T_DOUBLE,  CHECK);
 582     _byte_mirror    =
 583       java_lang_Class::create_basic_type_mirror("byte",   T_BYTE, CHECK);
 584     _bool_mirror    =
 585       java_lang_Class::create_basic_type_mirror("boolean",T_BOOLEAN, CHECK);
 586     _char_mirror    =
 587       java_lang_Class::create_basic_type_mirror("char",   T_CHAR, CHECK);
 588     _long_mirror    =
 589       java_lang_Class::create_basic_type_mirror("long",   T_LONG, CHECK);
 590     _short_mirror   =
 591       java_lang_Class::create_basic_type_mirror("short",  T_SHORT,   CHECK);
 592     _void_mirror    =
 593       java_lang_Class::create_basic_type_mirror("void",   T_VOID, CHECK);
 594 
 595     _mirrors[T_INT]     = _int_mirror;
 596     _mirrors[T_FLOAT]   = _float_mirror;
 597     _mirrors[T_DOUBLE]  = _double_mirror;
 598     _mirrors[T_BYTE]    = _byte_mirror;
 599     _mirrors[T_BOOLEAN] = _bool_mirror;
 600     _mirrors[T_CHAR]    = _char_mirror;
 601     _mirrors[T_LONG]    = _long_mirror;
 602     _mirrors[T_SHORT]   = _short_mirror;
 603     _mirrors[T_VOID]    = _void_mirror;
 604     //_mirrors[T_OBJECT]  = instanceKlass::cast(_object_klass)->java_mirror();
 605     //_mirrors[T_ARRAY]   = instanceKlass::cast(_object_klass)->java_mirror();
 606   }
 607 }
 608 
 609 void Universe::fixup_mirrors(TRAPS) {
 610   // Bootstrap problem: all classes gets a mirror (java.lang.Class instance) assigned eagerly,
 611   // but we cannot do that for classes created before java.lang.Class is loaded. Here we simply
 612   // walk over permanent objects created so far (mostly classes) and fixup their mirrors. Note
 613   // that the number of objects allocated at this point is very small.
 614   assert(SystemDictionary::Class_klass_loaded(), "java.lang.Class should be loaded");
 615   FixupMirrorClosure blk;
 616   Universe::heap()->permanent_object_iterate(&blk);
 617 }
 618 
 619 
 620 static bool has_run_finalizers_on_exit = false;
 621 
 622 void Universe::run_finalizers_on_exit() {
 623   if (has_run_finalizers_on_exit) return;
 624   has_run_finalizers_on_exit = true;
 625 
 626   // Called on VM exit. This ought to be run in a separate thread.
 627   if (TraceReferenceGC) tty->print_cr("Callback to run finalizers on exit");
 628   {
 629     PRESERVE_EXCEPTION_MARK;
 630     KlassHandle finalizer_klass(THREAD, SystemDictionary::Finalizer_klass());
 631     JavaValue result(T_VOID);
 632     JavaCalls::call_static(
 633       &result,
 634       finalizer_klass,
 635       vmSymbolHandles::run_finalizers_on_exit_name(),
 636       vmSymbolHandles::void_method_signature(),
 637       THREAD
 638     );
 639     // Ignore any pending exceptions
 640     CLEAR_PENDING_EXCEPTION;
 641   }
 642 }
 643 
 644 
 645 // initialize_vtable could cause gc if
 646 // 1) we specified true to initialize_vtable and
 647 // 2) this ran after gc was enabled
 648 // In case those ever change we use handles for oops
 649 void Universe::reinitialize_vtable_of(KlassHandle k_h, TRAPS) {
 650   // init vtable of k and all subclasses
 651   Klass* ko = k_h()->klass_part();
 652   klassVtable* vt = ko->vtable();
 653   if (vt) vt->initialize_vtable(false, CHECK);
 654   if (ko->oop_is_instance()) {
 655     instanceKlass* ik = (instanceKlass*)ko;
 656     for (KlassHandle s_h(THREAD, ik->subklass()); s_h() != NULL; s_h = (THREAD, s_h()->klass_part()->next_sibling())) {
 657       reinitialize_vtable_of(s_h, CHECK);
 658     }
 659   }
 660 }
 661 
 662 
 663 void initialize_itable_for_klass(klassOop k, TRAPS) {
 664   instanceKlass::cast(k)->itable()->initialize_itable(false, CHECK);
 665 }
 666 
 667 
 668 void Universe::reinitialize_itables(TRAPS) {
 669   SystemDictionary::classes_do(initialize_itable_for_klass, CHECK);
 670 
 671 }
 672 
 673 
 674 bool Universe::on_page_boundary(void* addr) {
 675   return ((uintptr_t) addr) % os::vm_page_size() == 0;
 676 }
 677 
 678 
 679 bool Universe::should_fill_in_stack_trace(Handle throwable) {
 680   // never attempt to fill in the stack trace of preallocated errors that do not have
 681   // backtrace. These errors are kept alive forever and may be "re-used" when all
 682   // preallocated errors with backtrace have been consumed. Also need to avoid
 683   // a potential loop which could happen if an out of memory occurs when attempting
 684   // to allocate the backtrace.
 685   return ((throwable() != Universe::_out_of_memory_error_java_heap) &&
 686           (throwable() != Universe::_out_of_memory_error_perm_gen)  &&
 687           (throwable() != Universe::_out_of_memory_error_array_size) &&
 688           (throwable() != Universe::_out_of_memory_error_gc_overhead_limit));
 689 }
 690 
 691 
 692 oop Universe::gen_out_of_memory_error(oop default_err) {
 693   // generate an out of memory error:
 694   // - if there is a preallocated error with backtrace available then return it wth
 695   //   a filled in stack trace.
 696   // - if there are no preallocated errors with backtrace available then return
 697   //   an error without backtrace.
 698   int next;
 699   if (_preallocated_out_of_memory_error_avail_count > 0) {
 700     next = (int)Atomic::add(-1, &_preallocated_out_of_memory_error_avail_count);
 701     assert(next < (int)PreallocatedOutOfMemoryErrorCount, "avail count is corrupt");
 702   } else {
 703     next = -1;
 704   }
 705   if (next < 0) {
 706     // all preallocated errors have been used.
 707     // return default
 708     return default_err;
 709   } else {
 710     // get the error object at the slot and set set it to NULL so that the
 711     // array isn't keeping it alive anymore.
 712     oop exc = preallocated_out_of_memory_errors()->obj_at(next);
 713     assert(exc != NULL, "slot has been used already");
 714     preallocated_out_of_memory_errors()->obj_at_put(next, NULL);
 715 
 716     // use the message from the default error
 717     oop msg = java_lang_Throwable::message(default_err);
 718     assert(msg != NULL, "no message");
 719     java_lang_Throwable::set_message(exc, msg);
 720 
 721     // populate the stack trace and return it.
 722     java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(exc);
 723     return exc;
 724   }
 725 }
 726 
 727 static intptr_t non_oop_bits = 0;
 728 
 729 void* Universe::non_oop_word() {
 730   // Neither the high bits nor the low bits of this value is allowed
 731   // to look like (respectively) the high or low bits of a real oop.
 732   //
 733   // High and low are CPU-specific notions, but low always includes
 734   // the low-order bit.  Since oops are always aligned at least mod 4,
 735   // setting the low-order bit will ensure that the low half of the
 736   // word will never look like that of a real oop.
 737   //
 738   // Using the OS-supplied non-memory-address word (usually 0 or -1)
 739   // will take care of the high bits, however many there are.
 740 
 741   if (non_oop_bits == 0) {
 742     non_oop_bits = (intptr_t)os::non_memory_address_word() | 1;
 743   }
 744 
 745   return (void*)non_oop_bits;
 746 }
 747 
 748 jint universe_init() {
 749   assert(!Universe::_fully_initialized, "called after initialize_vtables");
 750   guarantee(1 << LogHeapWordSize == sizeof(HeapWord),
 751          "LogHeapWordSize is incorrect.");
 752   guarantee(sizeof(oop) >= sizeof(HeapWord), "HeapWord larger than oop?");
 753   guarantee(sizeof(oop) % sizeof(HeapWord) == 0,
 754             "oop size is not not a multiple of HeapWord size");
 755   TraceTime timer("Genesis", TraceStartupTime);
 756   GC_locker::lock();  // do not allow gc during bootstrapping
 757   JavaClasses::compute_hard_coded_offsets();
 758 
 759   // Get map info from shared archive file.
 760   if (DumpSharedSpaces)
 761     UseSharedSpaces = false;
 762 
 763   FileMapInfo* mapinfo = NULL;
 764   if (UseSharedSpaces) {
 765     mapinfo = NEW_C_HEAP_OBJ(FileMapInfo);
 766     memset(mapinfo, 0, sizeof(FileMapInfo));
 767 
 768     // Open the shared archive file, read and validate the header. If
 769     // initialization files, shared spaces [UseSharedSpaces] are
 770     // disabled and the file is closed.
 771 
 772     if (mapinfo->initialize()) {
 773       FileMapInfo::set_current_info(mapinfo);
 774     } else {
 775       assert(!mapinfo->is_open() && !UseSharedSpaces,
 776              "archive file not closed or shared spaces not disabled.");
 777     }
 778   }
 779 
 780   jint status = Universe::initialize_heap();
 781   if (status != JNI_OK) {
 782     return status;
 783   }
 784 
 785   // We have a heap so create the methodOop caches before
 786   // CompactingPermGenGen::initialize_oops() tries to populate them.
 787   Universe::_finalizer_register_cache = new LatestMethodOopCache();
 788   Universe::_loader_addClass_cache    = new LatestMethodOopCache();
 789   Universe::_reflect_invoke_cache     = new ActiveMethodOopsCache();
 790 
 791   if (UseSharedSpaces) {
 792 
 793     // Read the data structures supporting the shared spaces (shared
 794     // system dictionary, symbol table, etc.).  After that, access to
 795     // the file (other than the mapped regions) is no longer needed, and
 796     // the file is closed. Closing the file does not affect the
 797     // currently mapped regions.
 798 
 799     CompactingPermGenGen::initialize_oops();
 800     mapinfo->close();
 801 
 802   } else {
 803     SymbolTable::create_table();
 804     StringTable::create_table();
 805     ClassLoader::create_package_info_table();
 806   }
 807 
 808   return JNI_OK;
 809 }
 810 
 811 // Choose the heap base address and oop encoding mode
 812 // when compressed oops are used:
 813 // Unscaled  - Use 32-bits oops without encoding when
 814 //     NarrowOopHeapBaseMin + heap_size < 4Gb
 815 // ZeroBased - Use zero based compressed oops with encoding when
 816 //     NarrowOopHeapBaseMin + heap_size < 32Gb
 817 // HeapBased - Use compressed oops with heap base + encoding.
 818 
 819 // 4Gb
 820 static const uint64_t NarrowOopHeapMax = (uint64_t(max_juint) + 1);
 821 // 32Gb
 822 // OopEncodingHeapMax == NarrowOopHeapMax << LogMinObjAlignmentInBytes;
 823 
 824 char* Universe::preferred_heap_base(size_t heap_size, NARROW_OOP_MODE mode) {
 825   size_t base = 0;
 826 #ifdef _LP64
 827   if (UseCompressedOops) {
 828     assert(mode == UnscaledNarrowOop  ||
 829            mode == ZeroBasedNarrowOop ||
 830            mode == HeapBasedNarrowOop, "mode is invalid");
 831     const size_t total_size = heap_size + HeapBaseMinAddress;
 832     // Return specified base for the first request.
 833     if (!FLAG_IS_DEFAULT(HeapBaseMinAddress) && (mode == UnscaledNarrowOop)) {
 834       base = HeapBaseMinAddress;
 835     } else if (total_size <= OopEncodingHeapMax && (mode != HeapBasedNarrowOop)) {
 836       if (total_size <= NarrowOopHeapMax && (mode == UnscaledNarrowOop) &&
 837           (Universe::narrow_oop_shift() == 0)) {
 838         // Use 32-bits oops without encoding and
 839         // place heap's top on the 4Gb boundary
 840         base = (NarrowOopHeapMax - heap_size);
 841       } else {
 842         // Can't reserve with NarrowOopShift == 0
 843         Universe::set_narrow_oop_shift(LogMinObjAlignmentInBytes);
 844         if (mode == UnscaledNarrowOop ||
 845             mode == ZeroBasedNarrowOop && total_size <= NarrowOopHeapMax) {
 846           // Use zero based compressed oops with encoding and
 847           // place heap's top on the 32Gb boundary in case
 848           // total_size > 4Gb or failed to reserve below 4Gb.
 849           base = (OopEncodingHeapMax - heap_size);
 850         }
 851       }
 852     } else {
 853       // Can't reserve below 32Gb.
 854       Universe::set_narrow_oop_shift(LogMinObjAlignmentInBytes);
 855     }
 856     // Set narrow_oop_base and narrow_oop_use_implicit_null_checks
 857     // used in ReservedHeapSpace() constructors.
 858     // The final values will be set in initialize_heap() below.
 859     if (base != 0 && (base + heap_size) <= OopEncodingHeapMax) {
 860       // Use zero based compressed oops
 861       Universe::set_narrow_oop_base(NULL);
 862       // Don't need guard page for implicit checks in indexed
 863       // addressing mode with zero based Compressed Oops.
 864       Universe::set_narrow_oop_use_implicit_null_checks(true);
 865     } else {
 866       // Set to a non-NULL value so the ReservedSpace ctor computes
 867       // the correct no-access prefix.
 868       // The final value will be set in initialize_heap() below.
 869       Universe::set_narrow_oop_base((address)NarrowOopHeapMax);
 870 #ifdef _WIN64
 871       if (UseLargePages) {
 872         // Cannot allocate guard pages for implicit checks in indexed
 873         // addressing mode when large pages are specified on windows.
 874         Universe::set_narrow_oop_use_implicit_null_checks(false);
 875       }
 876 #endif //  _WIN64
 877     }
 878   }
 879 #endif
 880   return (char*)base; // also return NULL (don't care) for 32-bit VM
 881 }
 882 
 883 jint Universe::initialize_heap() {
 884 
 885   if (UseParallelGC) {
 886 #ifndef SERIALGC
 887     Universe::_collectedHeap = new ParallelScavengeHeap();
 888 #else  // SERIALGC
 889     fatal("UseParallelGC not supported in java kernel vm.");
 890 #endif // SERIALGC
 891 
 892   } else if (UseG1GC) {
 893 #ifndef SERIALGC
 894     G1CollectorPolicy* g1p = new G1CollectorPolicy_BestRegionsFirst();
 895     G1CollectedHeap* g1h = new G1CollectedHeap(g1p);
 896     Universe::_collectedHeap = g1h;
 897 #else  // SERIALGC
 898     fatal("UseG1GC not supported in java kernel vm.");
 899 #endif // SERIALGC
 900 
 901   } else {
 902     GenCollectorPolicy *gc_policy;
 903 
 904     if (UseSerialGC) {
 905       gc_policy = new MarkSweepPolicy();
 906     } else if (UseConcMarkSweepGC) {
 907 #ifndef SERIALGC
 908       if (UseAdaptiveSizePolicy) {
 909         gc_policy = new ASConcurrentMarkSweepPolicy();
 910       } else {
 911         gc_policy = new ConcurrentMarkSweepPolicy();
 912       }
 913 #else   // SERIALGC
 914     fatal("UseConcMarkSweepGC not supported in java kernel vm.");
 915 #endif // SERIALGC
 916     } else { // default old generation
 917       gc_policy = new MarkSweepPolicy();
 918     }
 919 
 920     Universe::_collectedHeap = new GenCollectedHeap(gc_policy);
 921   }
 922 
 923   jint status = Universe::heap()->initialize();
 924   if (status != JNI_OK) {
 925     return status;
 926   }
 927 
 928 #ifdef _LP64
 929   if (UseCompressedOops) {
 930     // Subtract a page because something can get allocated at heap base.
 931     // This also makes implicit null checking work, because the
 932     // memory+1 page below heap_base needs to cause a signal.
 933     // See needs_explicit_null_check.
 934     // Only set the heap base for compressed oops because it indicates
 935     // compressed oops for pstack code.
 936     if (PrintCompressedOopsMode) {
 937       tty->cr();
 938       tty->print("heap address: "PTR_FORMAT, Universe::heap()->base());
 939     }
 940     if ((uint64_t)Universe::heap()->reserved_region().end() > OopEncodingHeapMax) {
 941       // Can't reserve heap below 32Gb.
 942       Universe::set_narrow_oop_base(Universe::heap()->base() - os::vm_page_size());
 943       Universe::set_narrow_oop_shift(LogMinObjAlignmentInBytes);
 944       if (PrintCompressedOopsMode) {
 945         tty->print(", Compressed Oops with base: "PTR_FORMAT, Universe::narrow_oop_base());
 946       }
 947     } else {
 948       Universe::set_narrow_oop_base(0);
 949       if (PrintCompressedOopsMode) {
 950         tty->print(", zero based Compressed Oops");
 951       }
 952 #ifdef _WIN64
 953       if (!Universe::narrow_oop_use_implicit_null_checks()) {
 954         // Don't need guard page for implicit checks in indexed addressing
 955         // mode with zero based Compressed Oops.
 956         Universe::set_narrow_oop_use_implicit_null_checks(true);
 957       }
 958 #endif //  _WIN64
 959       if((uint64_t)Universe::heap()->reserved_region().end() > NarrowOopHeapMax) {
 960         // Can't reserve heap below 4Gb.
 961         Universe::set_narrow_oop_shift(LogMinObjAlignmentInBytes);
 962       } else {
 963         Universe::set_narrow_oop_shift(0);
 964         if (PrintCompressedOopsMode) {
 965           tty->print(", 32-bits Oops");
 966         }
 967       }
 968     }
 969     if (PrintCompressedOopsMode) {
 970       tty->cr();
 971       tty->cr();
 972     }
 973   }
 974   assert(Universe::narrow_oop_base() == (Universe::heap()->base() - os::vm_page_size()) ||
 975          Universe::narrow_oop_base() == NULL, "invalid value");
 976   assert(Universe::narrow_oop_shift() == LogMinObjAlignmentInBytes ||
 977          Universe::narrow_oop_shift() == 0, "invalid value");
 978 #endif
 979 
 980   // We will never reach the CATCH below since Exceptions::_throw will cause
 981   // the VM to exit if an exception is thrown during initialization
 982 
 983   if (UseTLAB) {
 984     assert(Universe::heap()->supports_tlab_allocation(),
 985            "Should support thread-local allocation buffers");
 986     ThreadLocalAllocBuffer::startup_initialization();
 987   }
 988   return JNI_OK;
 989 }
 990 
 991 // It's the caller's repsonsibility to ensure glitch-freedom
 992 // (if required).
 993 void Universe::update_heap_info_at_gc() {
 994   _heap_capacity_at_last_gc = heap()->capacity();
 995   _heap_used_at_last_gc     = heap()->used();
 996 }
 997 
 998 
 999 
1000 void universe2_init() {
1001   EXCEPTION_MARK;
1002   Universe::genesis(CATCH);
1003   // Although we'd like to verify here that the state of the heap
1004   // is good, we can't because the main thread has not yet added
1005   // itself to the threads list (so, using current interfaces
1006   // we can't "fill" its TLAB), unless TLABs are disabled.
1007   if (VerifyBeforeGC && !UseTLAB &&
1008       Universe::heap()->total_collections() >= VerifyGCStartAt) {
1009      Universe::heap()->prepare_for_verify();
1010      Universe::verify();   // make sure we're starting with a clean slate
1011   }
1012 }
1013 
1014 
1015 // This function is defined in JVM.cpp
1016 extern void initialize_converter_functions();
1017 
1018 bool universe_post_init() {
1019   Universe::_fully_initialized = true;
1020   EXCEPTION_MARK;
1021   { ResourceMark rm;
1022     Interpreter::initialize();      // needed for interpreter entry points
1023     if (!UseSharedSpaces) {
1024       KlassHandle ok_h(THREAD, SystemDictionary::Object_klass());
1025       Universe::reinitialize_vtable_of(ok_h, CHECK_false);
1026       Universe::reinitialize_itables(CHECK_false);
1027     }
1028   }
1029 
1030   klassOop k;
1031   instanceKlassHandle k_h;
1032   if (!UseSharedSpaces) {
1033     // Setup preallocated empty java.lang.Class array
1034     Universe::_the_empty_class_klass_array = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_false);
1035     // Setup preallocated OutOfMemoryError errors
1036     k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_OutOfMemoryError(), true, CHECK_false);
1037     k_h = instanceKlassHandle(THREAD, k);
1038     Universe::_out_of_memory_error_java_heap = k_h->allocate_permanent_instance(CHECK_false);
1039     Universe::_out_of_memory_error_perm_gen = k_h->allocate_permanent_instance(CHECK_false);
1040     Universe::_out_of_memory_error_array_size = k_h->allocate_permanent_instance(CHECK_false);
1041     Universe::_out_of_memory_error_gc_overhead_limit =
1042       k_h->allocate_permanent_instance(CHECK_false);
1043 
1044     // Setup preallocated NullPointerException
1045     // (this is currently used for a cheap & dirty solution in compiler exception handling)
1046     k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_NullPointerException(), true, CHECK_false);
1047     Universe::_null_ptr_exception_instance = instanceKlass::cast(k)->allocate_permanent_instance(CHECK_false);
1048     // Setup preallocated ArithmeticException
1049     // (this is currently used for a cheap & dirty solution in compiler exception handling)
1050     k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_ArithmeticException(), true, CHECK_false);
1051     Universe::_arithmetic_exception_instance = instanceKlass::cast(k)->allocate_permanent_instance(CHECK_false);
1052     // Virtual Machine Error for when we get into a situation we can't resolve
1053     k = SystemDictionary::resolve_or_fail(
1054       vmSymbolHandles::java_lang_VirtualMachineError(), true, CHECK_false);
1055     bool linked = instanceKlass::cast(k)->link_class_or_fail(CHECK_false);
1056     if (!linked) {
1057       tty->print_cr("Unable to link/verify VirtualMachineError class");
1058       return false; // initialization failed
1059     }
1060     Universe::_virtual_machine_error_instance =
1061       instanceKlass::cast(k)->allocate_permanent_instance(CHECK_false);
1062   }
1063   if (!DumpSharedSpaces) {
1064     // These are the only Java fields that are currently set during shared space dumping.
1065     // We prefer to not handle this generally, so we always reinitialize these detail messages.
1066     Handle msg = java_lang_String::create_from_str("Java heap space", CHECK_false);
1067     java_lang_Throwable::set_message(Universe::_out_of_memory_error_java_heap, msg());
1068 
1069     msg = java_lang_String::create_from_str("PermGen space", CHECK_false);
1070     java_lang_Throwable::set_message(Universe::_out_of_memory_error_perm_gen, msg());
1071 
1072     msg = java_lang_String::create_from_str("Requested array size exceeds VM limit", CHECK_false);
1073     java_lang_Throwable::set_message(Universe::_out_of_memory_error_array_size, msg());
1074 
1075     msg = java_lang_String::create_from_str("GC overhead limit exceeded", CHECK_false);
1076     java_lang_Throwable::set_message(Universe::_out_of_memory_error_gc_overhead_limit, msg());
1077 
1078     msg = java_lang_String::create_from_str("/ by zero", CHECK_false);
1079     java_lang_Throwable::set_message(Universe::_arithmetic_exception_instance, msg());
1080 
1081     // Setup the array of errors that have preallocated backtrace
1082     k = Universe::_out_of_memory_error_java_heap->klass();
1083     assert(k->klass_part()->name() == vmSymbols::java_lang_OutOfMemoryError(), "should be out of memory error");
1084     k_h = instanceKlassHandle(THREAD, k);
1085 
1086     int len = (StackTraceInThrowable) ? (int)PreallocatedOutOfMemoryErrorCount : 0;
1087     Universe::_preallocated_out_of_memory_error_array = oopFactory::new_objArray(k_h(), len, CHECK_false);
1088     for (int i=0; i<len; i++) {
1089       oop err = k_h->allocate_permanent_instance(CHECK_false);
1090       Handle err_h = Handle(THREAD, err);
1091       java_lang_Throwable::allocate_backtrace(err_h, CHECK_false);
1092       Universe::preallocated_out_of_memory_errors()->obj_at_put(i, err_h());
1093     }
1094     Universe::_preallocated_out_of_memory_error_avail_count = (jint)len;
1095   }
1096 
1097 
1098   // Setup static method for registering finalizers
1099   // The finalizer klass must be linked before looking up the method, in
1100   // case it needs to get rewritten.
1101   instanceKlass::cast(SystemDictionary::Finalizer_klass())->link_class(CHECK_false);
1102   methodOop m = instanceKlass::cast(SystemDictionary::Finalizer_klass())->find_method(
1103                                   vmSymbols::register_method_name(),
1104                                   vmSymbols::register_method_signature());
1105   if (m == NULL || !m->is_static()) {
1106     THROW_MSG_(vmSymbols::java_lang_NoSuchMethodException(),
1107       "java.lang.ref.Finalizer.register", false);
1108   }
1109   Universe::_finalizer_register_cache->init(
1110     SystemDictionary::Finalizer_klass(), m, CHECK_false);
1111 
1112   // Resolve on first use and initialize class.
1113   // Note: No race-condition here, since a resolve will always return the same result
1114 
1115   // Setup method for security checks
1116   k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_reflect_Method(), true, CHECK_false);
1117   k_h = instanceKlassHandle(THREAD, k);
1118   k_h->link_class(CHECK_false);
1119   m = k_h->find_method(vmSymbols::invoke_name(), vmSymbols::object_object_array_object_signature());
1120   if (m == NULL || m->is_static()) {
1121     THROW_MSG_(vmSymbols::java_lang_NoSuchMethodException(),
1122       "java.lang.reflect.Method.invoke", false);
1123   }
1124   Universe::_reflect_invoke_cache->init(k_h(), m, CHECK_false);
1125 
1126   // Setup method for registering loaded classes in class loader vector
1127   instanceKlass::cast(SystemDictionary::ClassLoader_klass())->link_class(CHECK_false);
1128   m = instanceKlass::cast(SystemDictionary::ClassLoader_klass())->find_method(vmSymbols::addClass_name(), vmSymbols::class_void_signature());
1129   if (m == NULL || m->is_static()) {
1130     THROW_MSG_(vmSymbols::java_lang_NoSuchMethodException(),
1131       "java.lang.ClassLoader.addClass", false);
1132   }
1133   Universe::_loader_addClass_cache->init(
1134     SystemDictionary::ClassLoader_klass(), m, CHECK_false);
1135 
1136   // The folowing is initializing converter functions for serialization in
1137   // JVM.cpp. If we clean up the StrictMath code above we may want to find
1138   // a better solution for this as well.
1139   initialize_converter_functions();
1140 
1141   // This needs to be done before the first scavenge/gc, since
1142   // it's an input to soft ref clearing policy.
1143   {
1144     MutexLocker x(Heap_lock);
1145     Universe::update_heap_info_at_gc();
1146   }
1147 
1148   // ("weak") refs processing infrastructure initialization
1149   Universe::heap()->post_initialize();
1150 
1151   GC_locker::unlock();  // allow gc after bootstrapping
1152 
1153   MemoryService::set_universe_heap(Universe::_collectedHeap);
1154   return true;
1155 }
1156 
1157 
1158 void Universe::compute_base_vtable_size() {
1159   _base_vtable_size = ClassLoader::compute_Object_vtable();
1160 }
1161 
1162 
1163 // %%% The Universe::flush_foo methods belong in CodeCache.
1164 
1165 // Flushes compiled methods dependent on dependee.
1166 void Universe::flush_dependents_on(instanceKlassHandle dependee) {
1167   assert_lock_strong(Compile_lock);
1168 
1169   if (CodeCache::number_of_nmethods_with_dependencies() == 0) return;
1170 
1171   // CodeCache can only be updated by a thread_in_VM and they will all be
1172   // stopped dring the safepoint so CodeCache will be safe to update without
1173   // holding the CodeCache_lock.
1174 
1175   DepChange changes(dependee);
1176 
1177   // Compute the dependent nmethods
1178   if (CodeCache::mark_for_deoptimization(changes) > 0) {
1179     // At least one nmethod has been marked for deoptimization
1180     VM_Deoptimize op;
1181     VMThread::execute(&op);
1182   }
1183 }
1184 
1185 #ifdef HOTSWAP
1186 // Flushes compiled methods dependent on dependee in the evolutionary sense
1187 void Universe::flush_evol_dependents_on(instanceKlassHandle ev_k_h) {
1188   // --- Compile_lock is not held. However we are at a safepoint.
1189   assert_locked_or_safepoint(Compile_lock);
1190   if (CodeCache::number_of_nmethods_with_dependencies() == 0) return;
1191 
1192   // CodeCache can only be updated by a thread_in_VM and they will all be
1193   // stopped dring the safepoint so CodeCache will be safe to update without
1194   // holding the CodeCache_lock.
1195 
1196   // Compute the dependent nmethods
1197   if (CodeCache::mark_for_evol_deoptimization(ev_k_h) > 0) {
1198     // At least one nmethod has been marked for deoptimization
1199 
1200     // All this already happens inside a VM_Operation, so we'll do all the work here.
1201     // Stuff copied from VM_Deoptimize and modified slightly.
1202 
1203     // We do not want any GCs to happen while we are in the middle of this VM operation
1204     ResourceMark rm;
1205     DeoptimizationMarker dm;
1206 
1207     // Deoptimize all activations depending on marked nmethods
1208     Deoptimization::deoptimize_dependents();
1209 
1210     // Make the dependent methods not entrant (in VM_Deoptimize they are made zombies)
1211     CodeCache::make_marked_nmethods_not_entrant();
1212   }
1213 }
1214 #endif // HOTSWAP
1215 
1216 
1217 // Flushes compiled methods dependent on dependee
1218 void Universe::flush_dependents_on_method(methodHandle m_h) {
1219   // --- Compile_lock is not held. However we are at a safepoint.
1220   assert_locked_or_safepoint(Compile_lock);
1221 
1222   // CodeCache can only be updated by a thread_in_VM and they will all be
1223   // stopped dring the safepoint so CodeCache will be safe to update without
1224   // holding the CodeCache_lock.
1225 
1226   // Compute the dependent nmethods
1227   if (CodeCache::mark_for_deoptimization(m_h()) > 0) {
1228     // At least one nmethod has been marked for deoptimization
1229 
1230     // All this already happens inside a VM_Operation, so we'll do all the work here.
1231     // Stuff copied from VM_Deoptimize and modified slightly.
1232 
1233     // We do not want any GCs to happen while we are in the middle of this VM operation
1234     ResourceMark rm;
1235     DeoptimizationMarker dm;
1236 
1237     // Deoptimize all activations depending on marked nmethods
1238     Deoptimization::deoptimize_dependents();
1239 
1240     // Make the dependent methods not entrant (in VM_Deoptimize they are made zombies)
1241     CodeCache::make_marked_nmethods_not_entrant();
1242   }
1243 }
1244 
1245 void Universe::print() { print_on(gclog_or_tty); }
1246 
1247 void Universe::print_on(outputStream* st) {
1248   st->print_cr("Heap");
1249   heap()->print_on(st);
1250 }
1251 
1252 void Universe::print_heap_at_SIGBREAK() {
1253   if (PrintHeapAtSIGBREAK) {
1254     MutexLocker hl(Heap_lock);
1255     print_on(tty);
1256     tty->cr();
1257     tty->flush();
1258   }
1259 }
1260 
1261 void Universe::print_heap_before_gc(outputStream* st) {
1262   st->print_cr("{Heap before GC invocations=%u (full %u):",
1263                heap()->total_collections(),
1264                heap()->total_full_collections());
1265   heap()->print_on(st);
1266 }
1267 
1268 void Universe::print_heap_after_gc(outputStream* st) {
1269   st->print_cr("Heap after GC invocations=%u (full %u):",
1270                heap()->total_collections(),
1271                heap()->total_full_collections());
1272   heap()->print_on(st);
1273   st->print_cr("}");
1274 }
1275 
1276 void Universe::verify(bool allow_dirty, bool silent, bool option) {
1277   if (SharedSkipVerify) {
1278     return;
1279   }
1280 
1281   // The use of _verify_in_progress is a temporary work around for
1282   // 6320749.  Don't bother with a creating a class to set and clear
1283   // it since it is only used in this method and the control flow is
1284   // straight forward.
1285   _verify_in_progress = true;
1286 
1287   COMPILER2_PRESENT(
1288     assert(!DerivedPointerTable::is_active(),
1289          "DPT should not be active during verification "
1290          "(of thread stacks below)");
1291   )
1292 
1293   ResourceMark rm;
1294   HandleMark hm;  // Handles created during verification can be zapped
1295   _verify_count++;
1296 
1297   if (!silent) gclog_or_tty->print("[Verifying ");
1298   if (!silent) gclog_or_tty->print("threads ");
1299   Threads::verify();
1300   heap()->verify(allow_dirty, silent, option);
1301 
1302   if (!silent) gclog_or_tty->print("syms ");
1303   SymbolTable::verify();
1304   if (!silent) gclog_or_tty->print("strs ");
1305   StringTable::verify();
1306   {
1307     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1308     if (!silent) gclog_or_tty->print("zone ");
1309     CodeCache::verify();
1310   }
1311   if (!silent) gclog_or_tty->print("dict ");
1312   SystemDictionary::verify();
1313   if (!silent) gclog_or_tty->print("hand ");
1314   JNIHandles::verify();
1315   if (!silent) gclog_or_tty->print("C-heap ");
1316   os::check_heap();
1317   if (!silent) gclog_or_tty->print_cr("]");
1318 
1319   _verify_in_progress = false;
1320 }
1321 
1322 // Oop verification (see MacroAssembler::verify_oop)
1323 
1324 static uintptr_t _verify_oop_data[2]   = {0, (uintptr_t)-1};
1325 static uintptr_t _verify_klass_data[2] = {0, (uintptr_t)-1};
1326 
1327 
1328 static void calculate_verify_data(uintptr_t verify_data[2],
1329                                   HeapWord* low_boundary,
1330                                   HeapWord* high_boundary) {
1331   assert(low_boundary < high_boundary, "bad interval");
1332 
1333   // decide which low-order bits we require to be clear:
1334   size_t alignSize = MinObjAlignmentInBytes;
1335   size_t min_object_size = CollectedHeap::min_fill_size();
1336 
1337   // make an inclusive limit:
1338   uintptr_t max = (uintptr_t)high_boundary - min_object_size*wordSize;
1339   uintptr_t min = (uintptr_t)low_boundary;
1340   assert(min < max, "bad interval");
1341   uintptr_t diff = max ^ min;
1342 
1343   // throw away enough low-order bits to make the diff vanish
1344   uintptr_t mask = (uintptr_t)(-1);
1345   while ((mask & diff) != 0)
1346     mask <<= 1;
1347   uintptr_t bits = (min & mask);
1348   assert(bits == (max & mask), "correct mask");
1349   // check an intermediate value between min and max, just to make sure:
1350   assert(bits == ((min + (max-min)/2) & mask), "correct mask");
1351 
1352   // require address alignment, too:
1353   mask |= (alignSize - 1);
1354 
1355   if (!(verify_data[0] == 0 && verify_data[1] == (uintptr_t)-1)) {
1356     assert(verify_data[0] == mask && verify_data[1] == bits, "mask stability");
1357   }
1358   verify_data[0] = mask;
1359   verify_data[1] = bits;
1360 }
1361 
1362 
1363 // Oop verification (see MacroAssembler::verify_oop)
1364 #ifndef PRODUCT
1365 
1366 uintptr_t Universe::verify_oop_mask() {
1367   MemRegion m = heap()->reserved_region();
1368   calculate_verify_data(_verify_oop_data,
1369                         m.start(),
1370                         m.end());
1371   return _verify_oop_data[0];
1372 }
1373 
1374 
1375 
1376 uintptr_t Universe::verify_oop_bits() {
1377   verify_oop_mask();
1378   return _verify_oop_data[1];
1379 }
1380 
1381 
1382 uintptr_t Universe::verify_klass_mask() {
1383   /* $$$
1384   // A klass can never live in the new space.  Since the new and old
1385   // spaces can change size, we must settle for bounds-checking against
1386   // the bottom of the world, plus the smallest possible new and old
1387   // space sizes that may arise during execution.
1388   size_t min_new_size = Universe::new_size();   // in bytes
1389   size_t min_old_size = Universe::old_size();   // in bytes
1390   calculate_verify_data(_verify_klass_data,
1391           (HeapWord*)((uintptr_t)_new_gen->low_boundary + min_new_size + min_old_size),
1392           _perm_gen->high_boundary);
1393                         */
1394   // Why doesn't the above just say that klass's always live in the perm
1395   // gen?  I'll see if that seems to work...
1396   MemRegion permanent_reserved;
1397   switch (Universe::heap()->kind()) {
1398   default:
1399     // ???: What if a CollectedHeap doesn't have a permanent generation?
1400     ShouldNotReachHere();
1401     break;
1402   case CollectedHeap::GenCollectedHeap:
1403   case CollectedHeap::G1CollectedHeap: {
1404     SharedHeap* sh = (SharedHeap*) Universe::heap();
1405     permanent_reserved = sh->perm_gen()->reserved();
1406    break;
1407   }
1408 #ifndef SERIALGC
1409   case CollectedHeap::ParallelScavengeHeap: {
1410     ParallelScavengeHeap* psh = (ParallelScavengeHeap*) Universe::heap();
1411     permanent_reserved = psh->perm_gen()->reserved();
1412     break;
1413   }
1414 #endif // SERIALGC
1415   }
1416   calculate_verify_data(_verify_klass_data,
1417                         permanent_reserved.start(),
1418                         permanent_reserved.end());
1419 
1420   return _verify_klass_data[0];
1421 }
1422 
1423 
1424 
1425 uintptr_t Universe::verify_klass_bits() {
1426   verify_klass_mask();
1427   return _verify_klass_data[1];
1428 }
1429 
1430 
1431 uintptr_t Universe::verify_mark_mask() {
1432   return markOopDesc::lock_mask_in_place;
1433 }
1434 
1435 
1436 
1437 uintptr_t Universe::verify_mark_bits() {
1438   intptr_t mask = verify_mark_mask();
1439   intptr_t bits = (intptr_t)markOopDesc::prototype();
1440   assert((bits & ~mask) == 0, "no stray header bits");
1441   return bits;
1442 }
1443 #endif // PRODUCT
1444 
1445 
1446 void Universe::compute_verify_oop_data() {
1447   verify_oop_mask();
1448   verify_oop_bits();
1449   verify_mark_mask();
1450   verify_mark_bits();
1451   verify_klass_mask();
1452   verify_klass_bits();
1453 }
1454 
1455 
1456 void CommonMethodOopCache::init(klassOop k, methodOop m, TRAPS) {
1457   if (!UseSharedSpaces) {
1458     _klass = k;
1459   }
1460 #ifndef PRODUCT
1461   else {
1462     // sharing initilization should have already set up _klass
1463     assert(_klass != NULL, "just checking");
1464   }
1465 #endif
1466 
1467   _method_idnum = m->method_idnum();
1468   assert(_method_idnum >= 0, "sanity check");
1469 }
1470 
1471 
1472 ActiveMethodOopsCache::~ActiveMethodOopsCache() {
1473   if (_prev_methods != NULL) {
1474     for (int i = _prev_methods->length() - 1; i >= 0; i--) {
1475       jweak method_ref = _prev_methods->at(i);
1476       if (method_ref != NULL) {
1477         JNIHandles::destroy_weak_global(method_ref);
1478       }
1479     }
1480     delete _prev_methods;
1481     _prev_methods = NULL;
1482   }
1483 }
1484 
1485 
1486 void ActiveMethodOopsCache::add_previous_version(const methodOop method) {
1487   assert(Thread::current()->is_VM_thread(),
1488     "only VMThread can add previous versions");
1489 
1490   if (_prev_methods == NULL) {
1491     // This is the first previous version so make some space.
1492     // Start with 2 elements under the assumption that the class
1493     // won't be redefined much.
1494     _prev_methods = new (ResourceObj::C_HEAP) GrowableArray<jweak>(2, true);
1495   }
1496 
1497   // RC_TRACE macro has an embedded ResourceMark
1498   RC_TRACE(0x00000100,
1499     ("add: %s(%s): adding prev version ref for cached method @%d",
1500     method->name()->as_C_string(), method->signature()->as_C_string(),
1501     _prev_methods->length()));
1502 
1503   methodHandle method_h(method);
1504   jweak method_ref = JNIHandles::make_weak_global(method_h);
1505   _prev_methods->append(method_ref);
1506 
1507   // Using weak references allows previous versions of the cached
1508   // method to be GC'ed when they are no longer needed. Since the
1509   // caller is the VMThread and we are at a safepoint, this is a good
1510   // time to clear out unused weak references.
1511 
1512   for (int i = _prev_methods->length() - 1; i >= 0; i--) {
1513     jweak method_ref = _prev_methods->at(i);
1514     assert(method_ref != NULL, "weak method ref was unexpectedly cleared");
1515     if (method_ref == NULL) {
1516       _prev_methods->remove_at(i);
1517       // Since we are traversing the array backwards, we don't have to
1518       // do anything special with the index.
1519       continue;  // robustness
1520     }
1521 
1522     methodOop m = (methodOop)JNIHandles::resolve(method_ref);
1523     if (m == NULL) {
1524       // this method entry has been GC'ed so remove it
1525       JNIHandles::destroy_weak_global(method_ref);
1526       _prev_methods->remove_at(i);
1527     } else {
1528       // RC_TRACE macro has an embedded ResourceMark
1529       RC_TRACE(0x00000400, ("add: %s(%s): previous cached method @%d is alive",
1530         m->name()->as_C_string(), m->signature()->as_C_string(), i));
1531     }
1532   }
1533 } // end add_previous_version()
1534 
1535 
1536 bool ActiveMethodOopsCache::is_same_method(const methodOop method) const {
1537   instanceKlass* ik = instanceKlass::cast(klass());
1538   methodOop check_method = ik->method_with_idnum(method_idnum());
1539   assert(check_method != NULL, "sanity check");
1540   if (check_method == method) {
1541     // done with the easy case
1542     return true;
1543   }
1544 
1545   if (_prev_methods != NULL) {
1546     // The cached method has been redefined at least once so search
1547     // the previous versions for a match.
1548     for (int i = 0; i < _prev_methods->length(); i++) {
1549       jweak method_ref = _prev_methods->at(i);
1550       assert(method_ref != NULL, "weak method ref was unexpectedly cleared");
1551       if (method_ref == NULL) {
1552         continue;  // robustness
1553       }
1554 
1555       check_method = (methodOop)JNIHandles::resolve(method_ref);
1556       if (check_method == method) {
1557         // a previous version matches
1558         return true;
1559       }
1560     }
1561   }
1562 
1563   // either no previous versions or no previous version matched
1564   return false;
1565 }
1566 
1567 
1568 methodOop LatestMethodOopCache::get_methodOop() {
1569   instanceKlass* ik = instanceKlass::cast(klass());
1570   methodOop m = ik->method_with_idnum(method_idnum());
1571   assert(m != NULL, "sanity check");
1572   return m;
1573 }
1574 
1575 
1576 #ifdef ASSERT
1577 // Release dummy object(s) at bottom of heap
1578 bool Universe::release_fullgc_alot_dummy() {
1579   MutexLocker ml(FullGCALot_lock);
1580   if (_fullgc_alot_dummy_array != NULL) {
1581     if (_fullgc_alot_dummy_next >= _fullgc_alot_dummy_array->length()) {
1582       // No more dummies to release, release entire array instead
1583       _fullgc_alot_dummy_array = NULL;
1584       return false;
1585     }
1586     if (!UseConcMarkSweepGC) {
1587       // Release dummy at bottom of old generation
1588       _fullgc_alot_dummy_array->obj_at_put(_fullgc_alot_dummy_next++, NULL);
1589     }
1590     // Release dummy at bottom of permanent generation
1591     _fullgc_alot_dummy_array->obj_at_put(_fullgc_alot_dummy_next++, NULL);
1592   }
1593   return true;
1594 }
1595 
1596 #endif // ASSERT