SDL_test_memory.h File Reference
`#include "begin_code.h"`
`#include "close_code.h"`
Include dependency graph for SDL_test_memory.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Functions

int | SDLTest_TrackAllocations ()
| Start tracking SDL memory allocations. More...
void | SDLTest_LogAllocations ()
| Print a log of any outstanding allocations. More...

## Detailed Description

Include file for SDL test framework.

This code is a part of the SDL2_test library, not the main SDL library.

Definition in file SDL_test_memory.h.

## Function Documentation

## SDLTest_LogAllocations()

void SDLTest_LogAllocations | ( |  | ) |

Print a log of any outstanding allocations.

Note
  This can be called after SDL_Quit()

Definition at line 222 of file SDL_test_memory.c.

References ADD_LINE, SDL_tracked_allocation::next, NULL, SDL_arraysize, SDL_Log, SDL_malloc_orig, SDL_PRIx64, SDL_snprintf, SDL_strlcpy, SDL_tracked_allocation::size, SDL_tracked_allocation::stack, and SDL_tracked_allocation::stack_names.

Referenced by SDLTest_CommonQuit().

{
char *message = NULL;
size_t message_size = 0;
char line[128], *tmp;
SDL_tracked_allocation *entry;
int index, count, stack_index;
Uint64 total_allocated;
if (!SDL_malloc_orig) {
return;
}
#define ADD_LINE() \
message_size += (SDL_strlen(line) + 1); \
tmp = (char *)SDL_realloc_orig(message, message_size); \
if (!tmp) { \
return; \
} \
message = tmp; \
SDL_strlcat(message, line, message_size)
SDL_strlcpy(line, "Memory allocations:\n", sizeof(line));
ADD_LINE();
SDL_strlcpy(line, "Expect 2 allocations from within SDL_GetErrBuf()\n", sizeof(line));
ADD_LINE();
count = 0;
total_allocated = 0;
for (index = 0; index < SDL_arraysize(s_tracked_allocations); ++index) {
for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
SDL_snprintf(line, sizeof(line), "Allocation %d: %d bytes\n", count, (int)entry->size);
ADD_LINE();
/* Start at stack index 1 to skip our tracking functions */
for (stack_index = 1; stack_index < SDL_arraysize(entry->stack); ++stack_index) {
if (!entry->stack[stack_index]) {
break;
}
SDL_snprintf(line, sizeof(line), "\t0x%"SDL_PRIx64": %s\n", entry->stack[stack_index], entry->stack_names[stack_index]);
ADD_LINE();
}
total_allocated += entry->size;
++count;
}
}
SDL_snprintf(line, sizeof(line), "Total: %.2f Kb in %d allocations\n", (float)total_allocated / 1024, count);
ADD_LINE();
#undef ADD_LINE
SDL_Log("%s", message);
}
SDL_strlcpy
#define SDL_strlcpy
Definition: SDL_dynapi_overrides.h:394
SDL_tracked_allocation::stack
Uint64 stack[10]
Definition: SDL_test_memory.c:43
message
GLuint GLsizei const GLchar * message
Definition: SDL_opengl_glext.h:2483
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
SDL_tracked_allocation
Definition: SDL_test_memory.c:39
Uint64
uint64_t Uint64
Definition: SDL_stdinc.h:194
s_tracked_allocations
static SDL_tracked_allocation * s_tracked_allocations[256]
Definition: SDL_test_memory.c:54
ADD_LINE
#define ADD_LINE()
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
index
GLuint index
Definition: SDL_opengl_glext.h:660
SDL_PRIx64
#define SDL_PRIx64
Definition: SDL_stdinc.h:227
NULL
#define NULL
Definition: begin_code.h:164
SDL_tracked_allocation::size
size_t size
Definition: SDL_test_memory.c:42
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:40
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
SDL_malloc_orig
static SDL_malloc_func SDL_malloc_orig
Definition: SDL_test_memory.c:49
SDL_tracked_allocation::next
struct SDL_tracked_allocation * next
Definition: SDL_test_memory.c:45
SDL_tracked_allocation::stack_names
char stack_names[10][256]
Definition: SDL_test_memory.c:44

## SDLTest_TrackAllocations()

int SDLTest_TrackAllocations | ( |  | ) |

Start tracking SDL memory allocations.

Note
  This should be called before any other SDL functions for complete tracking coverage

Definition at line 197 of file SDL_test_memory.c.

References s_previous_allocations, SDL_calloc_orig, SDL_free_orig, SDL_GetMemoryFunctions, SDL_GetNumAllocations, SDL_Log, SDL_malloc_orig, SDL_realloc_orig, SDL_SetMemoryFunctions, SDLTest_Crc32Init(), SDLTest_TrackedCalloc(), SDLTest_TrackedFree(), SDLTest_TrackedMalloc(), and SDLTest_TrackedRealloc().

Referenced by SDLTest_CommonCreateState().

{
if (SDL_malloc_orig) {
return 0;
}
SDLTest_Crc32Init(&s_crc32_context);
s_previous_allocations = SDL_GetNumAllocations();
if (s_previous_allocations != 0) {
SDL_Log("SDLTest_TrackAllocations(): There are %d previous allocations, disabling free() validation", s_previous_allocations);
}
SDL_GetMemoryFunctions(&SDL_malloc_orig,
&SDL_calloc_orig,
&SDL_realloc_orig,
&SDL_free_orig);
SDL_SetMemoryFunctions(SDLTest_TrackedMalloc,
SDLTest_TrackedCalloc,
SDLTest_TrackedRealloc,
SDLTest_TrackedFree);
return 0;
}
s_previous_allocations
static int s_previous_allocations
Definition: SDL_test_memory.c:53
SDL_calloc_orig
static SDL_calloc_func SDL_calloc_orig
Definition: SDL_test_memory.c:50
SDLTest_TrackedFree
static void SDLTest_TrackedFree(void *ptr)
Definition: SDL_test_memory.c:184
SDLTest_TrackedMalloc
static void * SDLTest_TrackedMalloc(size_t size)
Definition: SDL_test_memory.c:147
SDL_SetMemoryFunctions
#define SDL_SetMemoryFunctions
Definition: SDL_dynapi_overrides.h:641
SDLTest_TrackedCalloc
static void * SDLTest_TrackedCalloc(size_t nmemb, size_t size)
Definition: SDL_test_memory.c:158
SDL_realloc_orig
static SDL_realloc_func SDL_realloc_orig
Definition: SDL_test_memory.c:51
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_GetMemoryFunctions
#define SDL_GetMemoryFunctions
Definition: SDL_dynapi_overrides.h:640
SDL_free_orig
static SDL_free_func SDL_free_orig
Definition: SDL_test_memory.c:52
SDLTest_Crc32Init
int SDLTest_Crc32Init(SDLTest_Crc32Context *crcContext)
Initialize the CRC context.
Definition: SDL_test_crc32.c:34
SDL_malloc_orig
static SDL_malloc_func SDL_malloc_orig
Definition: SDL_test_memory.c:49
SDL_GetNumAllocations
#define SDL_GetNumAllocations
Definition: SDL_dynapi_overrides.h:642
SDLTest_TrackedRealloc
static void * SDLTest_TrackedRealloc(void *ptr, size_t size)
Definition: SDL_test_memory.c:169
s_crc32_context
static SDLTest_Crc32Context s_crc32_context
Definition: SDL_test_memory.c:48