Java - HotSpot

# Code Snippets

# New Object

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
CASE(_new): {
  u2 index = Bytes::get_Java_u2(pc+1);

  // Attempt TLAB allocation first.
  //
  // To do this, we need to make sure:
  //   - klass is initialized
  //   - klass can be fastpath allocated (e.g. does not have finalizer)
  //   - TLAB accepts the allocation
  ConstantPool* constants = istate->method()->constants();
  if (UseTLAB && !constants->tag_at(index).is_unresolved_klass()) {
    Klass* entry = constants->resolved_klass_at(index);
    InstanceKlass* ik = InstanceKlass::cast(entry);
    if (ik->is_initialized() && ik->can_be_fastpath_allocated()) {
      size_t obj_size = ik->size_helper();
      HeapWord* result = THREAD->tlab().allocate(obj_size);
      if (result != nullptr) {
        // Initialize object field block:
        //   - if TLAB is pre-zeroed, we can skip this path
        //   - in debug mode, ThreadLocalAllocBuffer::allocate mangles
        //     this area, and we still need to initialize it
        if (DEBUG_ONLY(true ||) !ZeroTLAB) {
          size_t hdr_size = oopDesc::header_size();
          Copy::fill_to_words(result + hdr_size, obj_size - hdr_size, 0);
        }

        // Initialize header, mirrors MemAllocator.
        oopDesc::set_mark(result, markWord::prototype());
        oopDesc::set_klass_gap(result, 0);
        oopDesc::release_set_klass(result, ik);

        oop obj = cast_to_oop(result);

        // Must prevent reordering of stores for object initialization
        // with stores that publish the new object.
        OrderAccess::storestore();
        SET_STACK_OBJECT(obj, 0);
        UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1);
      }
    }
  }
  // Slow case allocation
  CALL_VM(InterpreterRuntime::_new(THREAD, METHOD->constants(), index),
          handle_exception);
  // Must prevent reordering of stores for object initialization
  // with stores that publish the new object.
  OrderAccess::storestore();
  SET_STACK_OBJECT(THREAD->vm_result(), 0);
  THREAD->set_vm_result(nullptr);
  UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1);
}

# References

Licensed under CC BY-NC-SA 4.0
Get Things Done
Built with Hugo
Theme Stack designed by Jimmy