Skip to content

Commit

Permalink
Disable tracking memory allocation counts by default
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Dec 21, 2024
1 parent 3ce68f8 commit 883223a
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/stdlib/SDL_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6352,6 +6352,17 @@ static struct
real_malloc, real_calloc, real_realloc, real_free, { 0 }
};

// Define this if you want to track the number of allocations active
// #define TRACK_ALLOCATION_COUNT
#ifdef 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
#define INCREMENT_ALLOCATION_COUNT()
#define DECREMENT_ALLOCATION_COUNT()
#endif


void SDL_GetOriginalMemoryFunctions(SDL_malloc_func *malloc_func,
SDL_calloc_func *calloc_func,
SDL_realloc_func *realloc_func,
Expand Down Expand Up @@ -6430,7 +6441,7 @@ void *SDL_malloc(size_t size)

mem = s_mem.malloc_func(size);
if (mem) {
SDL_AtomicIncRef(&s_mem.num_allocations);
INCREMENT_ALLOCATION_COUNT();
} else {
SDL_OutOfMemory();
}
Expand All @@ -6449,7 +6460,7 @@ void *SDL_calloc(size_t nmemb, size_t size)

mem = s_mem.calloc_func(nmemb, size);
if (mem) {
SDL_AtomicIncRef(&s_mem.num_allocations);
INCREMENT_ALLOCATION_COUNT();
} else {
SDL_OutOfMemory();
}
Expand All @@ -6467,7 +6478,7 @@ void *SDL_realloc(void *ptr, size_t size)

mem = s_mem.realloc_func(ptr, size);
if (mem && !ptr) {
SDL_AtomicIncRef(&s_mem.num_allocations);
INCREMENT_ALLOCATION_COUNT();
} else if (!mem) {
SDL_OutOfMemory();
}
Expand All @@ -6482,5 +6493,5 @@ void SDL_free(void *ptr)
}

s_mem.free_func(ptr);
(void)SDL_AtomicDecRef(&s_mem.num_allocations);
DECREMENT_ALLOCATION_COUNT();
}

0 comments on commit 883223a

Please sign in to comment.