1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _KERNEL_DEBUG_HEAP_H 6 #define _KERNEL_DEBUG_HEAP_H 7 8 #include <debug.h> 9 10 11 struct DebugAllocPool; 12 typedef struct DebugAllocPool debug_alloc_pool; 13 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 debug_alloc_pool* create_debug_alloc_pool(); 20 void delete_debug_alloc_pool(debug_alloc_pool* pool); 21 void* debug_malloc(size_t size); 22 void debug_free(void* address); 23 void debug_heap_init(); 24 25 #ifdef __cplusplus 26 } 27 #endif 28 29 30 #ifdef __cplusplus 31 32 struct kdebug_alloc_t {}; 33 extern const kdebug_alloc_t kdebug_alloc; 34 35 inline void* 36 operator new(size_t size, const kdebug_alloc_t&) throw() 37 { 38 return debug_malloc(size); 39 } 40 41 namespace DebugAlloc { 42 template<typename Type> 43 inline void 44 destroy(Type* object) 45 { 46 if (object != NULL) { 47 object->~Type(); 48 debug_free(object); 49 // NOTE: Doesn't work for multiple inheritence! 50 } 51 } 52 } 53 54 struct DebugAllocPoolScope { 55 DebugAllocPoolScope() 56 { 57 fPool = create_debug_alloc_pool(); 58 } 59 60 ~DebugAllocPoolScope() 61 { 62 delete_debug_alloc_pool(fPool); 63 } 64 65 private: 66 DebugAllocPool* fPool; 67 }; 68 69 #endif // __cplusplus 70 71 #endif /* _KERNEL_DEBUG_HEAP_H */ 72