Mon, 09 Jun 2008 11:51:19 -0400
6687581: Make CMS work with compressed oops
Summary: Make FreeChunk read markword instead of LSB in _klass pointer to indicate that it's a FreeChunk for compressed oops.
Reviewed-by: ysr, jmasa
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 oopDesc {
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 int _length; // number of elements in the array
45 // only set to non-zero if constant pool is merged by RedefineClasses
46 int _orig_length;
48 void set_tags(typeArrayOop tags) { oop_store_without_check((oop*)&_tags, tags); }
49 void tag_at_put(int which, jbyte t) { tags()->byte_at_put(which, t); }
50 void release_tag_at_put(int which, jbyte t) { tags()->release_byte_at_put(which, t); }
52 private:
53 intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(constantPoolOopDesc)); }
54 oop* tags_addr() { return (oop*)&_tags; }
55 oop* cache_addr() { return (oop*)&_cache; }
57 oop* obj_at_addr(int which) const {
58 assert(is_within_bounds(which), "index out of bounds");
59 return (oop*) &base()[which];
60 }
62 jint* int_at_addr(int which) const {
63 assert(is_within_bounds(which), "index out of bounds");
64 return (jint*) &base()[which];
65 }
67 jlong* long_at_addr(int which) const {
68 assert(is_within_bounds(which), "index out of bounds");
69 return (jlong*) &base()[which];
70 }
72 jfloat* float_at_addr(int which) const {
73 assert(is_within_bounds(which), "index out of bounds");
74 return (jfloat*) &base()[which];
75 }
77 jdouble* double_at_addr(int which) const {
78 assert(is_within_bounds(which), "index out of bounds");
79 return (jdouble*) &base()[which];
80 }
82 public:
83 typeArrayOop tags() const { return _tags; }
85 // Klass holding pool
86 klassOop pool_holder() const { return _pool_holder; }
87 void set_pool_holder(klassOop k) { oop_store_without_check((oop*)&_pool_holder, (oop) k); }
88 oop* pool_holder_addr() { return (oop*)&_pool_holder; }
90 // Interpreter runtime support
91 constantPoolCacheOop cache() const { return _cache; }
92 void set_cache(constantPoolCacheOop cache){ oop_store((oop*)&_cache, cache); }
94 // Assembly code support
95 static int tags_offset_in_bytes() { return offset_of(constantPoolOopDesc, _tags); }
96 static int cache_offset_in_bytes() { return offset_of(constantPoolOopDesc, _cache); }
97 static int pool_holder_offset_in_bytes() { return offset_of(constantPoolOopDesc, _pool_holder); }
99 // Storing constants
101 void klass_at_put(int which, klassOop k) {
102 oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
103 // The interpreter assumes when the tag is stored, the klass is resolved
104 // and the klassOop is a klass rather than a symbolOop, so we need
105 // hardware store ordering here.
106 release_tag_at_put(which, JVM_CONSTANT_Class);
107 if (UseConcMarkSweepGC) {
108 // In case the earlier card-mark was consumed by a concurrent
109 // marking thread before the tag was updated, redirty the card.
110 oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
111 }
112 }
114 // For temporary use while constructing constant pool
115 void klass_index_at_put(int which, int name_index) {
116 tag_at_put(which, JVM_CONSTANT_ClassIndex);
117 *int_at_addr(which) = name_index;
118 }
120 // Temporary until actual use
121 void unresolved_klass_at_put(int which, symbolOop s) {
122 // Overwrite the old index with a GC friendly value so
123 // that if GC looks during the transition it won't try
124 // to treat a small integer as oop.
125 *obj_at_addr(which) = NULL;
126 release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass);
127 oop_store_without_check(obj_at_addr(which), oop(s));
128 }
130 // Temporary until actual use
131 void unresolved_string_at_put(int which, symbolOop s) {
132 *obj_at_addr(which) = NULL;
133 release_tag_at_put(which, JVM_CONSTANT_UnresolvedString);
134 oop_store_without_check(obj_at_addr(which), oop(s));
135 }
137 void int_at_put(int which, jint i) {
138 tag_at_put(which, JVM_CONSTANT_Integer);
139 *int_at_addr(which) = i;
140 }
142 void long_at_put(int which, jlong l) {
143 tag_at_put(which, JVM_CONSTANT_Long);
144 // *long_at_addr(which) = l;
145 Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l));
146 }
148 void float_at_put(int which, jfloat f) {
149 tag_at_put(which, JVM_CONSTANT_Float);
150 *float_at_addr(which) = f;
151 }
153 void double_at_put(int which, jdouble d) {
154 tag_at_put(which, JVM_CONSTANT_Double);
155 // *double_at_addr(which) = d;
156 // u8 temp = *(u8*) &d;
157 Bytes::put_native_u8((address) double_at_addr(which), *((u8*) &d));
158 }
160 void symbol_at_put(int which, symbolOop s) {
161 tag_at_put(which, JVM_CONSTANT_Utf8);
162 oop_store_without_check(obj_at_addr(which), oop(s));
163 }
165 void string_at_put(int which, oop str) {
166 oop_store((volatile oop*)obj_at_addr(which), str);
167 release_tag_at_put(which, JVM_CONSTANT_String);
168 if (UseConcMarkSweepGC) {
169 // In case the earlier card-mark was consumed by a concurrent
170 // marking thread before the tag was updated, redirty the card.
171 oop_store_without_check((volatile oop *)obj_at_addr(which), str);
172 }
173 }
175 // For temporary use while constructing constant pool
176 void string_index_at_put(int which, int string_index) {
177 tag_at_put(which, JVM_CONSTANT_StringIndex);
178 *int_at_addr(which) = string_index;
179 }
181 void field_at_put(int which, int class_index, int name_and_type_index) {
182 tag_at_put(which, JVM_CONSTANT_Fieldref);
183 *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
184 }
186 void method_at_put(int which, int class_index, int name_and_type_index) {
187 tag_at_put(which, JVM_CONSTANT_Methodref);
188 *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
189 }
191 void interface_method_at_put(int which, int class_index, int name_and_type_index) {
192 tag_at_put(which, JVM_CONSTANT_InterfaceMethodref);
193 *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index; // Not so nice
194 }
196 void name_and_type_at_put(int which, int name_index, int signature_index) {
197 tag_at_put(which, JVM_CONSTANT_NameAndType);
198 *int_at_addr(which) = ((jint) signature_index<<16) | name_index; // Not so nice
199 }
201 // Tag query
203 constantTag tag_at(int which) const { return (constantTag)tags()->byte_at_acquire(which); }
205 // Whether the entry is a pointer that must be GC'd.
206 bool is_pointer_entry(int which) {
207 constantTag tag = tag_at(which);
208 return tag.is_klass() ||
209 tag.is_unresolved_klass() ||
210 tag.is_symbol() ||
211 tag.is_unresolved_string() ||
212 tag.is_string();
213 }
215 // Fetching constants
217 klassOop klass_at(int which, TRAPS) {
218 constantPoolHandle h_this(THREAD, this);
219 return klass_at_impl(h_this, which, CHECK_NULL);
220 }
222 symbolOop klass_name_at(int which); // Returns the name, w/o resolving.
224 klassOop resolved_klass_at(int which) { // Used by Compiler
225 guarantee(tag_at(which).is_klass(), "Corrupted constant pool");
226 // Must do an acquire here in case another thread resolved the klass
227 // behind our back, lest we later load stale values thru the oop.
228 return klassOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
229 }
231 // This method should only be used with a cpool lock or during parsing or gc
232 symbolOop unresolved_klass_at(int which) { // Temporary until actual use
233 symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
234 // check that the klass is still unresolved.
235 assert(tag_at(which).is_unresolved_klass(), "Corrupted constant pool");
236 return s;
237 }
239 // RedefineClasses() API support:
240 symbolOop klass_at_noresolve(int which) { return klass_name_at(which); }
242 jint int_at(int which) {
243 assert(tag_at(which).is_int(), "Corrupted constant pool");
244 return *int_at_addr(which);
245 }
247 jlong long_at(int which) {
248 assert(tag_at(which).is_long(), "Corrupted constant pool");
249 // return *long_at_addr(which);
250 u8 tmp = Bytes::get_native_u8((address)&base()[which]);
251 return *((jlong*)&tmp);
252 }
254 jfloat float_at(int which) {
255 assert(tag_at(which).is_float(), "Corrupted constant pool");
256 return *float_at_addr(which);
257 }
259 jdouble double_at(int which) {
260 assert(tag_at(which).is_double(), "Corrupted constant pool");
261 u8 tmp = Bytes::get_native_u8((address)&base()[which]);
262 return *((jdouble*)&tmp);
263 }
265 symbolOop symbol_at(int which) {
266 assert(tag_at(which).is_utf8(), "Corrupted constant pool");
267 return symbolOop(*obj_at_addr(which));
268 }
270 oop string_at(int which, TRAPS) {
271 constantPoolHandle h_this(THREAD, this);
272 return string_at_impl(h_this, which, CHECK_NULL);
273 }
275 // only called when we are sure a string entry is already resolved (via an
276 // earlier string_at call.
277 oop resolved_string_at(int which) {
278 assert(tag_at(which).is_string(), "Corrupted constant pool");
279 // Must do an acquire here in case another thread resolved the klass
280 // behind our back, lest we later load stale values thru the oop.
281 return (oop)OrderAccess::load_ptr_acquire(obj_at_addr(which));
282 }
284 // This method should only be used with a cpool lock or during parsing or gc
285 symbolOop unresolved_string_at(int which) { // Temporary until actual use
286 symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
287 // check that the string is still unresolved.
288 assert(tag_at(which).is_unresolved_string(), "Corrupted constant pool");
289 return s;
290 }
292 // Returns an UTF8 for a CONSTANT_String entry at a given index.
293 // UTF8 char* representation was chosen to avoid conversion of
294 // java_lang_Strings at resolved entries into symbolOops
295 // or vice versa.
296 char* string_at_noresolve(int which);
298 jint name_and_type_at(int which) {
299 assert(tag_at(which).is_name_and_type(), "Corrupted constant pool");
300 return *int_at_addr(which);
301 }
303 // The following methods (klass_ref_at, klass_ref_at_noresolve, name_ref_at,
304 // signature_ref_at, klass_ref_index_at, name_and_type_ref_index_at,
305 // name_ref_index_at, signature_ref_index_at) all expect constant pool indices
306 // from the bytecodes to be passed in, which are actually potentially byte-swapped
307 // contstant pool cache indices. See field_or_method_at.
309 // Lookup for entries consisting of (klass_index, name_and_type index)
310 klassOop klass_ref_at(int which, TRAPS);
311 symbolOop klass_ref_at_noresolve(int which);
312 symbolOop name_ref_at(int which);
313 symbolOop signature_ref_at(int which); // the type descriptor
315 int klass_ref_index_at(int which);
316 int name_and_type_ref_index_at(int which);
318 // Lookup for entries consisting of (name_index, signature_index)
319 int name_ref_index_at(int which);
320 int signature_ref_index_at(int which);
322 BasicType basic_type_for_signature_at(int which);
324 // Resolve string constants (to prevent allocation during compilation)
325 void resolve_string_constants(TRAPS) {
326 constantPoolHandle h_this(THREAD, this);
327 resolve_string_constants_impl(h_this, CHECK);
328 }
330 // Klass name matches name at offset
331 bool klass_name_at_matches(instanceKlassHandle k, int which);
333 // Sizing
334 int length() const { return _length; }
335 void set_length(int length) { _length = length; }
337 // Tells whether index is within bounds.
338 bool is_within_bounds(int index) const {
339 return 0 <= index && index < length();
340 }
342 static int header_size() { return sizeof(constantPoolOopDesc)/HeapWordSize; }
343 static int object_size(int length) { return align_object_size(header_size() + length); }
344 int object_size() { return object_size(length()); }
346 friend class constantPoolKlass;
347 friend class ClassFileParser;
348 friend class SystemDictionary;
350 // Used by compiler to prevent classloading.
351 static klassOop klass_at_if_loaded (constantPoolHandle this_oop, int which);
352 static klassOop klass_ref_at_if_loaded (constantPoolHandle this_oop, int which);
353 // Same as above - but does LinkResolving.
354 static klassOop klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int which, TRAPS);
356 // Routines currently used for annotations (only called by jvm.cpp) but which might be used in the
357 // future by other Java code. These take constant pool indices rather than possibly-byte-swapped
358 // constant pool cache indices as do the peer methods above.
359 symbolOop uncached_name_ref_at(int which);
360 symbolOop uncached_signature_ref_at(int which);
361 int uncached_klass_ref_index_at(int which);
362 int uncached_name_and_type_ref_index_at(int which);
364 // Sharing
365 int pre_resolve_shared_klasses(TRAPS);
366 void shared_symbols_iterate(OopClosure* closure0);
367 void shared_tags_iterate(OopClosure* closure0);
368 void shared_strings_iterate(OopClosure* closure0);
370 // Debugging
371 const char* printable_name_at(int which) PRODUCT_RETURN0;
373 private:
375 // Takes either a constant pool cache index in possibly byte-swapped
376 // byte order (which comes from the bytecodes after rewriting) or,
377 // if "uncached" is true, a vanilla constant pool index
378 jint field_or_method_at(int which, bool uncached) {
379 int i = -1;
380 if (uncached || cache() == NULL) {
381 i = which;
382 } else {
383 // change byte-ordering and go via cache
384 i = cache()->entry_at(Bytes::swap_u2(which))->constant_pool_index();
385 }
386 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
387 return *int_at_addr(i);
388 }
390 // Used while constructing constant pool (only by ClassFileParser)
391 jint klass_index_at(int which) {
392 assert(tag_at(which).is_klass_index(), "Corrupted constant pool");
393 return *int_at_addr(which);
394 }
396 jint string_index_at(int which) {
397 assert(tag_at(which).is_string_index(), "Corrupted constant pool");
398 return *int_at_addr(which);
399 }
401 // Performs the LinkResolver checks
402 static void verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle klass, TRAPS);
404 // Implementation of methods that needs an exposed 'this' pointer, in order to
405 // handle GC while executing the method
406 static klassOop klass_at_impl(constantPoolHandle this_oop, int which, TRAPS);
407 static oop string_at_impl(constantPoolHandle this_oop, int which, TRAPS);
409 // Resolve string constants (to prevent allocation during compilation)
410 static void resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS);
412 public:
413 // Merging constantPoolOop support:
414 bool compare_entry_to(int index1, constantPoolHandle cp2, int index2, TRAPS);
415 void copy_cp_to(int start_i, int end_i, constantPoolHandle to_cp, int to_i,
416 TRAPS);
417 void copy_entry_to(int from_i, constantPoolHandle to_cp, int to_i, TRAPS);
418 int find_matching_entry(int pattern_i, constantPoolHandle search_cp, TRAPS);
419 int orig_length() const { return _orig_length; }
420 void set_orig_length(int orig_length) { _orig_length = orig_length; }
423 // JVMTI accesss - GetConstantPool, RetransformClasses, ...
424 friend class JvmtiConstantPoolReconstituter;
426 private:
427 jint cpool_entry_size(jint idx);
428 jint hash_entries_to(SymbolHashMap *symmap, SymbolHashMap *classmap);
430 // Copy cpool bytes into byte array.
431 // Returns:
432 // int > 0, count of the raw cpool bytes that have been copied
433 // 0, OutOfMemory error
434 // -1, Internal error
435 int copy_cpool_bytes(int cpool_size,
436 SymbolHashMap* tbl,
437 unsigned char *bytes);
438 };
440 class SymbolHashMapEntry : public CHeapObj {
441 private:
442 unsigned int _hash; // 32-bit hash for item
443 SymbolHashMapEntry* _next; // Next element in the linked list for this bucket
444 symbolOop _symbol; // 1-st part of the mapping: symbol => value
445 u2 _value; // 2-nd part of the mapping: symbol => value
447 public:
448 unsigned int hash() const { return _hash; }
449 void set_hash(unsigned int hash) { _hash = hash; }
451 SymbolHashMapEntry* next() const { return _next; }
452 void set_next(SymbolHashMapEntry* next) { _next = next; }
454 symbolOop symbol() const { return _symbol; }
455 void set_symbol(symbolOop sym) { _symbol = sym; }
457 u2 value() const { return _value; }
458 void set_value(u2 value) { _value = value; }
460 SymbolHashMapEntry(unsigned int hash, symbolOop symbol, u2 value)
461 : _hash(hash), _symbol(symbol), _value(value), _next(NULL) {}
463 }; // End SymbolHashMapEntry class
466 class SymbolHashMapBucket : public CHeapObj {
468 private:
469 SymbolHashMapEntry* _entry;
471 public:
472 SymbolHashMapEntry* entry() const { return _entry; }
473 void set_entry(SymbolHashMapEntry* entry) { _entry = entry; }
474 void clear() { _entry = NULL; }
476 }; // End SymbolHashMapBucket class
479 class SymbolHashMap: public CHeapObj {
481 private:
482 // Default number of entries in the table
483 enum SymbolHashMap_Constants {
484 _Def_HashMap_Size = 256
485 };
487 int _table_size;
488 SymbolHashMapBucket* _buckets;
490 void initialize_table(int table_size) {
491 _table_size = table_size;
492 _buckets = NEW_C_HEAP_ARRAY(SymbolHashMapBucket, table_size);
493 for (int index = 0; index < table_size; index++) {
494 _buckets[index].clear();
495 }
496 }
498 public:
500 int table_size() const { return _table_size; }
502 SymbolHashMap() { initialize_table(_Def_HashMap_Size); }
503 SymbolHashMap(int table_size) { initialize_table(table_size); }
505 // hash P(31) from Kernighan & Ritchie
506 static unsigned int compute_hash(const char* str, int len) {
507 unsigned int hash = 0;
508 while (len-- > 0) {
509 hash = 31*hash + (unsigned) *str;
510 str++;
511 }
512 return hash;
513 }
515 SymbolHashMapEntry* bucket(int i) {
516 return _buckets[i].entry();
517 }
519 void add_entry(symbolOop sym, u2 value);
520 SymbolHashMapEntry* find_entry(symbolOop sym);
522 u2 symbol_to_value(symbolOop sym) {
523 SymbolHashMapEntry *entry = find_entry(sym);
524 return (entry == NULL) ? 0 : entry->value();
525 }
527 ~SymbolHashMap() {
528 SymbolHashMapEntry* next;
529 for (int i = 0; i < _table_size; i++) {
530 for (SymbolHashMapEntry* cur = bucket(i); cur != NULL; cur = next) {
531 next = cur->next();
532 delete(cur);
533 }
534 }
535 delete _buckets;
536 }
537 }; // End SymbolHashMap class