]>
Commit | Line | Data |
---|---|---|
8356cc1d | 1 | //////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // Author: Artur Szostak | |
4 | // Email: artur@alice.phy.uct.ac.za | artursz@iafrica.com | |
5 | // | |
6 | //////////////////////////////////////////////////////////////////////////////// | |
7 | ||
8 | #include "new.hpp" | |
9 | #include "Utils.hpp" | |
10 | #include <stdlib.h> | |
11 | ||
12 | namespace | |
13 | { | |
14 | ||
15 | #ifdef DEBUG | |
16 | ||
17 | /* Perform a check that on program termination all memory was released properly. | |
18 | The internal list structures are implemented as linked lists using malloc and | |
19 | free to allocate and release memory. | |
20 | Can not use new or delete operators because thats what we are checking, if new | |
21 | and delete were called properly. | |
22 | */ | |
23 | class CheckMemoryAlloc | |
24 | { | |
25 | public: | |
26 | ||
27 | CheckMemoryAlloc() | |
28 | { | |
69d7cf2e | 29 | fAllocList = NULL; |
30 | fArrayAllocList = NULL; | |
8356cc1d | 31 | }; |
32 | ||
33 | #ifdef CHECK_MEMORY_ALLOC_ON_EXIT | |
34 | ~CheckMemoryAlloc() | |
35 | { | |
69d7cf2e | 36 | Assert( fAllocList == NULL ); |
37 | Assert( fArrayAllocList == NULL ); | |
8356cc1d | 38 | }; |
39 | #endif // CHECK_MEMORY_ALLOC_ON_EXIT | |
40 | ||
41 | void AddAlloc(void* ptr) | |
42 | { | |
43 | Node* newnode = (Node*) malloc( sizeof(Node) ); | |
44 | newnode->memory = ptr; | |
69d7cf2e | 45 | newnode->next = fAllocList; |
46 | fAllocList = newnode; | |
8356cc1d | 47 | }; |
48 | ||
49 | bool RemoveAlloc(void* ptr) | |
50 | { | |
51 | Node* previous = NULL; | |
69d7cf2e | 52 | Node* current = fAllocList; |
8356cc1d | 53 | while (current != NULL) |
54 | { | |
55 | if (current->memory == ptr) break; | |
56 | previous = current; | |
57 | current = current->next; | |
58 | }; | |
59 | if (current == NULL) return false; | |
60 | if (previous != NULL) | |
61 | previous->next = current->next; | |
62 | else | |
69d7cf2e | 63 | fAllocList = current->next; |
8356cc1d | 64 | free(( void*)current ); |
65 | return true; | |
66 | }; | |
67 | ||
68 | void AddArrayAlloc(void* ptr) | |
69 | { | |
70 | Node* newnode = (Node*) malloc( sizeof(Node) ); | |
71 | newnode->memory = ptr; | |
69d7cf2e | 72 | newnode->next = fArrayAllocList; |
73 | fArrayAllocList = newnode; | |
8356cc1d | 74 | }; |
75 | ||
76 | bool RemoveArrayAlloc(void* ptr) | |
77 | { | |
78 | Node* previous = NULL; | |
69d7cf2e | 79 | Node* current = fArrayAllocList; |
8356cc1d | 80 | while (current != NULL) |
81 | { | |
82 | if (current->memory == ptr) break; | |
83 | previous = current; | |
84 | current = current->next; | |
85 | }; | |
86 | if (current == NULL) return false; | |
87 | if (previous != NULL) | |
88 | previous->next = current->next; | |
89 | else | |
69d7cf2e | 90 | fArrayAllocList = current->next; |
8356cc1d | 91 | free( (void*)current ); |
92 | return true; | |
93 | }; | |
94 | ||
95 | private: | |
96 | ||
97 | struct Node | |
98 | { | |
99 | void* memory; | |
100 | Node* next; | |
101 | }; | |
102 | ||
69d7cf2e | 103 | Node* fAllocList; |
104 | Node* fArrayAllocList; | |
8356cc1d | 105 | |
106 | } checkmem; | |
107 | ||
108 | #endif // DEBUG | |
109 | ||
110 | }; // end of namespace | |
111 | ||
112 | ||
797bcdee | 113 | void* operator new (size_t size) throw (std::bad_alloc) |
8356cc1d | 114 | { |
115 | void* memory = malloc(size); | |
69d7cf2e | 116 | if (memory == NULL) AliHLTMUONCoreThrowOutOfMemory(); |
8356cc1d | 117 | DebugMsg(99, "new(" << size << ") allocated: " << memory); |
118 | DebugCode( checkmem.AddAlloc(memory) ); | |
119 | return memory; | |
120 | }; | |
121 | ||
122 | ||
797bcdee | 123 | void* operator new [] (size_t size) throw (std::bad_alloc) |
8356cc1d | 124 | { |
125 | void* memory = malloc(size); | |
69d7cf2e | 126 | if (memory == NULL) AliHLTMUONCoreThrowOutOfMemory(); |
8356cc1d | 127 | DebugMsg(99, "new [] (" << size << ") allocated: " << memory); |
128 | DebugCode( checkmem.AddArrayAlloc(memory) ); | |
129 | return memory; | |
130 | }; | |
131 | ||
132 | ||
133 | void operator delete (void* memory) throw () | |
134 | { | |
135 | DebugMsg(99, "delete(" << memory << ")"); | |
136 | DebugCode(Assert( checkmem.RemoveAlloc(memory) )); | |
137 | free(memory); | |
138 | }; | |
139 | ||
140 | ||
141 | void operator delete [] (void* memory) throw () | |
142 | { | |
143 | DebugMsg(99, "delete [] (" << memory << ")"); | |
144 | DebugCode(Assert( checkmem.RemoveArrayAlloc(memory) )); | |
145 | free(memory); | |
146 | }; | |
147 |