static TFileEntry * FindPatchEntry(TMPQArchive * ha, TFileEntry * pFileEntry) { TFileEntry * pPatchEntry = NULL; TFileEntry * pTempEntry; char szFileName[MAX_PATH+1]; // Go while there are patches while(ha->haPatch != NULL) { // Move to the patch archive ha = ha->haPatch; szFileName[0] = 0; // Prepare the prefix for the file name if(ha->pPatchPrefix != NULL) StringCopyA(szFileName, ha->pPatchPrefix->szPatchPrefix, MAX_PATH); StringCatA(szFileName, pFileEntry->szFileName, MAX_PATH); // Try to find the file there pTempEntry = GetFileEntryExact(ha, szFileName, 0, NULL); if(pTempEntry != NULL) pPatchEntry = pTempEntry; } // Return the found patch entry return pPatchEntry; }
//=========================================================== // // Desc: Add a new memory info to the hash table // If it successfully executes, it will return true, otherwise false. // // [In] // ptr - The start address allocated // size - The memory size of ptr // file - The file name calling this function // line - The line number calling this function // // [Return] // true/false // //=========================================================== // Hash table version bool new_AddMemoryInfo( void *ptr, unsigned int size, const char *file, int line ) { StructMemUnit *pMemUnit = (StructMemUnit*)malloc( sizeof(StructMemUnit) ); if( pMemUnit ) { // the name name of allocating this memory unit if( StringLengthA( file ) < NEW_MEM_FILE_NAME_MAX ) { //StringCopyA( pMemUnit->szFileName, NEW_MEM_FILE_NAME_MAX, file ); StringCopyA( pMemUnit->szFileName, file ); } else { StringCopyNA( pMemUnit->szFileName, file, 8 ); pMemUnit->szFileName[8] = '\0'; StringCatA( pMemUnit->szFileName, "..." ); StringCatA( pMemUnit->szFileName, &file[_g_long_string_size] ); } // the line number of allocating this memory unit pMemUnit->nFileLine = line; // the length of allocating this memory unit pMemUnit->nSize = size; // the address of allocating this memory unit pMemUnit->pPtr = ptr; // count the amnount of memory _g_new_memory.m_memory_used_cur += size; if( _g_new_memory.m_memory_used_cur > _g_new_memory.m_memory_used_max ) { _g_new_memory.m_memory_used_max = _g_new_memory.m_memory_used_cur; } // add this mem-unit to the _g_new_memory unsigned int dwAddress = (unsigned int)(pMemUnit->pPtr); _g_new_memory.AppendItem( (void*)&dwAddress, sizeof(dwAddress), pMemUnit ); } else { return false; } return true; }
static bool DoMPQSearch_FileEntry( TMPQSearch * hs, SFILE_FIND_DATA * lpFindFileData, TMPQArchive * ha, TMPQHash * pHashEntry, TFileEntry * pFileEntry) { TFileEntry * pPatchEntry; HANDLE hFile = NULL; const char * szFileName; size_t nPrefixLength = (ha->pPatchPrefix != NULL) ? ha->pPatchPrefix->nLength : 0; DWORD dwBlockIndex; char szNameBuff[MAX_PATH]; // Is it a file but not a patch file? if((pFileEntry->dwFlags & hs->dwFlagMask) == MPQ_FILE_EXISTS) { // Now we have to check if this file was not enumerated before if(!FileWasFoundBefore(ha, hs, pFileEntry)) { // if(pFileEntry != NULL && !_stricmp(pFileEntry->szFileName, "TriggerLibs\\NativeLib.galaxy")) // DebugBreak(); // Find a patch to this file pPatchEntry = FindPatchEntry(ha, pFileEntry); if(pPatchEntry == NULL) pPatchEntry = pFileEntry; // Prepare the block index dwBlockIndex = (DWORD)(pFileEntry - ha->pFileTable); // Get the file name. If it's not known, we will create pseudo-name szFileName = pFileEntry->szFileName; if(szFileName == NULL) { // Open the file by its pseudo-name. sprintf(szNameBuff, "File%08u.xxx", (unsigned int)dwBlockIndex); if(SFileOpenFileEx((HANDLE)hs->ha, szNameBuff, SFILE_OPEN_BASE_FILE, &hFile)) { SFileGetFileName(hFile, szNameBuff); szFileName = szNameBuff; SFileCloseFile(hFile); } } // If the file name is still NULL, we cannot include the file to search results if(szFileName != NULL) { // Check the file name against the wildcard if(CheckWildCard(szFileName + nPrefixLength, hs->szSearchMask)) { // Fill the found entry. hash entry and block index are taken from the base MPQ lpFindFileData->dwHashIndex = HASH_ENTRY_FREE; lpFindFileData->dwBlockIndex = dwBlockIndex; lpFindFileData->dwFileSize = pPatchEntry->dwFileSize; lpFindFileData->dwFileFlags = pPatchEntry->dwFlags; lpFindFileData->dwCompSize = pPatchEntry->dwCmpSize; lpFindFileData->lcLocale = 0; // pPatchEntry->lcLocale; // Fill the filetime lpFindFileData->dwFileTimeHi = (DWORD)(pPatchEntry->FileTime >> 32); lpFindFileData->dwFileTimeLo = (DWORD)(pPatchEntry->FileTime); // Fill-in the entries from hash table entry, if given if(pHashEntry != NULL) { lpFindFileData->dwHashIndex = (DWORD)(pHashEntry - ha->pHashTable); lpFindFileData->lcLocale = pHashEntry->lcLocale; } // Fill the file name and plain file name StringCopyA(lpFindFileData->cFileName, szFileName + nPrefixLength, MAX_PATH-1); lpFindFileData->szPlainName = (char *)GetPlainFileName(lpFindFileData->cFileName); return true; } } }
bool new_DumpMemoryInfo( const tchar *szFileNameT ) #if _MSC_VER >= 1400 { char szFileName[GT_PATH_MAX_LEN]; #ifdef BASEDEF_UNICODE OSA::UnicodeToAnsi(szFileNameT, -1, szFileName, GT_PATH_MAX_LEN); #else /* BASEDEF_UNICODE */ StringCopyA(szFileName, szFileNameT); #endif /* BASEDEF_UNICODE */ FILE *fp = NULL; static char buf[512]; if( 0 == StringLengthA( szFileName ) ) { // the file name is invalid return false; } fp = fopen( szFileName, "w" ); if( NULL == fp ) { return false; } sprintf_s( buf, "\n" ); fprintf( fp, buf ); sprintf_s( buf, " Debug File\n" ); fprintf( fp, buf ); sprintf_s( buf, "----------------------------------------------\n" ); fprintf( fp, buf ); sprintf_s( buf, " This file can help you to find memory leaks.\n" ); fprintf( fp, buf ); sprintf_s( buf, "----------------------------------------------\n" ); fprintf( fp, buf ); sprintf_s( buf, "\n" ); fprintf( fp, buf ); sprintf_s( buf, " Debug Information \n" ); fprintf( fp, buf ); sprintf_s( buf, "=====================\n" ); fprintf( fp, buf ); sprintf_s( buf, "\n" ); fprintf( fp, buf ); //---------------------------------------// fprintf( fp, "Checking memory leaks...\n" ); fprintf( fp, "\n" ); // check the memory list and release the memory units unfreed uint32 dwNumUnits = _g_new_memory.GetNumItems(); sprintf_s( buf, 256, "The Number of Memory-Units Unfreed : %ld\n", dwNumUnits ); fprintf( fp, buf ); fprintf( fp, "\n" ); uint32 dwSize = 0; HashObjectItem<StructMemUnit*>* pHashItem = NULL; StructMemUnit *pMemUnit = NULL; uint32 dwCount = 0; for( uint32 j=0; j<_g_new_memory.m_dwSizeOfMap; j++ ) { dwNumUnits = _g_new_memory.m_pResMap[j].m_ResList.GetNumObject(); if( dwNumUnits ) { if( _g_new_memory.m_pResMap[j].m_ResList.Start() ) { for( uint32 i=0; i<dwNumUnits; i++ ) { // Get a memory unit pHashItem = _g_new_memory.m_pResMap[j].m_ResList.GetObjectData(); pMemUnit = (StructMemUnit*)pHashItem->m_UserData; // Get the size of the memory dwSize += pMemUnit->nSize; // Output the buffer sprintf_s( buf, 256, "(%ld) - FILE: %s, LINE: %ld, SIZE: %ld Bytes\n", dwCount, pMemUnit->szFileName, pMemUnit->nFileLine, pMemUnit->nSize ); dwCount++; fprintf( fp, buf ); } // for(i) _g_new_memory.m_pResMap[j].m_ResList.End(); } } } // for(j) const unsigned int nOneKB = 1024; const unsigned int nOneMB = 1024*1024; fprintf( fp, "\n" ); if( dwSize < nOneKB ) { sprintf_s( buf, 256, "The Amount of Memory Unfreed : %ld Byte\n", dwSize ); } else if( dwSize >= nOneKB && dwSize < nOneMB) { sprintf_s( buf, 256, "The Amount of Memory Unfreed : %ld Byte(%.2f KB)\n", dwSize, (FLOAT)dwSize/1024.0f ); } else // dwSize >= 1024*1024 MB { sprintf_s( buf, 256, "The Amount of Memory Unfreed : %ld Byte(%.2f MB)\n", dwSize, (FLOAT)dwSize/(1024.0f*1024.0f) ); } fprintf( fp, buf ); if( _g_new_memory.m_memory_used_max < nOneMB ) { sprintf_s( buf, 256, "The Maximum Amount of Memory Used : %ld Byte\n", _g_new_memory.m_memory_used_max ); } else if( _g_new_memory.m_memory_used_max >= nOneKB && _g_new_memory.m_memory_used_max < nOneMB) { sprintf_s( buf, 256, "The Maximum Amount of Memory Used : %ld Byte/(%.2f KB)\n", _g_new_memory.m_memory_used_max, (FLOAT)_g_new_memory.m_memory_used_max/1024.0f ); } else // _g_new_memory.m_memory_used_max >= 1024*1024 KBs(1 MB) { sprintf_s( buf, 256, "The Maximum Amount of Memory Used : %ld Byte/(%.2f MB)\n", _g_new_memory.m_memory_used_max, (FLOAT)_g_new_memory.m_memory_used_max/(1024.0f*1024.0f) ); } fprintf( fp, buf ); fprintf( fp, "\n" ); //---------------------------------------// fclose( fp ); fp = NULL; return true; }