Added --randmem test parameter

This commit is contained in:
Brick 2023-08-27 20:05:43 +01:00 committed by Sam Lantinga
parent ea68bb8027
commit 82e481b520
3 changed files with 66 additions and 14 deletions

View File

@ -42,7 +42,14 @@ extern "C" {
*
* \note This should be called before any other SDL functions for complete tracking coverage
*/
int SDLTest_TrackAllocations(void);
void SDLTest_TrackAllocations(void);
/**
* \brief Fill allocations with random data
*
* \note This implicitly calls SDLTest_TrackAllocations()
*/
void SDLTest_RandFillAllocations();
/**
* \brief Print a log of any outstanding allocations

View File

@ -25,6 +25,7 @@
static const char *common_usage[] = {
"[-h | --help]",
"[--trackmem]",
"[--randmem]",
"[--log all|error|system|audio|video|render|input]",
};
@ -95,7 +96,8 @@ SDLTest_CommonState *SDLTest_CommonCreateState(char **argv, Uint32 flags)
for (i = 1; argv[i]; ++i) {
if (SDL_strcasecmp(argv[i], "--trackmem") == 0) {
SDLTest_TrackAllocations();
break;
} else if (SDL_strcasecmp(argv[i], "--randmem") == 0) {
SDLTest_RandFillAllocations();
}
}
@ -170,6 +172,10 @@ int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
/* Already handled in SDLTest_CommonCreateState() */
return 1;
}
if (SDL_strcasecmp(argv[index], "--randmem") == 0) {
/* Already handled in SDLTest_CommonCreateState() */
return 1;
}
if (SDL_strcasecmp(argv[index], "--log") == 0) {
++index;
if (!argv[index]) {

View File

@ -48,6 +48,7 @@ static SDL_realloc_func SDL_realloc_orig = NULL;
static SDL_free_func SDL_free_orig = NULL;
static int s_previous_allocations = 0;
static SDL_tracked_allocation *s_tracked_allocations[256];
static SDL_bool s_randfill_allocations = SDL_FALSE;
static unsigned int get_allocation_bucket(void *mem)
{
@ -58,16 +59,28 @@ static unsigned int get_allocation_bucket(void *mem)
return index;
}
static SDL_bool SDL_IsAllocationTracked(void *mem)
static SDL_tracked_allocation* SDL_GetTrackedAllocation(void *mem)
{
SDL_tracked_allocation *entry;
int index = get_allocation_bucket(mem);
for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
if (mem == entry->mem) {
return SDL_TRUE;
return entry;
}
}
return SDL_FALSE;
return NULL;
}
static size_t SDL_GetTrackedAllocationSize(void *mem)
{
SDL_tracked_allocation *entry = SDL_GetTrackedAllocation(mem);
return entry ? entry->size : SIZE_MAX;
}
static SDL_bool SDL_IsAllocationTracked(void *mem)
{
return SDL_GetTrackedAllocation(mem) != NULL;
}
static void SDL_TrackAllocation(void *mem, size_t size)
@ -140,6 +153,19 @@ static void SDL_UntrackAllocation(void *mem)
}
}
static void rand_fill_memory(void* ptr, size_t start, size_t end)
{
Uint8* mem = (Uint8*) ptr;
size_t i;
if (!s_randfill_allocations)
return;
for (i = start; i < end; ++i) {
mem[i] = SDLTest_RandomUint8();
}
}
static void *SDLCALL SDLTest_TrackedMalloc(size_t size)
{
void *mem;
@ -147,6 +173,7 @@ static void *SDLCALL SDLTest_TrackedMalloc(size_t size)
mem = SDL_malloc_orig(size);
if (mem) {
SDL_TrackAllocation(mem, size);
rand_fill_memory(mem, 0, size);
}
return mem;
}
@ -165,14 +192,20 @@ static void *SDLCALL SDLTest_TrackedCalloc(size_t nmemb, size_t size)
static void *SDLCALL SDLTest_TrackedRealloc(void *ptr, size_t size)
{
void *mem;
SDL_assert(ptr == NULL || SDL_IsAllocationTracked(ptr));
size_t old_size = 0;
if (ptr) {
old_size = SDL_GetTrackedAllocationSize(ptr);
SDL_assert(old_size != SIZE_MAX);
}
mem = SDL_realloc_orig(ptr, size);
if (mem && mem != ptr) {
if (ptr) {
SDL_UntrackAllocation(ptr);
}
if (ptr) {
SDL_UntrackAllocation(ptr);
}
if (mem) {
SDL_TrackAllocation(mem, size);
if (size > old_size) {
rand_fill_memory(mem, old_size, size);
}
}
return mem;
}
@ -190,10 +223,10 @@ static void SDLCALL SDLTest_TrackedFree(void *ptr)
SDL_free_orig(ptr);
}
int SDLTest_TrackAllocations(void)
void SDLTest_TrackAllocations(void)
{
if (SDL_malloc_orig) {
return 0;
return;
}
SDLTest_Crc32Init(&s_crc32_context);
@ -212,7 +245,13 @@ int SDLTest_TrackAllocations(void)
SDLTest_TrackedCalloc,
SDLTest_TrackedRealloc,
SDLTest_TrackedFree);
return 0;
}
void SDLTest_RandFillAllocations()
{
SDLTest_TrackAllocations();
s_randfill_allocations = SDL_TRUE;
}
void SDLTest_LogAllocations(void)