1 /*
   2  * Copyright (c) 1999, 2009, 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 "incls/_precompiled.incl"
  26 #include "incls/_ciObjectFactory.cpp.incl"
  27 
  28 // ciObjectFactory
  29 //
  30 // This class handles requests for the creation of new instances
  31 // of ciObject and its subclasses.  It contains a caching mechanism
  32 // which ensures that for each oop, at most one ciObject is created.
  33 // This invariant allows more efficient implementation of ciObject.
  34 //
  35 // Implementation note: the oop->ciObject mapping is represented as
  36 // a table stored in an array.  Even though objects are moved
  37 // by the garbage collector, the compactor preserves their relative
  38 // order; address comparison of oops (in perm space) is safe so long
  39 // as we prohibit GC during our comparisons.  We currently use binary
  40 // search to find the oop in the table, and inserting a new oop
  41 // into the table may be costly.  If this cost ends up being
  42 // problematic the underlying data structure can be switched to some
  43 // sort of balanced binary tree.
  44 
  45 GrowableArray<ciObject*>* ciObjectFactory::_shared_ci_objects = NULL;
  46 ciSymbol*                 ciObjectFactory::_shared_ci_symbols[vmSymbols::SID_LIMIT];
  47 int                       ciObjectFactory::_shared_ident_limit = 0;
  48 volatile bool             ciObjectFactory::_initialized = false;
  49 
  50 
  51 // ------------------------------------------------------------------
  52 // ciObjectFactory::ciObjectFactory
  53 ciObjectFactory::ciObjectFactory(Arena* arena,
  54                                  int expected_size) {
  55 
  56   for (int i = 0; i < NON_PERM_BUCKETS; i++) {
  57     _non_perm_bucket[i] = NULL;
  58   }
  59   _non_perm_count = 0;
  60 
  61   _next_ident = _shared_ident_limit;
  62   _arena = arena;
  63   _ci_objects = new (arena) GrowableArray<ciObject*>(arena, expected_size, 0, NULL);
  64 
  65   // If the shared ci objects exist append them to this factory's objects
  66 
  67   if (_shared_ci_objects != NULL) {
  68     _ci_objects->appendAll(_shared_ci_objects);
  69   }
  70 
  71   _unloaded_methods = new (arena) GrowableArray<ciMethod*>(arena, 4, 0, NULL);
  72   _unloaded_klasses = new (arena) GrowableArray<ciKlass*>(arena, 8, 0, NULL);
  73   _unloaded_instances = new (arena) GrowableArray<ciInstance*>(arena, 4, 0, NULL);
  74   _return_addresses =
  75     new (arena) GrowableArray<ciReturnAddress*>(arena, 8, 0, NULL);
  76 }
  77 
  78 // ------------------------------------------------------------------
  79 // ciObjectFactory::ciObjectFactory
  80 void ciObjectFactory::initialize() {
  81   ASSERT_IN_VM;
  82   JavaThread* thread = JavaThread::current();
  83   HandleMark  handle_mark(thread);
  84 
  85   // This Arena is long lived and exists in the resource mark of the
  86   // compiler thread that initializes the initial ciObjectFactory which
  87   // creates the shared ciObjects that all later ciObjectFactories use.
  88   Arena* arena = new Arena();
  89   ciEnv initial(arena);
  90   ciEnv* env = ciEnv::current();
  91   env->_factory->init_shared_objects();
  92 
  93   _initialized = true;
  94 
  95 }
  96 
  97 void ciObjectFactory::init_shared_objects() {
  98 
  99   _next_ident = 1;  // start numbering CI objects at 1
 100 
 101   {
 102     // Create the shared symbols, but not in _shared_ci_objects.
 103     int i;
 104     for (i = vmSymbols::FIRST_SID; i < vmSymbols::SID_LIMIT; i++) {
 105       symbolHandle sym_handle = vmSymbolHandles::symbol_handle_at((vmSymbols::SID) i);
 106       assert(vmSymbols::find_sid(sym_handle()) == i, "1-1 mapping");
 107       ciSymbol* sym = new (_arena) ciSymbol(sym_handle, (vmSymbols::SID) i);
 108       init_ident_of(sym);
 109       _shared_ci_symbols[i] = sym;
 110     }
 111 #ifdef ASSERT
 112     for (i = vmSymbols::FIRST_SID; i < vmSymbols::SID_LIMIT; i++) {
 113       symbolHandle sym_handle = vmSymbolHandles::symbol_handle_at((vmSymbols::SID) i);
 114       ciSymbol* sym = vm_symbol_at((vmSymbols::SID) i);
 115       assert(sym->get_oop() == sym_handle(), "oop must match");
 116     }
 117     assert(ciSymbol::void_class_signature()->get_oop() == vmSymbols::void_class_signature(), "spot check");
 118 #endif
 119   }
 120 
 121   _ci_objects = new (_arena) GrowableArray<ciObject*>(_arena, 64, 0, NULL);
 122 
 123   for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) {
 124     BasicType t = (BasicType)i;
 125     if (type2name(t) != NULL && t != T_OBJECT && t != T_ARRAY && t != T_NARROWOOP) {
 126       ciType::_basic_types[t] = new (_arena) ciType(t);
 127       init_ident_of(ciType::_basic_types[t]);
 128     }
 129   }
 130 
 131   ciEnv::_null_object_instance = new (_arena) ciNullObject();
 132   init_ident_of(ciEnv::_null_object_instance);
 133   ciEnv::_method_klass_instance =
 134     get(Universe::methodKlassObj())->as_method_klass();
 135   ciEnv::_symbol_klass_instance =
 136     get(Universe::symbolKlassObj())->as_symbol_klass();
 137   ciEnv::_klass_klass_instance =
 138     get(Universe::klassKlassObj())->as_klass_klass();
 139   ciEnv::_instance_klass_klass_instance =
 140     get(Universe::instanceKlassKlassObj())
 141       ->as_instance_klass_klass();
 142   ciEnv::_type_array_klass_klass_instance =
 143     get(Universe::typeArrayKlassKlassObj())
 144       ->as_type_array_klass_klass();
 145   ciEnv::_obj_array_klass_klass_instance =
 146     get(Universe::objArrayKlassKlassObj())
 147       ->as_obj_array_klass_klass();
 148 
 149 #define WK_KLASS_DEFN(name, ignore_s, opt)                              \
 150   if (SystemDictionary::name() != NULL) \
 151     ciEnv::_##name = get(SystemDictionary::name())->as_instance_klass();
 152 
 153   WK_KLASSES_DO(WK_KLASS_DEFN)
 154 #undef WK_KLASS_DEFN
 155 
 156   for (int len = -1; len != _ci_objects->length(); ) {
 157     len = _ci_objects->length();
 158     for (int i2 = 0; i2 < len; i2++) {
 159       ciObject* obj = _ci_objects->at(i2);
 160       if (obj->is_loaded() && obj->is_instance_klass()) {
 161         obj->as_instance_klass()->compute_nonstatic_fields();
 162       }
 163     }
 164   }
 165 
 166   ciEnv::_unloaded_cisymbol = (ciSymbol*) ciObjectFactory::get(vmSymbols::dummy_symbol_oop());
 167   // Create dummy instanceKlass and objArrayKlass object and assign them idents
 168   ciEnv::_unloaded_ciinstance_klass = new (_arena) ciInstanceKlass(ciEnv::_unloaded_cisymbol, NULL, NULL);
 169   init_ident_of(ciEnv::_unloaded_ciinstance_klass);
 170   ciEnv::_unloaded_ciobjarrayklass = new (_arena) ciObjArrayKlass(ciEnv::_unloaded_cisymbol, ciEnv::_unloaded_ciinstance_klass, 1);
 171   init_ident_of(ciEnv::_unloaded_ciobjarrayklass);
 172   assert(ciEnv::_unloaded_ciobjarrayklass->is_obj_array_klass(), "just checking");
 173 
 174   get(Universe::boolArrayKlassObj());
 175   get(Universe::charArrayKlassObj());
 176   get(Universe::singleArrayKlassObj());
 177   get(Universe::doubleArrayKlassObj());
 178   get(Universe::byteArrayKlassObj());
 179   get(Universe::shortArrayKlassObj());
 180   get(Universe::intArrayKlassObj());
 181   get(Universe::longArrayKlassObj());
 182 
 183 
 184 
 185   assert(_non_perm_count == 0, "no shared non-perm objects");
 186 
 187   // The shared_ident_limit is the first ident number that will
 188   // be used for non-shared objects.  That is, numbers less than
 189   // this limit are permanently assigned to shared CI objects,
 190   // while the higher numbers are recycled afresh by each new ciEnv.
 191 
 192   _shared_ident_limit = _next_ident;
 193   _shared_ci_objects = _ci_objects;
 194 }
 195 
 196 // ------------------------------------------------------------------
 197 // ciObjectFactory::get
 198 //
 199 // Get the ciObject corresponding to some oop.  If the ciObject has
 200 // already been created, it is returned.  Otherwise, a new ciObject
 201 // is created.
 202 ciObject* ciObjectFactory::get(oop key) {
 203   ASSERT_IN_VM;
 204 
 205 #ifdef ASSERT
 206   if (CIObjectFactoryVerify) {
 207     oop last = NULL;
 208     for (int j = 0; j< _ci_objects->length(); j++) {
 209       oop o = _ci_objects->at(j)->get_oop();
 210       assert(last < o, "out of order");
 211       last = o;
 212     }
 213   }
 214 #endif // ASSERT
 215   int len = _ci_objects->length();
 216   int index = find(key, _ci_objects);
 217 #ifdef ASSERT
 218   if (CIObjectFactoryVerify) {
 219     for (int i=0; i<_ci_objects->length(); i++) {
 220       if (_ci_objects->at(i)->get_oop() == key) {
 221         assert(index == i, " bad lookup");
 222       }
 223     }
 224   }
 225 #endif
 226   if (!is_found_at(index, key, _ci_objects)) {
 227     // Check in the non-perm area before putting it in the list.
 228     NonPermObject* &bucket = find_non_perm(key);
 229     if (bucket != NULL) {
 230       return bucket->object();
 231     }
 232 
 233     // Check in the shared symbol area before putting it in the list.
 234     if (key->is_symbol()) {
 235       vmSymbols::SID sid = vmSymbols::find_sid((symbolOop)key);
 236       if (sid != vmSymbols::NO_SID) {
 237         // do not pollute the main cache with it
 238         return vm_symbol_at(sid);
 239       }
 240     }
 241 
 242     // The ciObject does not yet exist.  Create it and insert it
 243     // into the cache.
 244     Handle keyHandle(key);
 245     ciObject* new_object = create_new_object(keyHandle());
 246     assert(keyHandle() == new_object->get_oop(), "must be properly recorded");
 247     init_ident_of(new_object);
 248     if (!new_object->is_perm()) {
 249       // Not a perm-space object.
 250       insert_non_perm(bucket, keyHandle(), new_object);
 251       return new_object;
 252     }
 253     if (len != _ci_objects->length()) {
 254       // creating the new object has recursively entered new objects
 255       // into the table.  We need to recompute our index.
 256       index = find(keyHandle(), _ci_objects);
 257     }
 258     assert(!is_found_at(index, keyHandle(), _ci_objects), "no double insert");
 259     insert(index, new_object, _ci_objects);
 260     return new_object;
 261   }
 262   return _ci_objects->at(index);
 263 }
 264 
 265 // ------------------------------------------------------------------
 266 // ciObjectFactory::create_new_object
 267 //
 268 // Create a new ciObject from an oop.
 269 //
 270 // Implementation note: this functionality could be virtual behavior
 271 // of the oop itself.  For now, we explicitly marshal the object.
 272 ciObject* ciObjectFactory::create_new_object(oop o) {
 273   EXCEPTION_CONTEXT;
 274 
 275   if (o->is_symbol()) {
 276     symbolHandle h_o(THREAD, (symbolOop)o);
 277     assert(vmSymbols::find_sid(h_o()) == vmSymbols::NO_SID, "");
 278     return new (arena()) ciSymbol(h_o, vmSymbols::NO_SID);
 279   } else if (o->is_klass()) {
 280     KlassHandle h_k(THREAD, (klassOop)o);
 281     Klass* k = ((klassOop)o)->klass_part();
 282     if (k->oop_is_instance()) {
 283       return new (arena()) ciInstanceKlass(h_k);
 284     } else if (k->oop_is_objArray()) {
 285       return new (arena()) ciObjArrayKlass(h_k);
 286     } else if (k->oop_is_typeArray()) {
 287       return new (arena()) ciTypeArrayKlass(h_k);
 288     } else if (k->oop_is_method()) {
 289       return new (arena()) ciMethodKlass(h_k);
 290     } else if (k->oop_is_symbol()) {
 291       return new (arena()) ciSymbolKlass(h_k);
 292     } else if (k->oop_is_klass()) {
 293       if (k->oop_is_objArrayKlass()) {
 294         return new (arena()) ciObjArrayKlassKlass(h_k);
 295       } else if (k->oop_is_typeArrayKlass()) {
 296         return new (arena()) ciTypeArrayKlassKlass(h_k);
 297       } else if (k->oop_is_instanceKlass()) {
 298         return new (arena()) ciInstanceKlassKlass(h_k);
 299       } else {
 300         assert(o == Universe::klassKlassObj(), "bad klassKlass");
 301         return new (arena()) ciKlassKlass(h_k);
 302       }
 303     }
 304   } else if (o->is_method()) {
 305     methodHandle h_m(THREAD, (methodOop)o);
 306     return new (arena()) ciMethod(h_m);
 307   } else if (o->is_methodData()) {
 308     methodDataHandle h_md(THREAD, (methodDataOop)o);
 309     return new (arena()) ciMethodData(h_md);
 310   } else if (o->is_instance()) {
 311     instanceHandle h_i(THREAD, (instanceOop)o);
 312     if (java_dyn_CallSite::is_instance(o))
 313       return new (arena()) ciCallSite(h_i);
 314     else if (java_dyn_MethodHandle::is_instance(o))
 315       return new (arena()) ciMethodHandle(h_i);
 316     else
 317       return new (arena()) ciInstance(h_i);
 318   } else if (o->is_objArray()) {
 319     objArrayHandle h_oa(THREAD, (objArrayOop)o);
 320     return new (arena()) ciObjArray(h_oa);
 321   } else if (o->is_typeArray()) {
 322     typeArrayHandle h_ta(THREAD, (typeArrayOop)o);
 323     return new (arena()) ciTypeArray(h_ta);
 324   } else if (o->is_constantPoolCache()) {
 325     constantPoolCacheHandle h_cpc(THREAD, (constantPoolCacheOop) o);
 326     return new (arena()) ciCPCache(h_cpc);
 327   }
 328 
 329   // The oop is of some type not supported by the compiler interface.
 330   ShouldNotReachHere();
 331   return NULL;
 332 }
 333 
 334 //------------------------------------------------------------------
 335 // ciObjectFactory::get_unloaded_method
 336 //
 337 // Get the ciMethod representing an unloaded/unfound method.
 338 //
 339 // Implementation note: unloaded methods are currently stored in
 340 // an unordered array, requiring a linear-time lookup for each
 341 // unloaded method.  This may need to change.
 342 ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder,
 343                                                ciSymbol*        name,
 344                                                ciSymbol*        signature) {
 345   for (int i=0; i<_unloaded_methods->length(); i++) {
 346     ciMethod* entry = _unloaded_methods->at(i);
 347     if (entry->holder()->equals(holder) &&
 348         entry->name()->equals(name) &&
 349         entry->signature()->as_symbol()->equals(signature)) {
 350       // We've found a match.
 351       return entry;
 352     }
 353   }
 354 
 355   // This is a new unloaded method.  Create it and stick it in
 356   // the cache.
 357   ciMethod* new_method = new (arena()) ciMethod(holder, name, signature);
 358 
 359   init_ident_of(new_method);
 360   _unloaded_methods->append(new_method);
 361 
 362   return new_method;
 363 }
 364 
 365 //------------------------------------------------------------------
 366 // ciObjectFactory::get_unloaded_klass
 367 //
 368 // Get a ciKlass representing an unloaded klass.
 369 //
 370 // Implementation note: unloaded klasses are currently stored in
 371 // an unordered array, requiring a linear-time lookup for each
 372 // unloaded klass.  This may need to change.
 373 ciKlass* ciObjectFactory::get_unloaded_klass(ciKlass* accessing_klass,
 374                                              ciSymbol* name,
 375                                              bool create_if_not_found) {
 376   EXCEPTION_CONTEXT;
 377   oop loader = NULL;
 378   oop domain = NULL;
 379   if (accessing_klass != NULL) {
 380     loader = accessing_klass->loader();
 381     domain = accessing_klass->protection_domain();
 382   }
 383   for (int i=0; i<_unloaded_klasses->length(); i++) {
 384     ciKlass* entry = _unloaded_klasses->at(i);
 385     if (entry->name()->equals(name) &&
 386         entry->loader() == loader &&
 387         entry->protection_domain() == domain) {
 388       // We've found a match.
 389       return entry;
 390     }
 391   }
 392 
 393   if (!create_if_not_found)
 394     return NULL;
 395 
 396   // This is a new unloaded klass.  Create it and stick it in
 397   // the cache.
 398   ciKlass* new_klass = NULL;
 399 
 400   // Two cases: this is an unloaded objArrayKlass or an
 401   // unloaded instanceKlass.  Deal with both.
 402   if (name->byte_at(0) == '[') {
 403     // Decompose the name.'
 404     jint dimension = 0;
 405     symbolOop element_name = NULL;
 406     BasicType element_type= FieldType::get_array_info(name->get_symbolOop(),
 407                                                       &dimension,
 408                                                       &element_name,
 409                                                       THREAD);
 410     if (HAS_PENDING_EXCEPTION) {
 411       CLEAR_PENDING_EXCEPTION;
 412       CURRENT_THREAD_ENV->record_out_of_memory_failure();
 413       return ciEnv::_unloaded_ciobjarrayklass;
 414     }
 415     assert(element_type != T_ARRAY, "unsuccessful decomposition");
 416     ciKlass* element_klass = NULL;
 417     if (element_type == T_OBJECT) {
 418       ciEnv *env = CURRENT_THREAD_ENV;
 419       ciSymbol* ci_name = env->get_object(element_name)->as_symbol();
 420       element_klass =
 421         env->get_klass_by_name(accessing_klass, ci_name, false)->as_instance_klass();
 422     } else {
 423       assert(dimension > 1, "one dimensional type arrays are always loaded.");
 424 
 425       // The type array itself takes care of one of the dimensions.
 426       dimension--;
 427 
 428       // The element klass is a typeArrayKlass.
 429       element_klass = ciTypeArrayKlass::make(element_type);
 430     }
 431     new_klass = new (arena()) ciObjArrayKlass(name, element_klass, dimension);
 432   } else {
 433     jobject loader_handle = NULL;
 434     jobject domain_handle = NULL;
 435     if (accessing_klass != NULL) {
 436       loader_handle = accessing_klass->loader_handle();
 437       domain_handle = accessing_klass->protection_domain_handle();
 438     }
 439     new_klass = new (arena()) ciInstanceKlass(name, loader_handle, domain_handle);
 440   }
 441   init_ident_of(new_klass);
 442   _unloaded_klasses->append(new_klass);
 443 
 444   return new_klass;
 445 }
 446 
 447 
 448 //------------------------------------------------------------------
 449 // ciObjectFactory::get_unloaded_instance
 450 //
 451 // Get a ciInstance representing an as-yet undetermined instance of a given class.
 452 //
 453 ciInstance* ciObjectFactory::get_unloaded_instance(ciInstanceKlass* instance_klass) {
 454   for (int i=0; i<_unloaded_instances->length(); i++) {
 455     ciInstance* entry = _unloaded_instances->at(i);
 456     if (entry->klass()->equals(instance_klass)) {
 457       // We've found a match.
 458       return entry;
 459     }
 460   }
 461 
 462   // This is a new unloaded instance.  Create it and stick it in
 463   // the cache.
 464   ciInstance* new_instance = new (arena()) ciInstance(instance_klass);
 465 
 466   init_ident_of(new_instance);
 467   _unloaded_instances->append(new_instance);
 468 
 469   // make sure it looks the way we want:
 470   assert(!new_instance->is_loaded(), "");
 471   assert(new_instance->klass() == instance_klass, "");
 472 
 473   return new_instance;
 474 }
 475 
 476 
 477 //------------------------------------------------------------------
 478 // ciObjectFactory::get_unloaded_klass_mirror
 479 //
 480 // Get a ciInstance representing an unresolved klass mirror.
 481 //
 482 // Currently, this ignores the parameters and returns a unique unloaded instance.
 483 ciInstance* ciObjectFactory::get_unloaded_klass_mirror(ciKlass*  type) {
 484   assert(ciEnv::_Class_klass != NULL, "");
 485   return get_unloaded_instance(ciEnv::_Class_klass->as_instance_klass());
 486 }
 487 
 488 //------------------------------------------------------------------
 489 // ciObjectFactory::get_unloaded_method_handle_constant
 490 //
 491 // Get a ciInstance representing an unresolved method handle constant.
 492 //
 493 // Currently, this ignores the parameters and returns a unique unloaded instance.
 494 ciInstance* ciObjectFactory::get_unloaded_method_handle_constant(ciKlass*  holder,
 495                                                                  ciSymbol* name,
 496                                                                  ciSymbol* signature,
 497                                                                  int       ref_kind) {
 498   if (ciEnv::_MethodHandle_klass == NULL)  return NULL;
 499   return get_unloaded_instance(ciEnv::_MethodHandle_klass->as_instance_klass());
 500 }
 501 
 502 //------------------------------------------------------------------
 503 // ciObjectFactory::get_unloaded_method_type_constant
 504 //
 505 // Get a ciInstance representing an unresolved method type constant.
 506 //
 507 // Currently, this ignores the parameters and returns a unique unloaded instance.
 508 ciInstance* ciObjectFactory::get_unloaded_method_type_constant(ciSymbol* signature) {
 509   if (ciEnv::_MethodType_klass == NULL)  return NULL;
 510   return get_unloaded_instance(ciEnv::_MethodType_klass->as_instance_klass());
 511 }
 512 
 513 
 514 
 515 //------------------------------------------------------------------
 516 // ciObjectFactory::get_empty_methodData
 517 //
 518 // Get the ciMethodData representing the methodData for a method with
 519 // none.
 520 ciMethodData* ciObjectFactory::get_empty_methodData() {
 521   ciMethodData* new_methodData = new (arena()) ciMethodData();
 522   init_ident_of(new_methodData);
 523   return new_methodData;
 524 }
 525 
 526 //------------------------------------------------------------------
 527 // ciObjectFactory::get_return_address
 528 //
 529 // Get a ciReturnAddress for a specified bci.
 530 ciReturnAddress* ciObjectFactory::get_return_address(int bci) {
 531   for (int i=0; i<_return_addresses->length(); i++) {
 532     ciReturnAddress* entry = _return_addresses->at(i);
 533     if (entry->bci() == bci) {
 534       // We've found a match.
 535       return entry;
 536     }
 537   }
 538 
 539   ciReturnAddress* new_ret_addr = new (arena()) ciReturnAddress(bci);
 540   init_ident_of(new_ret_addr);
 541   _return_addresses->append(new_ret_addr);
 542   return new_ret_addr;
 543 }
 544 
 545 // ------------------------------------------------------------------
 546 // ciObjectFactory::init_ident_of
 547 void ciObjectFactory::init_ident_of(ciObject* obj) {
 548   obj->set_ident(_next_ident++);
 549 }
 550 
 551 
 552 // ------------------------------------------------------------------
 553 // ciObjectFactory::find
 554 //
 555 // Use binary search to find the position of this oop in the cache.
 556 // If there is no entry in the cache corresponding to this oop, return
 557 // the position at which the oop should be inserted.
 558 int ciObjectFactory::find(oop key, GrowableArray<ciObject*>* objects) {
 559   int min = 0;
 560   int max = objects->length()-1;
 561 
 562   // print_contents();
 563 
 564   while (max >= min) {
 565     int mid = (max + min) / 2;
 566     oop value = objects->at(mid)->get_oop();
 567     if (value < key) {
 568       min = mid + 1;
 569     } else if (value > key) {
 570       max = mid - 1;
 571     } else {
 572       return mid;
 573     }
 574   }
 575   return min;
 576 }
 577 
 578 // ------------------------------------------------------------------
 579 // ciObjectFactory::is_found_at
 580 //
 581 // Verify that the binary seach found the given key.
 582 bool ciObjectFactory::is_found_at(int index, oop key, GrowableArray<ciObject*>* objects) {
 583   return (index < objects->length() &&
 584           objects->at(index)->get_oop() == key);
 585 }
 586 
 587 
 588 // ------------------------------------------------------------------
 589 // ciObjectFactory::insert
 590 //
 591 // Insert a ciObject into the table at some index.
 592 void ciObjectFactory::insert(int index, ciObject* obj, GrowableArray<ciObject*>* objects) {
 593   int len = objects->length();
 594   if (len == index) {
 595     objects->append(obj);
 596   } else {
 597     objects->append(objects->at(len-1));
 598     int pos;
 599     for (pos = len-2; pos >= index; pos--) {
 600       objects->at_put(pos+1,objects->at(pos));
 601     }
 602     objects->at_put(index, obj);
 603   }
 604 #ifdef ASSERT
 605   if (CIObjectFactoryVerify) {
 606     oop last = NULL;
 607     for (int j = 0; j< objects->length(); j++) {
 608       oop o = objects->at(j)->get_oop();
 609       assert(last < o, "out of order");
 610       last = o;
 611     }
 612   }
 613 #endif // ASSERT
 614 }
 615 
 616 static ciObjectFactory::NonPermObject* emptyBucket = NULL;
 617 
 618 // ------------------------------------------------------------------
 619 // ciObjectFactory::find_non_perm
 620 //
 621 // Use a small hash table, hashed on the klass of the key.
 622 // If there is no entry in the cache corresponding to this oop, return
 623 // the null tail of the bucket into which the oop should be inserted.
 624 ciObjectFactory::NonPermObject* &ciObjectFactory::find_non_perm(oop key) {
 625   // Be careful:  is_perm might change from false to true.
 626   // Thus, there might be a matching perm object in the table.
 627   // If there is, this probe must find it.
 628   if (key->is_perm() && _non_perm_count == 0) {
 629     return emptyBucket;
 630   } else if (key->is_instance()) {
 631     if (key->klass() == SystemDictionary::Class_klass()) {
 632       // class mirror instances are always perm
 633       return emptyBucket;
 634     }
 635     // fall through to probe
 636   } else if (key->is_array()) {
 637     // fall through to probe
 638   } else {
 639     // not an array or instance
 640     return emptyBucket;
 641   }
 642 
 643   ciObject* klass = get(key->klass());
 644   NonPermObject* *bp = &_non_perm_bucket[(unsigned) klass->hash() % NON_PERM_BUCKETS];
 645   for (NonPermObject* p; (p = (*bp)) != NULL; bp = &p->next()) {
 646     if (is_equal(p, key))  break;
 647   }
 648   return (*bp);
 649 }
 650 
 651 
 652 
 653 // ------------------------------------------------------------------
 654 // Code for for NonPermObject
 655 //
 656 inline ciObjectFactory::NonPermObject::NonPermObject(ciObjectFactory::NonPermObject* &bucket, oop key, ciObject* object) {
 657   assert(ciObjectFactory::is_initialized(), "");
 658   _object = object;
 659   _next = bucket;
 660   bucket = this;
 661 }
 662 
 663 
 664 
 665 // ------------------------------------------------------------------
 666 // ciObjectFactory::insert_non_perm
 667 //
 668 // Insert a ciObject into the non-perm table.
 669 void ciObjectFactory::insert_non_perm(ciObjectFactory::NonPermObject* &where, oop key, ciObject* obj) {
 670   assert(&where != &emptyBucket, "must not try to fill empty bucket");
 671   NonPermObject* p = new (arena()) NonPermObject(where, key, obj);
 672   assert(where == p && is_equal(p, key) && p->object() == obj, "entry must match");
 673   assert(find_non_perm(key) == p, "must find the same spot");
 674   ++_non_perm_count;
 675 }
 676 
 677 // ------------------------------------------------------------------
 678 // ciObjectFactory::vm_symbol_at
 679 // Get the ciSymbol corresponding to some index in vmSymbols.
 680 ciSymbol* ciObjectFactory::vm_symbol_at(int index) {
 681   assert(index >= vmSymbols::FIRST_SID && index < vmSymbols::SID_LIMIT, "oob");
 682   return _shared_ci_symbols[index];
 683 }
 684 
 685 // ------------------------------------------------------------------
 686 // ciObjectFactory::print_contents_impl
 687 void ciObjectFactory::print_contents_impl() {
 688   int len = _ci_objects->length();
 689   tty->print_cr("ciObjectFactory (%d) oop contents:", len);
 690   for (int i=0; i<len; i++) {
 691     _ci_objects->at(i)->print();
 692     tty->cr();
 693   }
 694 }
 695 
 696 // ------------------------------------------------------------------
 697 // ciObjectFactory::print_contents
 698 void ciObjectFactory::print_contents() {
 699   print();
 700   tty->cr();
 701   GUARDED_VM_ENTRY(print_contents_impl();)
 702 }
 703 
 704 // ------------------------------------------------------------------
 705 // ciObjectFactory::print
 706 //
 707 // Print debugging information about the object factory
 708 void ciObjectFactory::print() {
 709   tty->print("<ciObjectFactory oops=%d unloaded_methods=%d unloaded_instances=%d unloaded_klasses=%d>",
 710              _ci_objects->length(), _unloaded_methods->length(),
 711              _unloaded_instances->length(),
 712              _unloaded_klasses->length());
 713 }