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 // These functions are used because MSVC6 isn't able to convert an unsigned int64 to a floating-point value, and I couldn't think of a cleaner way to handle it. 00100 #if _MSC_VER <= 1200 00101 inline float32 int64u_float32 (int64u v) { return static_cast<float32>(static_cast<int64s>(v>>1))*2.0f + static_cast<float32>(static_cast<int64s>(v & 1)); } 00102 inline float64 int64u_float64 (int64u v) { return static_cast<float64>(static_cast<int64s>(v>>1))*2.0f + static_cast<float32>(static_cast<int64s>(v & 1)); } 00103 #else 00104 #pragma 00105 #pragma warning( disable : 4244 ) 00106 inline float32 int64u_float32 (int64u v) { return v; } 00107 inline float64 int64u_float64 (int64u v) { return v; } 00108 #pragma warning( default : 4244 ) 00109 #endif 00110 00111 } //namespace ZenLib 00112 00113 00114 /* 00115 #include <list> 00116 #include <stdio.h> 00117 00118 00119 typedef struct { 00120 DWORD address; 00121 DWORD size; 00122 char file[64]; 00123 DWORD line; 00124 } ALLOC_INFO; 00125 00126 typedef std::list<ALLOC_INFO*> AllocList; 00127 00128 AllocList *allocList; 00129 00130 void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum) 00131 { 00132 ALLOC_INFO *info; 00133 00134 if(!allocList) { 00135 allocList = new(AllocList); 00136 } 00137 00138 info = new(ALLOC_INFO); 00139 info->address = addr; 00140 strncpy(info->file, fname, 63); 00141 info->line = lnum; 00142 info->size = asize; 00143 allocList->insert(allocList->begin(), info); 00144 }; 00145 00146 void RemoveTrack(DWORD addr) 00147 { 00148 AllocList::iterator i; 00149 00150 if(!allocList) 00151 return; 00152 for(i = allocList->begin(); i != allocList->end(); i++) 00153 { 00154 if((*i)->address == addr) 00155 { 00156 allocList->remove((*i)); 00157 break; 00158 } 00159 } 00160 }; 00161 00162 #ifdef _DEBUG 00163 inline void * __cdecl operator new(unsigned int size, 00164 const char *file, int line) 00165 { 00166 void *ptr = (void *)malloc(size); 00167 AddTrack((DWORD)ptr, size, file, line); 00168 return(ptr); 00169 }; 00170 inline void * __cdecl operator new[](unsigned int size, 00171 const char *file, int line) 00172 { 00173 void *ptr = (void *)malloc(size); 00174 AddTrack((DWORD)ptr, size, file, line); 00175 return(ptr); 00176 }; 00177 inline void __cdecl operator delete(void *p) 00178 { 00179 RemoveTrack((DWORD)p); 00180 free(p); 00181 }; 00182 inline void __cdecl operator delete[](void *p) 00183 { 00184 RemoveTrack((DWORD)p); 00185 free(p); 00186 }; 00187 #define DEBUG_NEW new(__FILE__, __LINE__) 00188 #else 00189 #define DEBUG_NEW new 00190 #endif 00191 #define new DEBUG_NEW 00192 00193 00194 void DumpUnfreed() 00195 { 00196 AllocList::iterator i; 00197 DWORD totalSize = 0; 00198 00199 FILE *stream; 00200 stream = fopen("d:\\MemoryLeak.txt", "w+"); 00201 00202 00203 if(!allocList) 00204 return; 00205 00206 for(i = allocList->begin(); i != allocList->end(); i++) { 00207 fprintf(stream, "%-50s:\t\tLine %d,\t\tAddress %d\t%d unfreed\n", 00208 (*i)->file, (*i)->line, (*i)->address, (*i)->size); 00209 totalSize += (*i)->size; 00210 } 00211 fprintf(stream, "-----------------------------------------------------------\n"); 00212 fprintf(stream, "Total Unfreed: %d bytes\n", totalSize); 00213 }; 00214 00215 00216 */ 00217 #endif