Skip to content

Commit

Permalink
Revert "Fix memalloc problems (R2Northstar#728)"
Browse files Browse the repository at this point in the history
This reverts commit e9c6bb1.
  • Loading branch information
wolf109909 committed Oct 21, 2024
1 parent 7da8f88 commit 11fd807
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 41 deletions.
36 changes: 5 additions & 31 deletions primedev/core/memalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// TODO: rename to malloc and free after removing statically compiled .libs

void* _malloc_base(size_t n)
extern "C" void* _malloc_base(size_t n)
{
// allocate into static buffer if g_pMemAllocSingleton isn't initialised
if (!g_pMemAllocSingleton)
Expand All @@ -17,23 +17,23 @@ void* _malloc_base(size_t n)
return _malloc_base(n);
}*/

void _free_base(void* p)
extern "C" void _free_base(void* p)
{
if (!g_pMemAllocSingleton)
TryCreateGlobalMemAlloc();

g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p);
}

void* _realloc_base(void* oldPtr, size_t size)
extern "C" void* _realloc_base(void* oldPtr, size_t size)
{
if (!g_pMemAllocSingleton)
TryCreateGlobalMemAlloc();

return g_pMemAllocSingleton->m_vtable->Realloc(g_pMemAllocSingleton, oldPtr, size);
}

void* _calloc_base(size_t n, size_t size)
extern "C" void* _calloc_base(size_t n, size_t size)
{
size_t bytes = n * size;
void* memory = _malloc_base(bytes);
Expand All @@ -44,33 +44,7 @@ void* _calloc_base(size_t n, size_t size)
return memory;
}

void* _recalloc_base(void* const block, size_t const count, size_t const size)
{
if (!block)
return _calloc_base(count, size);

const size_t new_size = count * size;
const size_t old_size = _msize(block);

void* const memory = _realloc_base(block, new_size);

if (memory && old_size < new_size)
{
memset(static_cast<char*>(memory) + old_size, 0, new_size - old_size);
}

return memory;
}

size_t _msize(void* const block)
{
if (!g_pMemAllocSingleton)
TryCreateGlobalMemAlloc();

return g_pMemAllocSingleton->m_vtable->GetSize(g_pMemAllocSingleton, block);
}

char* _strdup_base(const char* src)
extern "C" char* _strdup_base(const char* src)
{
char* str;
char* p;
Expand Down
16 changes: 6 additions & 10 deletions primedev/core/memalloc.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#pragma once

#include <malloc.h>

#include "rapidjson/document.h"
// #include "include/rapidjson/allocators.h"

// The prelude is needed for these to be usable by the CRT
extern "C" __declspec(noinline) void* __cdecl _malloc_base(size_t const size);
extern "C" __declspec(noinline) void* __cdecl _calloc_base(size_t const count, size_t const size);
extern "C" __declspec(noinline) void* __cdecl _realloc_base(void* const block, size_t const size);
extern "C" __declspec(noinline) void* __cdecl _recalloc_base(void* const block, size_t const count, size_t const size);
extern "C" __declspec(noinline) void __cdecl _free_base(void* const block);
extern "C" __declspec(noinline) size_t __cdecl _msize(void* const block);
extern "C" __declspec(noinline) char* __cdecl _strdup_base(const char* src);
extern "C" void* _malloc_base(size_t size);
extern "C" void* _calloc_base(size_t const count, size_t const size);
extern "C" void* _realloc_base(void* block, size_t size);
extern "C" void* _recalloc_base(void* const block, size_t const count, size_t const size);
extern "C" void _free_base(void* const block);
extern "C" char* _strdup_base(const char* src);

void* operator new(size_t n);
void operator delete(void* p) noexcept;
Expand Down

0 comments on commit 11fd807

Please sign in to comment.