Skip to content

Commit

Permalink
Added DB_InitZoneMem
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Maxxx committed Apr 8, 2018
1 parent 198693d commit 4588813
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 114 deletions.
3 changes: 2 additions & 1 deletion database/DatabaseModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ namespace database
CReplaceFunc::Create(0x0049EE50, 0x0049EE55, DB_GetStreamPos);
CReplaceFunc::Create(0x0049EE80, 0x0049EEBD, DB_IncStreamPos);


CReplaceFunc::Create(0x0044A9D0, 0x0044AA29, DB_InitXFile);
CReplaceFunc::Create(0x0044AA30, 0x0044AAF0, DB_UpdateXFileSizes);

CReplaceFunc::Create(0x0049EC60, 0x0049ECDE, DB_InitZoneMem);
}

CDatabaseModule* CDatabaseModule::GetModule()
Expand Down
124 changes: 13 additions & 111 deletions database/db_stream_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,63 +13,13 @@ namespace database

void DB_ReserveStreamMemory()
{
#pragma region One block allocation
#if 0
// Try to reserve all possible size in one piece: around 800 mb.
byte* pMem = static_cast<byte*>(VirtualAlloc(nullptr, STREAM_OUT_SIZE + STREAM_SIZE + TEMP_STREAM_SIZE, MEM_RESERVE, PAGE_READWRITE));
if (pMem)
{
g_streamOutMemory = pMem;
g_streamMemory = g_streamOutMemory + STREAM_OUT_SIZE;
g_tempStreamMemory = g_streamMemory + STREAM_SIZE;
g_bPatchedAlloc = true;
return;
}
#endif
#pragma endregion

#pragma region Original allocation
#if 0
g_bPatchedAlloc = false; // I don't actually know if this code can be called more than once.

// Oops, we failed. Let's fall back to original code.
g_streamOutMemory = static_cast<byte*>(VirtualAlloc(0, STREAM_OUT_SIZE, MEM_RESERVE, PAGE_READWRITE));
QASSERTMSG(g_streamOutMemory, "VirtualAlloc returned NULL; may be out of memory");

g_streamMemory = static_cast<byte*>(VirtualAlloc(0, STREAM_SIZE, MEM_RESERVE, PAGE_READWRITE));
QASSERTMSG(g_streamMemory, "VirtualAlloc returned NULL; may be out of memory");

g_tempStreamMemory = static_cast<byte*>(VirtualAlloc(0, TEMP_STREAM_SIZE, MEM_RESERVE, PAGE_READWRITE));
QASSERTMSG(g_tempStreamMemory, "VirtualAlloc returned NULL; may be out of memory");
#endif
#pragma endregion

#pragma region Static allocation
g_streamOutMemory = g_HeapMem;
g_streamMemory = g_streamOutMemory + STREAM_OUT_SIZE;
g_tempStreamMemory = g_streamMemory + STREAM_SIZE;
//g_bPatchedAlloc = true;
#pragma endregion
}

void DB_ReleaseStreamMemory()
{
#pragma region Original allocation
#if 0
VirtualFree(g_streamOutMemory, 0, MEM_RELEASE);

#pragma region One block allocation
// If we're reserving memory by patch logic then no additional release required.
// Otherwise we have to free other two data blocks.
if (!g_bPatchedAlloc)
{
VirtualFree(g_tempStreamMemory, 0, MEM_RELEASE);
VirtualFree(g_streamMemory, 0, MEM_RELEASE);
}
#pragma endregion
#endif
#pragma endregion

g_streamOutMemory = nullptr;
g_tempStreamMemory = nullptr;
g_streamMemory = nullptr;
Expand Down Expand Up @@ -107,12 +57,6 @@ namespace database
{
QASSERT(g_streamPosStart == g_streamMemory);

#pragma region Original allocation
#if 0
VirtualFree(g_streamMemory, STREAM_SIZE, MEM_DECOMMIT);
#endif
#pragma endregion

g_streamPosStart = 0;
g_streamPosSave = 0;
}
Expand All @@ -121,12 +65,6 @@ namespace database
{
QASSERT(g_tempStreamPosStart == g_tempStreamMemory);

#pragma region Original allocation
#if 0
VirtualFree(g_tempStreamMemory, TEMP_STREAM_SIZE, MEM_DECOMMIT);
#endif
#pragma endregion

g_tempStreamPosStart = 0;
g_tempStreamPos = 0;
}
Expand All @@ -135,12 +73,6 @@ namespace database
{
QASSERT(g_streamOut == g_streamOutMemory);

#pragma region Original allocation
#if 0
VirtualFree(g_streamOutMemory, STREAM_OUT_SIZE, MEM_DECOMMIT);
#endif
#pragma endregion

g_streamOut = 0;
}

Expand All @@ -150,18 +82,6 @@ namespace database
QASSERT(g_tempStreamPos >= g_tempStreamPosStart);
QASSERT(g_tempStreamPos + Size_ <= g_tempStreamPosStart + TEMP_STREAM_SIZE);

#pragma region Original allocation
#if 0
unsigned int startPage = PAGE_ROUNDUP((unsigned int)g_tempStreamPos.Raw());
unsigned int endPage = PAGE_ROUNDUP((unsigned int)g_tempStreamPos.Raw() + Size_);
if (startPage != endPage)
{
LPVOID buf = VirtualAlloc(reinterpret_cast<LPVOID>(startPage), endPage - startPage, MEM_COMMIT, PAGE_READWRITE);
QASSERT(buf);
}
#endif
#pragma endregion

byte* pMem = g_tempStreamPos;
g_tempStreamPos += Size_;
return pMem;
Expand All @@ -171,12 +91,6 @@ namespace database
{
QASSERT(g_tempStreamPosStart);

#pragma region Original allocation
#if 0
VirtualFree(g_tempStreamPosStart, TEMP_STREAM_SIZE - (g_tempStreamPosStart - g_tempStreamMemory), MEM_DECOMMIT);
#endif
#pragma endregion

g_tempStreamPos = g_tempStreamPosStart;
}

Expand Down Expand Up @@ -207,18 +121,6 @@ namespace database
{
QASSERT((g_streamOutPos + Size_) - g_streamOut <= STREAM_OUT_SIZE);

#pragma region Original allocation
#if 0
uint startPage = PAGE_ROUNDUP((uint)g_streamOutPos.Raw());
uint endPage = PAGE_ROUNDUP((uint)g_streamOutPos.Raw() + Size_);
if (startPage != endPage)
{
LPVOID buf = VirtualAlloc((LPVOID)startPage, endPage - startPage, MEM_COMMIT, PAGE_READWRITE);
QASSERT(buf);
}
#endif
#pragma endregion

memcpy(g_streamOutPos, Src_, Size_);
*Dst_ = g_streamOutPos;
g_streamOutPos += Size_;
Expand All @@ -235,18 +137,6 @@ namespace database
QASSERT(g_streamPosSave >= g_streamPosStart);
QASSERT(g_streamPosSave + Size_ <= g_streamPosStart + STREAM_SIZE);

#pragma region Original allocation
#if 0
uint startPage = PAGE_ROUNDUP((uint)g_streamPosSave.Raw());
uint endPage = PAGE_ROUNDUP((uint)g_streamPosSave.Raw() + Size_);
if (startPage != endPage)
{
auto buf = VirtualAlloc((LPVOID)startPage, endPage - startPage, MEM_COMMIT, PAGE_READWRITE);
QASSERT(buf);
}
#endif
#pragma endregion

byte* result = g_streamPosSave;
g_streamPosSave += Size_;
return result;
Expand Down Expand Up @@ -284,8 +174,20 @@ namespace database

for (int i = 1; i < MAX_XFILE_COUNT; ++i)
{
g_streamZoneMem.Raw()->blocks[i].size = g_streamBlockMem[i] - (uint)g_streamZoneMem.Raw()->blocks[i].data;
g_streamZoneMem.Raw()->blocks[i].size = (uint)g_streamBlockMem[i] - (uint)g_streamZoneMem.Raw()->blocks[i].data;
QASSERT(g_streamZoneMem.Raw()->blocks[i].size <= g_streamBlockSize[i]);
}
}

void DB_InitZoneMem(xfile_t *pXFile_)
{
g_streamZoneMem = pXFile_;
g_streamPos = pXFile_->blocks[0].data;
g_streamPosIndex = 0;
g_streamDelayIndex = 0;
g_streamSize = 0;
g_streamPosStackIndex = 0;
for (int i = 0; i < MAX_XFILE_COUNT; ++i)
g_streamBlockMem[i] = pXFile_->blocks[i].data;
}
}
2 changes: 2 additions & 0 deletions database/db_stream_save.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ namespace database

void _cdecl DB_InitXFile(xfile_t *pXFile_);
void _cdecl DB_UpdateXFileSizes();

void _cdecl DB_InitZoneMem(xfile_t *pXFile_);
} // end namespace database
3 changes: 2 additions & 1 deletion database/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ namespace database
CGlobalVar<int>& g_streamDelayIndex = CGlobalVar<int>::Create(0x118A54B4);
CGlobalArr<int, 0x2000>& g_streamDelayArray = CGlobalArr<int, 0x2000>::Create(0x118A54E8);
CGlobalVar<uint>& g_streamSize = CGlobalVar<uint>::Create(0x118A54BC);
CGlobalArr<uint, MAX_XFILE_COUNT>& g_streamBlockMem = CGlobalArr<uint, MAX_XFILE_COUNT>::Create(0x118A54C0);
CGlobalArr<byte*, MAX_XFILE_COUNT>& g_streamBlockMem = CGlobalArr<byte*, MAX_XFILE_COUNT>::Create(0x118A54C0);
CGlobalVar<uint>& g_streamPosStackIndex = CGlobalVar<uint>::Create(0x118A54A8);
}
3 changes: 2 additions & 1 deletion database/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace database
extern hooklib::CGlobalVar<int>& g_streamDelayIndex;
extern hooklib::CGlobalArr<int, 0x2000>& g_streamDelayArray;
extern hooklib::CGlobalVar<utility::uint>& g_streamSize;
extern hooklib::CGlobalArr<utility::uint, MAX_XFILE_COUNT>& g_streamBlockMem;
extern hooklib::CGlobalArr<utility::byte*, MAX_XFILE_COUNT>& g_streamBlockMem;
extern hooklib::CGlobalVar<utility::uint>& g_streamPosStackIndex;

extern utility::byte* g_streamMemory;
extern utility::byte* g_tempStreamMemory;
Expand Down

0 comments on commit 4588813

Please sign in to comment.