From ee3528037b89b48e4eaa386022b5e9a0a31a2488 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 18 Jun 2022 15:30:05 +0300 Subject: [PATCH] Cleanup memory.h See #42 --- src/memory.cc | 52 +++++++++++++++++++++++++++++++++++++++------------ src/memory.h | 36 ----------------------------------- 2 files changed, 40 insertions(+), 48 deletions(-) diff --git a/src/memory.cc b/src/memory.cc index 1386e8e..0ceda3b 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -1,31 +1,59 @@ #include "memory.h" #include "debug.h" +#include "memory_defs.h" #include #include #include +// 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(); +static void memoryBlockValidate(void* block); + // 0x51DED0 -MallocProc* gMallocProc = memoryBlockMallocImpl; +static MallocProc* gMallocProc = memoryBlockMallocImpl; // 0x51DED4 -ReallocProc* gReallocProc = memoryBlockReallocImpl; +static ReallocProc* gReallocProc = memoryBlockReallocImpl; // 0x51DED8 -FreeProc* gFreeProc = memoryBlockFreeImpl; +static FreeProc* gFreeProc = memoryBlockFreeImpl; // 0x51DEDC -int gMemoryBlocksCurrentCount = 0; +static int gMemoryBlocksCurrentCount = 0; // 0x51DEE0 -int gMemoryBlockMaximumCount = 0; +static int gMemoryBlockMaximumCount = 0; // 0x51DEE4 -size_t gMemoryBlocksCurrentSize = 0; +static size_t gMemoryBlocksCurrentSize = 0; // 0x51DEE8 -size_t gMemoryBlocksMaximumSize = 0; +static size_t gMemoryBlocksMaximumSize = 0; // 0x4C5A80 char* internal_strdup(const char* string) @@ -45,7 +73,7 @@ void* internal_malloc(size_t size) } // 0x4C5AD8 -void* memoryBlockMallocImpl(size_t size) +static void* memoryBlockMallocImpl(size_t size) { void* ptr = NULL; @@ -85,7 +113,7 @@ void* internal_realloc(void* ptr, size_t size) } // 0x4C5B58 -void* memoryBlockReallocImpl(void* ptr, size_t size) +static void* memoryBlockReallocImpl(void* ptr, size_t size) { if (ptr != NULL) { unsigned char* block = (unsigned char*)ptr - sizeof(MemoryBlockHeader); @@ -141,7 +169,7 @@ void internal_free(void* ptr) } // 0x4C5C2C -void memoryBlockFreeImpl(void* ptr) +static void memoryBlockFreeImpl(void* ptr) { if (ptr != NULL) { void* block = (unsigned char*)ptr - sizeof(MemoryBlockHeader); @@ -159,7 +187,7 @@ void memoryBlockFreeImpl(void* ptr) // NOTE: Not used. // // 0x4C5C5C -void memoryBlockPrintStats() +static void memoryBlockPrintStats() { if (gMallocProc == memoryBlockMallocImpl) { debugPrint("Current memory allocated: %6d blocks, %9u bytes total\n", gMemoryBlocksCurrentCount, gMemoryBlocksCurrentSize); @@ -172,7 +200,7 @@ void memoryBlockPrintStats() // [block] is a pointer to the the memory block itself, not it's data. // // 0x4C5CE4 -void memoryBlockValidate(void* block) +static void memoryBlockValidate(void* block) { MemoryBlockHeader* header = (MemoryBlockHeader*)block; if (header->guard != MEMORY_BLOCK_HEADER_GUARD) { diff --git a/src/memory.h b/src/memory.h index 3db421e..755bea5 100644 --- a/src/memory.h +++ b/src/memory.h @@ -1,45 +1,9 @@ #ifndef MEMORY_H #define MEMORY_H -#include "memory_defs.h" - -// 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; - -extern MallocProc* gMallocProc; -extern ReallocProc* gReallocProc; -extern FreeProc* gFreeProc; -extern int gMemoryBlocksCurrentCount; -extern int gMemoryBlockMaximumCount; -extern size_t gMemoryBlocksCurrentSize; -extern size_t gMemoryBlocksMaximumSize; - char* internal_strdup(const char* string); void* internal_malloc(size_t size); -void* memoryBlockMallocImpl(size_t size); void* internal_realloc(void* ptr, size_t size); -void* memoryBlockReallocImpl(void* ptr, size_t size); void internal_free(void* ptr); -void memoryBlockFreeImpl(void* ptr); -void memoryBlockPrintStats(); -void memoryBlockValidate(void* block); #endif /* MEMORY_H */