Simple Directmedia Layer

SDL_GetNumAllocations returns -1 when allocation counting is disabled

authored by

Anonymous Maarten and committed by
Sam Lantinga
f8d8bf80 a33144fb

+15 -19
+2 -1
include/SDL3/SDL_stdinc.h
··· 1542 1542 /** 1543 1543 * Get the number of outstanding (unfreed) allocations. 1544 1544 * 1545 - * \returns the number of allocations. 1545 + * \returns the number of allocations or 1546 + * -1 if allocation counting is disabled. 1546 1547 * 1547 1548 * \threadsafety It is safe to call this function from any thread. 1548 1549 *
+6 -2
src/stdlib/SDL_malloc.c
··· 6353 6353 }; 6354 6354 6355 6355 // Define this if you want to track the number of allocations active 6356 - // #define TRACK_ALLOCATION_COUNT 6357 - #ifdef TRACK_ALLOCATION_COUNT 6356 + // #define SDL_TRACK_ALLOCATION_COUNT 6357 + #ifdef SDL_TRACK_ALLOCATION_COUNT 6358 6358 #define INCREMENT_ALLOCATION_COUNT() (void)SDL_AtomicIncRef(&s_mem.num_allocations) 6359 6359 #define DECREMENT_ALLOCATION_COUNT() (void)SDL_AtomicDecRef(&s_mem.num_allocations) 6360 6360 #else ··· 6428 6428 6429 6429 int SDL_GetNumAllocations(void) 6430 6430 { 6431 + #ifdef SDL_TRACK_ALLOCATION_COUNT 6431 6432 return SDL_GetAtomicInt(&s_mem.num_allocations); 6433 + #else 6434 + return -1; 6435 + #endif 6432 6436 } 6433 6437 6434 6438 void *SDL_malloc(size_t size)
+7 -16
src/test/SDL_test_memory.c
··· 71 71 static SDL_calloc_func SDL_calloc_orig = NULL; 72 72 static SDL_realloc_func SDL_realloc_orig = NULL; 73 73 static SDL_free_func SDL_free_orig = NULL; 74 - #ifdef TRACK_ALLOCATION_COUNT 75 74 static int s_previous_allocations = 0; 76 - #else 77 75 static int s_unknown_frees = 0; 78 - #endif 79 76 static SDL_tracked_allocation *s_tracked_allocations[256]; 80 77 static bool s_randfill_allocations = false; 81 78 static SDL_AtomicInt s_lock; ··· 215 212 } 216 213 prev = entry; 217 214 } 218 - #ifndef TRACK_ALLOCATION_COUNT 219 - s_unknown_frees += 1; 220 - #endif 215 + if (s_tracked_allocations < 0) { 216 + s_unknown_frees += 1; 217 + } 221 218 UNLOCK_ALLOCATOR(); 222 219 } 223 220 ··· 284 281 return; 285 282 } 286 283 287 - #ifdef TRACK_ALLOCATION_COUNT 288 - if (!s_previous_allocations) { 284 + if (s_previous_allocations == 0) { 289 285 SDL_assert(SDL_IsAllocationTracked(ptr)); 290 286 } 291 - #endif 292 287 SDL_UntrackAllocation(ptr); 293 288 SDL_free_orig(ptr); 294 289 } ··· 301 296 302 297 SDLTest_Crc32Init(&s_crc32_context); 303 298 304 - #ifdef TRACK_ALLOCATION_COUNT 305 299 s_previous_allocations = SDL_GetNumAllocations(); 306 - if (s_previous_allocations != 0) { 300 + if (s_previous_allocations < 0) { 301 + SDL_Log("SDL was built without allocation count support, disabling free() validation"); 302 + } else if (s_previous_allocations != 0) { 307 303 SDL_Log("SDLTest_TrackAllocations(): There are %d previous allocations, disabling free() validation", s_previous_allocations); 308 304 } 309 - #else 310 - SDL_Log("SDL was built without allocation count support, disabling free() validation"); 311 - #endif 312 305 #ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP 313 306 do { 314 307 /* Don't use SDL_GetHint: SDL_malloc is off limits. */ ··· 454 447 } 455 448 (void)SDL_snprintf(line, sizeof(line), "Total: %.2f Kb in %d allocations", total_allocated / 1024.0, count); 456 449 ADD_LINE(); 457 - #ifndef TRACK_ALLOCATION_COUNT 458 450 if (s_unknown_frees != 0) { 459 451 (void)SDL_snprintf(line, sizeof(line), ", %d unknown frees", s_unknown_frees); 460 452 ADD_LINE(); 461 453 } 462 - #endif 463 454 (void)SDL_snprintf(line, sizeof(line), "\n"); 464 455 ADD_LINE(); 465 456 #undef ADD_LINE