00001 // ZenLib::ZenUtils - Very small utilities 00002 // Copyright (C) 2002-2003 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 int8 LittleEndian2int8 (const uint8* List); 00058 uint8 LittleEndian2uint8 (const uint8* List); 00059 int16 LittleEndian2int16 (const uint8* List); 00060 uint16 LittleEndian2uint16 (const uint8* List); 00061 int32 LittleEndian2int32 (const uint8* List); 00062 uint32 LittleEndian2uint32 (const uint8* List); 00063 int64 LittleEndian2int64 (const uint8* List); 00064 uint64 LittleEndian2uint64 (const uint8* List); 00065 00066 //--------------------------------------------------------------------------- 00067 //Big Endians 00068 int8 BigEndian2int8 (const uint8* List); 00069 uint8 BigEndian2uint8 (const uint8* List); 00070 int16 BigEndian2int16 (const uint8* List); 00071 uint16 BigEndian2uint16 (const uint8* List); 00072 int32 BigEndian2int32 (const uint8* List); 00073 uint32 BigEndian2uint32 (const uint8* List); 00074 int64 BigEndian2int64 (const uint8* List); 00075 uint64 BigEndian2uint64 (const uint8* List); 00076 00077 //--------------------------------------------------------------------------- 00078 // int32 - int64 00079 #if (MAXTYPES >= 64) 00080 int64 int32_int64 ( int32 High, uint32 Low); 00081 int64 uint32_uint64 ( uint32 High, uint32 Low); 00082 void int64_int32 ( int64 BigInt, int32 &High, uint32 &Low); 00083 void uint64_uint32 (uint64 BigInt, uint32 &High, uint32 &Low); 00084 #endif 00085 00086 //--------------------------------------------------------------------------- 00087 // Floats and ints 00088 int32 Float2int32 (float F, int Rounded=1); 00089 uint32 Float2uint32 (float F, int Rounded=1); 00090 int64 Float2int64 (float F, int Rounded=1); 00091 uint64 Float2uint64 (float F, int Rounded=1); 00092 00093 } //namespace ZenLib 00094 00095 00096 /* 00097 #include <list> 00098 #include <stdio.h> 00099 00100 00101 typedef struct { 00102 DWORD address; 00103 DWORD size; 00104 char file[64]; 00105 DWORD line; 00106 } ALLOC_INFO; 00107 00108 typedef std::list<ALLOC_INFO*> AllocList; 00109 00110 AllocList *allocList; 00111 00112 void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum) 00113 { 00114 ALLOC_INFO *info; 00115 00116 if(!allocList) { 00117 allocList = new(AllocList); 00118 } 00119 00120 info = new(ALLOC_INFO); 00121 info->address = addr; 00122 strncpy(info->file, fname, 63); 00123 info->line = lnum; 00124 info->size = asize; 00125 allocList->insert(allocList->begin(), info); 00126 }; 00127 00128 void RemoveTrack(DWORD addr) 00129 { 00130 AllocList::iterator i; 00131 00132 if(!allocList) 00133 return; 00134 for(i = allocList->begin(); i != allocList->end(); i++) 00135 { 00136 if((*i)->address == addr) 00137 { 00138 allocList->remove((*i)); 00139 break; 00140 } 00141 } 00142 }; 00143 00144 #ifdef _DEBUG 00145 inline void * __cdecl operator new(unsigned int size, 00146 const char *file, int line) 00147 { 00148 void *ptr = (void *)malloc(size); 00149 AddTrack((DWORD)ptr, size, file, line); 00150 return(ptr); 00151 }; 00152 inline void * __cdecl operator new[](unsigned int size, 00153 const char *file, int line) 00154 { 00155 void *ptr = (void *)malloc(size); 00156 AddTrack((DWORD)ptr, size, file, line); 00157 return(ptr); 00158 }; 00159 inline void __cdecl operator delete(void *p) 00160 { 00161 RemoveTrack((DWORD)p); 00162 free(p); 00163 }; 00164 inline void __cdecl operator delete[](void *p) 00165 { 00166 RemoveTrack((DWORD)p); 00167 free(p); 00168 }; 00169 #define DEBUG_NEW new(__FILE__, __LINE__) 00170 #else 00171 #define DEBUG_NEW new 00172 #endif 00173 #define new DEBUG_NEW 00174 00175 00176 void DumpUnfreed() 00177 { 00178 AllocList::iterator i; 00179 DWORD totalSize = 0; 00180 00181 FILE *stream; 00182 stream = fopen("d:\\MemoryLeak.txt", "w+"); 00183 00184 00185 if(!allocList) 00186 return; 00187 00188 for(i = allocList->begin(); i != allocList->end(); i++) { 00189 fprintf(stream, "%-50s:\t\tLine %d,\t\tAddress %d\t%d unfreed\n", 00190 (*i)->file, (*i)->line, (*i)->address, (*i)->size); 00191 totalSize += (*i)->size; 00192 } 00193 fprintf(stream, "-----------------------------------------------------------\n"); 00194 fprintf(stream, "Total Unfreed: %d bytes\n", totalSize); 00195 }; 00196 00197 00198 */ 00199 #endif