//------------------------------------------------------------------- // __Pointer_MapX.cpp //------------------------------------------------------------------- #include using namespace std; #include "__Pointer_MapX.h" //------------------------------------------------------------------- // Global variable!!! __Pointer_MapX __pointer_mapX; //------------------------------------------------------------------- // Hash function #define HASH(a) (((unsigned int) a) % table_size) //------------------------------------------------------------------- // Public operations __Pointer_MapX::__Pointer_MapX () { int index = table_size - 1; nextstamp = 0; while (index >= 0) { map[index] = (Node*) 0; index--; } table_entries = 0; } //------------------------------------------------------------------- void __Pointer_MapX::Report_Error (char* msg, long l, char* f) { cerr << "\n\n==================================================="; if (l != 0) { cerr << "\n\nPointer error at:" << "\n\nFile: " << f << "\nLine #: " << l; } else { cerr << "\n\nPointer error:"; } cerr << "\n\n" << msg << "\n---------------------------------------------------\n"; exit (1); } //------------------------------------------------------------------- void __Pointer_MapX::Add (void* a, long& t, long l, char* f) { int index = HASH (a); Node* p = new Node; p->address = a; t = p->timestamp = nextstamp++; p->line_number = l; p->file_name = f; p->ref_count = 0; p->next = map[index]; map[index] = p; table_entries++; } //------------------------------------------------------------------- void __Pointer_MapX::Remove (void* a) { int index = HASH (a); Node* p = map[index]; if (p->address == a) { map[index] = p->next; delete p; } else { Node* q = p->next; while (q->address != a) { p = q; q = p->next; } p->next = q->next; delete q; } table_entries--; } //------------------------------------------------------------------- bool __Pointer_MapX::Contains (void* a, long t) { int index = HASH (a); Node* p = map[index]; while ((p != 0) && (p->address != a)) { p = p->next; } if (p == 0) { return false; } else { return p->timestamp == t; } } //------------------------------------------------------------------- void __Pointer_MapX::Increment_Ref_Count (void* a) { int index = HASH (a); Node* p = map[index]; while (p->address != a) { p = p->next; } p->ref_count++; } //------------------------------------------------------------------- void __Pointer_MapX::Decrement_Ref_Count (void* a) { int index = HASH (a); Node* p = map[index]; while (p->address != a) { p = p->next; } p->ref_count--; } //------------------------------------------------------------------- long __Pointer_MapX::Ref_Count (void* a) { int index = HASH (a); Node* p = map[index]; while (p->address != a) { p = p->next; } return p->ref_count; } //------------------------------------------------------------------- void __Pointer_MapX::Report_Allocation () { if (table_entries > 0) { int index = table_size - 1; cerr << "\n============================================================\n" << "Pointer report: currently allocated memory locations:\n" << "-----------------------------------------------------\n"; while (index >= 0) { Node* p = map[index]; while (p != 0) { cerr << p->address << " (allocated from line " << p->line_number << " of file " << p->file_name << ") ref = " << p->ref_count << "\n"; p = p->next; } index--; } cerr << "============================================================\n\n"; } } //------------------------------------------------------------------- void Report_Storage_Allocation () { __pointer_mapX.Report_Allocation (); }