Corrected overloading of operator new (icc)
[u/mrichter/AliRoot.git] / HLT / MUON / src / new.cxx
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                 {
29                         alloc_list = NULL;
30                         array_alloc_list = NULL;
31                 };
32         
33 #ifdef CHECK_MEMORY_ALLOC_ON_EXIT
34                 ~CheckMemoryAlloc()
35                 {
36                         Assert( alloc_list == NULL );
37                         Assert( array_alloc_list == NULL );
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;
45                         newnode->next = alloc_list;
46                         alloc_list = newnode;
47                 };
48                 
49                 bool RemoveAlloc(void* ptr)
50                 {
51                         Node* previous = NULL;
52                         Node* current = alloc_list;
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
63                                 alloc_list = current->next;
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;
72                         newnode->next = array_alloc_list;
73                         array_alloc_list = newnode;
74                 };
75                 
76                 bool RemoveArrayAlloc(void* ptr)
77                 {
78                         Node* previous = NULL;
79                         Node* current = array_alloc_list;
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
90                                 array_alloc_list = current->next;
91                         free( (void*)current );
92                         return true;
93                 };
94                 
95         private:
96         
97                 struct Node
98                 {
99                         void* memory;
100                         Node* next;
101                 };
102         
103                 Node* alloc_list;
104                 Node* array_alloc_list;
105         
106         } checkmem;
107         
108 #endif // DEBUG
109
110 }; // end of namespace
111
112
113 void* operator new (size_t size) throw (std::bad_alloc)
114 {
115         void* memory = malloc(size);
116         if (memory == NULL) dHLT::ThrowOutOfMemory();
117         DebugMsg(99, "new(" << size << ") allocated: " << memory);
118         DebugCode( checkmem.AddAlloc(memory) );
119         return memory;
120 };
121
122
123 void* operator new [] (size_t size) throw (std::bad_alloc)
124 {
125         void* memory = malloc(size);
126         if (memory == NULL) dHLT::ThrowOutOfMemory();
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