diff --git a/common/include/u_mem.h b/common/include/u_mem.h index cf15a7a31..71ef9b022 100644 --- a/common/include/u_mem.h +++ b/common/include/u_mem.h @@ -29,6 +29,13 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. #define MEM_K 1.5 // Dynamic array growth factor +#ifdef DEBUG_BIAS_MEMORY_ALLOCATIONS +#include "compiler-array.h" +#define DXX_DEBUG_BIAS_MEMORY_ALLOCATION (sizeof(array)) +#else +#define DXX_DEBUG_BIAS_MEMORY_ALLOCATION (0) +#endif + #ifdef DEBUG_MEMORY_ALLOCATIONS void mem_init(void); @@ -50,10 +57,32 @@ void mem_validate_heap(); #else -#define mem_malloc(size,var,file,line) ((void)var,(void)file,(void)line,malloc((size))) -#define mem_calloc(nmemb,size,var,file,line) ((void)var,(void)file,(void)line,calloc((nmemb),(size))) -#define mem_realloc(ptr,size,var,file,line) ((void)var,(void)file,(void)line,realloc((ptr),(size))) -#define mem_free free +#ifdef DEBUG_BIAS_MEMORY_ALLOCATIONS +#define bias_malloc(SIZE) ({ \ + auto p = malloc((SIZE) + DXX_DEBUG_BIAS_MEMORY_ALLOCATION); \ + p ? p + DXX_DEBUG_BIAS_MEMORY_ALLOCATION : p; \ + }) +/* Bias calloc wastes a bit extra to keep the math simple */ +#define bias_calloc(NMEMB,SIZE) ({ \ + auto p = calloc((NMEMB), (SIZE) + DXX_DEBUG_BIAS_MEMORY_ALLOCATION); \ + p ? p + DXX_DEBUG_BIAS_MEMORY_ALLOCATION : p; \ + }) +#define bias_realloc(PTR,SIZE) ({ \ + auto p = realloc(reinterpret_cast(PTR) - DXX_DEBUG_BIAS_MEMORY_ALLOCATION, (SIZE) + DXX_DEBUG_BIAS_MEMORY_ALLOCATION); \ + p ? p + DXX_DEBUG_BIAS_MEMORY_ALLOCATION : p; \ + }) +#define bias_free(PTR) free(reinterpret_cast(PTR) - DXX_DEBUG_BIAS_MEMORY_ALLOCATION) +#else +#define bias_malloc malloc +#define bias_calloc calloc +#define bias_realloc realloc +#define bias_free free +#endif + +#define mem_malloc(size,var,file,line) ((void)var,(void)file,(void)line,bias_malloc((size))) +#define mem_calloc(nmemb,size,var,file,line) ((void)var,(void)file,(void)line,bias_calloc((nmemb),(size))) +#define mem_realloc(ptr,size,var,file,line) ((void)var,(void)file,(void)line,bias_realloc((ptr),(size))) +#define mem_free bias_free static inline void mem_init(void) { @@ -75,7 +104,6 @@ T *CALLOC(T *&r, std::size_t count, const char *var, const char *file, unsigned } #define d_malloc(size) mem_malloc((size),"Unknown", __FILE__,__LINE__ ) -#define d_calloc(nmemb,size) mem_calloc((nmemb),(size),"Unknown", __FILE__,__LINE__ ) #define d_realloc(ptr,size) mem_realloc((ptr),(size),"Unknown", __FILE__,__LINE__ ) template static inline void d_free(T *&ptr) diff --git a/similar/mem/mem.cpp b/similar/mem/mem.cpp index 666414086..10fb38e9b 100644 --- a/similar/mem/mem.cpp +++ b/similar/mem/mem.cpp @@ -57,10 +57,8 @@ static int free_list[MAX_INDEX]; static int num_blocks = 0; static int Initialized = 0; - static int LargestIndex = 0; - -int out_of_memory = 0; +static int out_of_memory = 0; void mem_init() { @@ -130,8 +128,7 @@ void *mem_malloc(size_t size, const char * var, const char * filename, unsigned //con_printf(CON_CRITICAL, "\tBlock '%s' created in %s, line %d.", Varname[id], Filename[id], LineNum[id] ); Error( "MEM_OUT_OF_SLOTS" ); } - - ptr = malloc( size+CHECKSIZE ); + ptr = malloc(size + DXX_DEBUG_BIAS_MEMORY_ALLOCATION + CHECKSIZE); if (ptr==NULL) { @@ -140,7 +137,7 @@ void *mem_malloc(size_t size, const char * var, const char * filename, unsigned con_printf(CON_CRITICAL, "\tBlock '%s' created in %s, line %d.", Varname[id], Filename[id], LineNum[id] ); Error( "MEM_OUT_OF_MEMORY" ); } - + ptr = reinterpret_cast(ptr) + DXX_DEBUG_BIAS_MEMORY_ALLOCATION; MallocBase[id] = ptr; MallocSize[id] = size; Varname[id] = var; @@ -238,7 +235,6 @@ void mem_free( void * buffer ) Int3(); return; } - id = mem_find_id( buffer ); if (id==-1 && (!out_of_memory)) @@ -253,6 +249,7 @@ void mem_free( void * buffer ) BytesMalloced -= MallocSize[id]; + buffer = reinterpret_cast(buffer) - DXX_DEBUG_BIAS_MEMORY_ALLOCATION; free( buffer ); Present[id] = 0; @@ -344,9 +341,6 @@ static unsigned int SmallestAddress = 0xFFFFFFF; static unsigned int LargestAddress = 0x0; static unsigned int BytesMalloced = 0; -#define CHECKSIZE 16 -#define CHECKBYTE 0xFC - void mem_init() { Initialized = 1;