diff options
author | BobTheBob <32057864+BobTheBob9@users.noreply.github.com> | 2021-12-03 20:55:19 +0000 |
---|---|---|
committer | BobTheBob <32057864+BobTheBob9@users.noreply.github.com> | 2021-12-03 20:55:19 +0000 |
commit | 9e8232e97db1cfe9f0f9a69bdfde5048a6290df1 (patch) | |
tree | be1324c39dd1680335f4bfaf3d1660b5d6e31514 /NorthstarDedicatedTest/memalloc.cpp | |
parent | f33bab4fb3586fd06896a7730bce8913c2616b78 (diff) | |
download | NorthstarLauncher-9e8232e97db1cfe9f0f9a69bdfde5048a6290df1.tar.gz NorthstarLauncher-9e8232e97db1cfe9f0f9a69bdfde5048a6290df1.zip |
slightly awful fix for allocation issues
Diffstat (limited to 'NorthstarDedicatedTest/memalloc.cpp')
-rw-r--r-- | NorthstarDedicatedTest/memalloc.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp new file mode 100644 index 00000000..c7cfbb6a --- /dev/null +++ b/NorthstarDedicatedTest/memalloc.cpp @@ -0,0 +1,40 @@ +#include "pch.h" +#include "memalloc.h" +#include "gameutils.h" + +// so for anyone reading this code, you may be curious why the fuck i'm overriding new to alloc into a static 100k buffer +// pretty much, the issue here is that we need to use the game's memory allocator (g_pMemAllocSingleton) or risk heap corruptions, but this allocator is defined in tier0 +// as such, it doesn't exist when we inject +// initially i wanted to just call malloc and free until g_pMemAllocSingleton was initialised, but the issue then becomes that we might try to +// call g_pMemAllocSingleton->Free on memory that was allocated with malloc, which will cause game to crash +// so, the best idea i had for this was to just alloc 100k of memory, have all pre-tier0 allocations use that +// (from what i can tell we hit about 12k before tier0 is loaded atm in debug builds, so it's more than enough) +// then just use the game's allocator after that +// yes, this means we leak 100k of memory, idk how else to do this without breaking stuff + +const int STATIC_ALLOC_SIZE = 100000; + +size_t g_iStaticAllocated = 0; +char pStaticAllocBuf[STATIC_ALLOC_SIZE]; + +void* operator new(size_t n) +{ + // allocate into static buffer if g_pMemAllocSingleton isn't initialised + if (g_pMemAllocSingleton) + return g_pMemAllocSingleton->m_vtable->Alloc(g_pMemAllocSingleton, n); + else + { + void* ret = pStaticAllocBuf + g_iStaticAllocated; + g_iStaticAllocated += n; + return ret; + } +} + +void operator delete(void* p) +{ + // if it was allocated into the static buffer, just do nothing, safest way to deal with it + if (p >= pStaticAllocBuf && p <= pStaticAllocBuf + STATIC_ALLOC_SIZE) + return; + + g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p); +}
\ No newline at end of file |