xref: /haiku/headers/private/kernel/debug_heap.h (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
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_calloc(size_t num, size_t size);
23 void				debug_free(void* address);
24 void				debug_heap_init();
25 
26 #ifdef __cplusplus
27 }
28 #endif
29 
30 
31 #ifdef __cplusplus
32 
33 struct kdebug_alloc_t {};
34 extern const kdebug_alloc_t kdebug_alloc;
35 
36 inline void*
37 operator new(size_t size, const kdebug_alloc_t&) throw()
38 {
39 	return debug_malloc(size);
40 }
41 
42 namespace DebugAlloc {
43 	template<typename Type>
44 	inline void
45 	destroy(Type* object)
46 	{
47 		if (object != NULL) {
48 			object->~Type();
49 			debug_free(object);
50 				// NOTE: Doesn't work for multiple inheritence!
51 		}
52 	}
53 }
54 
55 struct DebugAllocPoolScope {
56 	DebugAllocPoolScope()
57 	{
58 		fPool = create_debug_alloc_pool();
59 	}
60 
61 	~DebugAllocPoolScope()
62 	{
63 		delete_debug_alloc_pool(fPool);
64 	}
65 
66 private:
67 	DebugAllocPool*	fPool;
68 };
69 
70 #endif	// __cplusplus
71 
72 #endif	/* _KERNEL_DEBUG_HEAP_H */
73