#include "precompiled.h" #include "0ad_warning_disable.h" # include # include # include # include "sr_mem_control.h" # include "sr_output.h" //=== Memory Control ======================================================================= // the folowing structure is used only internally // I could have used a balanced tree, but since this is only for debug // it is better to maintain it as simple as possible and as a stand alone file. struct srMemData { const char* type; const char* file; int line; int size; void* addr; void set ( const char* c, char* f, int l, int s, void* a ) { type=c; file=f; line=l; size=s; addr=a; } }; // it is not nice to use such static things intead of classes, // but this is the only way to let the macro mechanism work. static bool changed=false; static srMemData *mem_data=0; static int mem_data_size=0; static int mem_data_capacity=0; static unsigned mem_bytes_used=0; static SrOutput& output=sr_out; static int cmp_func ( const void* pt1, const void* pt2 ) { const srMemData *a = (const srMemData*)pt1; const srMemData *b = (const srMemData*)pt2; int c = strcmp ( a->file, b->file ); if ( c!=0 ) return c; c = strcmp ( a->type, b->type ); if ( c!=0 ) return c; return b->size-a->size; } static void sort () { qsort ( mem_data, mem_data_size, sizeof(srMemData), cmp_func ); } void *sr_mem_control_alloc ( const char* type, char* file, int line, int size, void* addr ) { if ( !addr ) { //lineout (); output<<"\nsr_control_malloc: Zero Pointer Allocated!\n" << "File:"<0 && *file!='\\' && *file!='/' ) { file--; i--; } if ( i>0 ) file++; changed=true; mem_data[mem_data_size++].set ( type, file, line, size, addr ); mem_bytes_used += size; return addr; } bool sr_mem_control_free ( char* file, int line, void* addr ) { if ( !addr ) return true; for ( int i=0; i0 ) output.put ( " File Type Line Size Addr\n"); for ( int i=0; i