Print this page
rev 6875 : 8056240: Investigate increased GC remark time after class unloading changes in CRM Fuse
Reviewed-by: mgerdin, coleenp, bdelsart
Split |
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/oops/constantPool.cpp
+++ new/src/share/vm/oops/constantPool.cpp
1 1 /*
2 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 *
23 23 */
24 24
25 25 #include "precompiled.hpp"
26 26 #include "classfile/classLoaderData.hpp"
27 27 #include "classfile/javaClasses.hpp"
28 28 #include "classfile/metadataOnStackMark.hpp"
29 29 #include "classfile/symbolTable.hpp"
30 30 #include "classfile/systemDictionary.hpp"
31 31 #include "classfile/vmSymbols.hpp"
32 32 #include "interpreter/linkResolver.hpp"
33 33 #include "memory/heapInspection.hpp"
34 34 #include "memory/metadataFactory.hpp"
35 35 #include "memory/oopFactory.hpp"
36 36 #include "oops/constantPool.hpp"
37 37 #include "oops/instanceKlass.hpp"
38 38 #include "oops/objArrayKlass.hpp"
39 39 #include "runtime/fieldType.hpp"
40 40 #include "runtime/init.hpp"
41 41 #include "runtime/javaCalls.hpp"
42 42 #include "runtime/signature.hpp"
43 43 #include "runtime/vframe.hpp"
44 44
45 45 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
46 46
47 47 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
48 48 // Tags are RW but comment below applies to tags also.
49 49 Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL);
50 50
51 51 int size = ConstantPool::size(length);
52 52
53 53 // CDS considerations:
54 54 // Allocate read-write but may be able to move to read-only at dumping time
55 55 // if all the klasses are resolved. The only other field that is writable is
56 56 // the resolved_references array, which is recreated at startup time.
57 57 // But that could be moved to InstanceKlass (although a pain to access from
58 58 // assembly code). Maybe it could be moved to the cpCache which is RW.
59 59 return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
60 60 }
61 61
62 62 ConstantPool::ConstantPool(Array<u1>* tags) {
63 63 set_length(tags->length());
64 64 set_tags(NULL);
65 65 set_cache(NULL);
66 66 set_reference_map(NULL);
67 67 set_resolved_references(NULL);
68 68 set_operands(NULL);
69 69 set_pool_holder(NULL);
70 70 set_flags(0);
71 71
72 72 // only set to non-zero if constant pool is merged by RedefineClasses
73 73 set_version(0);
74 74 set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
75 75
76 76 // initialize tag array
77 77 int length = tags->length();
78 78 for (int index = 0; index < length; index++) {
79 79 tags->at_put(index, JVM_CONSTANT_Invalid);
80 80 }
81 81 set_tags(tags);
82 82 }
83 83
84 84 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
85 85 MetadataFactory::free_metadata(loader_data, cache());
86 86 set_cache(NULL);
87 87 MetadataFactory::free_array<u2>(loader_data, reference_map());
88 88 set_reference_map(NULL);
89 89
90 90 MetadataFactory::free_array<jushort>(loader_data, operands());
91 91 set_operands(NULL);
92 92
93 93 release_C_heap_structures();
94 94
95 95 // free tag array
96 96 MetadataFactory::free_array<u1>(loader_data, tags());
97 97 set_tags(NULL);
98 98 }
99 99
100 100 void ConstantPool::release_C_heap_structures() {
101 101 // walk constant pool and decrement symbol reference counts
102 102 unreference_symbols();
103 103
104 104 delete _lock;
105 105 set_lock(NULL);
106 106 }
107 107
108 108 objArrayOop ConstantPool::resolved_references() const {
109 109 return (objArrayOop)JNIHandles::resolve(_resolved_references);
110 110 }
111 111
112 112 // Create resolved_references array and mapping array for original cp indexes
113 113 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
114 114 // to map it back for resolving and some unlikely miscellaneous uses.
115 115 // The objects created by invokedynamic are appended to this list.
116 116 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
117 117 intStack reference_map,
118 118 int constant_pool_map_length,
119 119 TRAPS) {
120 120 // Initialized the resolved object cache.
121 121 int map_length = reference_map.length();
122 122 if (map_length > 0) {
123 123 // Only need mapping back to constant pool entries. The map isn't used for
124 124 // invokedynamic resolved_reference entries. For invokedynamic entries,
125 125 // the constant pool cache index has the mapping back to both the constant
126 126 // pool and to the resolved reference index.
127 127 if (constant_pool_map_length > 0) {
128 128 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
129 129
130 130 for (int i = 0; i < constant_pool_map_length; i++) {
131 131 int x = reference_map.at(i);
132 132 assert(x == (int)(jushort) x, "klass index is too big");
133 133 om->at_put(i, (jushort)x);
134 134 }
135 135 set_reference_map(om);
136 136 }
137 137
138 138 // Create Java array for holding resolved strings, methodHandles,
139 139 // methodTypes, invokedynamic and invokehandle appendix objects, etc.
140 140 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
141 141 Handle refs_handle (THREAD, (oop)stom); // must handleize.
142 142 set_resolved_references(loader_data->add_handle(refs_handle));
143 143 }
144 144 }
145 145
146 146 // CDS support. Create a new resolved_references array.
147 147 void ConstantPool::restore_unshareable_info(TRAPS) {
148 148
149 149 // Only create the new resolved references array and lock if it hasn't been
150 150 // attempted before
151 151 if (resolved_references() != NULL) return;
152 152
153 153 // restore the C++ vtable from the shared archive
154 154 restore_vtable();
155 155
156 156 if (SystemDictionary::Object_klass_loaded()) {
157 157 // Recreate the object array and add to ClassLoaderData.
158 158 int map_length = resolved_reference_length();
159 159 if (map_length > 0) {
160 160 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
161 161 Handle refs_handle (THREAD, (oop)stom); // must handleize.
162 162
163 163 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
164 164 set_resolved_references(loader_data->add_handle(refs_handle));
165 165 }
166 166
167 167 // Also need to recreate the mutex. Make sure this matches the constructor
168 168 set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
169 169 }
170 170 }
171 171
172 172 void ConstantPool::remove_unshareable_info() {
173 173 // Resolved references are not in the shared archive.
174 174 // Save the length for restoration. It is not necessarily the same length
175 175 // as reference_map.length() if invokedynamic is saved.
176 176 set_resolved_reference_length(
177 177 resolved_references() != NULL ? resolved_references()->length() : 0);
178 178 set_resolved_references(NULL);
179 179 set_lock(NULL);
180 180 }
181 181
182 182 int ConstantPool::cp_to_object_index(int cp_index) {
183 183 // this is harder don't do this so much.
184 184 int i = reference_map()->find(cp_index);
185 185 // We might not find the index for jsr292 call.
186 186 return (i < 0) ? _no_index_sentinel : i;
187 187 }
188 188
189 189 Klass* ConstantPool::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
190 190 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
191 191 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
192 192 // tag is not updated atomicly.
193 193
194 194 CPSlot entry = this_oop->slot_at(which);
195 195 if (entry.is_resolved()) {
196 196 assert(entry.get_klass()->is_klass(), "must be");
197 197 // Already resolved - return entry.
198 198 return entry.get_klass();
199 199 }
200 200
201 201 // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
202 202 // already has updated the object
203 203 assert(THREAD->is_Java_thread(), "must be a Java thread");
204 204 bool do_resolve = false;
205 205 bool in_error = false;
206 206
207 207 // Create a handle for the mirror. This will preserve the resolved class
208 208 // until the loader_data is registered.
209 209 Handle mirror_handle;
210 210
211 211 Symbol* name = NULL;
212 212 Handle loader;
213 213 { MonitorLockerEx ml(this_oop->lock());
214 214
215 215 if (this_oop->tag_at(which).is_unresolved_klass()) {
216 216 if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
217 217 in_error = true;
218 218 } else {
219 219 do_resolve = true;
220 220 name = this_oop->unresolved_klass_at(which);
221 221 loader = Handle(THREAD, this_oop->pool_holder()->class_loader());
222 222 }
223 223 }
224 224 } // unlocking constantPool
225 225
226 226
227 227 // The original attempt to resolve this constant pool entry failed so find the
228 228 // original error and throw it again (JVMS 5.4.3).
229 229 if (in_error) {
230 230 Symbol* error = SystemDictionary::find_resolution_error(this_oop, which);
231 231 guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
232 232 ResourceMark rm;
233 233 // exception text will be the class name
234 234 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
235 235 THROW_MSG_0(error, className);
236 236 }
237 237
238 238 if (do_resolve) {
239 239 // this_oop must be unlocked during resolve_or_fail
240 240 oop protection_domain = this_oop->pool_holder()->protection_domain();
241 241 Handle h_prot (THREAD, protection_domain);
242 242 Klass* k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
243 243 KlassHandle k;
244 244 if (!HAS_PENDING_EXCEPTION) {
245 245 k = KlassHandle(THREAD, k_oop);
246 246 // preserve the resolved klass.
247 247 mirror_handle = Handle(THREAD, k_oop->java_mirror());
248 248 // Do access check for klasses
249 249 verify_constant_pool_resolve(this_oop, k, THREAD);
250 250 }
251 251
252 252 // Failed to resolve class. We must record the errors so that subsequent attempts
253 253 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
254 254 if (HAS_PENDING_EXCEPTION) {
255 255 ResourceMark rm;
256 256 Symbol* error = PENDING_EXCEPTION->klass()->name();
257 257
258 258 bool throw_orig_error = false;
259 259 {
260 260 MonitorLockerEx ml(this_oop->lock());
261 261
262 262 // some other thread has beaten us and has resolved the class.
263 263 if (this_oop->tag_at(which).is_klass()) {
264 264 CLEAR_PENDING_EXCEPTION;
265 265 entry = this_oop->resolved_klass_at(which);
266 266 return entry.get_klass();
267 267 }
268 268
269 269 if (!PENDING_EXCEPTION->
270 270 is_a(SystemDictionary::LinkageError_klass())) {
271 271 // Just throw the exception and don't prevent these classes from
272 272 // being loaded due to virtual machine errors like StackOverflow
273 273 // and OutOfMemoryError, etc, or if the thread was hit by stop()
274 274 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
275 275 }
276 276 else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
277 277 SystemDictionary::add_resolution_error(this_oop, which, error);
278 278 this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
279 279 } else {
280 280 // some other thread has put the class in error state.
281 281 error = SystemDictionary::find_resolution_error(this_oop, which);
282 282 assert(error != NULL, "checking");
283 283 throw_orig_error = true;
284 284 }
285 285 } // unlocked
286 286
287 287 if (throw_orig_error) {
288 288 CLEAR_PENDING_EXCEPTION;
289 289 ResourceMark rm;
290 290 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
291 291 THROW_MSG_0(error, className);
292 292 }
293 293
294 294 return 0;
295 295 }
296 296
297 297 if (TraceClassResolution && !k()->oop_is_array()) {
298 298 // skip resolving the constant pool so that this code get's
299 299 // called the next time some bytecodes refer to this class.
300 300 ResourceMark rm;
301 301 int line_number = -1;
302 302 const char * source_file = NULL;
303 303 if (JavaThread::current()->has_last_Java_frame()) {
304 304 // try to identify the method which called this function.
305 305 vframeStream vfst(JavaThread::current());
306 306 if (!vfst.at_end()) {
307 307 line_number = vfst.method()->line_number_from_bci(vfst.bci());
308 308 Symbol* s = vfst.method()->method_holder()->source_file_name();
309 309 if (s != NULL) {
310 310 source_file = s->as_C_string();
311 311 }
312 312 }
313 313 }
314 314 if (k() != this_oop->pool_holder()) {
315 315 // only print something if the classes are different
316 316 if (source_file != NULL) {
317 317 tty->print("RESOLVE %s %s %s:%d\n",
318 318 this_oop->pool_holder()->external_name(),
319 319 InstanceKlass::cast(k())->external_name(), source_file, line_number);
320 320 } else {
321 321 tty->print("RESOLVE %s %s\n",
322 322 this_oop->pool_holder()->external_name(),
323 323 InstanceKlass::cast(k())->external_name());
324 324 }
325 325 }
326 326 return k();
327 327 } else {
328 328 MonitorLockerEx ml(this_oop->lock());
329 329 // Only updated constant pool - if it is resolved.
330 330 do_resolve = this_oop->tag_at(which).is_unresolved_klass();
331 331 if (do_resolve) {
332 332 ClassLoaderData* this_key = this_oop->pool_holder()->class_loader_data();
333 333 this_key->record_dependency(k(), CHECK_NULL); // Can throw OOM
334 334 this_oop->klass_at_put(which, k());
335 335 }
336 336 }
337 337 }
338 338
339 339 entry = this_oop->resolved_klass_at(which);
340 340 assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point");
341 341 return entry.get_klass();
342 342 }
343 343
344 344
345 345 // Does not update ConstantPool* - to avoid any exception throwing. Used
346 346 // by compiler and exception handling. Also used to avoid classloads for
347 347 // instanceof operations. Returns NULL if the class has not been loaded or
348 348 // if the verification of constant pool failed
349 349 Klass* ConstantPool::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
350 350 CPSlot entry = this_oop->slot_at(which);
351 351 if (entry.is_resolved()) {
352 352 assert(entry.get_klass()->is_klass(), "must be");
353 353 return entry.get_klass();
354 354 } else {
355 355 assert(entry.is_unresolved(), "must be either symbol or klass");
356 356 Thread *thread = Thread::current();
357 357 Symbol* name = entry.get_symbol();
358 358 oop loader = this_oop->pool_holder()->class_loader();
359 359 oop protection_domain = this_oop->pool_holder()->protection_domain();
360 360 Handle h_prot (thread, protection_domain);
361 361 Handle h_loader (thread, loader);
362 362 Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
363 363
364 364 if (k != NULL) {
365 365 // Make sure that resolving is legal
366 366 EXCEPTION_MARK;
367 367 KlassHandle klass(THREAD, k);
368 368 // return NULL if verification fails
369 369 verify_constant_pool_resolve(this_oop, klass, THREAD);
370 370 if (HAS_PENDING_EXCEPTION) {
371 371 CLEAR_PENDING_EXCEPTION;
372 372 return NULL;
373 373 }
374 374 return klass();
375 375 } else {
376 376 return k;
377 377 }
378 378 }
379 379 }
380 380
381 381
382 382 Klass* ConstantPool::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
383 383 return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
384 384 }
385 385
386 386
387 387 Method* ConstantPool::method_at_if_loaded(constantPoolHandle cpool,
388 388 int which) {
389 389 if (cpool->cache() == NULL) return NULL; // nothing to load yet
390 390 int cache_index = decode_cpcache_index(which, true);
391 391 if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
392 392 // FIXME: should be an assert
393 393 if (PrintMiscellaneous && (Verbose||WizardMode)) {
394 394 tty->print_cr("bad operand %d in:", which); cpool->print();
395 395 }
396 396 return NULL;
397 397 }
398 398 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
399 399 return e->method_if_resolved(cpool);
400 400 }
401 401
402 402
403 403 bool ConstantPool::has_appendix_at_if_loaded(constantPoolHandle cpool, int which) {
404 404 if (cpool->cache() == NULL) return false; // nothing to load yet
405 405 int cache_index = decode_cpcache_index(which, true);
406 406 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
407 407 return e->has_appendix();
408 408 }
409 409
410 410 oop ConstantPool::appendix_at_if_loaded(constantPoolHandle cpool, int which) {
411 411 if (cpool->cache() == NULL) return NULL; // nothing to load yet
412 412 int cache_index = decode_cpcache_index(which, true);
413 413 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
414 414 return e->appendix_if_resolved(cpool);
415 415 }
416 416
417 417
418 418 bool ConstantPool::has_method_type_at_if_loaded(constantPoolHandle cpool, int which) {
419 419 if (cpool->cache() == NULL) return false; // nothing to load yet
420 420 int cache_index = decode_cpcache_index(which, true);
421 421 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
422 422 return e->has_method_type();
423 423 }
424 424
425 425 oop ConstantPool::method_type_at_if_loaded(constantPoolHandle cpool, int which) {
426 426 if (cpool->cache() == NULL) return NULL; // nothing to load yet
427 427 int cache_index = decode_cpcache_index(which, true);
428 428 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
429 429 return e->method_type_if_resolved(cpool);
430 430 }
431 431
432 432
433 433 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) {
434 434 int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
435 435 return symbol_at(name_index);
436 436 }
437 437
438 438
439 439 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) {
440 440 int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
441 441 return symbol_at(signature_index);
442 442 }
443 443
444 444
445 445 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) {
446 446 int i = which;
447 447 if (!uncached && cache() != NULL) {
448 448 if (ConstantPool::is_invokedynamic_index(which)) {
449 449 // Invokedynamic index is index into resolved_references
450 450 int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
451 451 pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
452 452 assert(tag_at(pool_index).is_name_and_type(), "");
453 453 return pool_index;
454 454 }
455 455 // change byte-ordering and go via cache
456 456 i = remap_instruction_operand_from_cache(which);
457 457 } else {
458 458 if (tag_at(which).is_invoke_dynamic()) {
459 459 int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
460 460 assert(tag_at(pool_index).is_name_and_type(), "");
461 461 return pool_index;
462 462 }
463 463 }
464 464 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
465 465 assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
466 466 jint ref_index = *int_at_addr(i);
467 467 return extract_high_short_from_int(ref_index);
468 468 }
469 469
470 470
471 471 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) {
472 472 guarantee(!ConstantPool::is_invokedynamic_index(which),
473 473 "an invokedynamic instruction does not have a klass");
474 474 int i = which;
475 475 if (!uncached && cache() != NULL) {
476 476 // change byte-ordering and go via cache
477 477 i = remap_instruction_operand_from_cache(which);
478 478 }
479 479 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
480 480 jint ref_index = *int_at_addr(i);
481 481 return extract_low_short_from_int(ref_index);
482 482 }
483 483
484 484
485 485
486 486 int ConstantPool::remap_instruction_operand_from_cache(int operand) {
487 487 int cpc_index = operand;
488 488 DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
489 489 assert((int)(u2)cpc_index == cpc_index, "clean u2");
490 490 int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
491 491 return member_index;
492 492 }
493 493
494 494
495 495 void ConstantPool::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
496 496 if (k->oop_is_instance() || k->oop_is_objArray()) {
497 497 instanceKlassHandle holder (THREAD, this_oop->pool_holder());
498 498 Klass* elem_oop = k->oop_is_instance() ? k() : ObjArrayKlass::cast(k())->bottom_klass();
499 499 KlassHandle element (THREAD, elem_oop);
500 500
501 501 // The element type could be a typeArray - we only need the access check if it is
502 502 // an reference to another class
503 503 if (element->oop_is_instance()) {
504 504 LinkResolver::check_klass_accessability(holder, element, CHECK);
505 505 }
506 506 }
507 507 }
508 508
509 509
510 510 int ConstantPool::name_ref_index_at(int which_nt) {
511 511 jint ref_index = name_and_type_at(which_nt);
512 512 return extract_low_short_from_int(ref_index);
513 513 }
514 514
515 515
516 516 int ConstantPool::signature_ref_index_at(int which_nt) {
517 517 jint ref_index = name_and_type_at(which_nt);
518 518 return extract_high_short_from_int(ref_index);
519 519 }
520 520
521 521
522 522 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
523 523 return klass_at(klass_ref_index_at(which), CHECK_NULL);
524 524 }
525 525
526 526
527 527 Symbol* ConstantPool::klass_name_at(int which) {
528 528 assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
529 529 "Corrupted constant pool");
530 530 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
531 531 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
532 532 // tag is not updated atomicly.
533 533 CPSlot entry = slot_at(which);
534 534 if (entry.is_resolved()) {
535 535 // Already resolved - return entry's name.
536 536 assert(entry.get_klass()->is_klass(), "must be");
537 537 return entry.get_klass()->name();
538 538 } else {
539 539 assert(entry.is_unresolved(), "must be either symbol or klass");
540 540 return entry.get_symbol();
541 541 }
542 542 }
543 543
544 544 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
545 545 jint ref_index = klass_ref_index_at(which);
546 546 return klass_at_noresolve(ref_index);
547 547 }
548 548
549 549 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
550 550 jint ref_index = uncached_klass_ref_index_at(which);
551 551 return klass_at_noresolve(ref_index);
552 552 }
553 553
554 554 char* ConstantPool::string_at_noresolve(int which) {
555 555 Symbol* s = unresolved_string_at(which);
556 556 if (s == NULL) {
557 557 return (char*)"<pseudo-string>";
558 558 } else {
559 559 return unresolved_string_at(which)->as_C_string();
560 560 }
561 561 }
562 562
563 563 BasicType ConstantPool::basic_type_for_signature_at(int which) {
564 564 return FieldType::basic_type(symbol_at(which));
565 565 }
566 566
567 567
568 568 void ConstantPool::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
569 569 for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
570 570 if (this_oop->tag_at(index).is_string()) {
571 571 this_oop->string_at(index, CHECK);
572 572 }
573 573 }
574 574 }
575 575
576 576 // Resolve all the classes in the constant pool. If they are all resolved,
577 577 // the constant pool is read-only. Enhancement: allocate cp entries to
578 578 // another metaspace, and copy to read-only or read-write space if this
579 579 // bit is set.
580 580 bool ConstantPool::resolve_class_constants(TRAPS) {
581 581 constantPoolHandle cp(THREAD, this);
582 582 for (int index = 1; index < length(); index++) { // Index 0 is unused
583 583 if (tag_at(index).is_unresolved_klass() &&
584 584 klass_at_if_loaded(cp, index) == NULL) {
585 585 return false;
586 586 }
587 587 }
588 588 // set_preresolution(); or some bit for future use
589 589 return true;
590 590 }
591 591
592 592 // If resolution for MethodHandle or MethodType fails, save the exception
593 593 // in the resolution error table, so that the same exception is thrown again.
594 594 void ConstantPool::save_and_throw_exception(constantPoolHandle this_oop, int which,
595 595 int tag, TRAPS) {
596 596 ResourceMark rm;
597 597 Symbol* error = PENDING_EXCEPTION->klass()->name();
598 598 MonitorLockerEx ml(this_oop->lock()); // lock cpool to change tag.
599 599
600 600 int error_tag = (tag == JVM_CONSTANT_MethodHandle) ?
601 601 JVM_CONSTANT_MethodHandleInError : JVM_CONSTANT_MethodTypeInError;
602 602
603 603 if (!PENDING_EXCEPTION->
604 604 is_a(SystemDictionary::LinkageError_klass())) {
605 605 // Just throw the exception and don't prevent these classes from
606 606 // being loaded due to virtual machine errors like StackOverflow
607 607 // and OutOfMemoryError, etc, or if the thread was hit by stop()
608 608 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
609 609
610 610 } else if (this_oop->tag_at(which).value() != error_tag) {
611 611 SystemDictionary::add_resolution_error(this_oop, which, error);
612 612 this_oop->tag_at_put(which, error_tag);
613 613 } else {
614 614 // some other thread has put the class in error state.
615 615 error = SystemDictionary::find_resolution_error(this_oop, which);
616 616 assert(error != NULL, "checking");
617 617 CLEAR_PENDING_EXCEPTION;
618 618 THROW_MSG(error, "");
619 619 }
620 620 }
621 621
622 622
623 623 // Called to resolve constants in the constant pool and return an oop.
624 624 // Some constant pool entries cache their resolved oop. This is also
625 625 // called to create oops from constants to use in arguments for invokedynamic
626 626 oop ConstantPool::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
627 627 oop result_oop = NULL;
628 628 Handle throw_exception;
629 629
630 630 if (cache_index == _possible_index_sentinel) {
631 631 // It is possible that this constant is one which is cached in the objects.
632 632 // We'll do a linear search. This should be OK because this usage is rare.
633 633 assert(index > 0, "valid index");
634 634 cache_index = this_oop->cp_to_object_index(index);
635 635 }
636 636 assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
637 637 assert(index == _no_index_sentinel || index >= 0, "");
638 638
639 639 if (cache_index >= 0) {
640 640 result_oop = this_oop->resolved_references()->obj_at(cache_index);
641 641 if (result_oop != NULL) {
642 642 return result_oop;
643 643 // That was easy...
644 644 }
645 645 index = this_oop->object_to_cp_index(cache_index);
646 646 }
647 647
648 648 jvalue prim_value; // temp used only in a few cases below
649 649
650 650 int tag_value = this_oop->tag_at(index).value();
651 651
652 652 switch (tag_value) {
653 653
654 654 case JVM_CONSTANT_UnresolvedClass:
655 655 case JVM_CONSTANT_UnresolvedClassInError:
656 656 case JVM_CONSTANT_Class:
657 657 {
658 658 assert(cache_index == _no_index_sentinel, "should not have been set");
659 659 Klass* resolved = klass_at_impl(this_oop, index, CHECK_NULL);
660 660 // ldc wants the java mirror.
661 661 result_oop = resolved->java_mirror();
662 662 break;
663 663 }
664 664
665 665 case JVM_CONSTANT_String:
666 666 assert(cache_index != _no_index_sentinel, "should have been set");
667 667 if (this_oop->is_pseudo_string_at(index)) {
668 668 result_oop = this_oop->pseudo_string_at(index, cache_index);
669 669 break;
670 670 }
671 671 result_oop = string_at_impl(this_oop, index, cache_index, CHECK_NULL);
672 672 break;
673 673
674 674 case JVM_CONSTANT_MethodHandleInError:
675 675 case JVM_CONSTANT_MethodTypeInError:
676 676 {
677 677 Symbol* error = SystemDictionary::find_resolution_error(this_oop, index);
678 678 guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
679 679 ResourceMark rm;
680 680 THROW_MSG_0(error, "");
681 681 break;
682 682 }
683 683
684 684 case JVM_CONSTANT_MethodHandle:
685 685 {
686 686 int ref_kind = this_oop->method_handle_ref_kind_at(index);
687 687 int callee_index = this_oop->method_handle_klass_index_at(index);
688 688 Symbol* name = this_oop->method_handle_name_ref_at(index);
689 689 Symbol* signature = this_oop->method_handle_signature_ref_at(index);
690 690 if (PrintMiscellaneous)
691 691 tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
692 692 ref_kind, index, this_oop->method_handle_index_at(index),
693 693 callee_index, name->as_C_string(), signature->as_C_string());
694 694 KlassHandle callee;
695 695 { Klass* k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
696 696 callee = KlassHandle(THREAD, k);
697 697 }
698 698 KlassHandle klass(THREAD, this_oop->pool_holder());
699 699 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
700 700 callee, name, signature,
701 701 THREAD);
702 702 result_oop = value();
703 703 if (HAS_PENDING_EXCEPTION) {
704 704 save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL);
705 705 }
706 706 break;
707 707 }
708 708
709 709 case JVM_CONSTANT_MethodType:
710 710 {
711 711 Symbol* signature = this_oop->method_type_signature_at(index);
712 712 if (PrintMiscellaneous)
713 713 tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
714 714 index, this_oop->method_type_index_at(index),
715 715 signature->as_C_string());
716 716 KlassHandle klass(THREAD, this_oop->pool_holder());
717 717 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
718 718 result_oop = value();
719 719 if (HAS_PENDING_EXCEPTION) {
720 720 save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL);
721 721 }
722 722 break;
723 723 }
724 724
725 725 case JVM_CONSTANT_Integer:
726 726 assert(cache_index == _no_index_sentinel, "should not have been set");
727 727 prim_value.i = this_oop->int_at(index);
728 728 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
729 729 break;
730 730
731 731 case JVM_CONSTANT_Float:
732 732 assert(cache_index == _no_index_sentinel, "should not have been set");
733 733 prim_value.f = this_oop->float_at(index);
734 734 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
735 735 break;
736 736
737 737 case JVM_CONSTANT_Long:
738 738 assert(cache_index == _no_index_sentinel, "should not have been set");
739 739 prim_value.j = this_oop->long_at(index);
740 740 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
741 741 break;
742 742
743 743 case JVM_CONSTANT_Double:
744 744 assert(cache_index == _no_index_sentinel, "should not have been set");
745 745 prim_value.d = this_oop->double_at(index);
746 746 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
747 747 break;
748 748
749 749 default:
750 750 DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
751 751 this_oop(), index, cache_index, tag_value) );
752 752 assert(false, "unexpected constant tag");
753 753 break;
754 754 }
755 755
756 756 if (cache_index >= 0) {
757 757 // Cache the oop here also.
758 758 Handle result_handle(THREAD, result_oop);
759 759 MonitorLockerEx ml(this_oop->lock()); // don't know if we really need this
760 760 oop result = this_oop->resolved_references()->obj_at(cache_index);
761 761 // Benign race condition: resolved_references may already be filled in while we were trying to lock.
762 762 // The important thing here is that all threads pick up the same result.
763 763 // It doesn't matter which racing thread wins, as long as only one
764 764 // result is used by all threads, and all future queries.
765 765 // That result may be either a resolved constant or a failure exception.
766 766 if (result == NULL) {
767 767 this_oop->resolved_references()->obj_at_put(cache_index, result_handle());
768 768 return result_handle();
769 769 } else {
770 770 // Return the winning thread's result. This can be different than
771 771 // result_handle() for MethodHandles.
772 772 return result;
773 773 }
774 774 } else {
775 775 return result_oop;
776 776 }
777 777 }
778 778
779 779 oop ConstantPool::uncached_string_at(int which, TRAPS) {
780 780 Symbol* sym = unresolved_string_at(which);
781 781 oop str = StringTable::intern(sym, CHECK_(NULL));
782 782 assert(java_lang_String::is_instance(str), "must be string");
783 783 return str;
784 784 }
785 785
786 786
787 787 oop ConstantPool::resolve_bootstrap_specifier_at_impl(constantPoolHandle this_oop, int index, TRAPS) {
788 788 assert(this_oop->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool");
789 789
790 790 Handle bsm;
791 791 int argc;
792 792 {
793 793 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments
794 794 // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry.
795 795 // It is accompanied by the optional arguments.
796 796 int bsm_index = this_oop->invoke_dynamic_bootstrap_method_ref_index_at(index);
797 797 oop bsm_oop = this_oop->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL);
798 798 if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) {
799 799 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle");
800 800 }
801 801
802 802 // Extract the optional static arguments.
803 803 argc = this_oop->invoke_dynamic_argument_count_at(index);
804 804 if (argc == 0) return bsm_oop;
805 805
806 806 bsm = Handle(THREAD, bsm_oop);
807 807 }
808 808
809 809 objArrayHandle info;
810 810 {
811 811 objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL);
812 812 info = objArrayHandle(THREAD, info_oop);
813 813 }
814 814
815 815 info->obj_at_put(0, bsm());
816 816 for (int i = 0; i < argc; i++) {
817 817 int arg_index = this_oop->invoke_dynamic_argument_index_at(index, i);
818 818 oop arg_oop = this_oop->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL);
819 819 info->obj_at_put(1+i, arg_oop);
820 820 }
821 821
822 822 return info();
823 823 }
824 824
825 825 oop ConstantPool::string_at_impl(constantPoolHandle this_oop, int which, int obj_index, TRAPS) {
826 826 // If the string has already been interned, this entry will be non-null
827 827 oop str = this_oop->resolved_references()->obj_at(obj_index);
828 828 if (str != NULL) return str;
829 829 Symbol* sym = this_oop->unresolved_string_at(which);
830 830 str = StringTable::intern(sym, CHECK_(NULL));
831 831 this_oop->string_at_put(which, obj_index, str);
832 832 assert(java_lang_String::is_instance(str), "must be string");
833 833 return str;
834 834 }
835 835
836 836
837 837 bool ConstantPool::klass_name_at_matches(instanceKlassHandle k,
838 838 int which) {
839 839 // Names are interned, so we can compare Symbol*s directly
840 840 Symbol* cp_name = klass_name_at(which);
841 841 return (cp_name == k->name());
842 842 }
843 843
844 844
845 845 // Iterate over symbols and decrement ones which are Symbol*s.
846 846 // This is done during GC so do not need to lock constantPool unless we
847 847 // have per-thread safepoints.
848 848 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
849 849 // these symbols but didn't increment the reference count.
850 850 void ConstantPool::unreference_symbols() {
851 851 for (int index = 1; index < length(); index++) { // Index 0 is unused
852 852 constantTag tag = tag_at(index);
853 853 if (tag.is_symbol()) {
854 854 symbol_at(index)->decrement_refcount();
855 855 }
856 856 }
857 857 }
858 858
859 859
860 860 // Compare this constant pool's entry at index1 to the constant pool
861 861 // cp2's entry at index2.
862 862 bool ConstantPool::compare_entry_to(int index1, constantPoolHandle cp2,
863 863 int index2, TRAPS) {
864 864
865 865 // The error tags are equivalent to non-error tags when comparing
866 866 jbyte t1 = tag_at(index1).non_error_value();
867 867 jbyte t2 = cp2->tag_at(index2).non_error_value();
868 868
869 869 if (t1 != t2) {
870 870 // Not the same entry type so there is nothing else to check. Note
871 871 // that this style of checking will consider resolved/unresolved
872 872 // class pairs as different.
873 873 // From the ConstantPool* API point of view, this is correct
874 874 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
875 875 // plays out in the context of ConstantPool* merging.
876 876 return false;
877 877 }
878 878
879 879 switch (t1) {
880 880 case JVM_CONSTANT_Class:
881 881 {
882 882 Klass* k1 = klass_at(index1, CHECK_false);
883 883 Klass* k2 = cp2->klass_at(index2, CHECK_false);
884 884 if (k1 == k2) {
885 885 return true;
886 886 }
887 887 } break;
888 888
889 889 case JVM_CONSTANT_ClassIndex:
890 890 {
891 891 int recur1 = klass_index_at(index1);
892 892 int recur2 = cp2->klass_index_at(index2);
893 893 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
894 894 if (match) {
895 895 return true;
896 896 }
897 897 } break;
898 898
899 899 case JVM_CONSTANT_Double:
900 900 {
901 901 jdouble d1 = double_at(index1);
902 902 jdouble d2 = cp2->double_at(index2);
903 903 if (d1 == d2) {
904 904 return true;
905 905 }
906 906 } break;
907 907
908 908 case JVM_CONSTANT_Fieldref:
909 909 case JVM_CONSTANT_InterfaceMethodref:
910 910 case JVM_CONSTANT_Methodref:
911 911 {
912 912 int recur1 = uncached_klass_ref_index_at(index1);
913 913 int recur2 = cp2->uncached_klass_ref_index_at(index2);
914 914 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
915 915 if (match) {
916 916 recur1 = uncached_name_and_type_ref_index_at(index1);
917 917 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
918 918 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
919 919 if (match) {
920 920 return true;
921 921 }
922 922 }
923 923 } break;
924 924
925 925 case JVM_CONSTANT_Float:
926 926 {
927 927 jfloat f1 = float_at(index1);
928 928 jfloat f2 = cp2->float_at(index2);
929 929 if (f1 == f2) {
930 930 return true;
931 931 }
932 932 } break;
933 933
934 934 case JVM_CONSTANT_Integer:
935 935 {
936 936 jint i1 = int_at(index1);
937 937 jint i2 = cp2->int_at(index2);
938 938 if (i1 == i2) {
939 939 return true;
940 940 }
941 941 } break;
942 942
943 943 case JVM_CONSTANT_Long:
944 944 {
945 945 jlong l1 = long_at(index1);
946 946 jlong l2 = cp2->long_at(index2);
947 947 if (l1 == l2) {
948 948 return true;
949 949 }
950 950 } break;
951 951
952 952 case JVM_CONSTANT_NameAndType:
953 953 {
954 954 int recur1 = name_ref_index_at(index1);
955 955 int recur2 = cp2->name_ref_index_at(index2);
956 956 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
957 957 if (match) {
958 958 recur1 = signature_ref_index_at(index1);
959 959 recur2 = cp2->signature_ref_index_at(index2);
960 960 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
961 961 if (match) {
962 962 return true;
963 963 }
964 964 }
965 965 } break;
966 966
967 967 case JVM_CONSTANT_StringIndex:
968 968 {
969 969 int recur1 = string_index_at(index1);
970 970 int recur2 = cp2->string_index_at(index2);
971 971 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
972 972 if (match) {
973 973 return true;
974 974 }
975 975 } break;
976 976
977 977 case JVM_CONSTANT_UnresolvedClass:
978 978 {
979 979 Symbol* k1 = unresolved_klass_at(index1);
980 980 Symbol* k2 = cp2->unresolved_klass_at(index2);
981 981 if (k1 == k2) {
982 982 return true;
983 983 }
984 984 } break;
985 985
986 986 case JVM_CONSTANT_MethodType:
987 987 {
988 988 int k1 = method_type_index_at_error_ok(index1);
989 989 int k2 = cp2->method_type_index_at_error_ok(index2);
990 990 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
991 991 if (match) {
992 992 return true;
993 993 }
994 994 } break;
995 995
996 996 case JVM_CONSTANT_MethodHandle:
997 997 {
998 998 int k1 = method_handle_ref_kind_at_error_ok(index1);
999 999 int k2 = cp2->method_handle_ref_kind_at_error_ok(index2);
1000 1000 if (k1 == k2) {
1001 1001 int i1 = method_handle_index_at_error_ok(index1);
1002 1002 int i2 = cp2->method_handle_index_at_error_ok(index2);
1003 1003 bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
1004 1004 if (match) {
1005 1005 return true;
1006 1006 }
1007 1007 }
1008 1008 } break;
1009 1009
1010 1010 case JVM_CONSTANT_InvokeDynamic:
1011 1011 {
1012 1012 int k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
1013 1013 int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
1014 1014 int i1 = invoke_dynamic_bootstrap_specifier_index(index1);
1015 1015 int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2);
1016 1016 // separate statements and variables because CHECK_false is used
1017 1017 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
1018 1018 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
1019 1019 return (match_entry && match_operand);
1020 1020 } break;
1021 1021
1022 1022 case JVM_CONSTANT_String:
1023 1023 {
1024 1024 Symbol* s1 = unresolved_string_at(index1);
1025 1025 Symbol* s2 = cp2->unresolved_string_at(index2);
1026 1026 if (s1 == s2) {
1027 1027 return true;
1028 1028 }
1029 1029 } break;
1030 1030
1031 1031 case JVM_CONSTANT_Utf8:
1032 1032 {
1033 1033 Symbol* s1 = symbol_at(index1);
1034 1034 Symbol* s2 = cp2->symbol_at(index2);
1035 1035 if (s1 == s2) {
1036 1036 return true;
1037 1037 }
1038 1038 } break;
1039 1039
1040 1040 // Invalid is used as the tag for the second constant pool entry
1041 1041 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1042 1042 // not be seen by itself.
1043 1043 case JVM_CONSTANT_Invalid: // fall through
1044 1044
1045 1045 default:
1046 1046 ShouldNotReachHere();
1047 1047 break;
1048 1048 }
1049 1049
1050 1050 return false;
1051 1051 } // end compare_entry_to()
1052 1052
1053 1053
1054 1054 // Resize the operands array with delta_len and delta_size.
1055 1055 // Used in RedefineClasses for CP merge.
1056 1056 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
1057 1057 int old_len = operand_array_length(operands());
1058 1058 int new_len = old_len + delta_len;
1059 1059 int min_len = (delta_len > 0) ? old_len : new_len;
1060 1060
1061 1061 int old_size = operands()->length();
1062 1062 int new_size = old_size + delta_size;
1063 1063 int min_size = (delta_size > 0) ? old_size : new_size;
1064 1064
1065 1065 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1066 1066 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
1067 1067
1068 1068 // Set index in the resized array for existing elements only
1069 1069 for (int idx = 0; idx < min_len; idx++) {
1070 1070 int offset = operand_offset_at(idx); // offset in original array
1071 1071 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
1072 1072 }
1073 1073 // Copy the bootstrap specifiers only
1074 1074 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
1075 1075 new_ops->adr_at(2*new_len),
1076 1076 (min_size - 2*min_len) * sizeof(u2));
1077 1077 // Explicitly deallocate old operands array.
1078 1078 // Note, it is not needed for 7u backport.
1079 1079 if ( operands() != NULL) { // the safety check
1080 1080 MetadataFactory::free_array<u2>(loader_data, operands());
1081 1081 }
1082 1082 set_operands(new_ops);
1083 1083 } // end resize_operands()
1084 1084
1085 1085
1086 1086 // Extend the operands array with the length and size of the ext_cp operands.
1087 1087 // Used in RedefineClasses for CP merge.
1088 1088 void ConstantPool::extend_operands(constantPoolHandle ext_cp, TRAPS) {
1089 1089 int delta_len = operand_array_length(ext_cp->operands());
1090 1090 if (delta_len == 0) {
1091 1091 return; // nothing to do
1092 1092 }
1093 1093 int delta_size = ext_cp->operands()->length();
1094 1094
1095 1095 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger");
1096 1096
1097 1097 if (operand_array_length(operands()) == 0) {
1098 1098 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1099 1099 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
1100 1100 // The first element index defines the offset of second part
1101 1101 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
1102 1102 set_operands(new_ops);
1103 1103 } else {
1104 1104 resize_operands(delta_len, delta_size, CHECK);
1105 1105 }
1106 1106
1107 1107 } // end extend_operands()
1108 1108
1109 1109
1110 1110 // Shrink the operands array to a smaller array with new_len length.
1111 1111 // Used in RedefineClasses for CP merge.
1112 1112 void ConstantPool::shrink_operands(int new_len, TRAPS) {
1113 1113 int old_len = operand_array_length(operands());
1114 1114 if (new_len == old_len) {
1115 1115 return; // nothing to do
1116 1116 }
1117 1117 assert(new_len < old_len, "shrunken operands array must be smaller");
1118 1118
1119 1119 int free_base = operand_next_offset_at(new_len - 1);
1120 1120 int delta_len = new_len - old_len;
1121 1121 int delta_size = 2*delta_len + free_base - operands()->length();
1122 1122
1123 1123 resize_operands(delta_len, delta_size, CHECK);
1124 1124
1125 1125 } // end shrink_operands()
1126 1126
1127 1127
1128 1128 void ConstantPool::copy_operands(constantPoolHandle from_cp,
1129 1129 constantPoolHandle to_cp,
1130 1130 TRAPS) {
1131 1131
1132 1132 int from_oplen = operand_array_length(from_cp->operands());
1133 1133 int old_oplen = operand_array_length(to_cp->operands());
1134 1134 if (from_oplen != 0) {
1135 1135 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
1136 1136 // append my operands to the target's operands array
1137 1137 if (old_oplen == 0) {
1138 1138 // Can't just reuse from_cp's operand list because of deallocation issues
1139 1139 int len = from_cp->operands()->length();
1140 1140 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
1141 1141 Copy::conjoint_memory_atomic(
1142 1142 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
1143 1143 to_cp->set_operands(new_ops);
1144 1144 } else {
1145 1145 int old_len = to_cp->operands()->length();
1146 1146 int from_len = from_cp->operands()->length();
1147 1147 int old_off = old_oplen * sizeof(u2);
1148 1148 int from_off = from_oplen * sizeof(u2);
1149 1149 // Use the metaspace for the destination constant pool
1150 1150 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
1151 1151 int fillp = 0, len = 0;
1152 1152 // first part of dest
1153 1153 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
1154 1154 new_operands->adr_at(fillp),
1155 1155 (len = old_off) * sizeof(u2));
1156 1156 fillp += len;
1157 1157 // first part of src
1158 1158 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
1159 1159 new_operands->adr_at(fillp),
1160 1160 (len = from_off) * sizeof(u2));
1161 1161 fillp += len;
1162 1162 // second part of dest
1163 1163 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
1164 1164 new_operands->adr_at(fillp),
1165 1165 (len = old_len - old_off) * sizeof(u2));
1166 1166 fillp += len;
1167 1167 // second part of src
1168 1168 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
1169 1169 new_operands->adr_at(fillp),
1170 1170 (len = from_len - from_off) * sizeof(u2));
1171 1171 fillp += len;
1172 1172 assert(fillp == new_operands->length(), "");
1173 1173
1174 1174 // Adjust indexes in the first part of the copied operands array.
1175 1175 for (int j = 0; j < from_oplen; j++) {
1176 1176 int offset = operand_offset_at(new_operands, old_oplen + j);
1177 1177 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
1178 1178 offset += old_len; // every new tuple is preceded by old_len extra u2's
1179 1179 operand_offset_at_put(new_operands, old_oplen + j, offset);
1180 1180 }
1181 1181
1182 1182 // replace target operands array with combined array
1183 1183 to_cp->set_operands(new_operands);
1184 1184 }
1185 1185 }
1186 1186 } // end copy_operands()
1187 1187
1188 1188
1189 1189 // Copy this constant pool's entries at start_i to end_i (inclusive)
1190 1190 // to the constant pool to_cp's entries starting at to_i. A total of
1191 1191 // (end_i - start_i) + 1 entries are copied.
1192 1192 void ConstantPool::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i,
1193 1193 constantPoolHandle to_cp, int to_i, TRAPS) {
1194 1194
1195 1195
1196 1196 int dest_i = to_i; // leave original alone for debug purposes
1197 1197
1198 1198 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
1199 1199 copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
1200 1200
1201 1201 switch (from_cp->tag_at(src_i).value()) {
1202 1202 case JVM_CONSTANT_Double:
1203 1203 case JVM_CONSTANT_Long:
1204 1204 // double and long take two constant pool entries
1205 1205 src_i += 2;
1206 1206 dest_i += 2;
1207 1207 break;
1208 1208
1209 1209 default:
1210 1210 // all others take one constant pool entry
1211 1211 src_i++;
1212 1212 dest_i++;
1213 1213 break;
1214 1214 }
1215 1215 }
1216 1216 copy_operands(from_cp, to_cp, CHECK);
1217 1217
1218 1218 } // end copy_cp_to_impl()
1219 1219
1220 1220
1221 1221 // Copy this constant pool's entry at from_i to the constant pool
1222 1222 // to_cp's entry at to_i.
1223 1223 void ConstantPool::copy_entry_to(constantPoolHandle from_cp, int from_i,
1224 1224 constantPoolHandle to_cp, int to_i,
1225 1225 TRAPS) {
1226 1226
1227 1227 int tag = from_cp->tag_at(from_i).value();
1228 1228 switch (tag) {
1229 1229 case JVM_CONSTANT_Class:
1230 1230 {
1231 1231 Klass* k = from_cp->klass_at(from_i, CHECK);
1232 1232 to_cp->klass_at_put(to_i, k);
1233 1233 } break;
1234 1234
1235 1235 case JVM_CONSTANT_ClassIndex:
1236 1236 {
1237 1237 jint ki = from_cp->klass_index_at(from_i);
1238 1238 to_cp->klass_index_at_put(to_i, ki);
1239 1239 } break;
1240 1240
1241 1241 case JVM_CONSTANT_Double:
1242 1242 {
1243 1243 jdouble d = from_cp->double_at(from_i);
1244 1244 to_cp->double_at_put(to_i, d);
1245 1245 // double takes two constant pool entries so init second entry's tag
1246 1246 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1247 1247 } break;
1248 1248
1249 1249 case JVM_CONSTANT_Fieldref:
1250 1250 {
1251 1251 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1252 1252 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1253 1253 to_cp->field_at_put(to_i, class_index, name_and_type_index);
1254 1254 } break;
1255 1255
1256 1256 case JVM_CONSTANT_Float:
1257 1257 {
1258 1258 jfloat f = from_cp->float_at(from_i);
1259 1259 to_cp->float_at_put(to_i, f);
1260 1260 } break;
1261 1261
1262 1262 case JVM_CONSTANT_Integer:
1263 1263 {
1264 1264 jint i = from_cp->int_at(from_i);
1265 1265 to_cp->int_at_put(to_i, i);
1266 1266 } break;
1267 1267
1268 1268 case JVM_CONSTANT_InterfaceMethodref:
1269 1269 {
1270 1270 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1271 1271 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1272 1272 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
1273 1273 } break;
1274 1274
1275 1275 case JVM_CONSTANT_Long:
1276 1276 {
1277 1277 jlong l = from_cp->long_at(from_i);
1278 1278 to_cp->long_at_put(to_i, l);
1279 1279 // long takes two constant pool entries so init second entry's tag
1280 1280 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1281 1281 } break;
1282 1282
1283 1283 case JVM_CONSTANT_Methodref:
1284 1284 {
1285 1285 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1286 1286 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1287 1287 to_cp->method_at_put(to_i, class_index, name_and_type_index);
1288 1288 } break;
1289 1289
1290 1290 case JVM_CONSTANT_NameAndType:
1291 1291 {
1292 1292 int name_ref_index = from_cp->name_ref_index_at(from_i);
1293 1293 int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1294 1294 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1295 1295 } break;
1296 1296
1297 1297 case JVM_CONSTANT_StringIndex:
1298 1298 {
1299 1299 jint si = from_cp->string_index_at(from_i);
1300 1300 to_cp->string_index_at_put(to_i, si);
1301 1301 } break;
1302 1302
1303 1303 case JVM_CONSTANT_UnresolvedClass:
1304 1304 case JVM_CONSTANT_UnresolvedClassInError:
1305 1305 {
1306 1306 // Can be resolved after checking tag, so check the slot first.
1307 1307 CPSlot entry = from_cp->slot_at(from_i);
1308 1308 if (entry.is_resolved()) {
1309 1309 assert(entry.get_klass()->is_klass(), "must be");
1310 1310 // Already resolved
1311 1311 to_cp->klass_at_put(to_i, entry.get_klass());
1312 1312 } else {
1313 1313 to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
1314 1314 }
1315 1315 } break;
1316 1316
1317 1317 case JVM_CONSTANT_String:
1318 1318 {
1319 1319 Symbol* s = from_cp->unresolved_string_at(from_i);
1320 1320 to_cp->unresolved_string_at_put(to_i, s);
1321 1321 } break;
1322 1322
1323 1323 case JVM_CONSTANT_Utf8:
1324 1324 {
1325 1325 Symbol* s = from_cp->symbol_at(from_i);
1326 1326 // Need to increase refcount, the old one will be thrown away and deferenced
1327 1327 s->increment_refcount();
1328 1328 to_cp->symbol_at_put(to_i, s);
1329 1329 } break;
1330 1330
1331 1331 case JVM_CONSTANT_MethodType:
1332 1332 case JVM_CONSTANT_MethodTypeInError:
1333 1333 {
1334 1334 jint k = from_cp->method_type_index_at_error_ok(from_i);
1335 1335 to_cp->method_type_index_at_put(to_i, k);
1336 1336 } break;
1337 1337
1338 1338 case JVM_CONSTANT_MethodHandle:
1339 1339 case JVM_CONSTANT_MethodHandleInError:
1340 1340 {
1341 1341 int k1 = from_cp->method_handle_ref_kind_at_error_ok(from_i);
1342 1342 int k2 = from_cp->method_handle_index_at_error_ok(from_i);
1343 1343 to_cp->method_handle_index_at_put(to_i, k1, k2);
1344 1344 } break;
1345 1345
1346 1346 case JVM_CONSTANT_InvokeDynamic:
1347 1347 {
1348 1348 int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
1349 1349 int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
1350 1350 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands
1351 1351 to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1352 1352 } break;
1353 1353
1354 1354 // Invalid is used as the tag for the second constant pool entry
1355 1355 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1356 1356 // not be seen by itself.
1357 1357 case JVM_CONSTANT_Invalid: // fall through
1358 1358
1359 1359 default:
1360 1360 {
1361 1361 ShouldNotReachHere();
1362 1362 } break;
1363 1363 }
1364 1364 } // end copy_entry_to()
1365 1365
1366 1366
1367 1367 // Search constant pool search_cp for an entry that matches this
1368 1368 // constant pool's entry at pattern_i. Returns the index of a
1369 1369 // matching entry or zero (0) if there is no matching entry.
1370 1370 int ConstantPool::find_matching_entry(int pattern_i,
1371 1371 constantPoolHandle search_cp, TRAPS) {
1372 1372
1373 1373 // index zero (0) is not used
1374 1374 for (int i = 1; i < search_cp->length(); i++) {
1375 1375 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1376 1376 if (found) {
1377 1377 return i;
1378 1378 }
1379 1379 }
1380 1380
1381 1381 return 0; // entry not found; return unused index zero (0)
1382 1382 } // end find_matching_entry()
1383 1383
1384 1384
1385 1385 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1386 1386 // cp2's bootstrap specifier at idx2.
1387 1387 bool ConstantPool::compare_operand_to(int idx1, constantPoolHandle cp2, int idx2, TRAPS) {
1388 1388 int k1 = operand_bootstrap_method_ref_index_at(idx1);
1389 1389 int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2);
1390 1390 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1391 1391
1392 1392 if (!match) {
1393 1393 return false;
1394 1394 }
1395 1395 int argc = operand_argument_count_at(idx1);
1396 1396 if (argc == cp2->operand_argument_count_at(idx2)) {
1397 1397 for (int j = 0; j < argc; j++) {
1398 1398 k1 = operand_argument_index_at(idx1, j);
1399 1399 k2 = cp2->operand_argument_index_at(idx2, j);
1400 1400 match = compare_entry_to(k1, cp2, k2, CHECK_false);
1401 1401 if (!match) {
1402 1402 return false;
1403 1403 }
1404 1404 }
1405 1405 return true; // got through loop; all elements equal
1406 1406 }
1407 1407 return false;
1408 1408 } // end compare_operand_to()
1409 1409
1410 1410 // Search constant pool search_cp for a bootstrap specifier that matches
1411 1411 // this constant pool's bootstrap specifier at pattern_i index.
1412 1412 // Return the index of a matching bootstrap specifier or (-1) if there is no match.
1413 1413 int ConstantPool::find_matching_operand(int pattern_i,
1414 1414 constantPoolHandle search_cp, int search_len, TRAPS) {
1415 1415 for (int i = 0; i < search_len; i++) {
1416 1416 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
1417 1417 if (found) {
1418 1418 return i;
1419 1419 }
1420 1420 }
1421 1421 return -1; // bootstrap specifier not found; return unused index (-1)
1422 1422 } // end find_matching_operand()
1423 1423
1424 1424
1425 1425 #ifndef PRODUCT
1426 1426
1427 1427 const char* ConstantPool::printable_name_at(int which) {
1428 1428
1429 1429 constantTag tag = tag_at(which);
1430 1430
1431 1431 if (tag.is_string()) {
1432 1432 return string_at_noresolve(which);
1433 1433 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1434 1434 return klass_name_at(which)->as_C_string();
1435 1435 } else if (tag.is_symbol()) {
1436 1436 return symbol_at(which)->as_C_string();
1437 1437 }
1438 1438 return "";
1439 1439 }
1440 1440
1441 1441 #endif // PRODUCT
1442 1442
1443 1443
1444 1444 // JVMTI GetConstantPool support
1445 1445
1446 1446 // For debugging of constant pool
1447 1447 const bool debug_cpool = false;
1448 1448
1449 1449 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
1450 1450
1451 1451 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1452 1452 const char* WARN_MSG = "Must not be such entry!";
1453 1453 jint size = 0;
1454 1454 u2 idx1, idx2;
1455 1455
1456 1456 for (jint idx = 1; idx < cnt; idx++) {
1457 1457 jint ent_size = 0;
1458 1458 u1 tag = *bytes++;
1459 1459 size++; // count tag
1460 1460
1461 1461 printf("const #%03d, tag: %02d ", idx, tag);
1462 1462 switch(tag) {
1463 1463 case JVM_CONSTANT_Invalid: {
1464 1464 printf("Invalid");
1465 1465 break;
1466 1466 }
1467 1467 case JVM_CONSTANT_Unicode: {
1468 1468 printf("Unicode %s", WARN_MSG);
1469 1469 break;
1470 1470 }
1471 1471 case JVM_CONSTANT_Utf8: {
1472 1472 u2 len = Bytes::get_Java_u2(bytes);
1473 1473 char str[128];
1474 1474 if (len > 127) {
1475 1475 len = 127;
1476 1476 }
1477 1477 strncpy(str, (char *) (bytes+2), len);
1478 1478 str[len] = '\0';
1479 1479 printf("Utf8 \"%s\"", str);
1480 1480 ent_size = 2 + len;
1481 1481 break;
1482 1482 }
1483 1483 case JVM_CONSTANT_Integer: {
1484 1484 u4 val = Bytes::get_Java_u4(bytes);
1485 1485 printf("int %d", *(int *) &val);
1486 1486 ent_size = 4;
1487 1487 break;
1488 1488 }
1489 1489 case JVM_CONSTANT_Float: {
1490 1490 u4 val = Bytes::get_Java_u4(bytes);
1491 1491 printf("float %5.3ff", *(float *) &val);
1492 1492 ent_size = 4;
1493 1493 break;
1494 1494 }
1495 1495 case JVM_CONSTANT_Long: {
1496 1496 u8 val = Bytes::get_Java_u8(bytes);
1497 1497 printf("long "INT64_FORMAT, (int64_t) *(jlong *) &val);
1498 1498 ent_size = 8;
1499 1499 idx++; // Long takes two cpool slots
1500 1500 break;
1501 1501 }
1502 1502 case JVM_CONSTANT_Double: {
1503 1503 u8 val = Bytes::get_Java_u8(bytes);
1504 1504 printf("double %5.3fd", *(jdouble *)&val);
1505 1505 ent_size = 8;
1506 1506 idx++; // Double takes two cpool slots
1507 1507 break;
1508 1508 }
1509 1509 case JVM_CONSTANT_Class: {
1510 1510 idx1 = Bytes::get_Java_u2(bytes);
1511 1511 printf("class #%03d", idx1);
1512 1512 ent_size = 2;
1513 1513 break;
1514 1514 }
1515 1515 case JVM_CONSTANT_String: {
1516 1516 idx1 = Bytes::get_Java_u2(bytes);
1517 1517 printf("String #%03d", idx1);
1518 1518 ent_size = 2;
1519 1519 break;
1520 1520 }
1521 1521 case JVM_CONSTANT_Fieldref: {
1522 1522 idx1 = Bytes::get_Java_u2(bytes);
1523 1523 idx2 = Bytes::get_Java_u2(bytes+2);
1524 1524 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
1525 1525 ent_size = 4;
1526 1526 break;
1527 1527 }
1528 1528 case JVM_CONSTANT_Methodref: {
1529 1529 idx1 = Bytes::get_Java_u2(bytes);
1530 1530 idx2 = Bytes::get_Java_u2(bytes+2);
1531 1531 printf("Method #%03d, #%03d", idx1, idx2);
1532 1532 ent_size = 4;
1533 1533 break;
1534 1534 }
1535 1535 case JVM_CONSTANT_InterfaceMethodref: {
1536 1536 idx1 = Bytes::get_Java_u2(bytes);
1537 1537 idx2 = Bytes::get_Java_u2(bytes+2);
1538 1538 printf("InterfMethod #%03d, #%03d", idx1, idx2);
1539 1539 ent_size = 4;
1540 1540 break;
1541 1541 }
1542 1542 case JVM_CONSTANT_NameAndType: {
1543 1543 idx1 = Bytes::get_Java_u2(bytes);
1544 1544 idx2 = Bytes::get_Java_u2(bytes+2);
1545 1545 printf("NameAndType #%03d, #%03d", idx1, idx2);
1546 1546 ent_size = 4;
1547 1547 break;
1548 1548 }
1549 1549 case JVM_CONSTANT_ClassIndex: {
1550 1550 printf("ClassIndex %s", WARN_MSG);
1551 1551 break;
1552 1552 }
1553 1553 case JVM_CONSTANT_UnresolvedClass: {
1554 1554 printf("UnresolvedClass: %s", WARN_MSG);
1555 1555 break;
1556 1556 }
1557 1557 case JVM_CONSTANT_UnresolvedClassInError: {
1558 1558 printf("UnresolvedClassInErr: %s", WARN_MSG);
1559 1559 break;
1560 1560 }
1561 1561 case JVM_CONSTANT_StringIndex: {
1562 1562 printf("StringIndex: %s", WARN_MSG);
1563 1563 break;
1564 1564 }
1565 1565 }
1566 1566 printf(";\n");
1567 1567 bytes += ent_size;
1568 1568 size += ent_size;
1569 1569 }
1570 1570 printf("Cpool size: %d\n", size);
1571 1571 fflush(0);
1572 1572 return;
1573 1573 } /* end print_cpool_bytes */
1574 1574
1575 1575
1576 1576 // Returns size of constant pool entry.
1577 1577 jint ConstantPool::cpool_entry_size(jint idx) {
1578 1578 switch(tag_at(idx).value()) {
1579 1579 case JVM_CONSTANT_Invalid:
1580 1580 case JVM_CONSTANT_Unicode:
1581 1581 return 1;
1582 1582
1583 1583 case JVM_CONSTANT_Utf8:
1584 1584 return 3 + symbol_at(idx)->utf8_length();
1585 1585
1586 1586 case JVM_CONSTANT_Class:
1587 1587 case JVM_CONSTANT_String:
1588 1588 case JVM_CONSTANT_ClassIndex:
1589 1589 case JVM_CONSTANT_UnresolvedClass:
1590 1590 case JVM_CONSTANT_UnresolvedClassInError:
1591 1591 case JVM_CONSTANT_StringIndex:
1592 1592 case JVM_CONSTANT_MethodType:
1593 1593 case JVM_CONSTANT_MethodTypeInError:
1594 1594 return 3;
1595 1595
1596 1596 case JVM_CONSTANT_MethodHandle:
1597 1597 case JVM_CONSTANT_MethodHandleInError:
1598 1598 return 4; //tag, ref_kind, ref_index
1599 1599
1600 1600 case JVM_CONSTANT_Integer:
1601 1601 case JVM_CONSTANT_Float:
1602 1602 case JVM_CONSTANT_Fieldref:
1603 1603 case JVM_CONSTANT_Methodref:
1604 1604 case JVM_CONSTANT_InterfaceMethodref:
1605 1605 case JVM_CONSTANT_NameAndType:
1606 1606 return 5;
1607 1607
1608 1608 case JVM_CONSTANT_InvokeDynamic:
1609 1609 // u1 tag, u2 bsm, u2 nt
1610 1610 return 5;
1611 1611
1612 1612 case JVM_CONSTANT_Long:
1613 1613 case JVM_CONSTANT_Double:
1614 1614 return 9;
1615 1615 }
1616 1616 assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1617 1617 return 1;
1618 1618 } /* end cpool_entry_size */
1619 1619
1620 1620
1621 1621 // SymbolHashMap is used to find a constant pool index from a string.
1622 1622 // This function fills in SymbolHashMaps, one for utf8s and one for
1623 1623 // class names, returns size of the cpool raw bytes.
1624 1624 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap,
1625 1625 SymbolHashMap *classmap) {
1626 1626 jint size = 0;
1627 1627
1628 1628 for (u2 idx = 1; idx < length(); idx++) {
1629 1629 u2 tag = tag_at(idx).value();
1630 1630 size += cpool_entry_size(idx);
1631 1631
1632 1632 switch(tag) {
1633 1633 case JVM_CONSTANT_Utf8: {
1634 1634 Symbol* sym = symbol_at(idx);
1635 1635 symmap->add_entry(sym, idx);
1636 1636 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
1637 1637 break;
1638 1638 }
1639 1639 case JVM_CONSTANT_Class:
1640 1640 case JVM_CONSTANT_UnresolvedClass:
1641 1641 case JVM_CONSTANT_UnresolvedClassInError: {
1642 1642 Symbol* sym = klass_name_at(idx);
1643 1643 classmap->add_entry(sym, idx);
1644 1644 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
1645 1645 break;
1646 1646 }
1647 1647 case JVM_CONSTANT_Long:
1648 1648 case JVM_CONSTANT_Double: {
1649 1649 idx++; // Both Long and Double take two cpool slots
1650 1650 break;
1651 1651 }
1652 1652 }
1653 1653 }
1654 1654 return size;
1655 1655 } /* end hash_utf8_entries_to */
1656 1656
1657 1657
1658 1658 // Copy cpool bytes.
1659 1659 // Returns:
1660 1660 // 0, in case of OutOfMemoryError
1661 1661 // -1, in case of internal error
1662 1662 // > 0, count of the raw cpool bytes that have been copied
1663 1663 int ConstantPool::copy_cpool_bytes(int cpool_size,
1664 1664 SymbolHashMap* tbl,
1665 1665 unsigned char *bytes) {
1666 1666 u2 idx1, idx2;
1667 1667 jint size = 0;
1668 1668 jint cnt = length();
1669 1669 unsigned char *start_bytes = bytes;
1670 1670
1671 1671 for (jint idx = 1; idx < cnt; idx++) {
1672 1672 u1 tag = tag_at(idx).value();
1673 1673 jint ent_size = cpool_entry_size(idx);
1674 1674
1675 1675 assert(size + ent_size <= cpool_size, "Size mismatch");
1676 1676
1677 1677 *bytes = tag;
1678 1678 DBG(printf("#%03hd tag=%03hd, ", idx, tag));
1679 1679 switch(tag) {
1680 1680 case JVM_CONSTANT_Invalid: {
1681 1681 DBG(printf("JVM_CONSTANT_Invalid"));
1682 1682 break;
1683 1683 }
1684 1684 case JVM_CONSTANT_Unicode: {
1685 1685 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
1686 1686 DBG(printf("JVM_CONSTANT_Unicode"));
1687 1687 break;
1688 1688 }
1689 1689 case JVM_CONSTANT_Utf8: {
1690 1690 Symbol* sym = symbol_at(idx);
1691 1691 char* str = sym->as_utf8();
1692 1692 // Warning! It's crashing on x86 with len = sym->utf8_length()
1693 1693 int len = (int) strlen(str);
1694 1694 Bytes::put_Java_u2((address) (bytes+1), (u2) len);
1695 1695 for (int i = 0; i < len; i++) {
1696 1696 bytes[3+i] = (u1) str[i];
1697 1697 }
1698 1698 DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
1699 1699 break;
1700 1700 }
1701 1701 case JVM_CONSTANT_Integer: {
1702 1702 jint val = int_at(idx);
1703 1703 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1704 1704 break;
1705 1705 }
1706 1706 case JVM_CONSTANT_Float: {
1707 1707 jfloat val = float_at(idx);
1708 1708 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1709 1709 break;
1710 1710 }
1711 1711 case JVM_CONSTANT_Long: {
1712 1712 jlong val = long_at(idx);
1713 1713 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1714 1714 idx++; // Long takes two cpool slots
1715 1715 break;
1716 1716 }
1717 1717 case JVM_CONSTANT_Double: {
1718 1718 jdouble val = double_at(idx);
1719 1719 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1720 1720 idx++; // Double takes two cpool slots
1721 1721 break;
1722 1722 }
1723 1723 case JVM_CONSTANT_Class:
1724 1724 case JVM_CONSTANT_UnresolvedClass:
1725 1725 case JVM_CONSTANT_UnresolvedClassInError: {
1726 1726 *bytes = JVM_CONSTANT_Class;
1727 1727 Symbol* sym = klass_name_at(idx);
1728 1728 idx1 = tbl->symbol_to_value(sym);
1729 1729 assert(idx1 != 0, "Have not found a hashtable entry");
1730 1730 Bytes::put_Java_u2((address) (bytes+1), idx1);
1731 1731 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
1732 1732 break;
1733 1733 }
1734 1734 case JVM_CONSTANT_String: {
1735 1735 *bytes = JVM_CONSTANT_String;
1736 1736 Symbol* sym = unresolved_string_at(idx);
1737 1737 idx1 = tbl->symbol_to_value(sym);
1738 1738 assert(idx1 != 0, "Have not found a hashtable entry");
1739 1739 Bytes::put_Java_u2((address) (bytes+1), idx1);
1740 1740 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
1741 1741 break;
1742 1742 }
1743 1743 case JVM_CONSTANT_Fieldref:
1744 1744 case JVM_CONSTANT_Methodref:
1745 1745 case JVM_CONSTANT_InterfaceMethodref: {
1746 1746 idx1 = uncached_klass_ref_index_at(idx);
1747 1747 idx2 = uncached_name_and_type_ref_index_at(idx);
1748 1748 Bytes::put_Java_u2((address) (bytes+1), idx1);
1749 1749 Bytes::put_Java_u2((address) (bytes+3), idx2);
1750 1750 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
1751 1751 break;
1752 1752 }
1753 1753 case JVM_CONSTANT_NameAndType: {
1754 1754 idx1 = name_ref_index_at(idx);
1755 1755 idx2 = signature_ref_index_at(idx);
1756 1756 Bytes::put_Java_u2((address) (bytes+1), idx1);
1757 1757 Bytes::put_Java_u2((address) (bytes+3), idx2);
1758 1758 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
1759 1759 break;
1760 1760 }
1761 1761 case JVM_CONSTANT_ClassIndex: {
1762 1762 *bytes = JVM_CONSTANT_Class;
1763 1763 idx1 = klass_index_at(idx);
1764 1764 Bytes::put_Java_u2((address) (bytes+1), idx1);
1765 1765 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
1766 1766 break;
1767 1767 }
1768 1768 case JVM_CONSTANT_StringIndex: {
1769 1769 *bytes = JVM_CONSTANT_String;
1770 1770 idx1 = string_index_at(idx);
1771 1771 Bytes::put_Java_u2((address) (bytes+1), idx1);
1772 1772 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
1773 1773 break;
1774 1774 }
1775 1775 case JVM_CONSTANT_MethodHandle:
1776 1776 case JVM_CONSTANT_MethodHandleInError: {
1777 1777 *bytes = JVM_CONSTANT_MethodHandle;
1778 1778 int kind = method_handle_ref_kind_at_error_ok(idx);
1779 1779 idx1 = method_handle_index_at_error_ok(idx);
1780 1780 *(bytes+1) = (unsigned char) kind;
1781 1781 Bytes::put_Java_u2((address) (bytes+2), idx1);
1782 1782 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
1783 1783 break;
1784 1784 }
1785 1785 case JVM_CONSTANT_MethodType:
1786 1786 case JVM_CONSTANT_MethodTypeInError: {
1787 1787 *bytes = JVM_CONSTANT_MethodType;
1788 1788 idx1 = method_type_index_at_error_ok(idx);
1789 1789 Bytes::put_Java_u2((address) (bytes+1), idx1);
1790 1790 DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
1791 1791 break;
1792 1792 }
1793 1793 case JVM_CONSTANT_InvokeDynamic: {
1794 1794 *bytes = tag;
1795 1795 idx1 = extract_low_short_from_int(*int_at_addr(idx));
1796 1796 idx2 = extract_high_short_from_int(*int_at_addr(idx));
1797 1797 assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
1798 1798 Bytes::put_Java_u2((address) (bytes+1), idx1);
1799 1799 Bytes::put_Java_u2((address) (bytes+3), idx2);
1800 1800 DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
1801 1801 break;
1802 1802 }
1803 1803 }
1804 1804 DBG(printf("\n"));
1805 1805 bytes += ent_size;
1806 1806 size += ent_size;
1807 1807 }
1808 1808 assert(size == cpool_size, "Size mismatch");
1809 1809
↓ open down ↓ |
1809 lines elided |
↑ open up ↑ |
1810 1810 // Keep temorarily for debugging until it's stable.
1811 1811 DBG(print_cpool_bytes(cnt, start_bytes));
1812 1812 return (int)(bytes - start_bytes);
1813 1813 } /* end copy_cpool_bytes */
1814 1814
1815 1815 #undef DBG
1816 1816
1817 1817
1818 1818 void ConstantPool::set_on_stack(const bool value) {
1819 1819 if (value) {
1820 - _flags |= _on_stack;
1820 + int old_flags = *const_cast<volatile int *>(&_flags);
1821 + while ((old_flags & _on_stack) == 0) {
1822 + int new_flags = old_flags | _on_stack;
1823 + int result = Atomic::cmpxchg(new_flags, &_flags, old_flags);
1824 +
1825 + if (result == old_flags) {
1826 + // Succeeded.
1827 + MetadataOnStackMark::record(this, Thread::current());
1828 + return;
1829 + }
1830 + old_flags = result;
1831 + }
1821 1832 } else {
1833 + // Clearing is done single-threadedly.
1822 1834 _flags &= ~_on_stack;
1823 1835 }
1824 - if (value) MetadataOnStackMark::record(this);
1825 1836 }
1826 1837
1827 1838 // JSR 292 support for patching constant pool oops after the class is linked and
1828 1839 // the oop array for resolved references are created.
1829 1840 // We can't do this during classfile parsing, which is how the other indexes are
1830 1841 // patched. The other patches are applied early for some error checking
1831 1842 // so only defer the pseudo_strings.
1832 1843 void ConstantPool::patch_resolved_references(
1833 1844 GrowableArray<Handle>* cp_patches) {
1834 1845 assert(EnableInvokeDynamic, "");
1835 1846 for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
1836 1847 Handle patch = cp_patches->at(index);
1837 1848 if (patch.not_null()) {
1838 1849 assert (tag_at(index).is_string(), "should only be string left");
1839 1850 // Patching a string means pre-resolving it.
1840 1851 // The spelling in the constant pool is ignored.
1841 1852 // The constant reference may be any object whatever.
1842 1853 // If it is not a real interned string, the constant is referred
1843 1854 // to as a "pseudo-string", and must be presented to the CP
1844 1855 // explicitly, because it may require scavenging.
1845 1856 int obj_index = cp_to_object_index(index);
1846 1857 pseudo_string_at_put(index, obj_index, patch());
1847 1858 DEBUG_ONLY(cp_patches->at_put(index, Handle());)
1848 1859 }
1849 1860 }
1850 1861 #ifdef ASSERT
1851 1862 // Ensure that all the patches have been used.
1852 1863 for (int index = 0; index < cp_patches->length(); index++) {
1853 1864 assert(cp_patches->at(index).is_null(),
1854 1865 err_msg("Unused constant pool patch at %d in class file %s",
1855 1866 index,
1856 1867 pool_holder()->external_name()));
1857 1868 }
1858 1869 #endif // ASSERT
1859 1870 }
1860 1871
1861 1872 #ifndef PRODUCT
1862 1873
1863 1874 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
1864 1875 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
1865 1876 guarantee(obj->is_constantPool(), "object must be constant pool");
1866 1877 constantPoolHandle cp(THREAD, (ConstantPool*)obj);
1867 1878 guarantee(cp->pool_holder() != NULL, "must be fully loaded");
1868 1879
1869 1880 for (int i = 0; i< cp->length(); i++) {
1870 1881 if (cp->tag_at(i).is_unresolved_klass()) {
1871 1882 // This will force loading of the class
1872 1883 Klass* klass = cp->klass_at(i, CHECK);
1873 1884 if (klass->oop_is_instance()) {
1874 1885 // Force initialization of class
1875 1886 InstanceKlass::cast(klass)->initialize(CHECK);
1876 1887 }
1877 1888 }
1878 1889 }
1879 1890 }
1880 1891
1881 1892 #endif
1882 1893
1883 1894
1884 1895 // Printing
1885 1896
1886 1897 void ConstantPool::print_on(outputStream* st) const {
1887 1898 EXCEPTION_MARK;
1888 1899 assert(is_constantPool(), "must be constantPool");
1889 1900 st->print_cr("%s", internal_name());
1890 1901 if (flags() != 0) {
1891 1902 st->print(" - flags: 0x%x", flags());
1892 1903 if (has_preresolution()) st->print(" has_preresolution");
1893 1904 if (on_stack()) st->print(" on_stack");
1894 1905 st->cr();
1895 1906 }
1896 1907 if (pool_holder() != NULL) {
1897 1908 st->print_cr(" - holder: " INTPTR_FORMAT, pool_holder());
1898 1909 }
1899 1910 st->print_cr(" - cache: " INTPTR_FORMAT, cache());
1900 1911 st->print_cr(" - resolved_references: " INTPTR_FORMAT, (void *)resolved_references());
1901 1912 st->print_cr(" - reference_map: " INTPTR_FORMAT, reference_map());
1902 1913
1903 1914 for (int index = 1; index < length(); index++) { // Index 0 is unused
1904 1915 ((ConstantPool*)this)->print_entry_on(index, st);
1905 1916 switch (tag_at(index).value()) {
1906 1917 case JVM_CONSTANT_Long :
1907 1918 case JVM_CONSTANT_Double :
1908 1919 index++; // Skip entry following eigth-byte constant
1909 1920 }
1910 1921
1911 1922 }
1912 1923 st->cr();
1913 1924 }
1914 1925
1915 1926 // Print one constant pool entry
1916 1927 void ConstantPool::print_entry_on(const int index, outputStream* st) {
1917 1928 EXCEPTION_MARK;
1918 1929 st->print(" - %3d : ", index);
1919 1930 tag_at(index).print_on(st);
1920 1931 st->print(" : ");
1921 1932 switch (tag_at(index).value()) {
1922 1933 case JVM_CONSTANT_Class :
1923 1934 { Klass* k = klass_at(index, CATCH);
1924 1935 guarantee(k != NULL, "need klass");
1925 1936 k->print_value_on(st);
1926 1937 st->print(" {0x%lx}", (address)k);
1927 1938 }
1928 1939 break;
1929 1940 case JVM_CONSTANT_Fieldref :
1930 1941 case JVM_CONSTANT_Methodref :
1931 1942 case JVM_CONSTANT_InterfaceMethodref :
1932 1943 st->print("klass_index=%d", uncached_klass_ref_index_at(index));
1933 1944 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
1934 1945 break;
1935 1946 case JVM_CONSTANT_String :
1936 1947 if (is_pseudo_string_at(index)) {
1937 1948 oop anObj = pseudo_string_at(index);
1938 1949 anObj->print_value_on(st);
1939 1950 st->print(" {0x%lx}", (address)anObj);
1940 1951 } else {
1941 1952 unresolved_string_at(index)->print_value_on(st);
1942 1953 }
1943 1954 break;
1944 1955 case JVM_CONSTANT_Integer :
1945 1956 st->print("%d", int_at(index));
1946 1957 break;
1947 1958 case JVM_CONSTANT_Float :
1948 1959 st->print("%f", float_at(index));
1949 1960 break;
1950 1961 case JVM_CONSTANT_Long :
1951 1962 st->print_jlong(long_at(index));
1952 1963 break;
1953 1964 case JVM_CONSTANT_Double :
1954 1965 st->print("%lf", double_at(index));
1955 1966 break;
1956 1967 case JVM_CONSTANT_NameAndType :
1957 1968 st->print("name_index=%d", name_ref_index_at(index));
1958 1969 st->print(" signature_index=%d", signature_ref_index_at(index));
1959 1970 break;
1960 1971 case JVM_CONSTANT_Utf8 :
1961 1972 symbol_at(index)->print_value_on(st);
1962 1973 break;
1963 1974 case JVM_CONSTANT_UnresolvedClass : // fall-through
1964 1975 case JVM_CONSTANT_UnresolvedClassInError: {
1965 1976 // unresolved_klass_at requires lock or safe world.
1966 1977 CPSlot entry = slot_at(index);
1967 1978 if (entry.is_resolved()) {
1968 1979 entry.get_klass()->print_value_on(st);
1969 1980 } else {
1970 1981 entry.get_symbol()->print_value_on(st);
1971 1982 }
1972 1983 }
1973 1984 break;
1974 1985 case JVM_CONSTANT_MethodHandle :
1975 1986 case JVM_CONSTANT_MethodHandleInError :
1976 1987 st->print("ref_kind=%d", method_handle_ref_kind_at_error_ok(index));
1977 1988 st->print(" ref_index=%d", method_handle_index_at_error_ok(index));
1978 1989 break;
1979 1990 case JVM_CONSTANT_MethodType :
1980 1991 case JVM_CONSTANT_MethodTypeInError :
1981 1992 st->print("signature_index=%d", method_type_index_at_error_ok(index));
1982 1993 break;
1983 1994 case JVM_CONSTANT_InvokeDynamic :
1984 1995 {
1985 1996 st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
1986 1997 st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
1987 1998 int argc = invoke_dynamic_argument_count_at(index);
1988 1999 if (argc > 0) {
1989 2000 for (int arg_i = 0; arg_i < argc; arg_i++) {
1990 2001 int arg = invoke_dynamic_argument_index_at(index, arg_i);
1991 2002 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
1992 2003 }
1993 2004 st->print("}");
1994 2005 }
1995 2006 }
1996 2007 break;
1997 2008 default:
1998 2009 ShouldNotReachHere();
1999 2010 break;
2000 2011 }
2001 2012 st->cr();
2002 2013 }
2003 2014
2004 2015 void ConstantPool::print_value_on(outputStream* st) const {
2005 2016 assert(is_constantPool(), "must be constantPool");
2006 2017 st->print("constant pool [%d]", length());
2007 2018 if (has_preresolution()) st->print("/preresolution");
2008 2019 if (operands() != NULL) st->print("/operands[%d]", operands()->length());
2009 2020 print_address_on(st);
2010 2021 st->print(" for ");
2011 2022 pool_holder()->print_value_on(st);
2012 2023 if (pool_holder() != NULL) {
2013 2024 bool extra = (pool_holder()->constants() != this);
2014 2025 if (extra) st->print(" (extra)");
2015 2026 }
2016 2027 if (cache() != NULL) {
2017 2028 st->print(" cache=" PTR_FORMAT, cache());
2018 2029 }
2019 2030 }
2020 2031
2021 2032 #if INCLUDE_SERVICES
2022 2033 // Size Statistics
2023 2034 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2024 2035 sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this));
2025 2036 sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags()));
2026 2037 sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache()));
2027 2038 sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2028 2039 sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map()));
2029 2040
2030 2041 sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2031 2042 sz->_cp_refmap_bytes;
2032 2043 sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2033 2044 }
2034 2045 #endif // INCLUDE_SERVICES
2035 2046
2036 2047 // Verification
2037 2048
2038 2049 void ConstantPool::verify_on(outputStream* st) {
2039 2050 guarantee(is_constantPool(), "object must be constant pool");
2040 2051 for (int i = 0; i< length(); i++) {
2041 2052 constantTag tag = tag_at(i);
2042 2053 CPSlot entry = slot_at(i);
2043 2054 if (tag.is_klass()) {
2044 2055 if (entry.is_resolved()) {
2045 2056 guarantee(entry.get_klass()->is_klass(), "should be klass");
2046 2057 }
2047 2058 } else if (tag.is_unresolved_klass()) {
2048 2059 if (entry.is_resolved()) {
2049 2060 guarantee(entry.get_klass()->is_klass(), "should be klass");
2050 2061 }
2051 2062 } else if (tag.is_symbol()) {
2052 2063 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2053 2064 } else if (tag.is_string()) {
2054 2065 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2055 2066 }
2056 2067 }
2057 2068 if (cache() != NULL) {
2058 2069 // Note: cache() can be NULL before a class is completely setup or
2059 2070 // in temporary constant pools used during constant pool merging
2060 2071 guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2061 2072 }
2062 2073 if (pool_holder() != NULL) {
2063 2074 // Note: pool_holder() can be NULL in temporary constant pools
2064 2075 // used during constant pool merging
2065 2076 guarantee(pool_holder()->is_klass(), "should be klass");
2066 2077 }
2067 2078 }
2068 2079
2069 2080
2070 2081 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2071 2082 char *str = sym->as_utf8();
2072 2083 unsigned int hash = compute_hash(str, sym->utf8_length());
2073 2084 unsigned int index = hash % table_size();
2074 2085
2075 2086 // check if already in map
2076 2087 // we prefer the first entry since it is more likely to be what was used in
2077 2088 // the class file
2078 2089 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2079 2090 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2080 2091 if (en->hash() == hash && en->symbol() == sym) {
2081 2092 return; // already there
2082 2093 }
2083 2094 }
2084 2095
2085 2096 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
2086 2097 entry->set_next(bucket(index));
2087 2098 _buckets[index].set_entry(entry);
2088 2099 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2089 2100 }
2090 2101
2091 2102 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
2092 2103 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
2093 2104 char *str = sym->as_utf8();
2094 2105 int len = sym->utf8_length();
2095 2106 unsigned int hash = SymbolHashMap::compute_hash(str, len);
2096 2107 unsigned int index = hash % table_size();
2097 2108 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2098 2109 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2099 2110 if (en->hash() == hash && en->symbol() == sym) {
2100 2111 return en;
2101 2112 }
2102 2113 }
2103 2114 return NULL;
2104 2115 }
↓ open down ↓ |
270 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX