From d1aeb1c3b4f1c97fa0f797aa3569eae282cc2523 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Fri, 28 Jul 2023 13:09:43 +0200 Subject: [PATCH] Cleanup: add static assert for detect bad usage of MEM_SAFE_FREE Without this, there is not compilation error when doing e.g.: ``` int a; MEM_SAFE_FREE(a); ``` --- intern/guardedalloc/MEM_guardedalloc.h | 28 ++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/intern/guardedalloc/MEM_guardedalloc.h b/intern/guardedalloc/MEM_guardedalloc.h index 6ed6965934f..b42f73fec95 100644 --- a/intern/guardedalloc/MEM_guardedalloc.h +++ b/intern/guardedalloc/MEM_guardedalloc.h @@ -173,14 +173,26 @@ extern void (*MEM_reset_peak_memory)(void); /** Get the peak memory usage in bytes, including `mmap` allocations. */ extern size_t (*MEM_get_peak_memory)(void) ATTR_WARN_UNUSED_RESULT; -#define MEM_SAFE_FREE(v) \ - do { \ - void **_v = (void **)&(v); \ - if (*_v) { \ - MEM_freeN(*_v); \ - *_v = NULL; \ - } \ - } while (0) +#ifdef __cplusplus +# define MEM_SAFE_FREE(v) \ + do { \ + static_assert(std::is_pointer_v>); \ + void **_v = (void **)&(v); \ + if (*_v) { \ + MEM_freeN(*_v); \ + *_v = NULL; \ + } \ + } while (0) +#else +# define MEM_SAFE_FREE(v) \ + do { \ + void **_v = (void **)&(v); \ + if (*_v) { \ + MEM_freeN(*_v); \ + *_v = NULL; \ + } \ + } while (0) +#endif /* overhead for lockfree allocator (use to avoid slop-space) */ #define MEM_SIZE_OVERHEAD sizeof(size_t)