00001 // ZenLib::Utils - Very small utilities 00002 // Copyright (C) 2002-2004 Jérôme Martinez, Zen@MediaArea.net 00003 // 00004 // This software is provided 'as-is', without any express or implied 00005 // warranty. In no event will the authors be held liable for any damages 00006 // arising from the use of this software. 00007 // 00008 // Permission is granted to anyone to use this software for any purpose, 00009 // including commercial applications, and to alter it and redistribute it 00010 // freely, subject to the following restrictions: 00011 // 00012 // 1. The origin of this software must not be misrepresented; you must not 00013 // claim that you wrote the original software. If you use this software 00014 // in a product, an acknowledgment in the product documentation would be 00015 // appreciated but is not required. 00016 // 2. Altered source versions must be plainly marked as such, and must not be 00017 // misrepresented as being the original software. 00018 // 3. This notice may not be removed or altered from any source distribution. 00019 // 00020 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00021 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00022 // ZenUtils 00023 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00024 // 00025 // Version 0.1.0 00026 // ------------- 00027 // BigEndianToInt (8-64 bits) 00028 // LittleEndianToInt (8-64 bits) 00029 // 00030 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00031 // 00032 // 0.1.0 (Zen@MediaArea.net) 00033 // BigEndianToInt (8-64 bits) 00034 // LittleEndianToInt (8-64 bits) 00035 // 00036 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00037 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00038 00039 //--------------------------------------------------------------------------- 00040 #ifndef ZenUtilsH 00041 #define ZenUtilsH 00042 //--------------------------------------------------------------------------- 00043 00044 //--------------------------------------------------------------------------- 00045 #include <ZenLib/Conf.h> 00046 //--------------------------------------------------------------------------- 00047 00048 namespace ZenLib 00049 { 00050 00051 //*************************************************************************** 00052 // Integer transformations 00053 //*************************************************************************** 00054 00055 //--------------------------------------------------------------------------- 00056 //Little Endians 00057 int8s LittleEndian2int8s (const char* List); 00058 int8u LittleEndian2int8u (const char* List); 00059 int16s LittleEndian2int16s (const char* List); 00060 int16u LittleEndian2int16u (const char* List); 00061 int32s LittleEndian2int32s (const char* List); 00062 int32u LittleEndian2int32u (const char* List); 00063 #if (MAXTYPE_INT >= 64) 00064 int64s LittleEndian2int64s (const char* List); 00065 int64u LittleEndian2int64u (const char* List); 00066 #endif 00067 00068 //--------------------------------------------------------------------------- 00069 //Big Endians 00070 int8s BigEndian2int8s (const char* List); 00071 int8u BigEndian2int8u (const char* List); 00072 int16s BigEndian2int16s (const char* List); 00073 int16u BigEndian2int16u (const char* List); 00074 int32s BigEndian2int32s (const char* List); 00075 int32u BigEndian2int32u (const char* List); 00076 #if (MAXTYPE_INT >= 64) 00077 int64s BigEndian2int64s (const char* List); 00078 int64u BigEndian2int64u (const char* List); 00079 #endif 00080 00081 //--------------------------------------------------------------------------- 00082 // int32 - int64 00083 #if (MAXTYPE_INT >= 64) 00084 int64s int32s_int64s ( int32s High, int32u Low); 00085 int64u int32u_int64u ( int32u High, int32u Low); 00086 void int32s_int64s (int64s &BigInt, int32s High, int32u Low); 00087 void int32u_int64u (int64s &BigInt, int32u High, int32u Low); 00088 void int64s_int32s (int64s BigInt, int32s &High, int32u &Low); 00089 void int64u_int32u (int64u BigInt, int32u &High, int32u &Low); 00090 #endif 00091 00092 //--------------------------------------------------------------------------- 00093 // Floats and ints 00094 int32s float32_int32s (float32 F, bool Rounded=true); 00095 int64s float32_int64s (float32 F, bool Rounded=true); 00096 int32s float64_int32s (float64 F, bool Rounded=true); 00097 int64s float64_int64s (float64 F, bool Rounded=true); 00098 00099 } //namespace ZenLib 00100 00101 00102 /* 00103 #include <list> 00104 #include <stdio.h> 00105 00106 00107 typedef struct { 00108 DWORD address; 00109 DWORD size; 00110 char file[64]; 00111 DWORD line; 00112 } ALLOC_INFO; 00113 00114 typedef std::list<ALLOC_INFO*> AllocList; 00115 00116 AllocList *allocList; 00117 00118 void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum) 00119 { 00120 ALLOC_INFO *info; 00121 00122 if(!allocList) { 00123 allocList = new(AllocList); 00124 } 00125 00126 info = new(ALLOC_INFO); 00127 info->address = addr; 00128 strncpy(info->file, fname, 63); 00129 info->line = lnum; 00130 info->size = asize; 00131 allocList->insert(allocList->begin(), info); 00132 }; 00133 00134 void RemoveTrack(DWORD addr) 00135 { 00136 AllocList::iterator i; 00137 00138 if(!allocList) 00139 return; 00140 for(i = allocList->begin(); i != allocList->end(); i++) 00141 { 00142 if((*i)->address == addr) 00143 { 00144 allocList->remove((*i)); 00145 break; 00146 } 00147 } 00148 }; 00149 00150 #ifdef _DEBUG 00151 inline void * __cdecl operator new(unsigned int size, 00152 const char *file, int line) 00153 { 00154 void *ptr = (void *)malloc(size); 00155 AddTrack((DWORD)ptr, size, file, line); 00156 return(ptr); 00157 }; 00158 inline void * __cdecl operator new[](unsigned int size, 00159 const char *file, int line) 00160 { 00161 void *ptr = (void *)malloc(size); 00162 AddTrack((DWORD)ptr, size, file, line); 00163 return(ptr); 00164 }; 00165 inline void __cdecl operator delete(void *p) 00166 { 00167 RemoveTrack((DWORD)p); 00168 free(p); 00169 }; 00170 inline void __cdecl operator delete[](void *p) 00171 { 00172 RemoveTrack((DWORD)p); 00173 free(p); 00174 }; 00175 #define DEBUG_NEW new(__FILE__, __LINE__) 00176 #else 00177 #define DEBUG_NEW new 00178 #endif 00179 #define new DEBUG_NEW 00180 00181 00182 void DumpUnfreed() 00183 { 00184 AllocList::iterator i; 00185 DWORD totalSize = 0; 00186 00187 FILE *stream; 00188 stream = fopen("d:\\MemoryLeak.txt", "w+"); 00189 00190 00191 if(!allocList) 00192 return; 00193 00194 for(i = allocList->begin(); i != allocList->end(); i++) { 00195 fprintf(stream, "%-50s:\t\tLine %d,\t\tAddress %d\t%d unfreed\n", 00196 (*i)->file, (*i)->line, (*i)->address, (*i)->size); 00197 totalSize += (*i)->size; 00198 } 00199 fprintf(stream, "-----------------------------------------------------------\n"); 00200 fprintf(stream, "Total Unfreed: %d bytes\n", totalSize); 00201 }; 00202 00203 00204 */ 00205 #endif