HotSpot解释器代码片段

HotSpot解释器代码片段

// 确保常量池中存放的是已解释的类  
if (!constants->tag_at(index).is_unresolved_klass()) {  
  // 断言确保是klassOop和instanceKlassOop(这部分下一节介绍)  
  oop entry = (klassOop) *constants->obj_at_addr(index);  
  assert(entry->is_klass(), "Should be resolved klass");  
  klassOop k_entry = (klassOop) entry;  
  assert(k_entry->klass_part()->oop_is_instance(), "Should be instanceKlass");  
  instanceKlass* ik = (instanceKlass*) k_entry->klass_part();  
  // 确保对象所属类型已经经过初始化阶段  
  if ( ik->is_initialized() && ik->can_be_fastpath_allocated() ) {  
    // 取对象长度  
    size_t obj_size = ik->size_helper();  
    oop result = NULL;  
    // 记录是否需要将对象所有字段置零值  
    bool need_zero = !ZeroTLAB;  
    // 是否在TLAB中分配对象  
    if (UseTLAB) {  
      result = (oop) THREAD->tlab().allocate(obj_size);  
    }  
    if (result == NULL) {  
      need_zero = true;  
      // 直接在eden中分配对象  
retry:  
      HeapWord* compare_to = *Universe::heap()->top_addr();  
      HeapWord* new_top = compare_to + obj_size;  
      // cmpxchg是x86中的CAS指令,这里是一个C++方法,通过CAS方式分配空间,并发失败的话,转到retry中重试直至成功分配为止  
      if (new_top <= *Universe::heap()->end_addr()) {  
        if (Atomic::cmpxchg_ptr(new_top, Universe::heap()->top_addr(), compare_to) != compare_to) {  
          goto retry;  
        }  
        result = (oop) compare_to;  
      }  
    }  
    if (result != NULL) {  
      // 如果需要,为对象初始化零值  
      if (need_zero ) {  
        HeapWord* to_zero = (HeapWord*) result + sizeof(oopDesc) / oopSize;  
        obj_size -= sizeof(oopDesc) / oopSize;  
        if (obj_size > 0 ) {  
          memset(to_zero, 0, obj_size * HeapWordSize);  
        }  
      }  
      // 根据是否启用偏向锁,设置对象头信息  
      if (UseBiasedLocking) {  
        result->set_mark(ik->prototype_header());  
      } else {  
        result->set_mark(markOopDesc::prototype());  
      }  
      result->set_klass_gap(0);  
      result->set_klass(k_entry);  
      // 将对象引用入栈,继续执行下一条指令  
      SET_STACK_OBJECT(result, 0);  
      UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1);  
    }  
  }  
}  
Comments
Write a Comment