* Allow memory tracking inside spine

This commit is contained in:
jpoag 2014-05-08 10:49:46 -04:00
parent 39a5229a7f
commit ff07eb02d8
2 changed files with 16 additions and 7 deletions

View File

@ -32,8 +32,8 @@
#define SPINE_EXTENSION_H_ #define SPINE_EXTENSION_H_
/* All allocation uses these. */ /* All allocation uses these. */
#define MALLOC(TYPE,COUNT) ((TYPE*)_malloc(sizeof(TYPE) * COUNT)) #define MALLOC(TYPE,COUNT) ((TYPE*)_malloc(sizeof(TYPE) * COUNT, __FILE__, __LINE__))
#define CALLOC(TYPE,COUNT) ((TYPE*)_calloc(COUNT, sizeof(TYPE))) #define CALLOC(TYPE,COUNT) ((TYPE*)_calloc(COUNT, sizeof(TYPE), __FILE__, __LINE__))
#define NEW(TYPE) CALLOC(TYPE,1) #define NEW(TYPE) CALLOC(TYPE,1)
/* Gets the direct super class. Type safe. */ /* Gets the direct super class. Type safe. */
@ -97,11 +97,12 @@ char* _spUtil_readFile (const char* path, int* length);
* Internal API available for extension: * Internal API available for extension:
*/ */
void* _malloc (size_t size); void* _malloc (size_t size, const char* file, int line);
void* _calloc (size_t num, size_t size); void* _calloc (size_t num, size_t size, const char* file, int line);
void _free (void* ptr); void _free (void* ptr);
void _setMalloc (void* (*_malloc) (size_t size)); void _setMalloc (void* (*_malloc) (size_t size));
void _setDebugMalloc (void* (*_malloc) (size_t size, const char* file, int line));
void _setFree (void (*_free) (void* ptr)); void _setFree (void (*_free) (void* ptr));
char* _readFile (const char* path, int* length); char* _readFile (const char* path, int* length);

View File

@ -32,13 +32,17 @@
#include <stdio.h> #include <stdio.h>
static void* (*mallocFunc) (size_t size) = malloc; static void* (*mallocFunc) (size_t size) = malloc;
static void* (*debugMallocFunc) (size_t size, const char* file, int line) = NULL;
static void (*freeFunc) (void* ptr) = free; static void (*freeFunc) (void* ptr) = free;
void* _malloc (size_t size) { void* _malloc (size_t size, const char* file, int line) {
if(debugMallocFunc)
return debugMallocFunc(size, file, line);
return mallocFunc(size); return mallocFunc(size);
} }
void* _calloc (size_t num, size_t size) { void* _calloc (size_t num, size_t size, const char* file, int line) {
void* ptr = mallocFunc(num * size); void* ptr = _malloc(num * size, file, line);
if (ptr) memset(ptr, 0, num * size); if (ptr) memset(ptr, 0, num * size);
return ptr; return ptr;
} }
@ -46,6 +50,10 @@ void _free (void* ptr) {
freeFunc(ptr); freeFunc(ptr);
} }
void _setDebugMalloc(void* (*malloc) (size_t size, const char* file, int line)) {
debugMallocFunc = malloc;
}
void _setMalloc (void* (*malloc) (size_t size)) { void _setMalloc (void* (*malloc) (size_t size)) {
mallocFunc = malloc; mallocFunc = malloc;
} }