src/share/vm/memory/allocation.cpp

changeset 2040
0e35fa8ebccd
parent 1907
c18cbe5936b8
child 2043
2dfd013a7465
     1.1 --- a/src/share/vm/memory/allocation.cpp	Fri Jul 30 10:21:15 2010 -0700
     1.2 +++ b/src/share/vm/memory/allocation.cpp	Tue Aug 03 15:55:03 2010 -0700
     1.3 @@ -43,24 +43,68 @@
     1.4    switch (type) {
     1.5     case C_HEAP:
     1.6      res = (address)AllocateHeap(size, "C_Heap: ResourceOBJ");
     1.7 +    DEBUG_ONLY(set_allocation_type(res, C_HEAP);)
     1.8      break;
     1.9     case RESOURCE_AREA:
    1.10 +    // Will set allocation type in the resource object.
    1.11      res = (address)operator new(size);
    1.12      break;
    1.13     default:
    1.14      ShouldNotReachHere();
    1.15    }
    1.16 -  // Set allocation type in the resource object for assertion checks.
    1.17 -  DEBUG_ONLY(((ResourceObj *)res)->_allocation = type;)
    1.18    return res;
    1.19  }
    1.20  
    1.21  void ResourceObj::operator delete(void* p) {
    1.22    assert(((ResourceObj *)p)->allocated_on_C_heap(),
    1.23           "delete only allowed for C_HEAP objects");
    1.24 +  DEBUG_ONLY(((ResourceObj *)p)->_allocation = badHeapOopVal;)
    1.25    FreeHeap(p);
    1.26  }
    1.27  
    1.28 +#ifdef ASSERT
    1.29 +void ResourceObj::set_allocation_type(address res, allocation_type type) {
    1.30 +    // Set allocation type in the resource object
    1.31 +    uintptr_t allocation = (uintptr_t)res;
    1.32 +    assert((allocation & allocation_mask) == 0, "address should be aligned ot 4 bytes at least");
    1.33 +    assert(type <= allocation_mask, "incorrect allocation type");
    1.34 +    ((ResourceObj *)res)->_allocation = ~(allocation + type);
    1.35 +}
    1.36 +
    1.37 +ResourceObj::allocation_type ResourceObj::get_allocation_type() {
    1.38 +    assert(~(_allocation | allocation_mask) == (uintptr_t)this, "lost resource object");
    1.39 +    return (allocation_type)((~_allocation) & allocation_mask);
    1.40 +}
    1.41 +
    1.42 +ResourceObj::ResourceObj() { // default construtor
    1.43 +    if (~(_allocation | allocation_mask) != (uintptr_t)this) {
    1.44 +      set_allocation_type((address)this, STACK_OR_EMBEDDED);
    1.45 +    } else {
    1.46 +      assert(allocated_on_res_area() || allocated_on_C_heap() || allocated_on_arena(),
    1.47 +             "allocation_type should be set by operator new()");
    1.48 +    }
    1.49 +}
    1.50 +
    1.51 +ResourceObj::ResourceObj(const ResourceObj& r) { // default copy construtor
    1.52 +    // Used in ClassFileParser::parse_constant_pool_entries() for ClassFileStream.
    1.53 +    set_allocation_type((address)this, STACK_OR_EMBEDDED);
    1.54 +}
    1.55 +
    1.56 +ResourceObj& ResourceObj::operator=(const ResourceObj& r) { // default copy assignment
    1.57 +    // Used in InlineTree::ok_to_inline() for WarmCallInfo.
    1.58 +    assert(allocated_on_stack(), "copy only into local");
    1.59 +    // Keep current _allocation value;
    1.60 +    return *this;
    1.61 +}
    1.62 +
    1.63 +ResourceObj::~ResourceObj() {
    1.64 +    if (!allocated_on_C_heap()) { // operator delete() checks C_heap allocation_type.
    1.65 +      _allocation = badHeapOopVal;
    1.66 +    }
    1.67 +}
    1.68 +#endif // ASSERT
    1.69 +
    1.70 +
    1.71  void trace_heap_malloc(size_t size, const char* name, void* p) {
    1.72    // A lock is not needed here - tty uses a lock internally
    1.73    tty->print_cr("Heap malloc " INTPTR_FORMAT " %7d %s", p, size, name == NULL ? "" : name);

mercurial