aboutsummaryrefslogtreecommitdiff
path: root/LauncherInjector/memalloc.cpp
diff options
context:
space:
mode:
authorp0358 <p0358@users.noreply.github.com>2021-12-30 04:47:16 +0100
committerp0358 <p0358@users.noreply.github.com>2021-12-30 04:47:16 +0100
commit2404f063433064e90059e6b3153f663e10d1f884 (patch)
tree4ca5351bb75e404b93b6ce430c6cbf8ea1a6db86 /LauncherInjector/memalloc.cpp
parentb302ffcad2f433ccbe02674e381d4d27252bb122 (diff)
downloadNorthstarLauncher-2404f063433064e90059e6b3153f663e10d1f884.tar.gz
NorthstarLauncher-2404f063433064e90059e6b3153f663e10d1f884.zip
add realloc too
Diffstat (limited to 'LauncherInjector/memalloc.cpp')
-rw-r--r--LauncherInjector/memalloc.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/LauncherInjector/memalloc.cpp b/LauncherInjector/memalloc.cpp
index 1d0f13e6..936523d7 100644
--- a/LauncherInjector/memalloc.cpp
+++ b/LauncherInjector/memalloc.cpp
@@ -17,6 +17,7 @@ void LoadTier0Handle()
const int STATIC_ALLOC_SIZE = 16384;
size_t g_iStaticAllocated = 0;
+void* g_pLastAllocated = nullptr;
char pStaticAllocBuf[STATIC_ALLOC_SIZE];
// they should never be used here, except in LibraryLoadError // haha not true
@@ -29,6 +30,7 @@ void* malloc(size_t n)
{
void* ret = pStaticAllocBuf + g_iStaticAllocated;
g_iStaticAllocated += n;
+ g_pLastAllocated = ret;
return ret;
}
else
@@ -53,6 +55,29 @@ void free(void* p)
(*g_ppMemAllocSingleton)->m_vtable->Free(*g_ppMemAllocSingleton, p);
}
+void* realloc(void* old_ptr, size_t size) {
+ // it was allocated into the static buffer
+ if (old_ptr >= pStaticAllocBuf && old_ptr <= pStaticAllocBuf + STATIC_ALLOC_SIZE)
+ {
+ if (g_pLastAllocated == old_ptr)
+ {
+ // nothing was allocated after this
+ size_t old_size = g_iStaticAllocated - ((size_t)g_pLastAllocated - (size_t)pStaticAllocBuf);
+ size_t diff = size - old_size;
+ if (diff > 0)
+ g_iStaticAllocated += diff;
+ return old_ptr;
+ }
+ else
+ {
+ return malloc(size);
+ }
+ }
+
+ if (g_ppMemAllocSingleton && *g_ppMemAllocSingleton)
+ return (*g_ppMemAllocSingleton)->m_vtable->Realloc(*g_ppMemAllocSingleton, old_ptr, size);
+}
+
void* operator new(size_t n)
{
return malloc(n);