aboutsummaryrefslogtreecommitdiff
path: root/src/memdebug.h
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2024-09-08 18:36:07 +0300
committerkartofen <mladenovnasko0@gmail.com>2024-09-08 18:36:07 +0300
commit4308cd4abe5a75fb8410df929eac687cbd04032b (patch)
tree4a5871db2168cb96deab29be7aed36262511c0c1 /src/memdebug.h
parentdb32849ce314a93db01f877a057a91022fec7c8b (diff)
update mempool and add implement hash array mapped trie (not integrated)
Diffstat (limited to 'src/memdebug.h')
-rw-r--r--src/memdebug.h35
1 files changed, 19 insertions, 16 deletions
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 <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <time.h>
@@ -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