2022-05-19 01:51:26 -07:00
|
|
|
#include "memory_manager.h"
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-09-23 05:43:44 -07:00
|
|
|
namespace fallout {
|
|
|
|
|
2022-06-19 04:11:04 -07:00
|
|
|
typedef void(MemoryManagerPrintErrorProc)(const char* string);
|
|
|
|
|
|
|
|
static void memoryManagerDefaultPrintErrorImpl(const char* string);
|
|
|
|
static int memoryManagerPrintError(const char* format, ...);
|
|
|
|
[[noreturn]] static void memoryManagerFatalAllocationError(const char* func, size_t size, const char* file, int line);
|
|
|
|
static void* memoryManagerDefaultMallocImpl(size_t size);
|
|
|
|
static void* memoryManagerDefaultReallocImpl(void* ptr, size_t size);
|
|
|
|
static void memoryManagerDefaultFreeImpl(void* ptr);
|
|
|
|
|
2022-05-19 01:51:26 -07:00
|
|
|
// 0x519588
|
2022-06-19 04:11:04 -07:00
|
|
|
static MemoryManagerPrintErrorProc* gMemoryManagerPrintErrorProc = memoryManagerDefaultPrintErrorImpl;
|
2022-05-19 01:51:26 -07:00
|
|
|
|
|
|
|
// 0x51958C
|
2022-06-19 04:11:04 -07:00
|
|
|
static MallocProc* gMemoryManagerMallocProc = memoryManagerDefaultMallocImpl;
|
2022-05-19 01:51:26 -07:00
|
|
|
|
|
|
|
// 0x519590
|
2022-06-19 04:11:04 -07:00
|
|
|
static ReallocProc* gMemoryManagerReallocProc = memoryManagerDefaultReallocImpl;
|
2022-05-19 01:51:26 -07:00
|
|
|
|
|
|
|
// 0x519594
|
2022-06-19 04:11:04 -07:00
|
|
|
static FreeProc* gMemoryManagerFreeProc = memoryManagerDefaultFreeImpl;
|
2022-05-19 01:51:26 -07:00
|
|
|
|
|
|
|
// 0x4845B0
|
2022-06-19 04:11:04 -07:00
|
|
|
static void memoryManagerDefaultPrintErrorImpl(const char* string)
|
2022-05-19 01:51:26 -07:00
|
|
|
{
|
|
|
|
printf("%s", string);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x4845C8
|
2022-06-19 04:11:04 -07:00
|
|
|
static int memoryManagerPrintError(const char* format, ...)
|
2022-05-19 01:51:26 -07:00
|
|
|
{
|
2022-12-08 12:05:50 -08:00
|
|
|
// 0x631F7C
|
|
|
|
static char err[256];
|
|
|
|
|
2022-05-19 01:51:26 -07:00
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
if (gMemoryManagerPrintErrorProc != NULL) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2022-12-08 12:05:50 -08:00
|
|
|
length = vsnprintf(err, sizeof(err), format, args);
|
2022-05-19 01:51:26 -07:00
|
|
|
va_end(args);
|
|
|
|
|
2022-12-08 12:05:50 -08:00
|
|
|
gMemoryManagerPrintErrorProc(err);
|
2022-05-19 01:51:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x484610
|
2022-06-19 04:11:04 -07:00
|
|
|
[[noreturn]] static void memoryManagerFatalAllocationError(const char* func, size_t size, const char* file, int line)
|
2022-05-19 01:51:26 -07:00
|
|
|
{
|
|
|
|
memoryManagerPrintError("%s: Error allocating block of size %ld (%x), %s %d\n", func, size, size, file, line);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x48462C
|
2022-06-19 04:11:04 -07:00
|
|
|
static void* memoryManagerDefaultMallocImpl(size_t size)
|
2022-05-19 01:51:26 -07:00
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x484634
|
2022-06-19 04:11:04 -07:00
|
|
|
static void* memoryManagerDefaultReallocImpl(void* ptr, size_t size)
|
2022-05-19 01:51:26 -07:00
|
|
|
{
|
|
|
|
return realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x48463C
|
2022-06-19 04:11:04 -07:00
|
|
|
static void memoryManagerDefaultFreeImpl(void* ptr)
|
2022-05-19 01:51:26 -07:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x484644
|
|
|
|
void memoryManagerSetProcs(MallocProc* mallocProc, ReallocProc* reallocProc, FreeProc* freeProc)
|
|
|
|
{
|
|
|
|
gMemoryManagerMallocProc = mallocProc;
|
|
|
|
gMemoryManagerReallocProc = reallocProc;
|
|
|
|
gMemoryManagerFreeProc = freeProc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x484660
|
|
|
|
void* internal_malloc_safe(size_t size, const char* file, int line)
|
|
|
|
{
|
|
|
|
void* ptr = gMemoryManagerMallocProc(size);
|
|
|
|
if (ptr == NULL) {
|
|
|
|
memoryManagerFatalAllocationError("malloc", size, file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x4846B4
|
|
|
|
void* internal_realloc_safe(void* ptr, size_t size, const char* file, int line)
|
|
|
|
{
|
|
|
|
ptr = gMemoryManagerReallocProc(ptr, size);
|
|
|
|
if (ptr == NULL) {
|
|
|
|
memoryManagerFatalAllocationError("realloc", size, file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x484688
|
|
|
|
void internal_free_safe(void* ptr, const char* file, int line)
|
|
|
|
{
|
|
|
|
if (ptr == NULL) {
|
|
|
|
memoryManagerPrintError("free: free of a null ptr, %s %d\n", file, line);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
gMemoryManagerFreeProc(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x4846D8
|
|
|
|
void* internal_calloc_safe(int count, int size, const char* file, int line)
|
|
|
|
{
|
|
|
|
void* ptr = gMemoryManagerMallocProc(count * size);
|
|
|
|
if (ptr == NULL) {
|
|
|
|
memoryManagerFatalAllocationError("calloc", size, file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(ptr, 0, count * size);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0x484710
|
|
|
|
char* strdup_safe(const char* string, const char* file, int line)
|
|
|
|
{
|
|
|
|
size_t size = strlen(string) + 1;
|
2022-05-21 08:22:03 -07:00
|
|
|
char* copy = (char*)gMemoryManagerMallocProc(size);
|
2022-05-19 01:51:26 -07:00
|
|
|
if (copy == NULL) {
|
|
|
|
memoryManagerFatalAllocationError("strdup", size, file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(copy, string);
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
2022-09-23 05:43:44 -07:00
|
|
|
|
|
|
|
} // namespace fallout
|