/*code copyright by Gabriel Sechan. This code may be distributed, so long as this header remains in it unchanged and the distribution is free. All comments, bug fixes, great new ideas, additions you want in it, additions you have written in it and wish to spread, money, death threats, proposals of marriage (attractive females only), less formal proposals (same disclaimer), and ritualised worship of me can bve sent to gsechan@hotmail.com */ #ifndef GABEDEBUGLIB #define GABEDEBUGLIB #include #include #include #include // Set up constants to allow us to switch from runtime to debugtime // builds easily #ifdef DEBUGVERSION #define NewMalloc LibMalloc #define NewFree LibFree #define EnterFunction EnterFunc #define LeaveFunction LeaveFunc #define InitDebugLib InitDebugLibOn #define Delete LibDelete #define New LibNew #else #define NewMalloc WrapperMalloc #define NewFree WrapperFree #define EnterFunction WrapperEnterFunc #define LeaveFunction WrapperLeaveFunc #define InitDebugLib InitDebugLibOff #define Delete WrapperDelete #define New WrapperNew #endif #ifndef NULL #define NULL 0 #endif #define VOIDPTR void*& //generic linked list class, used in multiple places template class LibLinkedList{ class Node{ public: T data; Node* next; Node* prev; }; int size; Node *current,*head,*tail; void CopyAll(LibLinkedList&); void DeleteAll(); public: LibLinkedList(); LibLinkedList(LibLinkedList&); LibLinkedList& operator= (LibLinkedList&); ~LibLinkedList(); int InsertBefore(T); int InsertBefore(T,int); int InsertAfter(T); int InsertAfter(T,int); int Remove(); int Remove(int); int Get(T&); int Get(T&,int); void Goto(int); void First(); void Last(); void Next(); void Prev(); int Size(); }; //a class to hold cumulative information about a named ptr class NamedStats{ char* name; //name of pointer int last; //1 if last was an alloc, 0 if dealloc unsigned int numAllocs; //number of times named ptr was allocated unsigned int numDeallocs; //number of times it was deallocated unsigned int amountAllocated; //amount of memory allocated in total void CopyAll(NamedStats&); //copies a class to another instance void DeleteAll(); //deletes all dynamic memory public: NamedStats(char*); //constructors and Big 3 NamedStats(); NamedStats(NamedStats&); NamedStats& operator= (NamedStats&); ~NamedStats(); char* GetName(); //get functions unsigned int GetNumAllocs(); unsigned int GetTotalAllocs(); unsigned int GetNumDeallocs(); int GetLast(); void Dealloc(); //tells named location you have freed last alloc void Alloc(int); //tells named location you have allocated it }; //class to hold info about an allocated ptr class PtrData{ void* data; //actual ptr allocated unsigned int size; //amount allocated char *name; void CopyAll(PtrData&); //helper functions void DeleteAll(); public: PtrData(void*,int,char*); //constructors and big 3 PtrData(); PtrData(PtrData&); PtrData& operator = (PtrData&); ~PtrData(); unsigned int GetSize(); //get functions void* GetData(); char* GetName(); }; //our malloc functions. int LibMalloc(unsigned int,VOIDPTR,char* name=NULL); void* LibMalloc(unsigned int,char* name=NULL); // wrapper func, should just call malloc int WrapperMalloc(unsigned int,VOIDPTR,char* name=NULL); void* WrapperMalloc(unsigned int,char* name=NULL); //our free function int LibFree(void*,char* name=NULL); //wrapper for free int WrapperFree(void*,char* name=NULL); //starts debug library int InitDebugLib(FILE*,int); int InitDebugLib(ostream*,int); //free up lib resources void FreeDebugLib(); //add a new function to the stack void EnterFunc(char*); //remove one from stack void LeaveFunc(); //wrappers for above two void WrapperEnterFunc(char* func); void WrapperLeaveFunc(); //free all memory allocations not made by this library void FreeAll(); //Make a comment in the debugging log void NoteEvent(char*); //New. hook for new to get around broken compilers template T* LibNew(unsigned int,T*,char* name=NULL); template T* WrapperNew(unsigned int,T*,char* name=NULL); //Delete function. A hack to get delete calls to work. Should be //called before using the delete keyword. template void LibDelete(T* ptr,char* name=NULL); template void WrapperDelete(T* ptr,char*name=NULL); //show statistics for all memory names and the total program void ShowDebugStatistics(); //shows statistics for a name void ShowDebugStatistics(char*); #endif