diff options
author | p0358 <p0358@users.noreply.github.com> | 2021-12-30 04:47:16 +0100 |
---|---|---|
committer | p0358 <p0358@users.noreply.github.com> | 2021-12-30 04:47:16 +0100 |
commit | 2404f063433064e90059e6b3153f663e10d1f884 (patch) | |
tree | 4ca5351bb75e404b93b6ce430c6cbf8ea1a6db86 /NorthstarDedicatedTest/memalloc.cpp | |
parent | b302ffcad2f433ccbe02674e381d4d27252bb122 (diff) | |
download | NorthstarLauncher-2404f063433064e90059e6b3153f663e10d1f884.tar.gz NorthstarLauncher-2404f063433064e90059e6b3153f663e10d1f884.zip |
add realloc too
Diffstat (limited to 'NorthstarDedicatedTest/memalloc.cpp')
-rw-r--r-- | NorthstarDedicatedTest/memalloc.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp index cff0ecac..d301f1fa 100644 --- a/NorthstarDedicatedTest/memalloc.cpp +++ b/NorthstarDedicatedTest/memalloc.cpp @@ -15,6 +15,7 @@ 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; +void* g_pLastAllocated = nullptr; char pStaticAllocBuf[STATIC_ALLOC_SIZE]; // TODO: rename to malloc and free after removing statically compiled .libs @@ -53,6 +54,29 @@ void free_(void* p) g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, 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_pMemAllocSingleton) + return g_pMemAllocSingleton->m_vtable->Realloc(g_pMemAllocSingleton, old_ptr, size); +} + void* operator new(size_t n) { return malloc_(n); |