comparison src/share/vm/memory/allocation.cpp @ 1685:0e35fa8ebccd

6973963: SEGV in ciBlock::start_bci() with EA Summary: Added more checks into ResourceObj and growableArray to verify correctness of allocation type. Reviewed-by: never, coleenp, dholmes
author kvn
date Tue, 03 Aug 2010 15:55:03 -0700
parents c18cbe5936b8
children 2dfd013a7465
comparison
equal deleted inserted replaced
1684:66c5dadb4d61 1685:0e35fa8ebccd
41 void* ResourceObj::operator new(size_t size, allocation_type type) { 41 void* ResourceObj::operator new(size_t size, allocation_type type) {
42 address res; 42 address res;
43 switch (type) { 43 switch (type) {
44 case C_HEAP: 44 case C_HEAP:
45 res = (address)AllocateHeap(size, "C_Heap: ResourceOBJ"); 45 res = (address)AllocateHeap(size, "C_Heap: ResourceOBJ");
46 DEBUG_ONLY(set_allocation_type(res, C_HEAP);)
46 break; 47 break;
47 case RESOURCE_AREA: 48 case RESOURCE_AREA:
49 // Will set allocation type in the resource object.
48 res = (address)operator new(size); 50 res = (address)operator new(size);
49 break; 51 break;
50 default: 52 default:
51 ShouldNotReachHere(); 53 ShouldNotReachHere();
52 } 54 }
53 // Set allocation type in the resource object for assertion checks.
54 DEBUG_ONLY(((ResourceObj *)res)->_allocation = type;)
55 return res; 55 return res;
56 } 56 }
57 57
58 void ResourceObj::operator delete(void* p) { 58 void ResourceObj::operator delete(void* p) {
59 assert(((ResourceObj *)p)->allocated_on_C_heap(), 59 assert(((ResourceObj *)p)->allocated_on_C_heap(),
60 "delete only allowed for C_HEAP objects"); 60 "delete only allowed for C_HEAP objects");
61 DEBUG_ONLY(((ResourceObj *)p)->_allocation = badHeapOopVal;)
61 FreeHeap(p); 62 FreeHeap(p);
62 } 63 }
64
65 #ifdef ASSERT
66 void ResourceObj::set_allocation_type(address res, allocation_type type) {
67 // Set allocation type in the resource object
68 uintptr_t allocation = (uintptr_t)res;
69 assert((allocation & allocation_mask) == 0, "address should be aligned ot 4 bytes at least");
70 assert(type <= allocation_mask, "incorrect allocation type");
71 ((ResourceObj *)res)->_allocation = ~(allocation + type);
72 }
73
74 ResourceObj::allocation_type ResourceObj::get_allocation_type() {
75 assert(~(_allocation | allocation_mask) == (uintptr_t)this, "lost resource object");
76 return (allocation_type)((~_allocation) & allocation_mask);
77 }
78
79 ResourceObj::ResourceObj() { // default construtor
80 if (~(_allocation | allocation_mask) != (uintptr_t)this) {
81 set_allocation_type((address)this, STACK_OR_EMBEDDED);
82 } else {
83 assert(allocated_on_res_area() || allocated_on_C_heap() || allocated_on_arena(),
84 "allocation_type should be set by operator new()");
85 }
86 }
87
88 ResourceObj::ResourceObj(const ResourceObj& r) { // default copy construtor
89 // Used in ClassFileParser::parse_constant_pool_entries() for ClassFileStream.
90 set_allocation_type((address)this, STACK_OR_EMBEDDED);
91 }
92
93 ResourceObj& ResourceObj::operator=(const ResourceObj& r) { // default copy assignment
94 // Used in InlineTree::ok_to_inline() for WarmCallInfo.
95 assert(allocated_on_stack(), "copy only into local");
96 // Keep current _allocation value;
97 return *this;
98 }
99
100 ResourceObj::~ResourceObj() {
101 if (!allocated_on_C_heap()) { // operator delete() checks C_heap allocation_type.
102 _allocation = badHeapOopVal;
103 }
104 }
105 #endif // ASSERT
106
63 107
64 void trace_heap_malloc(size_t size, const char* name, void* p) { 108 void trace_heap_malloc(size_t size, const char* name, void* p) {
65 // A lock is not needed here - tty uses a lock internally 109 // A lock is not needed here - tty uses a lock internally
66 tty->print_cr("Heap malloc " INTPTR_FORMAT " %7d %s", p, size, name == NULL ? "" : name); 110 tty->print_cr("Heap malloc " INTPTR_FORMAT " %7d %s", p, size, name == NULL ? "" : name);
67 } 111 }