aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/memalloc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'NorthstarDedicatedTest/memalloc.cpp')
-rw-r--r--NorthstarDedicatedTest/memalloc.cpp91
1 files changed, 64 insertions, 27 deletions
diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp
index 113f56b9..1b9eaae8 100644
--- a/NorthstarDedicatedTest/memalloc.cpp
+++ b/NorthstarDedicatedTest/memalloc.cpp
@@ -2,39 +2,76 @@
#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
+// TODO: rename to malloc and free after removing statically compiled .libs
-const int STATIC_ALLOC_SIZE = 100000; // alot more than we need, could reduce to 50k or even 25k later potentially
-
-size_t g_iStaticAllocated = 0;
-char pStaticAllocBuf[STATIC_ALLOC_SIZE];
-
-void* operator new(size_t n)
+extern "C" void* _malloc_base(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
+ if (!g_pMemAllocSingleton)
{
- void* ret = pStaticAllocBuf + g_iStaticAllocated;
- g_iStaticAllocated += n;
- return ret;
- }
+ InitialiseTier0GameUtilFunctions(GetModuleHandleA("tier0.dll"));
+ }
+ return g_pMemAllocSingleton->m_vtable->Alloc(g_pMemAllocSingleton, n);
}
-void operator delete(void* p)
+/*extern "C" void* malloc(size_t n)
{
- // 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;
+ return _malloc_base(n);
+}*/
+extern "C" void _free_base(void* p)
+{
+ if (!g_pMemAllocSingleton)
+ {
+ spdlog::warn("Trying to free something before g_pMemAllocSingleton was ready, this should never happen");
+ InitialiseTier0GameUtilFunctions(GetModuleHandleA("tier0.dll"));
+ }
g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p);
-} \ No newline at end of file
+}
+
+
+extern "C" void* _realloc_base(void* oldPtr, size_t size)
+{
+ if (!g_pMemAllocSingleton)
+ {
+ InitialiseTier0GameUtilFunctions(GetModuleHandleA("tier0.dll"));
+ }
+ return g_pMemAllocSingleton->m_vtable->Realloc(g_pMemAllocSingleton, oldPtr, size);
+}
+
+extern "C" void* _calloc_base(size_t n, size_t size)
+{
+ size_t bytes = n * size;
+ void* memory = _malloc_base(bytes);
+ if (memory)
+ {
+ memset(memory, 0, bytes);
+ }
+ return memory;
+}
+
+extern "C" char* _strdup_base(const char* src)
+{
+ char* str;
+ char* p;
+ int len = 0;
+
+ while (src[len])
+ len++;
+ str = reinterpret_cast<char*>(_malloc_base(len + 1));
+ p = str;
+ while (*src)
+ *p++ = *src++;
+ *p = '\0';
+ return str;
+}
+
+void* operator new(size_t n)
+{
+ return _malloc_base(n);
+}
+
+void operator delete(void* p)
+{
+ _free_base(p);
+}// /FORCE:MULTIPLE \ No newline at end of file