mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-05-17 10:18:28 +00:00
Added --randmem test parameter
This commit is contained in:
parent
ea68bb8027
commit
82e481b520
3 changed files with 66 additions and 14 deletions
|
@ -42,7 +42,14 @@ extern "C" {
|
||||||
*
|
*
|
||||||
* \note This should be called before any other SDL functions for complete tracking coverage
|
* \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
|
* \brief Print a log of any outstanding allocations
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
static const char *common_usage[] = {
|
static const char *common_usage[] = {
|
||||||
"[-h | --help]",
|
"[-h | --help]",
|
||||||
"[--trackmem]",
|
"[--trackmem]",
|
||||||
|
"[--randmem]",
|
||||||
"[--log all|error|system|audio|video|render|input]",
|
"[--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) {
|
for (i = 1; argv[i]; ++i) {
|
||||||
if (SDL_strcasecmp(argv[i], "--trackmem") == 0) {
|
if (SDL_strcasecmp(argv[i], "--trackmem") == 0) {
|
||||||
SDLTest_TrackAllocations();
|
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() */
|
/* Already handled in SDLTest_CommonCreateState() */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
if (SDL_strcasecmp(argv[index], "--randmem") == 0) {
|
||||||
|
/* Already handled in SDLTest_CommonCreateState() */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
if (SDL_strcasecmp(argv[index], "--log") == 0) {
|
if (SDL_strcasecmp(argv[index], "--log") == 0) {
|
||||||
++index;
|
++index;
|
||||||
if (!argv[index]) {
|
if (!argv[index]) {
|
||||||
|
|
|
@ -48,6 +48,7 @@ static SDL_realloc_func SDL_realloc_orig = NULL;
|
||||||
static SDL_free_func SDL_free_orig = NULL;
|
static SDL_free_func SDL_free_orig = NULL;
|
||||||
static int s_previous_allocations = 0;
|
static int s_previous_allocations = 0;
|
||||||
static SDL_tracked_allocation *s_tracked_allocations[256];
|
static SDL_tracked_allocation *s_tracked_allocations[256];
|
||||||
|
static SDL_bool s_randfill_allocations = SDL_FALSE;
|
||||||
|
|
||||||
static unsigned int get_allocation_bucket(void *mem)
|
static unsigned int get_allocation_bucket(void *mem)
|
||||||
{
|
{
|
||||||
|
@ -58,16 +59,28 @@ static unsigned int get_allocation_bucket(void *mem)
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SDL_bool SDL_IsAllocationTracked(void *mem)
|
static SDL_tracked_allocation* SDL_GetTrackedAllocation(void *mem)
|
||||||
{
|
{
|
||||||
SDL_tracked_allocation *entry;
|
SDL_tracked_allocation *entry;
|
||||||
int index = get_allocation_bucket(mem);
|
int index = get_allocation_bucket(mem);
|
||||||
for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
|
for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
|
||||||
if (mem == entry->mem) {
|
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)
|
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)
|
static void *SDLCALL SDLTest_TrackedMalloc(size_t size)
|
||||||
{
|
{
|
||||||
void *mem;
|
void *mem;
|
||||||
|
@ -147,6 +173,7 @@ static void *SDLCALL SDLTest_TrackedMalloc(size_t size)
|
||||||
mem = SDL_malloc_orig(size);
|
mem = SDL_malloc_orig(size);
|
||||||
if (mem) {
|
if (mem) {
|
||||||
SDL_TrackAllocation(mem, size);
|
SDL_TrackAllocation(mem, size);
|
||||||
|
rand_fill_memory(mem, 0, size);
|
||||||
}
|
}
|
||||||
return mem;
|
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)
|
static void *SDLCALL SDLTest_TrackedRealloc(void *ptr, size_t size)
|
||||||
{
|
{
|
||||||
void *mem;
|
void *mem;
|
||||||
|
size_t old_size = 0;
|
||||||
SDL_assert(ptr == NULL || SDL_IsAllocationTracked(ptr));
|
if (ptr) {
|
||||||
|
old_size = SDL_GetTrackedAllocationSize(ptr);
|
||||||
|
SDL_assert(old_size != SIZE_MAX);
|
||||||
|
}
|
||||||
mem = SDL_realloc_orig(ptr, size);
|
mem = SDL_realloc_orig(ptr, size);
|
||||||
if (mem && mem != ptr) {
|
|
||||||
if (ptr) {
|
if (ptr) {
|
||||||
SDL_UntrackAllocation(ptr);
|
SDL_UntrackAllocation(ptr);
|
||||||
}
|
}
|
||||||
|
if (mem) {
|
||||||
SDL_TrackAllocation(mem, size);
|
SDL_TrackAllocation(mem, size);
|
||||||
|
if (size > old_size) {
|
||||||
|
rand_fill_memory(mem, old_size, size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
|
@ -190,10 +223,10 @@ static void SDLCALL SDLTest_TrackedFree(void *ptr)
|
||||||
SDL_free_orig(ptr);
|
SDL_free_orig(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SDLTest_TrackAllocations(void)
|
void SDLTest_TrackAllocations(void)
|
||||||
{
|
{
|
||||||
if (SDL_malloc_orig) {
|
if (SDL_malloc_orig) {
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDLTest_Crc32Init(&s_crc32_context);
|
SDLTest_Crc32Init(&s_crc32_context);
|
||||||
|
@ -212,7 +245,13 @@ int SDLTest_TrackAllocations(void)
|
||||||
SDLTest_TrackedCalloc,
|
SDLTest_TrackedCalloc,
|
||||||
SDLTest_TrackedRealloc,
|
SDLTest_TrackedRealloc,
|
||||||
SDLTest_TrackedFree);
|
SDLTest_TrackedFree);
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
void SDLTest_RandFillAllocations()
|
||||||
|
{
|
||||||
|
SDLTest_TrackAllocations();
|
||||||
|
|
||||||
|
s_randfill_allocations = SDL_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDLTest_LogAllocations(void)
|
void SDLTest_LogAllocations(void)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue