From f8d8bf8066cbc5223b950999baa84ff6715a46d9 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sat, 21 Dec 2024 20:25:06 +0100 Subject: [PATCH] SDL_GetNumAllocations returns -1 when allocation counting is disabled --- include/SDL3/SDL_stdinc.h | 3 ++- src/stdlib/SDL_malloc.c | 8 ++++++-- src/test/SDL_test_memory.c | 23 +++++++---------------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/include/SDL3/SDL_stdinc.h b/include/SDL3/SDL_stdinc.h index ab1fb59587..a70052fbc9 100644 --- a/include/SDL3/SDL_stdinc.h +++ b/include/SDL3/SDL_stdinc.h @@ -1542,7 +1542,8 @@ extern SDL_DECLSPEC void SDLCALL SDL_aligned_free(void *mem); /** * Get the number of outstanding (unfreed) allocations. * - * \returns the number of allocations. + * \returns the number of allocations or + * -1 if allocation counting is disabled. * * \threadsafety It is safe to call this function from any thread. * diff --git a/src/stdlib/SDL_malloc.c b/src/stdlib/SDL_malloc.c index 72e5354c90..d3fc367e56 100644 --- a/src/stdlib/SDL_malloc.c +++ b/src/stdlib/SDL_malloc.c @@ -6353,8 +6353,8 @@ static struct }; // Define this if you want to track the number of allocations active -// #define TRACK_ALLOCATION_COUNT -#ifdef TRACK_ALLOCATION_COUNT +// #define SDL_TRACK_ALLOCATION_COUNT +#ifdef SDL_TRACK_ALLOCATION_COUNT #define INCREMENT_ALLOCATION_COUNT() (void)SDL_AtomicIncRef(&s_mem.num_allocations) #define DECREMENT_ALLOCATION_COUNT() (void)SDL_AtomicDecRef(&s_mem.num_allocations) #else @@ -6428,7 +6428,11 @@ bool SDL_SetMemoryFunctions(SDL_malloc_func malloc_func, int SDL_GetNumAllocations(void) { +#ifdef SDL_TRACK_ALLOCATION_COUNT return SDL_GetAtomicInt(&s_mem.num_allocations); +#else + return -1; +#endif } void *SDL_malloc(size_t size) diff --git a/src/test/SDL_test_memory.c b/src/test/SDL_test_memory.c index a28dec9ebf..594acb3d6b 100644 --- a/src/test/SDL_test_memory.c +++ b/src/test/SDL_test_memory.c @@ -71,11 +71,8 @@ static SDL_malloc_func SDL_malloc_orig = NULL; static SDL_calloc_func SDL_calloc_orig = NULL; static SDL_realloc_func SDL_realloc_orig = NULL; static SDL_free_func SDL_free_orig = NULL; -#ifdef TRACK_ALLOCATION_COUNT static int s_previous_allocations = 0; -#else static int s_unknown_frees = 0; -#endif static SDL_tracked_allocation *s_tracked_allocations[256]; static bool s_randfill_allocations = false; static SDL_AtomicInt s_lock; @@ -215,9 +212,9 @@ static void SDL_UntrackAllocation(void *mem) } prev = entry; } -#ifndef TRACK_ALLOCATION_COUNT - s_unknown_frees += 1; -#endif + if (s_tracked_allocations < 0) { + s_unknown_frees += 1; + } UNLOCK_ALLOCATOR(); } @@ -284,11 +281,9 @@ static void SDLCALL SDLTest_TrackedFree(void *ptr) return; } -#ifdef TRACK_ALLOCATION_COUNT - if (!s_previous_allocations) { + if (s_previous_allocations == 0) { SDL_assert(SDL_IsAllocationTracked(ptr)); } -#endif SDL_UntrackAllocation(ptr); SDL_free_orig(ptr); } @@ -301,14 +296,12 @@ void SDLTest_TrackAllocations(void) SDLTest_Crc32Init(&s_crc32_context); -#ifdef TRACK_ALLOCATION_COUNT s_previous_allocations = SDL_GetNumAllocations(); - if (s_previous_allocations != 0) { + if (s_previous_allocations < 0) { + SDL_Log("SDL was built without allocation count support, disabling free() validation"); + } else if (s_previous_allocations != 0) { SDL_Log("SDLTest_TrackAllocations(): There are %d previous allocations, disabling free() validation", s_previous_allocations); } -#else - SDL_Log("SDL was built without allocation count support, disabling free() validation"); -#endif #ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP do { /* Don't use SDL_GetHint: SDL_malloc is off limits. */ @@ -454,12 +447,10 @@ void SDLTest_LogAllocations(void) } (void)SDL_snprintf(line, sizeof(line), "Total: %.2f Kb in %d allocations", total_allocated / 1024.0, count); ADD_LINE(); -#ifndef TRACK_ALLOCATION_COUNT if (s_unknown_frees != 0) { (void)SDL_snprintf(line, sizeof(line), ", %d unknown frees", s_unknown_frees); ADD_LINE(); } -#endif (void)SDL_snprintf(line, sizeof(line), "\n"); ADD_LINE(); #undef ADD_LINE