315 // ResourceObj's can be allocated within other objects, but don't use |
315 // ResourceObj's can be allocated within other objects, but don't use |
316 // new or delete (allocation_type is unknown). If new is used to allocate, |
316 // new or delete (allocation_type is unknown). If new is used to allocate, |
317 // use delete to deallocate. |
317 // use delete to deallocate. |
318 class ResourceObj ALLOCATION_SUPER_CLASS_SPEC { |
318 class ResourceObj ALLOCATION_SUPER_CLASS_SPEC { |
319 public: |
319 public: |
320 enum allocation_type { UNKNOWN = 0, C_HEAP, RESOURCE_AREA, ARENA }; |
320 enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 }; |
|
321 static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN; |
321 #ifdef ASSERT |
322 #ifdef ASSERT |
322 private: |
323 private: |
323 allocation_type _allocation; |
324 // When this object is allocated on stack the new() operator is not |
324 public: |
325 // called but garbage on stack may look like a valid allocation_type. |
325 bool allocated_on_C_heap() { return _allocation == C_HEAP; } |
326 // Store negated 'this' pointer when new() is called to distinguish cases. |
|
327 uintptr_t _allocation; |
|
328 public: |
|
329 allocation_type get_allocation_type() const; |
|
330 bool allocated_on_stack() const { return get_allocation_type() == STACK_OR_EMBEDDED; } |
|
331 bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; } |
|
332 bool allocated_on_C_heap() const { return get_allocation_type() == C_HEAP; } |
|
333 bool allocated_on_arena() const { return get_allocation_type() == ARENA; } |
|
334 ResourceObj(); // default construtor |
|
335 ResourceObj(const ResourceObj& r); // default copy construtor |
|
336 ResourceObj& operator=(const ResourceObj& r); // default copy assignment |
|
337 ~ResourceObj(); |
326 #endif // ASSERT |
338 #endif // ASSERT |
327 |
339 |
328 public: |
340 public: |
329 void* operator new(size_t size, allocation_type type); |
341 void* operator new(size_t size, allocation_type type); |
330 void* operator new(size_t size, Arena *arena) { |
342 void* operator new(size_t size, Arena *arena) { |
331 address res = (address)arena->Amalloc(size); |
343 address res = (address)arena->Amalloc(size); |
332 // Set allocation type in the resource object |
344 DEBUG_ONLY(set_allocation_type(res, ARENA);) |
333 DEBUG_ONLY(((ResourceObj *)res)->_allocation = ARENA;) |
|
334 return res; |
345 return res; |
335 } |
346 } |
336 void* operator new(size_t size) { |
347 void* operator new(size_t size) { |
337 address res = (address)resource_allocate_bytes(size); |
348 address res = (address)resource_allocate_bytes(size); |
338 // Set allocation type in the resource object |
349 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) |
339 DEBUG_ONLY(((ResourceObj *)res)->_allocation = RESOURCE_AREA;) |
|
340 return res; |
|
341 } |
|
342 void* operator new(size_t size, void* where, allocation_type type) { |
|
343 void* res = where; |
|
344 // Set allocation type in the resource object |
|
345 DEBUG_ONLY(((ResourceObj *)res)->_allocation = type;) |
|
346 return res; |
350 return res; |
347 } |
351 } |
348 void operator delete(void* p); |
352 void operator delete(void* p); |
349 }; |
353 }; |
350 |
354 |