diff --git a/Include/Win32/CxbxKrnl/EmuAlloc.h b/Include/Win32/CxbxKrnl/EmuAlloc.h index e5bdadb32..f95569662 100644 --- a/Include/Win32/CxbxKrnl/EmuAlloc.h +++ b/Include/Win32/CxbxKrnl/EmuAlloc.h @@ -35,12 +35,13 @@ #include #ifdef _DEBUG_ALLOC -#define CxbxMalloc(x) CxbxMallocDebug(x, __FILE__, __LINE__) -#define CxbxCalloc(x, y) CxbxCallocDebug(x, y, __FILE__, __LINE__) -#define CxbxFree(x) CxbxFreeDebug(x, __FILE__, __LINE__) -#define CxbxRtlAlloc(Heap, Flags, Bytes) CxbxRtlAllocDebug(Heap, Flags, Bytes, __FILE__, __LINE__) -#define CxbxRtlFree(Heap, Flags, pMem) CxbxRtlFreeDebug(Heap, Flags, pMem, __FILE__, __LINE__) +#define CxbxMalloc(x) CxbxMallocDebug(x, __FILE__, __LINE__) +#define CxbxCalloc(x, y) CxbxCallocDebug(x, y, __FILE__, __LINE__) +#define CxbxFree(x) CxbxFreeDebug(x, __FILE__, __LINE__) +#define CxbxRtlAlloc(Heap, Flags, Bytes) CxbxRtlAllocDebug(Heap, Flags, Bytes, __FILE__, __LINE__) +#define CxbxRtlFree(Heap, Flags, pMem) CxbxRtlFreeDebug(Heap, Flags, pMem, __FILE__, __LINE__) #define CxbxRtlRealloc(Heap, Flags, pMem, Bytes) CxbxRtlReallocDebug(Heap, Flags, pMem, Bytes, __FILE__, __LINE__) +#define CxbxRtlSizeHeap(Heap, Flags, pMem) CxbxRtlSizeHeapDebug(Heap, Flags, pMem, __FILE__, __LINE__) // ****************************************************************** // * CxbxMallocDebug - Debug track malloc @@ -79,8 +80,8 @@ void *CxbxRtlAllocDebug(HANDLE Heap, BOOL CxbxRtlFreeDebug(HANDLE Heap, DWORD Flags, PVOID pMem, - char *pFile, - int Line); + char *pFile, + int Line); // ****************************************************************** // * CxbxRtlReallocDebug - Debug track RTL realloc @@ -89,8 +90,17 @@ void *CxbxRtlReallocDebug(HANDLE Heap, DWORD Flags, PVOID pMem, SIZE_T Bytes, - char *pFile, - int Line); + char *pFile, + int Line); + +// ****************************************************************** +// * CxbxRtlHeapSizeDebug - Debug track RTL heap size +// ****************************************************************** +SIZE_T CxbxRtlSizeHeapDebug(HANDLE Heap, + DWORD Flags, + PVOID pMem, + char *pFile, + int Line); // ****************************************************************** // * CxbxAllocDump - Dump the memory allocations @@ -98,12 +108,13 @@ void *CxbxRtlReallocDebug(HANDLE Heap, void CxbxAllocDump(bool DumpData); #else // _DEBUG_ALLOC -#define CxbxMalloc(x) malloc(x) -#define CxbxCalloc(x, y) calloc(x, y) -#define CxbxFree(x) free(x) -#define CxbxRtlAlloc(Heap, Flags, Bytes) NtDll::RtlAllocateHeap(Heap, Flags, Bytes) -#define CxbxRtlFree(Heap, Flags, pMem) NtDll::RtlFreeHeap(Heap, Flags, pMem) +#define CxbxMalloc(x) malloc(x) +#define CxbxCalloc(x, y) calloc(x, y) +#define CxbxFree(x) free(x) +#define CxbxRtlAlloc(Heap, Flags, Bytes) NtDll::RtlAllocateHeap(Heap, Flags, Bytes) +#define CxbxRtlFree(Heap, Flags, pMem) NtDll::RtlFreeHeap(Heap, Flags, pMem) #define CxbxRtlRealloc(Heap, Flags, pMem, Bytes) NtDll::RtlReAllocateHeap(Heap, Flags, pMem, Bytes) +#define CxbxRtlSizeHeap(Heap, Flags, pMem) NtDll::RtlSizeHeap(Heap, Flags, pMem) #endif #endif // EMUALLOC_H diff --git a/Source/Win32/CxbxKrnl/EmuAlloc.cpp b/Source/Win32/CxbxKrnl/EmuAlloc.cpp index 591e4786d..abd9f1172 100644 --- a/Source/Win32/CxbxKrnl/EmuAlloc.cpp +++ b/Source/Win32/CxbxKrnl/EmuAlloc.cpp @@ -400,8 +400,8 @@ void *CxbxRtlAllocDebug(HANDLE Heap, BOOL CxbxRtlFreeDebug(HANDLE Heap, DWORD Flags, PVOID pMem, - char *pFile, - int Line) + char *pFile, + int Line) { BOOL Ret = FALSE; if (pMem == NULL) @@ -443,14 +443,14 @@ BOOL CxbxRtlFreeDebug(HANDLE Heap, } // ****************************************************************** -// * CxbxRtlReallocDEbug - Debug track RTL realloc +// * CxbxRtlReallocDebug - Debug track RTL realloc // ****************************************************************** void *CxbxRtlReallocDebug(HANDLE Heap, DWORD Flags, PVOID pMem, SIZE_T Bytes, - char *pFile, - int Line) + char *pFile, + int Line) { void *pRetMem = NULL; g_MemoryMutex.Lock(); @@ -481,7 +481,7 @@ void *CxbxRtlReallocDebug(HANDLE Heap, pRealloc->pFile, pRealloc->Size, pRealloc->Line, Bytes, pFile, Line); } - void *pNewMem = NtDll::RtlReAllocateHeap(Heap, Flags, pMem, Bytes + 2 * sizeof(MEMORY_GUARD)); + void *pNewMem = NtDll::RtlReAllocateHeap(Heap, Flags, GetMemStart(pRealloc), Bytes + 2 * sizeof(MEMORY_GUARD)); free(pRealloc->pFile); free(pRealloc); if(!pNewMem) @@ -510,4 +510,44 @@ void *CxbxRtlReallocDebug(HANDLE Heap, return pRetMem; } +// ****************************************************************** +// * CxbxRtlSizeHeapDebug - Debug track RTL heap size +// ****************************************************************** +SIZE_T CxbxRtlSizeHeapDebug(HANDLE Heap, + DWORD Flags, + PVOID pMem, + char *pFile, + int Line) +{ + SIZE_T Size = 0; + g_MemoryMutex.Lock(); + + CXBX_MEMORY_BLOCK *pBlock = FindMemoryBlock(pMem); + if(pBlock == NULL) + { + printf("CxbxRtlSizeHeap: size heap on non-existent block: 0x%.08X! " + " File: %s\n" + " Line: %d\n", + pMem, pFile, Line); + } + else + { + SIZE_T ActualSize = NtDll::RtlSizeHeap(Heap, Flags, GetMemStart(pBlock)) + - 2 * sizeof(MEMORY_GUARD); + if(ActualSize != pBlock->Size) + { + printf("CxbxRtlSizeHeap: heap size mismatch, RtlSizeHeap: %d Tracker: %d\n" + " File : %s\n" + " Line : %d\n", + ActualSize, + pBlock->Size, + pFile, + Line); + } + Size = ActualSize; + } + + g_MemoryMutex.Unlock(); + return Size; +} #endif // _DEBUG_ALLOC \ No newline at end of file diff --git a/Source/Win32/CxbxKrnl/EmuXapi.cpp b/Source/Win32/CxbxKrnl/EmuXapi.cpp index 14097d930..2bda8b1d7 100644 --- a/Source/Win32/CxbxKrnl/EmuXapi.cpp +++ b/Source/Win32/CxbxKrnl/EmuXapi.cpp @@ -214,7 +214,7 @@ SIZE_T WINAPI XTL::EmuRtlSizeHeap GetCurrentThreadId(), hHeap, dwFlags, lpMem); //*/ - SIZE_T ret = NtDll::RtlSizeHeap(hHeap, dwFlags, lpMem); + SIZE_T ret = CxbxRtlSizeHeap(hHeap, dwFlags, lpMem); EmuSwapFS(); // XBox FS