Added functions to query and set the SDL memory allocation functions:

SDL_GetMemoryFunctions()
    SDL_SetMemoryFunctions()
    SDL_GetNumAllocations()
This commit is contained in:
Sam Lantinga committed 2017-10-12 13:44:28 -07:00
1 parent 1887c54c68
commit 9c580e14c9
25 files changed
+311 -68

No files matched your search

+3
View File
@@ -637,3 +637,6 @@
#define SDL_Vulkan_GetDrawableSize SDL_Vulkan_GetDrawableSize_REAL
#define SDL_LockJoysticks SDL_LockJoysticks_REAL
#define SDL_UnlockJoysticks SDL_UnlockJoysticks_REAL
#define SDL_GetMemoryFunctions SDL_GetMemoryFunctions_REAL
#define SDL_SetMemoryFunctions SDL_SetMemoryFunctions_REAL
#define SDL_GetNumAllocations SDL_GetNumAllocations_REAL
+3
View File
@@ -671,3 +671,6 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_Vulkan_CreateSurface,(SDL_Window *a, VkInstance b,
SDL_DYNAPI_PROC(void,SDL_Vulkan_GetDrawableSize,(SDL_Window *a, int *b, int *c),(a,b,c),)
SDL_DYNAPI_PROC(void,SDL_LockJoysticks,(void),(),)
SDL_DYNAPI_PROC(void,SDL_UnlockJoysticks,(void),(),)
SDL_DYNAPI_PROC(void,SDL_GetMemoryFunctions,(SDL_malloc_func *a, SDL_calloc_func *b, SDL_realloc_func *c, SDL_free_func *d),(a,b,c,d),)
SDL_DYNAPI_PROC(int,SDL_SetMemoryFunctions,(SDL_malloc_func a, SDL_calloc_func b, SDL_realloc_func c, SDL_free_func d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_GetNumAllocations,(void),(),return)
+137 -35
View File
@@ -26,39 +26,11 @@
#include "../SDL_internal.h"
/* This file contains portable memory management functions for SDL */
#include "SDL_stdinc.h"
#include "SDL_atomic.h"
#include "SDL_error.h"
#if defined(HAVE_MALLOC)
void *SDL_malloc(size_t size)
{
if (!size) {
return malloc(1);
}
return malloc(size);
}
void *SDL_calloc(size_t nmemb, size_t size)
{
if (!size || !nmemb) {
return calloc(1,1);
}
return calloc(nmemb, size);
}
void *SDL_realloc(void *ptr, size_t size)
{
return realloc(ptr, size);
}
void SDL_free(void *ptr)
{
free(ptr);
}
#else /* the rest of this is a LOT of tapdancing to implement malloc. :) */
#ifndef HAVE_MALLOC
#define LACKS_SYS_TYPES_H
#define LACKS_STDIO_H
#define LACKS_STRINGS_H
@@ -66,6 +38,7 @@ void SDL_free(void *ptr)
#define LACKS_STDLIB_H
#define ABORT
#define USE_LOCKS 1
#define USE_DL_PREFIX
/*
This is a version (aka dlmalloc) of malloc/free/realloc written by
@@ -642,12 +615,12 @@ DEFAULT_MMAP_THRESHOLD default: 256K
#define MALLINFO_FIELD_TYPE size_t
#endif /* MALLINFO_FIELD_TYPE */
#ifndef memset
#define memset SDL_memset
#endif
#ifndef memcpy
#define memcpy SDL_memcpy
#define malloc SDL_malloc
#define calloc SDL_calloc
#define realloc SDL_realloc
#define free SDL_free
#endif
/*
mallopt tuning options. SVID/XPG defines four standard parameter
@@ -5271,4 +5244,133 @@ History:
#endif /* !HAVE_MALLOC */
#ifdef HAVE_MALLOC
#define real_malloc malloc
#define real_calloc calloc
#define real_realloc realloc
#define real_free free
#else
#define real_malloc dlmalloc
#define real_calloc dlcalloc
#define real_realloc dlrealloc
#define real_free dlfree
#endif
/* Memory functions used by SDL that can be replaced by the application */
static struct
{
SDL_malloc_func malloc_func;
SDL_calloc_func calloc_func;
SDL_realloc_func realloc_func;
SDL_free_func free_func;
SDL_atomic_t num_allocations;
} s_mem = {
real_malloc, real_calloc, real_realloc, real_free, { 0 }
};
void SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func,
SDL_calloc_func *calloc_func,
SDL_realloc_func *realloc_func,
SDL_free_func *free_func)
{
if (malloc_func) {
*malloc_func = s_mem.malloc_func;
}
if (calloc_func) {
*calloc_func = s_mem.calloc_func;
}
if (realloc_func) {
*realloc_func = s_mem.realloc_func;
}
if (free_func) {
*free_func = s_mem.free_func;
}
}
int SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
SDL_calloc_func calloc_func,
SDL_realloc_func realloc_func,
SDL_free_func free_func)
{
if (!malloc_func) {
return SDL_InvalidParamError("malloc_func");
}
if (!calloc_func) {
return SDL_InvalidParamError("calloc_func");
}
if (!realloc_func) {
return SDL_InvalidParamError("realloc_func");
}
if (!free_func) {
return SDL_InvalidParamError("free_func");
}
s_mem.malloc_func = malloc_func;
s_mem.calloc_func = calloc_func;
s_mem.realloc_func = realloc_func;
s_mem.free_func = free_func;
return 0;
}
int SDL_GetNumAllocations()
{
return SDL_AtomicGet(&s_mem.num_allocations);
}
void *SDL_malloc(size_t size)
{
void *mem;
if (!size) {
size = 1;
}
mem = s_mem.malloc_func(size);
if (mem) {
SDL_AtomicIncRef(&s_mem.num_allocations);
}
return mem;
}
void *SDL_calloc(size_t nmemb, size_t size)
{
void *mem;
if (!nmemb || !size) {
nmemb = 1;
size = 1;
}
mem = s_mem.calloc_func(nmemb, size);
if (mem) {
SDL_AtomicIncRef(&s_mem.num_allocations);
}
return mem;
}
void *SDL_realloc(void *ptr, size_t size)
{
void *mem;
if (!ptr && !size) {
size = 1;
}
mem = s_mem.realloc_func(ptr, size);
if (mem && !ptr) {
SDL_AtomicIncRef(&s_mem.num_allocations);
}
return mem;
}
void SDL_free(void *ptr)
{
if (!ptr) {
return;
}
s_mem.free_func(ptr);
SDL_AtomicDecRef(&s_mem.num_allocations);
}
/* vi: set ts=4 sw=4 expandtab: */
-4
View File
@@ -561,16 +561,12 @@ SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
char *
SDL_strdup(const char *string)
{
#if defined(HAVE_STRDUP)
return strdup(string);
#else
size_t len = SDL_strlen(string) + 1;
char *newstr = SDL_malloc(len);
if (newstr) {
SDL_strlcpy(newstr, string, len);
}
return newstr;
#endif /* HAVE_STRDUP */
}
char *
+21 -5
View File
@@ -47,7 +47,18 @@ static void SDL_snprintfcat(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL
SDLTest_CommonState *
SDLTest_CommonCreateState(char **argv, Uint32 flags)
{
SDLTest_CommonState *state = (SDLTest_CommonState *)SDL_calloc(1, sizeof(*state));
int i;
SDLTest_CommonState *state;
/* Do this first so we catch all allocations */
for (i = 1; argv[i]; ++i) {
if (SDL_strcasecmp(argv[i], "--trackmem") == 0) {
SDLTest_TrackAllocations();
break;
}
}
state = (SDLTest_CommonState *)SDL_calloc(1, sizeof(*state));
if (!state) {
SDL_OutOfMemory();
return NULL;
@@ -447,6 +458,10 @@ SDLTest_CommonArg(SDLTest_CommonState * state, int index)
state->audiospec.samples = (Uint16) SDL_atoi(argv[index]);
return 2;
}
if (SDL_strcasecmp(argv[index], "--trackmem") == 0) {
/* Already handled in SDLTest_CommonCreateState() */
return 1;
}
if ((SDL_strcasecmp(argv[index], "-h") == 0)
|| (SDL_strcasecmp(argv[index], "--help") == 0)) {
/* Print the usage message */
@@ -464,13 +479,13 @@ SDLTest_CommonUsage(SDLTest_CommonState * state)
{
switch (state->flags & (SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
case SDL_INIT_VIDEO:
return VIDEO_USAGE;
return "[--trackmem] " VIDEO_USAGE;
case SDL_INIT_AUDIO:
return AUDIO_USAGE;
return "[--trackmem] " AUDIO_USAGE;
case (SDL_INIT_VIDEO | SDL_INIT_AUDIO):
return VIDEO_USAGE " " AUDIO_USAGE;
return "[--trackmem] " VIDEO_USAGE " " AUDIO_USAGE;
default:
return "";
return "[--trackmem]";
}
}
@@ -1762,6 +1777,7 @@ SDLTest_CommonQuit(SDLTest_CommonState * state)
}
SDL_free(state);
SDL_Quit();
SDLTest_LogAllocations();
}
/* vi: set ts=4 sw=4 expandtab: */
-2
View File
@@ -69,7 +69,6 @@ int SDLTest_Crc32Init(SDLTest_Crc32Context *crcContext)
}
/* Complete CRC32 calculation on a memory block */
/* un-used
int SDLTest_Crc32Calc(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
{
if (SDLTest_Crc32CalcStart(crcContext,crc32)) {
@@ -86,7 +85,6 @@ int SDLTest_Crc32Calc(SDLTest_Crc32Context * crcContext, CrcUint8 *inBuf, CrcUin
return 0;
}
*/
/* Start crc calculation */