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