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 ()
 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().

223 {
224  char *message = NULL;
225  size_t message_size = 0;
226  char line[128], *tmp;
227  SDL_tracked_allocation *entry;
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;
251  for (index = 0; index < SDL_arraysize(s_tracked_allocations); ++index) {
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
GLuint GLsizei const GLchar * message
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
uint64_t Uint64
Definition: SDL_stdinc.h:194
static SDL_tracked_allocation * s_tracked_allocations[256]
#define ADD_LINE()
#define SDL_Log
GLuint index
#define SDL_PRIx64
Definition: SDL_stdinc.h:227
#define NULL
Definition: begin_code.h:164
#define SDL_snprintf
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
static SDL_malloc_func SDL_malloc_orig
struct SDL_tracked_allocation * next
char stack_names[10][256]

◆ 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().

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 
213  &SDL_free_orig);
214 
219  return 0;
220 }
static int s_previous_allocations
static SDL_calloc_func SDL_calloc_orig
static void SDLTest_TrackedFree(void *ptr)
static void * SDLTest_TrackedMalloc(size_t size)
#define SDL_SetMemoryFunctions
static void * SDLTest_TrackedCalloc(size_t nmemb, size_t size)
static SDL_realloc_func SDL_realloc_orig
#define SDL_Log
#define SDL_GetMemoryFunctions
static SDL_free_func SDL_free_orig
int SDLTest_Crc32Init(SDLTest_Crc32Context *crcContext)
Initialize the CRC context.
static SDL_malloc_func SDL_malloc_orig
#define SDL_GetNumAllocations
static void * SDLTest_TrackedRealloc(void *ptr, size_t size)
static SDLTest_Crc32Context s_crc32_context