From 923ff358ee9f4d37b70a224a49c8a4d6492989d1 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 23 Jun 2025 13:03:02 -0400 Subject: lib: Inline checking for NULL ptr in realloc Signed-off-by: Andrew Opalach --- include/al/array_typed.h | 2 +- include/al/lib.h | 10 +++++++--- include/al/str.h | 2 +- include/al/wstr.h | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) (limited to 'include/al') diff --git a/include/al/array_typed.h b/include/al/array_typed.h index 2092160..e4a6c53 100644 --- a/include/al/array_typed.h +++ b/include/al/array_typed.h @@ -84,7 +84,7 @@ } \ static inline void _al_array_reserve_internal(N, __VA_ARGS__)(array(N, __VA_ARGS__) *array, u32 size) \ { \ - array->alloc = al_growing_allocation((void **)&array->data, array->alloc, size, al_malloc, al_realloc); \ + array->alloc = al_growing_allocation((void **)&array->data, array->alloc, size, al_realloc); \ } \ static inline void al_array_free(N, __VA_ARGS__)(array(N, __VA_ARGS__) *array) \ { \ diff --git a/include/al/lib.h b/include/al/lib.h index e661572..3d6556b 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -18,6 +18,7 @@ typedef char type##__size_test[(!!(sizeof(type) == size)) * 2 - 1] #define AL_USE_STDLIB +//#define AL_PARANOID_REALLOC //#define AL_MEMORY_TRACKING //#define AL_FORCE_DISABLE_OUTPUT @@ -61,12 +62,16 @@ void al_set_alloc(void *(*malloc_func)(size_t), void *(*calloc_func)(size_t, siz s32 (*posix_memalign_func)(void **, size_t, size_t), #endif void (*free_func)(void *), void (*lock_func)(void), void (*unlock_func)(void)); -void al_malloc_stats(size_t *current, size_t *peak, size_t *total); +void al_malloc_stats(size_t *current, size_t *peak, size_t *total, size_t *ops); #else #ifdef AL_USE_STDLIB #define al_malloc malloc #define al_calloc calloc +#ifdef AL_PARANOID_REALLOC +static inline void *al_realloc(void *ptr, size_t n) { return ptr ? realloc(ptr, n) : malloc(n); } +#else #define al_realloc realloc +#endif #ifdef AL_HAVE_POSIX_MEMALIGN #define al_posix_memalign posix_memalign #endif @@ -246,8 +251,7 @@ static inline u16 al_u16_add_wrap(u16 v, u16 add, u16 max) // Once a growing allocation expands past this number, only grow in multiples of it. extern u32 al_grow_step; -u32 al_growing_allocation(void **ptr, u32 prev_size, u32 size, - void *(*malloc_func)(size_t), void *(*realloc_func)(void *, size_t)); +u32 al_growing_allocation(void **ptr, u32 prev_size, u32 size, void *(*realloc_func)(void *, size_t)); // Defaults to 4KB. extern size_t al_page_size; diff --git a/include/al/str.h b/include/al/str.h index 2055301..93b4f4a 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -39,7 +39,7 @@ static inline void al_str_free(str *s) static inline u32 al_str_reserve(str *s, u32 size) { - return al_growing_allocation((void **)&s->data, s->alloc, size, al_malloc, al_realloc); + return al_growing_allocation((void **)&s->data, s->alloc, size, al_realloc); } static inline void al_str_sized(str *s, u32 size) diff --git a/include/al/wstr.h b/include/al/wstr.h index 47dac45..c80eac2 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -49,7 +49,7 @@ static inline void al_wstr_free(wstr *w) static inline u32 al_wstr_reserve(wstr *w, u32 size) { - return al_growing_allocation((void **)&w->data, w->alloc, size, al_malloc, al_realloc); + return al_growing_allocation((void **)&w->data, w->alloc, size, al_realloc); } static inline void al_wstr_sized(wstr *w, u32 size) -- cgit v1.2.3-101-g0448