1 ////////////////////////////////////////////////////////////////////////////////
3 // Author: Artur Szostak
4 // Email: artur@alice.phy.uct.ac.za | artursz@iafrica.com
6 ////////////////////////////////////////////////////////////////////////////////
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.
23 class CheckMemoryAlloc
30 fArrayAllocList = NULL;
33 #ifdef CHECK_MEMORY_ALLOC_ON_EXIT
36 Assert( fAllocList == NULL );
37 Assert( fArrayAllocList == NULL );
39 #endif // CHECK_MEMORY_ALLOC_ON_EXIT
41 void AddAlloc(void* ptr)
43 Node* newnode = (Node*) malloc( sizeof(Node) );
44 newnode->memory = ptr;
45 newnode->next = fAllocList;
49 bool RemoveAlloc(void* ptr)
51 Node* previous = NULL;
52 Node* current = fAllocList;
53 while (current != NULL)
55 if (current->memory == ptr) break;
57 current = current->next;
59 if (current == NULL) return false;
61 previous->next = current->next;
63 fAllocList = current->next;
64 free(( void*)current );
68 void AddArrayAlloc(void* ptr)
70 Node* newnode = (Node*) malloc( sizeof(Node) );
71 newnode->memory = ptr;
72 newnode->next = fArrayAllocList;
73 fArrayAllocList = newnode;
76 bool RemoveArrayAlloc(void* ptr)
78 Node* previous = NULL;
79 Node* current = fArrayAllocList;
80 while (current != NULL)
82 if (current->memory == ptr) break;
84 current = current->next;
86 if (current == NULL) return false;
88 previous->next = current->next;
90 fArrayAllocList = current->next;
91 free( (void*)current );
104 Node* fArrayAllocList;
110 }; // end of namespace
113 void* operator new (size_t size) throw (std::bad_alloc)
115 void* memory = malloc(size);
116 if (memory == NULL) AliHLTMUONCoreThrowOutOfMemory();
117 DebugMsg(99, "new(" << size << ") allocated: " << memory);
118 DebugCode( checkmem.AddAlloc(memory) );
123 void* operator new [] (size_t size) throw (std::bad_alloc)
125 void* memory = malloc(size);
126 if (memory == NULL) AliHLTMUONCoreThrowOutOfMemory();
127 DebugMsg(99, "new [] (" << size << ") allocated: " << memory);
128 DebugCode( checkmem.AddArrayAlloc(memory) );
133 void operator delete (void* memory) throw ()
135 DebugMsg(99, "delete(" << memory << ")");
136 DebugCode(Assert( checkmem.RemoveAlloc(memory) ));
141 void operator delete [] (void* memory) throw ()
143 DebugMsg(99, "delete [] (" << memory << ")");
144 DebugCode(Assert( checkmem.RemoveArrayAlloc(memory) ));