From 4308cd4abe5a75fb8410df929eac687cbd04032b Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 8 Sep 2024 18:36:07 +0300 Subject: update mempool and add implement hash array mapped trie (not integrated) --- src/memdebug.h | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'src/memdebug.h') diff --git a/src/memdebug.h b/src/memdebug.h index 358203a..7d72a10 100644 --- a/src/memdebug.h +++ b/src/memdebug.h @@ -64,6 +64,7 @@ typedef long long int memdebug_suffix; #include #include +#include #include #include #include @@ -80,17 +81,19 @@ static FILE *_memdebug_fp = NULL; errno, strerror(errno)); \ } while(0) -#define MEMDEBUG_OUT_OF_BOUNDS_CHECK(addr, size) \ - do { \ - if(addr != NULL) { \ - *(size_t*)addr = size; \ - addr += sizeof(size_t); \ - \ - memdebug_suffix suffix = MEMDEBUG_MAGIC_SUFFIX; \ - memcpy(addr + size, &suffix, sizeof(suffix)); \ - } \ - } while(0); +static inline void *memdebug_add_out_of_bounds_check(void *addr, size_t size) +{ + uintptr_t size_addr = (uintptr_t)addr; + uintptr_t suffix_addr = size_addr + + sizeof(size) + size; + + memcpy((void *)size_addr, &size, sizeof(size)); + memdebug_suffix suffix = MEMDEBUG_MAGIC_SUFFIX; + memcpy((void *)suffix_addr, &suffix, sizeof(suffix)); + + return (void *)((uintptr_t)addr + sizeof(size)); +} void *__memdebug_malloc(size_t size, char *file, int line) { @@ -99,7 +102,7 @@ void *__memdebug_malloc(size_t size, char *file, int line) #else void *addr = MEMDEBUG_MALLOC_SYMBOL( size + MEMDEBUG_OUT_OF_BOUNDS_EXTRA_SIZE); - MEMDEBUG_OUT_OF_BOUNDS_CHECK(addr, size); + if(addr) addr = memdebug_add_out_of_bounds_check(addr, size); #endif MEMDEBUG_LOG_FUNC(malloc, addr, file, line); @@ -118,7 +121,7 @@ void *__memdebug_realloc(void *ptr, size_t size, char *file, int line) #else void *addr = MEMDEBUG_REALLOC_SYMBOL( ptr, size + MEMDEBUG_OUT_OF_BOUNDS_EXTRA_SIZE); - MEMDEBUG_OUT_OF_BOUNDS_CHECK(addr, size); + if(addr) addr = memdebug_add_out_of_bounds_check(addr, size); #endif MEMDEBUG_LOG_FUNC(realloc, addr, file, line); @@ -137,7 +140,7 @@ void *__memdebug_calloc(size_t nmemb, size_t size, char *file, int line) #else void *addr = MEMDEBUG_MALLOC_SYMBOL( nmemb * size + MEMDEBUG_OUT_OF_BOUNDS_EXTRA_SIZE); - MEMDEBUG_OUT_OF_BOUNDS_CHECK(addr, nmemb * size); + if(addr) addr = memdebug_add_out_of_bounds_check(addr, nmemb * size); memset(addr, 0, nmemb * size); #endif @@ -158,9 +161,9 @@ void __memdebug_free(void *ptr, char *file, int line) #ifdef MEMDEBUG_OUT_OF_BOUNDS if(ptr != NULL) { - size_t size = *(size_t *)(ptr - sizeof(size_t)); + size_t size = *(size_t *)((uintptr_t)ptr - sizeof(size_t)); memdebug_suffix suffix = 0; - memcpy(&suffix, ptr + size, sizeof(suffix)); + memcpy(&suffix, (void *)((uintptr_t)ptr + size), sizeof(suffix)); MEMDEBUG_LOG(", "); MEMDEBUG_LOG("out-of-bounds-check: "); @@ -169,7 +172,7 @@ void __memdebug_free(void *ptr, char *file, int line) MEMDEBUG_LOG("SUCCESS"); else MEMDEBUG_LOG("FAILED"); - ptr -= sizeof(size_t); + ptr = (void *)((uintptr_t)ptr + sizeof(size_t)); } #endif -- cgit v1.2.3