[ScriptError]: Cleanup (#741)

This commit is contained in:
Edo 2023-01-28 17:57:13 +00:00 committed by GitHub
parent 5b22de1e8f
commit 74a070042a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 101 additions and 56 deletions

View File

@ -1,4 +1,6 @@
#include <STDInclude.hpp>
#include "Game/Engine/Hunk.hpp"
#include "ScriptError.hpp"
#define SCRIPT_ERROR_PATCH
@ -12,8 +14,6 @@ namespace Components
Game::scrParserGlob_t ScriptError::scrParserGlob;
Game::scrParserPub_t ScriptError::scrParserPub;
Game::HunkUser* ScriptError::g_debugUser;
int ScriptError::Scr_IsInOpcodeMemory(const char* pos)
{
assert(Game::scrVarPub->programBuffer);
@ -569,7 +569,7 @@ namespace Components
{
for (unsigned int i = 0; i < scrParserPub.sourceBufferLookupLen; ++i)
{
Hunk_FreeDebugMem(scrParserPub.sourceBufferLookup[i].buf);
Game::Engine::Hunk_FreeDebugMem(scrParserPub.sourceBufferLookup[i].buf);
}
Z_VirtualFree(scrParserPub.sourceBufferLookup);
@ -582,11 +582,11 @@ namespace Components
{
if (scrParserGlob.saveSourceBufferLookup[i].sourceBuf)
{
Hunk_FreeDebugMem(scrParserGlob.saveSourceBufferLookup[i].buf);
Game::Engine::Hunk_FreeDebugMem(scrParserGlob.saveSourceBufferLookup[i].buf);
}
}
Hunk_FreeDebugMem(scrParserGlob.saveSourceBufferLookup);
Game::Engine::Hunk_FreeDebugMem(scrParserGlob.saveSourceBufferLookup);
scrParserGlob.saveSourceBufferLookup = nullptr;
}
}
@ -645,7 +645,7 @@ namespace Components
auto strLen = std::strlen(extFilename) + 1;
auto newLen = strLen + len + 2;
auto* buf = static_cast<char*>(Hunk_AllocDebugMem(static_cast<int>(newLen))); // Scr_AddSourceBufferInternal
auto* buf = static_cast<char*>(Game::Engine::Hunk_AllocDebugMem(static_cast<int>(newLen))); // Scr_AddSourceBufferInternal
strcpy(buf, extFilename);
auto* sourceBuf2 = sourceBuf ? buf + strLen : nullptr;
@ -858,7 +858,7 @@ namespace Components
Utils::Hook::Call<void()>(0x4D9620)();
Scr_InitOpcodeLookup();
Hunk_InitDebugMemory();
Game::Engine::Hunk_InitDebugMemory();
}
void ScriptError::SL_ShutdownSystem_Stub(unsigned int user)
@ -866,37 +866,7 @@ namespace Components
Utils::Hook::Call<void(unsigned int)>(0x4F46D0)(user);
Scr_ShutdownOpcodeLookup();
Hunk_ShutdownDebugMemory();
}
void ScriptError::Hunk_InitDebugMemory()
{
assert(Game::Sys_IsMainThread());
assert(!g_debugUser);
g_debugUser = Game::Hunk_UserCreate(0x1000000, "Hunk_InitDebugMemory", false, 0);
}
void ScriptError::Hunk_ShutdownDebugMemory()
{
assert(Game::Sys_IsMainThread());
assert(g_debugUser);
Game::Hunk_UserDestroy(g_debugUser);
g_debugUser = nullptr;
}
void* ScriptError::Hunk_AllocDebugMem(int size)
{
assert(Game::Sys_IsMainThread());
assert(g_debugUser);
return Game::Hunk_UserAlloc(g_debugUser, size, 4);
}
void ScriptError::Hunk_FreeDebugMem([[maybe_unused]] void* ptr)
{
assert(Game::Sys_IsMainThread());
assert(g_debugUser);
// Let's hope it gets cleared by Hunk_ShutdownDebugMemory
Game::Engine::Hunk_ShutdownDebugMemory();
}
ScriptError::ScriptError()

View File

@ -19,8 +19,6 @@ namespace Components
static Game::scrParserGlob_t scrParserGlob;
static Game::scrParserPub_t scrParserPub;
static Game::HunkUser* g_debugUser;
static void AddOpcodePos(unsigned int sourcePos, int type);
static void RemoveOpcodePos();
static void AddThreadStartOpcodePos(unsigned int sourcePos);
@ -58,10 +56,5 @@ namespace Components
static void MT_Reset_Stub();
static void SL_ShutdownSystem_Stub(unsigned int user);
static void Hunk_InitDebugMemory();
static void Hunk_ShutdownDebugMemory();
static void* Hunk_AllocDebugMem(int size);
static void Hunk_FreeDebugMem(void* ptr);
};
}

73
src/Game/Engine/Hunk.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <STDInclude.hpp>
#include "Hunk.hpp"
namespace Game::Engine
{
HunkUser* g_debugUser;
HunkUser* Hunk_UserCreate(int maxSize, const char* name, bool fixed, int type)
{
assert((!(maxSize % (64 * 1024))));
auto* user = static_cast<HunkUser*>(Z_VirtualReserve(maxSize));
Z_VirtualCommit(user, sizeof(HunkUser) - 4);
user->end = reinterpret_cast<int>(user->buf) + maxSize - (sizeof(HunkUser) - 4);
user->pos = reinterpret_cast<int>(user->buf);
assert((!(user->pos & 31)));
user->maxSize = maxSize;
user->name = name;
user->current = user;
user->fixed = fixed;
user->type = type;
assert(!user->next);
return user;
}
void Hunk_UserDestroy(HunkUser* user)
{
auto* current = user->next;
while (current)
{
auto* next = current->next;
Z_VirtualFree(current);
current = next;
}
Z_VirtualFree(user);
}
void Hunk_InitDebugMemory()
{
assert(Sys_IsMainThread());
assert(!g_debugUser);
g_debugUser = Hunk_UserCreate(0x1000000, "Hunk_InitDebugMemory", false, 0);
}
void Hunk_ShutdownDebugMemory()
{
assert(Sys_IsMainThread());
assert(g_debugUser);
Hunk_UserDestroy(g_debugUser);
g_debugUser = nullptr;
}
void* Hunk_AllocDebugMem(int size)
{
assert(Sys_IsMainThread());
assert(g_debugUser);
return Hunk_UserAlloc(g_debugUser, size, 4);
}
void Hunk_FreeDebugMem([[maybe_unused]] void* ptr)
{
assert(Sys_IsMainThread());
assert(g_debugUser);
// Let's hope it gets cleared by Hunk_ShutdownDebugMemory
}
}

17
src/Game/Engine/Hunk.hpp Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#define FIXED_HUNK_USER_COUNT 1
#define VIRTUAL_HUNK_USER_MAX 128
namespace Game::Engine
{
extern HunkUser* g_debugUser;
extern HunkUser* Hunk_UserCreate(int maxSize, const char* name, bool fixed, int type);
extern void Hunk_UserDestroy(HunkUser* user);
extern void Hunk_InitDebugMemory();
extern void Hunk_ShutdownDebugMemory();
extern void* Hunk_AllocDebugMem(int size);
extern void Hunk_FreeDebugMem(void* ptr);
}

View File

@ -5,8 +5,6 @@ namespace Game
Z_VirtualAlloc_t Z_VirtualAlloc = Z_VirtualAlloc_t(0x4CFBA0);
Z_Malloc_t Z_Malloc = Z_Malloc_t(0x4F3680);
Hunk_UserCreate_t Hunk_UserCreate = Hunk_UserCreate_t(0x430E90);
Hunk_UserDestroy_t Hunk_UserDestroy = Hunk_UserDestroy_t(0x435910);
Hunk_AllocateTempMemoryHigh_t Hunk_AllocateTempMemoryHigh = Hunk_AllocateTempMemoryHigh_t(0x475B30);
Hunk_UserAlloc_t Hunk_UserAlloc = Hunk_UserAlloc_t(0x45D1C0);
@ -16,7 +14,7 @@ namespace Game
{
assert((size >= 0));
return VirtualAlloc(ptr, size, (size > 0x20000 ? 0 : MEM_TOP_DOWN) | MEM_COMMIT, PAGE_READWRITE) != nullptr;
return VirtualAlloc(ptr, size, MEM_COMMIT, PAGE_READWRITE) != nullptr;
}
void Z_VirtualDecommitInternal(void* ptr, int size)
@ -55,7 +53,7 @@ namespace Game
{
assert((size >= 0));
void* buf = VirtualAlloc(nullptr, size, (size > 0x20000 ? 0 : MEM_TOP_DOWN) | MEM_RESERVE, PAGE_READWRITE);
void* buf = VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE);
assert(buf);
return buf;
}

View File

@ -8,12 +8,6 @@ namespace Game
typedef void*(*Z_Malloc_t)(int size);
extern Z_Malloc_t Z_Malloc;
typedef HunkUser*(*Hunk_UserCreate_t)(int maxSize, const char* name, bool fixed, int type);
extern Hunk_UserCreate_t Hunk_UserCreate;
typedef void(*Hunk_UserDestroy_t)(HunkUser* user);
extern Hunk_UserDestroy_t Hunk_UserDestroy;
typedef void*(*Hunk_AllocateTempMemoryHigh_t)(int size);
extern Hunk_AllocateTempMemoryHigh_t Hunk_AllocateTempMemoryHigh;
@ -25,7 +19,7 @@ namespace Game
constexpr auto PAGE_SIZE = 4096;
extern void* Z_VirtualReserve(int size);
extern [[nodiscard]] void* Z_VirtualReserve(int size);
extern void Z_VirtualCommit(void* ptr, int size);
extern void Z_VirtualDecommit(void* ptr, int size);
extern void Z_VirtualFree(void* ptr);