src/share/vm/oops/constantPoolOop.hpp

Wed, 05 Dec 2007 09:00:00 -0800

author
dcubed
date
Wed, 05 Dec 2007 09:00:00 -0800
changeset 451
f8236e79048a
parent 435
a61af66fc99e
child 548
ba764ed4b6f2
permissions
-rw-r--r--

6664627: Merge changes made only in hotspot 11 forward to jdk 7
Reviewed-by: jcoomes

     1 /*
     2  * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 // A constantPool is an array containing class constants as described in the
    26 // class file.
    27 //
    28 // Most of the constant pool entries are written during class parsing, which
    29 // is safe.  For klass and string types, the constant pool entry is
    30 // modified when the entry is resolved.  If a klass or string constant pool
    31 // entry is read without a lock, only the resolved state guarantees that
    32 // the entry in the constant pool is a klass or String object and
    33 // not a symbolOop.
    35 class SymbolHashMap;
    37 class constantPoolOopDesc : public arrayOopDesc {
    38   friend class VMStructs;
    39   friend class BytecodeInterpreter;  // Directly extracts an oop in the pool for fast instanceof/checkcast
    40  private:
    41   typeArrayOop         _tags; // the tag array describing the constant pool's contents
    42   constantPoolCacheOop _cache;         // the cache holding interpreter runtime information
    43   klassOop             _pool_holder;   // the corresponding class
    44   // only set to non-zero if constant pool is merged by RedefineClasses
    45   int                  _orig_length;
    47   void set_tags(typeArrayOop tags)             { oop_store_without_check((oop*)&_tags, tags); }
    48   void tag_at_put(int which, jbyte t)          { tags()->byte_at_put(which, t); }
    49   void release_tag_at_put(int which, jbyte t)  { tags()->release_byte_at_put(which, t); }
    51  private:
    52   intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(constantPoolOopDesc)); }
    53   oop* tags_addr()       { return (oop*)&_tags; }
    54   oop* cache_addr()      { return (oop*)&_cache; }
    56   oop* obj_at_addr(int which) const {
    57     assert(is_within_bounds(which), "index out of bounds");
    58     return (oop*) &base()[which];
    59   }
    61   jint* int_at_addr(int which) const {
    62     assert(is_within_bounds(which), "index out of bounds");
    63     return (jint*) &base()[which];
    64   }
    66   jlong* long_at_addr(int which) const {
    67     assert(is_within_bounds(which), "index out of bounds");
    68     return (jlong*) &base()[which];
    69   }
    71   jfloat* float_at_addr(int which) const {
    72     assert(is_within_bounds(which), "index out of bounds");
    73     return (jfloat*) &base()[which];
    74   }
    76   jdouble* double_at_addr(int which) const {
    77     assert(is_within_bounds(which), "index out of bounds");
    78     return (jdouble*) &base()[which];
    79   }
    81  public:
    82   typeArrayOop tags() const                 { return _tags; }
    84   // Klass holding pool
    85   klassOop pool_holder() const              { return _pool_holder; }
    86   void set_pool_holder(klassOop k)          { oop_store_without_check((oop*)&_pool_holder, (oop) k); }
    87   oop* pool_holder_addr()                   { return (oop*)&_pool_holder; }
    89   // Interpreter runtime support
    90   constantPoolCacheOop cache() const        { return _cache; }
    91   void set_cache(constantPoolCacheOop cache){ oop_store((oop*)&_cache, cache); }
    93   // Assembly code support
    94   static int tags_offset_in_bytes()         { return offset_of(constantPoolOopDesc, _tags); }
    95   static int cache_offset_in_bytes()        { return offset_of(constantPoolOopDesc, _cache); }
    96   static int pool_holder_offset_in_bytes()  { return offset_of(constantPoolOopDesc, _pool_holder); }
    98   // Storing constants
   100   void klass_at_put(int which, klassOop k) {
   101     oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
   102     // The interpreter assumes when the tag is stored, the klass is resolved
   103     // and the klassOop is a klass rather than a symbolOop, so we need
   104     // hardware store ordering here.
   105     release_tag_at_put(which, JVM_CONSTANT_Class);
   106     if (UseConcMarkSweepGC) {
   107       // In case the earlier card-mark was consumed by a concurrent
   108       // marking thread before the tag was updated, redirty the card.
   109       oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
   110     }
   111   }
   113   // For temporary use while constructing constant pool
   114   void klass_index_at_put(int which, int name_index) {
   115     tag_at_put(which, JVM_CONSTANT_ClassIndex);
   116     *int_at_addr(which) = name_index;
   117   }
   119   // Temporary until actual use
   120   void unresolved_klass_at_put(int which, symbolOop s) {
   121     // Overwrite the old index with a GC friendly value so
   122     // that if GC looks during the transition it won't try
   123     // to treat a small integer as oop.
   124     *obj_at_addr(which) = NULL;
   125     release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass);
   126     oop_store_without_check(obj_at_addr(which), oop(s));
   127   }
   129   // Temporary until actual use
   130   void unresolved_string_at_put(int which, symbolOop s) {
   131     *obj_at_addr(which) = NULL;
   132     release_tag_at_put(which, JVM_CONSTANT_UnresolvedString);
   133     oop_store_without_check(obj_at_addr(which), oop(s));
   134   }
   136   void int_at_put(int which, jint i) {
   137     tag_at_put(which, JVM_CONSTANT_Integer);
   138     *int_at_addr(which) = i;
   139   }
   141   void long_at_put(int which, jlong l) {
   142     tag_at_put(which, JVM_CONSTANT_Long);
   143     // *long_at_addr(which) = l;
   144     Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l));
   145   }
   147   void float_at_put(int which, jfloat f) {
   148     tag_at_put(which, JVM_CONSTANT_Float);
   149     *float_at_addr(which) = f;
   150   }
   152   void double_at_put(int which, jdouble d) {
   153     tag_at_put(which, JVM_CONSTANT_Double);
   154     // *double_at_addr(which) = d;
   155     // u8 temp = *(u8*) &d;
   156     Bytes::put_native_u8((address) double_at_addr(which), *((u8*) &d));
   157   }
   159   void symbol_at_put(int which, symbolOop s) {
   160     tag_at_put(which, JVM_CONSTANT_Utf8);
   161     oop_store_without_check(obj_at_addr(which), oop(s));
   162   }
   164   void string_at_put(int which, oop str) {
   165     oop_store((volatile oop*)obj_at_addr(which), str);
   166     release_tag_at_put(which, JVM_CONSTANT_String);
   167     if (UseConcMarkSweepGC) {
   168       // In case the earlier card-mark was consumed by a concurrent
   169       // marking thread before the tag was updated, redirty the card.
   170       oop_store_without_check((volatile oop *)obj_at_addr(which), str);
   171     }
   172   }
   174   // For temporary use while constructing constant pool
   175   void string_index_at_put(int which, int string_index) {
   176     tag_at_put(which, JVM_CONSTANT_StringIndex);
   177     *int_at_addr(which) = string_index;
   178   }
   180   void field_at_put(int which, int class_index, int name_and_type_index) {
   181     tag_at_put(which, JVM_CONSTANT_Fieldref);
   182     *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
   183   }
   185   void method_at_put(int which, int class_index, int name_and_type_index) {
   186     tag_at_put(which, JVM_CONSTANT_Methodref);
   187     *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
   188   }
   190   void interface_method_at_put(int which, int class_index, int name_and_type_index) {
   191     tag_at_put(which, JVM_CONSTANT_InterfaceMethodref);
   192     *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;  // Not so nice
   193   }
   195   void name_and_type_at_put(int which, int name_index, int signature_index) {
   196     tag_at_put(which, JVM_CONSTANT_NameAndType);
   197     *int_at_addr(which) = ((jint) signature_index<<16) | name_index;  // Not so nice
   198   }
   200   // Tag query
   202   constantTag tag_at(int which) const { return (constantTag)tags()->byte_at_acquire(which); }
   204   // Whether the entry is a pointer that must be GC'd.
   205   bool is_pointer_entry(int which) {
   206     constantTag tag = tag_at(which);
   207     return tag.is_klass() ||
   208       tag.is_unresolved_klass() ||
   209       tag.is_symbol() ||
   210       tag.is_unresolved_string() ||
   211       tag.is_string();
   212   }
   214   // Fetching constants
   216   klassOop klass_at(int which, TRAPS) {
   217     constantPoolHandle h_this(THREAD, this);
   218     return klass_at_impl(h_this, which, CHECK_NULL);
   219   }
   221   symbolOop klass_name_at(int which);  // Returns the name, w/o resolving.
   223   klassOop resolved_klass_at(int which) {  // Used by Compiler
   224     guarantee(tag_at(which).is_klass(), "Corrupted constant pool");
   225     // Must do an acquire here in case another thread resolved the klass
   226     // behind our back, lest we later load stale values thru the oop.
   227     return klassOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
   228   }
   230   // This method should only be used with a cpool lock or during parsing or gc
   231   symbolOop unresolved_klass_at(int which) {     // Temporary until actual use
   232     symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
   233     // check that the klass is still unresolved.
   234     assert(tag_at(which).is_unresolved_klass(), "Corrupted constant pool");
   235     return s;
   236   }
   238   // RedefineClasses() API support:
   239   symbolOop klass_at_noresolve(int which) { return klass_name_at(which); }
   241   jint int_at(int which) {
   242     assert(tag_at(which).is_int(), "Corrupted constant pool");
   243     return *int_at_addr(which);
   244   }
   246   jlong long_at(int which) {
   247     assert(tag_at(which).is_long(), "Corrupted constant pool");
   248     // return *long_at_addr(which);
   249     u8 tmp = Bytes::get_native_u8((address)&base()[which]);
   250     return *((jlong*)&tmp);
   251   }
   253   jfloat float_at(int which) {
   254     assert(tag_at(which).is_float(), "Corrupted constant pool");
   255     return *float_at_addr(which);
   256   }
   258   jdouble double_at(int which) {
   259     assert(tag_at(which).is_double(), "Corrupted constant pool");
   260     u8 tmp = Bytes::get_native_u8((address)&base()[which]);
   261     return *((jdouble*)&tmp);
   262   }
   264   symbolOop symbol_at(int which) {
   265     assert(tag_at(which).is_utf8(), "Corrupted constant pool");
   266     return symbolOop(*obj_at_addr(which));
   267   }
   269   oop string_at(int which, TRAPS) {
   270     constantPoolHandle h_this(THREAD, this);
   271     return string_at_impl(h_this, which, CHECK_NULL);
   272   }
   274   // only called when we are sure a string entry is already resolved (via an
   275   // earlier string_at call.
   276   oop resolved_string_at(int which) {
   277     assert(tag_at(which).is_string(), "Corrupted constant pool");
   278     // Must do an acquire here in case another thread resolved the klass
   279     // behind our back, lest we later load stale values thru the oop.
   280     return (oop)OrderAccess::load_ptr_acquire(obj_at_addr(which));
   281   }
   283   // This method should only be used with a cpool lock or during parsing or gc
   284   symbolOop unresolved_string_at(int which) {    // Temporary until actual use
   285     symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
   286     // check that the string is still unresolved.
   287     assert(tag_at(which).is_unresolved_string(), "Corrupted constant pool");
   288     return s;
   289   }
   291   // Returns an UTF8 for a CONSTANT_String entry at a given index.
   292   // UTF8 char* representation was chosen to avoid conversion of
   293   // java_lang_Strings at resolved entries into symbolOops
   294   // or vice versa.
   295   char* string_at_noresolve(int which);
   297   jint name_and_type_at(int which) {
   298     assert(tag_at(which).is_name_and_type(), "Corrupted constant pool");
   299     return *int_at_addr(which);
   300   }
   302   // The following methods (klass_ref_at, klass_ref_at_noresolve, name_ref_at,
   303   // signature_ref_at, klass_ref_index_at, name_and_type_ref_index_at,
   304   // name_ref_index_at, signature_ref_index_at) all expect constant pool indices
   305   // from the bytecodes to be passed in, which are actually potentially byte-swapped
   306   // contstant pool cache indices. See field_or_method_at.
   308   // Lookup for entries consisting of (klass_index, name_and_type index)
   309   klassOop klass_ref_at(int which, TRAPS);
   310   symbolOop klass_ref_at_noresolve(int which);
   311   symbolOop name_ref_at(int which);
   312   symbolOop signature_ref_at(int which);    // the type descriptor
   314   int klass_ref_index_at(int which);
   315   int name_and_type_ref_index_at(int which);
   317   // Lookup for entries consisting of (name_index, signature_index)
   318   int name_ref_index_at(int which);
   319   int signature_ref_index_at(int which);
   321   BasicType basic_type_for_signature_at(int which);
   323   // Resolve string constants (to prevent allocation during compilation)
   324   void resolve_string_constants(TRAPS) {
   325     constantPoolHandle h_this(THREAD, this);
   326     resolve_string_constants_impl(h_this, CHECK);
   327   }
   329   // Klass name matches name at offset
   330   bool klass_name_at_matches(instanceKlassHandle k, int which);
   332   // Sizing
   333   static int header_size()             { return sizeof(constantPoolOopDesc)/HeapWordSize; }
   334   static int object_size(int length)   { return align_object_size(header_size() + length); }
   335   int object_size()                    { return object_size(length()); }
   337   friend class constantPoolKlass;
   338   friend class ClassFileParser;
   339   friend class SystemDictionary;
   341   // Used by compiler to prevent classloading.
   342   static klassOop klass_at_if_loaded          (constantPoolHandle this_oop, int which);
   343   static klassOop klass_ref_at_if_loaded      (constantPoolHandle this_oop, int which);
   344   // Same as above - but does LinkResolving.
   345   static klassOop klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int which, TRAPS);
   347   // Routines currently used for annotations (only called by jvm.cpp) but which might be used in the
   348   // future by other Java code. These take constant pool indices rather than possibly-byte-swapped
   349   // constant pool cache indices as do the peer methods above.
   350   symbolOop uncached_name_ref_at(int which);
   351   symbolOop uncached_signature_ref_at(int which);
   352   int       uncached_klass_ref_index_at(int which);
   353   int       uncached_name_and_type_ref_index_at(int which);
   355   // Sharing
   356   int pre_resolve_shared_klasses(TRAPS);
   357   void shared_symbols_iterate(OopClosure* closure0);
   358   void shared_tags_iterate(OopClosure* closure0);
   359   void shared_strings_iterate(OopClosure* closure0);
   361   // Debugging
   362   const char* printable_name_at(int which) PRODUCT_RETURN0;
   364  private:
   366   // Takes either a constant pool cache index in possibly byte-swapped
   367   // byte order (which comes from the bytecodes after rewriting) or,
   368   // if "uncached" is true, a vanilla constant pool index
   369   jint field_or_method_at(int which, bool uncached) {
   370     int i = -1;
   371     if (uncached || cache() == NULL) {
   372       i = which;
   373     } else {
   374       // change byte-ordering and go via cache
   375       i = cache()->entry_at(Bytes::swap_u2(which))->constant_pool_index();
   376     }
   377     assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
   378     return *int_at_addr(i);
   379   }
   381   // Used while constructing constant pool (only by ClassFileParser)
   382   jint klass_index_at(int which) {
   383     assert(tag_at(which).is_klass_index(), "Corrupted constant pool");
   384     return *int_at_addr(which);
   385   }
   387   jint string_index_at(int which) {
   388     assert(tag_at(which).is_string_index(), "Corrupted constant pool");
   389     return *int_at_addr(which);
   390   }
   392   // Performs the LinkResolver checks
   393   static void verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle klass, TRAPS);
   395   // Implementation of methods that needs an exposed 'this' pointer, in order to
   396   // handle GC while executing the method
   397   static klassOop klass_at_impl(constantPoolHandle this_oop, int which, TRAPS);
   398   static oop string_at_impl(constantPoolHandle this_oop, int which, TRAPS);
   400   // Resolve string constants (to prevent allocation during compilation)
   401   static void resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS);
   403  public:
   404   // Merging constantPoolOop support:
   405   bool compare_entry_to(int index1, constantPoolHandle cp2, int index2, TRAPS);
   406   void copy_cp_to(int start_i, int end_i, constantPoolHandle to_cp, int to_i,
   407     TRAPS);
   408   void copy_entry_to(int from_i, constantPoolHandle to_cp, int to_i, TRAPS);
   409   int  find_matching_entry(int pattern_i, constantPoolHandle search_cp, TRAPS);
   410   int  orig_length() const                { return _orig_length; }
   411   void set_orig_length(int orig_length)   { _orig_length = orig_length; }
   414   // JVMTI accesss - GetConstantPool, RetransformClasses, ...
   415   friend class JvmtiConstantPoolReconstituter;
   417  private:
   418   jint cpool_entry_size(jint idx);
   419   jint hash_entries_to(SymbolHashMap *symmap, SymbolHashMap *classmap);
   421   // Copy cpool bytes into byte array.
   422   // Returns:
   423   //  int > 0, count of the raw cpool bytes that have been copied
   424   //        0, OutOfMemory error
   425   //       -1, Internal error
   426   int  copy_cpool_bytes(int cpool_size,
   427                         SymbolHashMap* tbl,
   428                         unsigned char *bytes);
   429 };
   431 class SymbolHashMapEntry : public CHeapObj {
   432  private:
   433   unsigned int        _hash;   // 32-bit hash for item
   434   SymbolHashMapEntry* _next;   // Next element in the linked list for this bucket
   435   symbolOop           _symbol; // 1-st part of the mapping: symbol => value
   436   u2                  _value;  // 2-nd part of the mapping: symbol => value
   438  public:
   439   unsigned   int hash() const             { return _hash;   }
   440   void       set_hash(unsigned int hash)  { _hash = hash;   }
   442   SymbolHashMapEntry* next() const        { return _next;   }
   443   void set_next(SymbolHashMapEntry* next) { _next = next;   }
   445   symbolOop  symbol() const               { return _symbol; }
   446   void       set_symbol(symbolOop sym)    { _symbol = sym;  }
   448   u2         value() const                {  return _value; }
   449   void       set_value(u2 value)          { _value = value; }
   451   SymbolHashMapEntry(unsigned int hash, symbolOop symbol, u2 value)
   452     : _hash(hash), _symbol(symbol), _value(value), _next(NULL) {}
   454 }; // End SymbolHashMapEntry class
   457 class SymbolHashMapBucket : public CHeapObj {
   459 private:
   460   SymbolHashMapEntry*    _entry;
   462 public:
   463   SymbolHashMapEntry* entry() const         {  return _entry; }
   464   void set_entry(SymbolHashMapEntry* entry) { _entry = entry; }
   465   void clear()                              { _entry = NULL;  }
   467 }; // End SymbolHashMapBucket class
   470 class SymbolHashMap: public CHeapObj {
   472  private:
   473   // Default number of entries in the table
   474   enum SymbolHashMap_Constants {
   475     _Def_HashMap_Size = 256
   476   };
   478   int                   _table_size;
   479   SymbolHashMapBucket*  _buckets;
   481   void initialize_table(int table_size) {
   482     _table_size = table_size;
   483     _buckets = NEW_C_HEAP_ARRAY(SymbolHashMapBucket, table_size);
   484     for (int index = 0; index < table_size; index++) {
   485       _buckets[index].clear();
   486     }
   487   }
   489  public:
   491   int table_size() const        { return _table_size; }
   493   SymbolHashMap()               { initialize_table(_Def_HashMap_Size); }
   494   SymbolHashMap(int table_size) { initialize_table(table_size); }
   496   // hash P(31) from Kernighan & Ritchie
   497   static unsigned int compute_hash(const char* str, int len) {
   498     unsigned int hash = 0;
   499     while (len-- > 0) {
   500       hash = 31*hash + (unsigned) *str;
   501       str++;
   502     }
   503     return hash;
   504   }
   506   SymbolHashMapEntry* bucket(int i) {
   507     return _buckets[i].entry();
   508   }
   510   void add_entry(symbolOop sym, u2 value);
   511   SymbolHashMapEntry* find_entry(symbolOop sym);
   513   u2 symbol_to_value(symbolOop sym) {
   514     SymbolHashMapEntry *entry = find_entry(sym);
   515     return (entry == NULL) ? 0 : entry->value();
   516   }
   518   ~SymbolHashMap() {
   519     SymbolHashMapEntry* next;
   520     for (int i = 0; i < _table_size; i++) {
   521       for (SymbolHashMapEntry* cur = bucket(i); cur != NULL; cur = next) {
   522         next = cur->next();
   523         delete(cur);
   524       }
   525     }
   526     delete _buckets;
   527   }
   528 }; // End SymbolHashMap class

mercurial