SDL 2.0
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 (void)
 Start tracking SDL memory allocations. More...
 
void SDLTest_LogAllocations (void)
 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 ( void  )

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.

223{
224 char *message = NULL;
225 size_t message_size = 0;
226 char line[128], *tmp;
228 int index, count, stack_index;
229 Uint64 total_allocated;
230
231 if (!SDL_malloc_orig) {
232 return;
233 }
234
235#define ADD_LINE() \
236 message_size += (SDL_strlen(line) + 1); \
237 tmp = (char *)SDL_realloc_orig(message, message_size); \
238 if (!tmp) { \
239 return; \
240 } \
241 message = tmp; \
242 SDL_strlcat(message, line, message_size)
243
244 SDL_strlcpy(line, "Memory allocations:\n", sizeof(line));
245 ADD_LINE();
246 SDL_strlcpy(line, "Expect 2 allocations from within SDL_GetErrBuf()\n", sizeof(line));
247 ADD_LINE();
248
249 count = 0;
250 total_allocated = 0;
252 for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
253 SDL_snprintf(line, sizeof(line), "Allocation %d: %d bytes\n", count, (int)entry->size);
254 ADD_LINE();
255 /* Start at stack index 1 to skip our tracking functions */
256 for (stack_index = 1; stack_index < SDL_arraysize(entry->stack); ++stack_index) {
257 if (!entry->stack[stack_index]) {
258 break;
259 }
260 SDL_snprintf(line, sizeof(line), "\t0x%"SDL_PRIx64": %s\n", entry->stack[stack_index], entry->stack_names[stack_index]);
261 ADD_LINE();
262 }
263 total_allocated += entry->size;
264 ++count;
265 }
266 }
267 SDL_snprintf(line, sizeof(line), "Total: %.2f Kb in %d allocations\n", (float)total_allocated / 1024, count);
268 ADD_LINE();
269#undef ADD_LINE
270
271 SDL_Log("%s", message);
272}
#define SDL_strlcpy
#define SDL_Log
#define SDL_snprintf
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
GLuint index
GLuint GLsizei const GLchar * message
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
uint64_t Uint64
Definition: SDL_stdinc.h:216
#define SDL_PRIx64
Definition: SDL_stdinc.h:249
#define ADD_LINE()
static SDL_malloc_func SDL_malloc_orig
static SDL_tracked_allocation * s_tracked_allocations[256]
#define NULL
Definition: begin_code.h:167
char stack_names[10][256]
struct SDL_tracked_allocation * next

References ADD_LINE, SDL_tracked_allocation::next, NULL, s_tracked_allocations, 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().

◆ SDLTest_TrackAllocations()

int SDLTest_TrackAllocations ( void  )

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.

198{
199 if (SDL_malloc_orig) {
200 return 0;
201 }
202
204
206 if (s_previous_allocations != 0) {
207 SDL_Log("SDLTest_TrackAllocations(): There are %d previous allocations, disabling free() validation", s_previous_allocations);
208 }
209
214
219 return 0;
220}
#define SDL_GetNumAllocations
#define SDL_GetMemoryFunctions
#define SDL_SetMemoryFunctions
int SDLTest_Crc32Init(SDLTest_Crc32Context *crcContext)
Initialize the CRC context.
static SDLTest_Crc32Context s_crc32_context
static int s_previous_allocations
static SDL_calloc_func SDL_calloc_orig
static void SDLTest_TrackedFree(void *ptr)
static SDL_realloc_func SDL_realloc_orig
static SDL_free_func SDL_free_orig
static void * SDLTest_TrackedMalloc(size_t size)
static void * SDLTest_TrackedCalloc(size_t nmemb, size_t size)
static void * SDLTest_TrackedRealloc(void *ptr, size_t size)

References s_crc32_context, 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().