constantPoolKlass.cpp 19.1 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35
#include "precompiled.hpp"
#include "classfile/javaClasses.hpp"
#include "gc_implementation/shared/markSweep.inline.hpp"
#include "gc_interface/collectedHeap.inline.hpp"
#include "memory/oopFactory.hpp"
#include "memory/permGen.hpp"
#include "memory/universe.inline.hpp"
#include "oops/constantPoolKlass.hpp"
#include "oops/constantPoolOop.hpp"
#include "oops/oop.inline.hpp"
#include "oops/oop.inline2.hpp"
36
#include "oops/symbol.hpp"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "runtime/handles.inline.hpp"
#ifdef TARGET_OS_FAMILY_linux
# include "thread_linux.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_solaris
# include "thread_solaris.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_windows
# include "thread_windows.inline.hpp"
#endif
#ifndef SERIALGC
#include "gc_implementation/parNew/parOopClosures.inline.hpp"
#include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
#include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
#include "memory/cardTableRS.hpp"
#include "oops/oop.pcgc.inline.hpp"
#endif
D
duke 已提交
54

55
constantPoolOop constantPoolKlass::allocate(int length, bool is_conc_safe, TRAPS) {
D
duke 已提交
56 57
  int size = constantPoolOopDesc::object_size(length);
  KlassHandle klass (THREAD, as_klassOop());
58 59 60 61 62 63 64 65
  assert(klass()->is_oop(), "Can't be null, else handlizing of c below won't work");
  constantPoolHandle pool;
  {
    constantPoolOop c =
      (constantPoolOop)CollectedHeap::permanent_obj_allocate(klass, size, CHECK_NULL);
    assert(c->klass_or_null() != NULL, "Handlizing below won't work");
    pool = constantPoolHandle(THREAD, c);
  }
D
duke 已提交
66

67 68 69 70 71 72
  pool->set_length(length);
  pool->set_tags(NULL);
  pool->set_cache(NULL);
  pool->set_operands(NULL);
  pool->set_pool_holder(NULL);
  pool->set_flags(0);
D
duke 已提交
73
  // only set to non-zero if constant pool is merged by RedefineClasses
74
  pool->set_orig_length(0);
75 76
  // if constant pool may change during RedefineClasses, it is created
  // unsafe for GC concurrent processing.
77
  pool->set_is_conc_safe(is_conc_safe);
D
duke 已提交
78 79
  // all fields are initialized; needed for GC

80 81 82
  // Note: because we may be in this "conc_unsafe" state when allocating
  // t_oop below, which may in turn cause a GC, it is imperative that our
  // size be correct, consistent and henceforth stable, at this stage.
83 84
  assert(pool->is_oop() && pool->is_parsable(), "Else size() below is unreliable");
  assert(size == pool->size(), "size() is wrong");
85

D
duke 已提交
86 87 88 89 90 91 92 93
  // initialize tag array
  typeArrayOop t_oop = oopFactory::new_permanent_byteArray(length, CHECK_NULL);
  typeArrayHandle tags (THREAD, t_oop);
  for (int index = 0; index < length; index++) {
    tags()->byte_at_put(index, JVM_CONSTANT_Invalid);
  }
  pool->set_tags(tags());

94
  // Check that our size was stable at its old value.
95
  assert(size == pool->size(), "size() changed");
D
duke 已提交
96 97 98 99 100
  return pool();
}

klassOop constantPoolKlass::create_klass(TRAPS) {
  constantPoolKlass o;
101 102 103 104 105
  KlassHandle h_this_klass(THREAD, Universe::klassKlassObj());
  KlassHandle k = base_create_klass(h_this_klass, header_size(), o.vtbl_value(), CHECK_NULL);
  // Make sure size calculation is right
  assert(k()->size() == align_object_size(header_size()), "wrong size for object");
  java_lang_Class::create_mirror(k, CHECK_NULL); // Allocate mirror
D
duke 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  return k();
}

int constantPoolKlass::oop_size(oop obj) const {
  assert(obj->is_constantPool(), "must be constantPool");
  return constantPoolOop(obj)->object_size();
}


void constantPoolKlass::oop_follow_contents(oop obj) {
  assert (obj->is_constantPool(), "obj must be constant pool");
  constantPoolOop cp = (constantPoolOop) obj;
  // Performance tweak: We skip iterating over the klass pointer since we
  // know that Universe::constantPoolKlassObj never moves.

  // If the tags array is null we are in the middle of allocating this constant pool
  if (cp->tags() != NULL) {
    // gc of constant pool contents
    oop* base = (oop*)cp->base();
    for (int i = 0; i < cp->length(); i++) {
      if (cp->is_pointer_entry(i)) {
        if (*base != NULL) MarkSweep::mark_and_push(base);
      }
      base++;
    }
    // gc of constant pool instance variables
    MarkSweep::mark_and_push(cp->tags_addr());
    MarkSweep::mark_and_push(cp->cache_addr());
134
    MarkSweep::mark_and_push(cp->operands_addr());
D
duke 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    MarkSweep::mark_and_push(cp->pool_holder_addr());
  }
}

#ifndef SERIALGC
void constantPoolKlass::oop_follow_contents(ParCompactionManager* cm,
                                            oop obj) {
  assert (obj->is_constantPool(), "obj must be constant pool");
  constantPoolOop cp = (constantPoolOop) obj;
  // Performance tweak: We skip iterating over the klass pointer since we
  // know that Universe::constantPoolKlassObj never moves.

  // If the tags array is null we are in the middle of allocating this constant
  // pool.
  if (cp->tags() != NULL) {
    // gc of constant pool contents
    oop* base = (oop*)cp->base();
    for (int i = 0; i < cp->length(); i++) {
      if (cp->is_pointer_entry(i)) {
        if (*base != NULL) PSParallelCompact::mark_and_push(cm, base);
      }
      base++;
    }
    // gc of constant pool instance variables
    PSParallelCompact::mark_and_push(cm, cp->tags_addr());
    PSParallelCompact::mark_and_push(cm, cp->cache_addr());
161
    PSParallelCompact::mark_and_push(cm, cp->operands_addr());
D
duke 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    PSParallelCompact::mark_and_push(cm, cp->pool_holder_addr());
  }
}
#endif // SERIALGC


int constantPoolKlass::oop_adjust_pointers(oop obj) {
  assert (obj->is_constantPool(), "obj must be constant pool");
  constantPoolOop cp = (constantPoolOop) obj;
  // Get size before changing pointers.
  // Don't call size() or oop_size() since that is a virtual call.
  int size = cp->object_size();
  // Performance tweak: We skip iterating over the klass pointer since we
  // know that Universe::constantPoolKlassObj never moves.

  // If the tags array is null we are in the middle of allocating this constant
  // pool.
  if (cp->tags() != NULL) {
    oop* base = (oop*)cp->base();
    for (int i = 0; i< cp->length();  i++) {
      if (cp->is_pointer_entry(i)) {
        MarkSweep::adjust_pointer(base);
      }
      base++;
    }
  }
  MarkSweep::adjust_pointer(cp->tags_addr());
  MarkSweep::adjust_pointer(cp->cache_addr());
190
  MarkSweep::adjust_pointer(cp->operands_addr());
D
duke 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  MarkSweep::adjust_pointer(cp->pool_holder_addr());
  return size;
}


int constantPoolKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
  assert (obj->is_constantPool(), "obj must be constant pool");
  // Performance tweak: We skip iterating over the klass pointer since we
  // know that Universe::constantPoolKlassObj never moves.
  constantPoolOop cp = (constantPoolOop) obj;
  // Get size before changing pointers.
  // Don't call size() or oop_size() since that is a virtual call.
  int size = cp->object_size();

  // If the tags array is null we are in the middle of allocating this constant
  // pool.
  if (cp->tags() != NULL) {
    oop* base = (oop*)cp->base();
    for (int i = 0; i < cp->length(); i++) {
      if (cp->is_pointer_entry(i)) {
        blk->do_oop(base);
      }
      base++;
    }
  }
  blk->do_oop(cp->tags_addr());
  blk->do_oop(cp->cache_addr());
218
  blk->do_oop(cp->operands_addr());
D
duke 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  blk->do_oop(cp->pool_holder_addr());
  return size;
}


int constantPoolKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
  assert (obj->is_constantPool(), "obj must be constant pool");
  // Performance tweak: We skip iterating over the klass pointer since we
  // know that Universe::constantPoolKlassObj never moves.
  constantPoolOop cp = (constantPoolOop) obj;
  // Get size before changing pointers.
  // Don't call size() or oop_size() since that is a virtual call.
  int size = cp->object_size();

  // If the tags array is null we are in the middle of allocating this constant
  // pool.
  if (cp->tags() != NULL) {
    oop* base = (oop*)cp->base();
    for (int i = 0; i < cp->length(); i++) {
      if (mr.contains(base)) {
        if (cp->is_pointer_entry(i)) {
          blk->do_oop(base);
        }
      }
      base++;
    }
  }
  oop* addr;
  addr = cp->tags_addr();
  blk->do_oop(addr);
  addr = cp->cache_addr();
  blk->do_oop(addr);
251 252
  addr = cp->operands_addr();
  blk->do_oop(addr);
D
duke 已提交
253 254 255 256 257
  addr = cp->pool_holder_addr();
  blk->do_oop(addr);
  return size;
}

258 259 260 261 262
bool constantPoolKlass::oop_is_conc_safe(oop obj) const {
  assert(obj->is_constantPool(), "must be constantPool");
  return constantPoolOop(obj)->is_conc_safe();
}

D
duke 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
#ifndef SERIALGC
int constantPoolKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
  assert (obj->is_constantPool(), "obj must be constant pool");
  constantPoolOop cp = (constantPoolOop) obj;

  // If the tags array is null we are in the middle of allocating this constant
  // pool.
  if (cp->tags() != NULL) {
    oop* base = (oop*)cp->base();
    for (int i = 0; i < cp->length(); ++i, ++base) {
      if (cp->is_pointer_entry(i)) {
        PSParallelCompact::adjust_pointer(base);
      }
    }
  }
  PSParallelCompact::adjust_pointer(cp->tags_addr());
  PSParallelCompact::adjust_pointer(cp->cache_addr());
280
  PSParallelCompact::adjust_pointer(cp->operands_addr());
D
duke 已提交
281 282 283 284 285 286
  PSParallelCompact::adjust_pointer(cp->pool_holder_addr());
  return cp->object_size();
}

void constantPoolKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
  assert(obj->is_constantPool(), "should be constant pool");
287
  constantPoolOop cp = (constantPoolOop) obj;
288
  if (cp->tags() != NULL &&
289
      (!JavaObjectsInPerm || (EnableInvokeDynamic && cp->has_pseudo_string()))) {
290
    for (int i = 1; i < cp->length(); ++i) {
291
      if (cp->tag_at(i).is_string()) {
292
        oop* base = cp->obj_at_addr_raw(i);
293 294 295 296 297 298
        if (PSScavenge::should_scavenge(base)) {
          pm->claim_or_forward_depth(base);
        }
      }
    }
  }
D
duke 已提交
299 300 301 302 303 304 305 306 307
}
#endif // SERIALGC

// Printing

void constantPoolKlass::oop_print_on(oop obj, outputStream* st) {
  EXCEPTION_MARK;
  oop anObj;
  assert(obj->is_constantPool(), "must be constantPool");
308
  Klass::oop_print_on(obj, st);
D
duke 已提交
309
  constantPoolOop cp = constantPoolOop(obj);
310
  if (cp->flags() != 0) {
311
    st->print(" - flags: 0x%x", cp->flags());
312
    if (cp->has_pseudo_string()) st->print(" has_pseudo_string");
313
    if (cp->has_invokedynamic()) st->print(" has_invokedynamic");
314 315
    st->cr();
  }
316
  st->print_cr(" - cache: " INTPTR_FORMAT, cp->cache());
D
duke 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

  for (int index = 1; index < cp->length(); index++) {      // Index 0 is unused
    st->print(" - %3d : ", index);
    cp->tag_at(index).print_on(st);
    st->print(" : ");
    switch (cp->tag_at(index).value()) {
      case JVM_CONSTANT_Class :
        { anObj = cp->klass_at(index, CATCH);
          anObj->print_value_on(st);
          st->print(" {0x%lx}", (address)anObj);
        }
        break;
      case JVM_CONSTANT_Fieldref :
      case JVM_CONSTANT_Methodref :
      case JVM_CONSTANT_InterfaceMethodref :
332 333
        st->print("klass_index=%d", cp->uncached_klass_ref_index_at(index));
        st->print(" name_and_type_index=%d", cp->uncached_name_and_type_ref_index_at(index));
D
duke 已提交
334 335 336
        break;
      case JVM_CONSTANT_UnresolvedString :
      case JVM_CONSTANT_String :
337 338 339 340 341
        if (cp->is_pseudo_string_at(index)) {
          anObj = cp->pseudo_string_at(index);
        } else {
          anObj = cp->string_at(index, CATCH);
        }
D
duke 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        anObj->print_value_on(st);
        st->print(" {0x%lx}", (address)anObj);
        break;
      case JVM_CONSTANT_Integer :
        st->print("%d", cp->int_at(index));
        break;
      case JVM_CONSTANT_Float :
        st->print("%f", cp->float_at(index));
        break;
      case JVM_CONSTANT_Long :
        st->print_jlong(cp->long_at(index));
        index++;   // Skip entry following eigth-byte constant
        break;
      case JVM_CONSTANT_Double :
        st->print("%lf", cp->double_at(index));
        index++;   // Skip entry following eigth-byte constant
        break;
      case JVM_CONSTANT_NameAndType :
        st->print("name_index=%d", cp->name_ref_index_at(index));
        st->print(" signature_index=%d", cp->signature_ref_index_at(index));
        break;
      case JVM_CONSTANT_Utf8 :
        cp->symbol_at(index)->print_value_on(st);
        break;
      case JVM_CONSTANT_UnresolvedClass :               // fall-through
      case JVM_CONSTANT_UnresolvedClassInError: {
        // unresolved_klass_at requires lock or safe world.
369 370 371 372 373 374
        CPSlot entry = cp->slot_at(index);
        if (entry.is_oop()) {
          entry.get_oop()->print_value_on(st);
        } else {
          entry.get_symbol()->print_value_on(st);
        }
D
duke 已提交
375 376
        }
        break;
377 378 379 380 381 382 383
      case JVM_CONSTANT_MethodHandle :
        st->print("ref_kind=%d", cp->method_handle_ref_kind_at(index));
        st->print(" ref_index=%d", cp->method_handle_index_at(index));
        break;
      case JVM_CONSTANT_MethodType :
        st->print("signature_index=%d", cp->method_type_index_at(index));
        break;
384
      case JVM_CONSTANT_InvokeDynamicTrans :
385
      case JVM_CONSTANT_InvokeDynamic :
386 387 388 389 390 391 392 393 394 395 396 397
        {
          st->print("bootstrap_method_index=%d", cp->invoke_dynamic_bootstrap_method_ref_index_at(index));
          st->print(" name_and_type_index=%d", cp->invoke_dynamic_name_and_type_ref_index_at(index));
          int argc = cp->invoke_dynamic_argument_count_at(index);
          if (argc > 0) {
            for (int arg_i = 0; arg_i < argc; arg_i++) {
              int arg = cp->invoke_dynamic_argument_index_at(index, arg_i);
              st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
            }
            st->print("}");
          }
        }
398
        break;
D
duke 已提交
399 400 401 402 403 404 405 406 407
      default:
        ShouldNotReachHere();
        break;
    }
    st->cr();
  }
  st->cr();
}

408 409 410 411 412 413
void constantPoolKlass::oop_print_value_on(oop obj, outputStream* st) {
  assert(obj->is_constantPool(), "must be constantPool");
  constantPoolOop cp = constantPoolOop(obj);
  st->print("constant pool [%d]", cp->length());
  if (cp->has_pseudo_string()) st->print("/pseudo_string");
  if (cp->has_invokedynamic()) st->print("/invokedynamic");
414
  if (cp->operands() != NULL)  st->print("/operands[%d]", cp->operands()->length());
415 416 417
  cp->print_address_on(st);
  st->print(" for ");
  cp->pool_holder()->print_value_on(st);
418 419 420
  if (cp->cache() != NULL) {
    st->print(" cache=" PTR_FORMAT, cp->cache());
  }
421 422
}

D
duke 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435
const char* constantPoolKlass::internal_name() const {
  return "{constant pool}";
}

// Verification

void constantPoolKlass::oop_verify_on(oop obj, outputStream* st) {
  Klass::oop_verify_on(obj, st);
  guarantee(obj->is_constantPool(), "object must be constant pool");
  constantPoolOop cp = constantPoolOop(obj);
  guarantee(cp->is_perm(), "should be in permspace");
  if (!cp->partially_loaded()) {
    for (int i = 0; i< cp->length();  i++) {
436
      CPSlot entry = cp->slot_at(i);
D
duke 已提交
437
      if (cp->tag_at(i).is_klass()) {
438 439 440 441
        if (entry.is_oop()) {
          guarantee(entry.get_oop()->is_perm(),     "should be in permspace");
          guarantee(entry.get_oop()->is_klass(),    "should be klass");
        }
D
duke 已提交
442 443
      }
      if (cp->tag_at(i).is_unresolved_klass()) {
444 445 446 447
        if (entry.is_oop()) {
          guarantee(entry.get_oop()->is_perm(),     "should be in permspace");
          guarantee(entry.get_oop()->is_klass(),    "should be klass");
        }
D
duke 已提交
448 449
      }
      if (cp->tag_at(i).is_symbol()) {
450
        guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
D
duke 已提交
451 452
      }
      if (cp->tag_at(i).is_unresolved_string()) {
453 454 455 456 457 458 459
        if (entry.is_oop()) {
          guarantee(entry.get_oop()->is_perm(),     "should be in permspace");
          guarantee(entry.get_oop()->is_instance(), "should be instance");
        }
        else {
          guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
        }
D
duke 已提交
460 461
      }
      if (cp->tag_at(i).is_string()) {
462
        if (!cp->has_pseudo_string()) {
463
          if (entry.is_oop()) {
464 465
            guarantee(!JavaObjectsInPerm || entry.get_oop()->is_perm(),
                      "should be in permspace");
466 467
            guarantee(entry.get_oop()->is_instance(), "should be instance");
          }
468 469 470
        } else {
          // can be non-perm, can be non-instance (array)
        }
D
duke 已提交
471
      }
472
      // FIXME: verify JSR 292 tags JVM_CONSTANT_MethodHandle, etc.
D
duke 已提交
473 474 475 476 477 478 479 480 481
    }
    guarantee(cp->tags()->is_perm(),         "should be in permspace");
    guarantee(cp->tags()->is_typeArray(),    "should be type array");
    if (cp->cache() != NULL) {
      // Note: cache() can be NULL before a class is completely setup or
      // in temporary constant pools used during constant pool merging
      guarantee(cp->cache()->is_perm(),              "should be in permspace");
      guarantee(cp->cache()->is_constantPoolCache(), "should be constant pool cache");
    }
482 483 484 485
    if (cp->operands() != NULL) {
      guarantee(cp->operands()->is_perm(),  "should be in permspace");
      guarantee(cp->operands()->is_typeArray(), "should be type array");
    }
D
duke 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
    if (cp->pool_holder() != NULL) {
      // Note: pool_holder() can be NULL in temporary constant pools
      // used during constant pool merging
      guarantee(cp->pool_holder()->is_perm(),  "should be in permspace");
      guarantee(cp->pool_holder()->is_klass(), "should be klass");
    }
  }
}

bool constantPoolKlass::oop_partially_loaded(oop obj) const {
  assert(obj->is_constantPool(), "object must be constant pool");
  constantPoolOop cp = constantPoolOop(obj);
  return cp->tags() == NULL || cp->pool_holder() == (klassOop) cp;   // Check whether pool holder points to self
}


void constantPoolKlass::oop_set_partially_loaded(oop obj) {
  assert(obj->is_constantPool(), "object must be constant pool");
  constantPoolOop cp = constantPoolOop(obj);
  assert(cp->pool_holder() == NULL, "just checking");
  cp->set_pool_holder((klassOop) cp);   // Temporarily set pool holder to point to self
}

#ifndef PRODUCT
// CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
void constantPoolKlass::preload_and_initialize_all_classes(oop obj, TRAPS) {
  guarantee(obj->is_constantPool(), "object must be constant pool");
  constantPoolHandle cp(THREAD, (constantPoolOop)obj);
  guarantee(!cp->partially_loaded(), "must be fully loaded");

  for (int i = 0; i< cp->length();  i++) {
    if (cp->tag_at(i).is_unresolved_klass()) {
      // This will force loading of the class
      klassOop klass = cp->klass_at(i, CHECK);
      if (klass->is_instance()) {
        // Force initialization of class
        instanceKlass::cast(klass)->initialize(CHECK);
      }
    }
  }
}

#endif