MmAlloc[*] aligment compensation on MmFree[*]

This commit is contained in:
Aaron Robinson 2003-11-10 02:42:51 +00:00
parent 75cbed605b
commit 00c15cd462
1 changed files with 62 additions and 0 deletions

View File

@ -72,6 +72,15 @@ PCSTProxyParam;
// Global Variable(s)
extern PVOID g_pfnThreadNotification = NULL;
// MmAllocateContiguousMemory[Ex] allocate unaligned data and then manually align the pointer,
// returning this to the Xbe. The original pointer must be retained in order to properly free.
struct _AlignCache
{
PVOID pOrigPtr;
PVOID pAligPtr;
}
g_pAlignCache[32] = {0};
// PsCreateSystemThread proxy procedure
#pragma warning(push)
#pragma warning(disable: 4731) // disable ebp modification warning
@ -730,6 +739,21 @@ XBSYSAPI EXPORTNUM(165) xboxkrnl::PVOID NTAPI xboxkrnl::MmAllocateContiguousMemo
}
#endif
DWORD dwAlignIndex = -1;
for(int v=0;v<32;v++)
{
if(g_pAlignCache[v].pOrigPtr == 0)
{
dwAlignIndex = v;
break;
}
}
// verify we have enough align cache entries
if(dwAlignIndex = -1)
EmuCleanup("Out of AlignCache slots!");
//
// NOTE: Kludgey (but necessary) solution:
//
@ -739,6 +763,8 @@ XBSYSAPI EXPORTNUM(165) xboxkrnl::PVOID NTAPI xboxkrnl::MmAllocateContiguousMemo
PVOID pRet = malloc(NumberOfBytes + 0x1000);
g_pAlignCache[dwAlignIndex].pOrigPtr = pRet;
// align to page boundary
{
DWORD dwRet = (DWORD)pRet;
@ -746,6 +772,8 @@ XBSYSAPI EXPORTNUM(165) xboxkrnl::PVOID NTAPI xboxkrnl::MmAllocateContiguousMemo
dwRet += 0x1000 - dwRet%0x1000;
pRet = (PVOID)dwRet;
g_pAlignCache[dwAlignIndex].pAligPtr = pRet;
}
#ifdef _DEBUG_TRACE
@ -787,6 +815,21 @@ XBSYSAPI EXPORTNUM(166) xboxkrnl::PVOID NTAPI xboxkrnl::MmAllocateContiguousMemo
}
#endif
DWORD dwAlignIndex = -1;
for(int v=0;v<32;v++)
{
if(g_pAlignCache[v].pOrigPtr == 0)
{
dwAlignIndex = v;
break;
}
}
// verify we have enough align cache entries
if(dwAlignIndex == -1)
EmuCleanup("Out of AlignCache slots!");
//
// NOTE: Kludgey (but necessary) solution:
//
@ -795,6 +838,8 @@ XBSYSAPI EXPORTNUM(166) xboxkrnl::PVOID NTAPI xboxkrnl::MmAllocateContiguousMemo
//
PVOID pRet = malloc(NumberOfBytes + 0x1000);
g_pAlignCache[dwAlignIndex].pOrigPtr = pRet;
// align to page boundary
{
@ -803,6 +848,8 @@ XBSYSAPI EXPORTNUM(166) xboxkrnl::PVOID NTAPI xboxkrnl::MmAllocateContiguousMemo
dwRet += 0x1000 - dwRet%0x1000;
pRet = (PVOID)dwRet;
g_pAlignCache[dwAlignIndex].pAligPtr = pRet;
}
static int count = 0;
@ -873,6 +920,21 @@ XBSYSAPI EXPORTNUM(171) VOID NTAPI xboxkrnl::MmFreeContiguousMemory
}
#endif
// retrieve correct allocation base address (since MmAllocContiguousMemory[Ex] aligns upward)
{
int v=0;
for(v=0;v<32;v++)
{
if(g_pAlignCache[v].pAligPtr == BaseAddress)
{
BaseAddress = g_pAlignCache[v].pOrigPtr;
g_pAlignCache[v].pAligPtr = 0;
g_pAlignCache[v].pOrigPtr = 0;
break;
}
}
}
if(BaseAddress != &xLaunchDataPage)
free(BaseAddress);