//------------------------------------------------------------------- // __Pointer_Map.cpp //------------------------------------------------------------------- #include #include "__Pointer_Map.h" //------------------------------------------------------------------- // Hash function #define HASH(a) (((unsigned int) a) % table_size) //------------------------------------------------------------------- // Public operations __Pointer_Map::__Pointer_Map () { int index = table_size - 1; nextstamp = 0; while (index >= 0) { map[index] = (Node*) 0; index--; } } //------------------------------------------------------------------- void __Pointer_Map::Report_Error (char* msg, long l, char* f) { cerr << "Pointer error"; if (l != 0) { cerr << " (at line " << l << " in file " << f << ")"; } cerr << "\n " << msg << '\n'; exit (1); } //------------------------------------------------------------------- void __Pointer_Map::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->next = map[index]; map[index] = p; } //------------------------------------------------------------------- void __Pointer_Map::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; } } //------------------------------------------------------------------- bool __Pointer_Map::Contains (void* a, long t) { int index = HASH (a); Node* p = map[index]; if (p == 0) { return false; } else if (p->address == a) { return p->timestamp == t; } else { Node* q = p->next; while (q != 0) { if (q->address == a) { p->next = q-> next; q->next = map[index]; map[index] = q; return q->timestamp == t; } else { p = q; q = p->next; } } return false; } } //------------------------------------------------------------------- void __Pointer_Map::Report_Allocation () { int index = table_size - 1; cerr << "=====================================================\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 << ")\n"; p = p->next; } index--; } cerr << "=====================================================\n"; }