Implement byte-tagging of memory
Using a magic value for memory allocated but uninitialized and another one for memory about-to-be-released. Helps in debugging unitialized members, dangling pointerts, etc. Disabled by default. Can be enabled for debug builds by defining `DEBUG_MEMORY_TAGGING`.
This commit is contained in:
parent
490a2ff0b9
commit
779693a79b
1 changed files with 26 additions and 2 deletions
|
@ -47,6 +47,10 @@ void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) {
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
size_t Memory::mem_usage = 0;
|
size_t Memory::mem_usage = 0;
|
||||||
size_t Memory::max_usage = 0;
|
size_t Memory::max_usage = 0;
|
||||||
|
#ifdef DEBUG_MEMORY_TAGGING
|
||||||
|
#define MEM_UNINIT_TAG 0xEA
|
||||||
|
#define MEM_RELEASED_TAG 0xAE
|
||||||
|
#endif
|
||||||
#define PREPAD true
|
#define PREPAD true
|
||||||
#else
|
#else
|
||||||
#define PREPAD p_pad_align
|
#define PREPAD p_pad_align
|
||||||
|
@ -73,8 +77,11 @@ void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
|
||||||
uint64_t *s = (uint64_t *)mem;
|
uint64_t *s = (uint64_t *)mem;
|
||||||
*s = p_bytes;
|
*s = p_bytes;
|
||||||
|
|
||||||
uint8_t *s8 = (uint8_t *)mem;
|
uint8_t *s8 = (uint8_t *)mem + PAD_ALIGN;
|
||||||
return s8 + PAD_ALIGN;
|
#if defined DEBUG_ENABLED && defined DEBUG_MEMORY_TAGGING
|
||||||
|
memset(s8, MEM_UNINIT_TAG, p_bytes);
|
||||||
|
#endif
|
||||||
|
return s8;
|
||||||
} else {
|
} else {
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
|
@ -95,10 +102,19 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
|
||||||
if (p_bytes == 0) {
|
if (p_bytes == 0) {
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
mem_usage -= *s;
|
mem_usage -= *s;
|
||||||
|
#ifdef DEBUG_MEMORY_TAGGING
|
||||||
|
memset(mem, MEM_RELEASED_TAG, PAD_ALIGN + *s);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
free(mem);
|
free(mem);
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
|
#if defined DEBUG_ENABLED && defined DEBUG_MEMORY_TAGGING
|
||||||
|
if (p_bytes < *s) {
|
||||||
|
memset(mem + PAD_ALIGN + p_bytes, MEM_RELEASED_TAG, *s - p_bytes);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
mem = (uint8_t *)realloc(mem, p_bytes + PAD_ALIGN);
|
mem = (uint8_t *)realloc(mem, p_bytes + PAD_ALIGN);
|
||||||
ERR_FAIL_COND_V(!mem, NULL);
|
ERR_FAIL_COND_V(!mem, NULL);
|
||||||
|
|
||||||
|
@ -106,6 +122,11 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
mem_usage -= *s;
|
mem_usage -= *s;
|
||||||
mem_usage += p_bytes;
|
mem_usage += p_bytes;
|
||||||
|
#ifdef DEBUG_MEMORY_TAGGING
|
||||||
|
if (p_bytes > *s) {
|
||||||
|
memset(mem + PAD_ALIGN + *s, MEM_UNINIT_TAG, p_bytes - *s);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
*s = p_bytes;
|
*s = p_bytes;
|
||||||
|
|
||||||
|
@ -131,6 +152,9 @@ void Memory::free_static(void *p_ptr, bool p_pad_align) {
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
const uint64_t s = *((uint64_t *)mem);
|
const uint64_t s = *((uint64_t *)mem);
|
||||||
mem_usage -= s;
|
mem_usage -= s;
|
||||||
|
#ifdef DEBUG_MEMORY_TAGGING
|
||||||
|
memset(mem, MEM_RELEASED_TAG, PAD_ALIGN + s);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
atomic_decrement(&alloc_count);
|
atomic_decrement(&alloc_count);
|
||||||
|
|
Loading…
Reference in a new issue