fallout2-ce/src/memory.cc

221 lines
5.6 KiB
C++
Raw Normal View History

2022-05-19 01:51:26 -07:00
#include "memory.h"
#include "debug.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2022-06-18 05:30:05 -07:00
// A special value that denotes a beginning of a memory block data.
#define MEMORY_BLOCK_HEADER_GUARD (0xFEEDFACE)
// A special value that denotes an ending of a memory block data.
#define MEMORY_BLOCK_FOOTER_GUARD (0xBEEFCAFE)
// A header of a memory block.
typedef struct MemoryBlockHeader {
// Size of the memory block including header and footer.
size_t size;
// See [MEMORY_BLOCK_HEADER_GUARD].
int guard;
} MemoryBlockHeader;
// A footer of a memory block.
typedef struct MemoryBlockFooter {
// See [MEMORY_BLOCK_FOOTER_GUARD].
int guard;
} MemoryBlockFooter;
static void* memoryBlockMallocImpl(size_t size);
static void* memoryBlockReallocImpl(void* ptr, size_t size);
static void memoryBlockFreeImpl(void* ptr);
static void memoryBlockPrintStats();
2022-09-01 08:41:37 -07:00
static void* mem_prep_block(void* block, size_t size);
2022-06-18 05:30:05 -07:00
static void memoryBlockValidate(void* block);
2022-05-19 01:51:26 -07:00
// 0x51DED0
2022-06-18 05:30:05 -07:00
static MallocProc* gMallocProc = memoryBlockMallocImpl;
2022-05-19 01:51:26 -07:00
// 0x51DED4
2022-06-18 05:30:05 -07:00
static ReallocProc* gReallocProc = memoryBlockReallocImpl;
2022-05-19 01:51:26 -07:00
// 0x51DED8
2022-06-18 05:30:05 -07:00
static FreeProc* gFreeProc = memoryBlockFreeImpl;
2022-05-19 01:51:26 -07:00
// 0x51DEDC
2022-06-18 05:30:05 -07:00
static int gMemoryBlocksCurrentCount = 0;
2022-05-19 01:51:26 -07:00
// 0x51DEE0
2022-06-18 05:30:05 -07:00
static int gMemoryBlockMaximumCount = 0;
2022-05-19 01:51:26 -07:00
// 0x51DEE4
2022-06-18 05:30:05 -07:00
static size_t gMemoryBlocksCurrentSize = 0;
2022-05-19 01:51:26 -07:00
// 0x51DEE8
2022-06-18 05:30:05 -07:00
static size_t gMemoryBlocksMaximumSize = 0;
2022-05-19 01:51:26 -07:00
// 0x4C5A80
char* internal_strdup(const char* string)
{
char* copy = NULL;
if (string != NULL) {
2022-05-21 08:22:03 -07:00
copy = (char*)gMallocProc(strlen(string) + 1);
2022-05-19 01:51:26 -07:00
strcpy(copy, string);
}
return copy;
}
// 0x4C5AD0
void* internal_malloc(size_t size)
{
return gMallocProc(size);
}
// 0x4C5AD8
2022-06-18 05:30:05 -07:00
static void* memoryBlockMallocImpl(size_t size)
2022-05-19 01:51:26 -07:00
{
void* ptr = NULL;
if (size != 0) {
size += sizeof(MemoryBlockHeader) + sizeof(MemoryBlockFooter);
2022-05-21 08:22:03 -07:00
unsigned char* block = (unsigned char*)malloc(size);
2022-05-19 01:51:26 -07:00
if (block != NULL) {
2022-09-01 08:41:37 -07:00
// NOTE: Uninline.
ptr = mem_prep_block(block, size);
2022-05-19 01:51:26 -07:00
gMemoryBlocksCurrentCount++;
if (gMemoryBlocksCurrentCount > gMemoryBlockMaximumCount) {
gMemoryBlockMaximumCount = gMemoryBlocksCurrentCount;
}
gMemoryBlocksCurrentSize += size;
if (gMemoryBlocksCurrentSize > gMemoryBlocksMaximumSize) {
gMemoryBlocksMaximumSize = gMemoryBlocksCurrentSize;
}
}
}
return ptr;
}
// 0x4C5B50
void* internal_realloc(void* ptr, size_t size)
{
return gReallocProc(ptr, size);
}
// 0x4C5B58
2022-06-18 05:30:05 -07:00
static void* memoryBlockReallocImpl(void* ptr, size_t size)
2022-05-19 01:51:26 -07:00
{
if (ptr != NULL) {
unsigned char* block = (unsigned char*)ptr - sizeof(MemoryBlockHeader);
MemoryBlockHeader* header = (MemoryBlockHeader*)block;
size_t oldSize = header->size;
gMemoryBlocksCurrentSize -= oldSize;
memoryBlockValidate(block);
if (size != 0) {
size += sizeof(MemoryBlockHeader) + sizeof(MemoryBlockFooter);
}
2022-05-21 08:22:03 -07:00
unsigned char* newBlock = (unsigned char*)realloc(block, size);
2022-05-19 01:51:26 -07:00
if (newBlock != NULL) {
gMemoryBlocksCurrentSize += size;
if (gMemoryBlocksCurrentSize > gMemoryBlocksMaximumSize) {
gMemoryBlocksMaximumSize = gMemoryBlocksCurrentSize;
}
2022-09-01 08:41:37 -07:00
// NOTE: Uninline.
ptr = mem_prep_block(newBlock, size);
2022-05-19 01:51:26 -07:00
} else {
if (size != 0) {
gMemoryBlocksCurrentSize += oldSize;
debugPrint("%s,%u: ", __FILE__, __LINE__); // "Memory.c", 195
debugPrint("Realloc failure.\n");
} else {
gMemoryBlocksCurrentCount--;
}
ptr = NULL;
}
} else {
ptr = gMallocProc(size);
}
return ptr;
}
// 0x4C5C24
void internal_free(void* ptr)
{
gFreeProc(ptr);
}
// 0x4C5C2C
2022-06-18 05:30:05 -07:00
static void memoryBlockFreeImpl(void* ptr)
2022-05-19 01:51:26 -07:00
{
if (ptr != NULL) {
void* block = (unsigned char*)ptr - sizeof(MemoryBlockHeader);
MemoryBlockHeader* header = (MemoryBlockHeader*)block;
memoryBlockValidate(block);
gMemoryBlocksCurrentSize -= header->size;
gMemoryBlocksCurrentCount--;
free(block);
}
}
// NOTE: Not used.
//
// 0x4C5C5C
2022-06-18 05:30:05 -07:00
static void memoryBlockPrintStats()
2022-05-19 01:51:26 -07:00
{
if (gMallocProc == memoryBlockMallocImpl) {
debugPrint("Current memory allocated: %6d blocks, %9u bytes total\n", gMemoryBlocksCurrentCount, gMemoryBlocksCurrentSize);
debugPrint("Max memory allocated: %6d blocks, %9u bytes total\n", gMemoryBlockMaximumCount, gMemoryBlocksMaximumSize);
}
}
2022-09-01 08:41:37 -07:00
// NOTE: Inlined.
//
// 0x4C5CC4
static void* mem_prep_block(void* block, size_t size)
{
MemoryBlockHeader* header;
MemoryBlockFooter* footer;
header = (MemoryBlockHeader*)block;
header->guard = MEMORY_BLOCK_HEADER_GUARD;
header->size = size;
footer = (MemoryBlockFooter*)((unsigned char*)block + size - sizeof(*footer));
footer->guard = MEMORY_BLOCK_FOOTER_GUARD;
return (unsigned char*)block + sizeof(*header);
}
2022-05-19 01:51:26 -07:00
// Validates integrity of the memory block.
//
// [block] is a pointer to the the memory block itself, not it's data.
//
// 0x4C5CE4
2022-06-18 05:30:05 -07:00
static void memoryBlockValidate(void* block)
2022-05-19 01:51:26 -07:00
{
MemoryBlockHeader* header = (MemoryBlockHeader*)block;
if (header->guard != MEMORY_BLOCK_HEADER_GUARD) {
debugPrint("Memory header stomped.\n");
}
MemoryBlockFooter* footer = (MemoryBlockFooter*)((unsigned char*)block + header->size - sizeof(MemoryBlockFooter));
if (footer->guard != MEMORY_BLOCK_FOOTER_GUARD) {
debugPrint("Memory footer stomped.\n");
}
}