Support biased pointers to help memory debuggers
This commit is contained in:
parent
513e7c05e9
commit
8358dfcd0b
|
@ -29,6 +29,13 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
||||||
|
|
||||||
#define MEM_K 1.5 // Dynamic array growth factor
|
#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<double, 2>))
|
||||||
|
#else
|
||||||
|
#define DXX_DEBUG_BIAS_MEMORY_ALLOCATION (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef DEBUG_MEMORY_ALLOCATIONS
|
#ifdef DEBUG_MEMORY_ALLOCATIONS
|
||||||
void mem_init(void);
|
void mem_init(void);
|
||||||
|
|
||||||
|
@ -50,10 +57,32 @@ void mem_validate_heap();
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#define mem_malloc(size,var,file,line) ((void)var,(void)file,(void)line,malloc((size)))
|
#ifdef DEBUG_BIAS_MEMORY_ALLOCATIONS
|
||||||
#define mem_calloc(nmemb,size,var,file,line) ((void)var,(void)file,(void)line,calloc((nmemb),(size)))
|
#define bias_malloc(SIZE) ({ \
|
||||||
#define mem_realloc(ptr,size,var,file,line) ((void)var,(void)file,(void)line,realloc((ptr),(size)))
|
auto p = malloc((SIZE) + DXX_DEBUG_BIAS_MEMORY_ALLOCATION); \
|
||||||
#define mem_free free
|
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<char *>(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<char *>(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)
|
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_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__ )
|
#define d_realloc(ptr,size) mem_realloc((ptr),(size),"Unknown", __FILE__,__LINE__ )
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static inline void d_free(T *&ptr)
|
static inline void d_free(T *&ptr)
|
||||||
|
|
|
@ -57,10 +57,8 @@ static int free_list[MAX_INDEX];
|
||||||
static int num_blocks = 0;
|
static int num_blocks = 0;
|
||||||
|
|
||||||
static int Initialized = 0;
|
static int Initialized = 0;
|
||||||
|
|
||||||
static int LargestIndex = 0;
|
static int LargestIndex = 0;
|
||||||
|
static int out_of_memory = 0;
|
||||||
int out_of_memory = 0;
|
|
||||||
|
|
||||||
void mem_init()
|
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] );
|
//con_printf(CON_CRITICAL, "\tBlock '%s' created in %s, line %d.", Varname[id], Filename[id], LineNum[id] );
|
||||||
Error( "MEM_OUT_OF_SLOTS" );
|
Error( "MEM_OUT_OF_SLOTS" );
|
||||||
}
|
}
|
||||||
|
ptr = malloc(size + DXX_DEBUG_BIAS_MEMORY_ALLOCATION + CHECKSIZE);
|
||||||
ptr = malloc( size+CHECKSIZE );
|
|
||||||
|
|
||||||
if (ptr==NULL)
|
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] );
|
con_printf(CON_CRITICAL, "\tBlock '%s' created in %s, line %d.", Varname[id], Filename[id], LineNum[id] );
|
||||||
Error( "MEM_OUT_OF_MEMORY" );
|
Error( "MEM_OUT_OF_MEMORY" );
|
||||||
}
|
}
|
||||||
|
ptr = reinterpret_cast<char *>(ptr) + DXX_DEBUG_BIAS_MEMORY_ALLOCATION;
|
||||||
MallocBase[id] = ptr;
|
MallocBase[id] = ptr;
|
||||||
MallocSize[id] = size;
|
MallocSize[id] = size;
|
||||||
Varname[id] = var;
|
Varname[id] = var;
|
||||||
|
@ -238,7 +235,6 @@ void mem_free( void * buffer )
|
||||||
Int3();
|
Int3();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
id = mem_find_id( buffer );
|
id = mem_find_id( buffer );
|
||||||
|
|
||||||
if (id==-1 && (!out_of_memory))
|
if (id==-1 && (!out_of_memory))
|
||||||
|
@ -253,6 +249,7 @@ void mem_free( void * buffer )
|
||||||
|
|
||||||
BytesMalloced -= MallocSize[id];
|
BytesMalloced -= MallocSize[id];
|
||||||
|
|
||||||
|
buffer = reinterpret_cast<char *>(buffer) - DXX_DEBUG_BIAS_MEMORY_ALLOCATION;
|
||||||
free( buffer );
|
free( buffer );
|
||||||
|
|
||||||
Present[id] = 0;
|
Present[id] = 0;
|
||||||
|
@ -344,9 +341,6 @@ static unsigned int SmallestAddress = 0xFFFFFFF;
|
||||||
static unsigned int LargestAddress = 0x0;
|
static unsigned int LargestAddress = 0x0;
|
||||||
static unsigned int BytesMalloced = 0;
|
static unsigned int BytesMalloced = 0;
|
||||||
|
|
||||||
#define CHECKSIZE 16
|
|
||||||
#define CHECKBYTE 0xFC
|
|
||||||
|
|
||||||
void mem_init()
|
void mem_init()
|
||||||
{
|
{
|
||||||
Initialized = 1;
|
Initialized = 1;
|
||||||
|
|
Loading…
Reference in a new issue