mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2025-12-21 17:56:04 +08:00
Defining SpineExtension class that can be overridden to provide memory allocation and file reading
This commit is contained in:
parent
0ca6803030
commit
af43d7cff1
@ -31,30 +31,65 @@
|
|||||||
#ifndef Spine_Extension_h
|
#ifndef Spine_Extension_h
|
||||||
#define Spine_Extension_h
|
#define Spine_Extension_h
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define SPINE_EXTENSION (SpineExtension::getInstance())
|
||||||
|
|
||||||
/* All allocation uses these. */
|
/* All allocation uses these. */
|
||||||
#define MALLOC(TYPE,COUNT) ((TYPE*)spineAlloc(sizeof(TYPE) * (COUNT), __FILE__, __LINE__))
|
#define MALLOC(TYPE,COUNT) ((TYPE*)SPINE_EXTENSION->spineAlloc(sizeof(TYPE) * (COUNT), __FILE__, __LINE__))
|
||||||
#define NEW(TYPE) ((TYPE*)spineAlloc(sizeof(TYPE), __FILE__, __LINE__))
|
#define NEW(TYPE) ((TYPE*)SPINE_EXTENSION->spineAlloc(sizeof(TYPE), __FILE__, __LINE__))
|
||||||
#define REALLOC(PTR,TYPE,COUNT) ((TYPE*)spineRealloc(PTR, sizeof(TYPE) * (COUNT), __FILE__, __LINE__))
|
#define REALLOC(PTR,TYPE,COUNT) ((TYPE*)SPINE_EXTENSION->spineRealloc(PTR, sizeof(TYPE) * (COUNT), __FILE__, __LINE__))
|
||||||
|
|
||||||
/* Frees memory. Can be used on const types. */
|
/* Frees memory. Can be used on const types. */
|
||||||
#define FREE(VALUE) spineFree((void*)VALUE)
|
#define FREE(VALUE) SPINE_EXTENSION->spineFree((void*)VALUE)
|
||||||
|
|
||||||
/* Call destructor and then frees memory. Can be used on const types. */
|
/* Call destructor and then frees memory. Can be used on const types. */
|
||||||
#define DESTROY(TYPE,VALUE) VALUE->~TYPE(); spineFree((void*)VALUE)
|
#define DESTROY(TYPE,VALUE) VALUE->~TYPE(); SPINE_EXTENSION->spineFree((void*)VALUE)
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
namespace Spine
|
namespace Spine
|
||||||
{
|
{
|
||||||
/// Implement this function to use your own memory allocator.
|
class SpineExtension
|
||||||
void* spineAlloc(size_t size, const char* file, int line);
|
{
|
||||||
|
public:
|
||||||
|
static void setInstance(SpineExtension* inSpineExtension);
|
||||||
|
|
||||||
void* spineRealloc(void* ptr, size_t size, const char* file, int line);
|
static SpineExtension* getInstance();
|
||||||
|
|
||||||
/// If you implement spineAlloc, you should also implement this function.
|
virtual ~SpineExtension();
|
||||||
void spineFree(void* mem);
|
|
||||||
|
|
||||||
char* spineReadFile(const char* path, int* length);
|
/// Implement this function to use your own memory allocator
|
||||||
|
virtual void* spineAlloc(size_t size, const char* file, int line) = 0;
|
||||||
|
|
||||||
|
virtual void* spineRealloc(void* ptr, size_t size, const char* file, int line) = 0;
|
||||||
|
|
||||||
|
/// If you provide a spineAllocFunc, you should also provide a spineFreeFunc
|
||||||
|
virtual void spineFree(void* mem) = 0;
|
||||||
|
|
||||||
|
virtual char* spineReadFile(const char* path, int* length);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
SpineExtension();
|
||||||
|
|
||||||
|
private:
|
||||||
|
static SpineExtension* _spineExtension;
|
||||||
|
};
|
||||||
|
|
||||||
|
class DefaultSpineExtension : public SpineExtension
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static DefaultSpineExtension* getInstance();
|
||||||
|
|
||||||
|
virtual ~DefaultSpineExtension();
|
||||||
|
|
||||||
|
virtual void* spineAlloc(size_t size, const char* file, int line);
|
||||||
|
|
||||||
|
virtual void* spineRealloc(void* ptr, size_t size, const char* file, int line);
|
||||||
|
|
||||||
|
virtual void spineFree(void* mem);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
DefaultSpineExtension();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* Spine_Extension_h */
|
#endif /* Spine_Extension_h */
|
||||||
|
|||||||
@ -53,7 +53,7 @@ namespace Spine
|
|||||||
memcpy(dir, path, dirLength);
|
memcpy(dir, path, dirLength);
|
||||||
dir[dirLength] = '\0';
|
dir[dirLength] = '\0';
|
||||||
|
|
||||||
data = spineReadFile(path, &length);
|
data = SPINE_EXTENSION->spineReadFile(path, &length);
|
||||||
if (data)
|
if (data)
|
||||||
{
|
{
|
||||||
load(data, length, dir);
|
load(data, length, dir);
|
||||||
|
|||||||
@ -30,31 +30,33 @@
|
|||||||
|
|
||||||
#include <spine/Extension.h>
|
#include <spine/Extension.h>
|
||||||
|
|
||||||
//#include <iostream>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
namespace Spine
|
namespace Spine
|
||||||
{
|
{
|
||||||
void* spineAlloc(size_t size, const char* file, int line)
|
SpineExtension* SpineExtension::_spineExtension = NULL;
|
||||||
{
|
|
||||||
//printf("spineAlloc size: %lu, file: %s, line: %d \n", size, file, line);
|
|
||||||
|
|
||||||
return malloc(size);
|
void SpineExtension::setInstance(SpineExtension* inValue)
|
||||||
|
{
|
||||||
|
assert(!_spineExtension);
|
||||||
|
|
||||||
|
_spineExtension = inValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* spineRealloc(void* ptr, size_t size, const char* file, int line)
|
SpineExtension* SpineExtension::getInstance()
|
||||||
{
|
{
|
||||||
//printf("spineRealloc size: %lu, file: %s, line: %d \n", size, file, line);
|
assert(_spineExtension);
|
||||||
|
|
||||||
return realloc(ptr, size);
|
return _spineExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
void spineFree(void* mem)
|
SpineExtension::~SpineExtension()
|
||||||
{
|
{
|
||||||
free(mem);
|
// Empty
|
||||||
}
|
}
|
||||||
|
|
||||||
char* spineReadFile(const char* path, int* length)
|
char* SpineExtension::spineReadFile(const char* path, int* length)
|
||||||
{
|
{
|
||||||
char *data;
|
char *data;
|
||||||
FILE *file = fopen(path, "rb");
|
FILE *file = fopen(path, "rb");
|
||||||
@ -70,4 +72,40 @@ namespace Spine
|
|||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SpineExtension::SpineExtension()
|
||||||
|
{
|
||||||
|
// Empty
|
||||||
|
}
|
||||||
|
|
||||||
|
DefaultSpineExtension* DefaultSpineExtension::getInstance()
|
||||||
|
{
|
||||||
|
static DefaultSpineExtension ret;
|
||||||
|
return &ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
DefaultSpineExtension::~DefaultSpineExtension()
|
||||||
|
{
|
||||||
|
// Empty
|
||||||
|
}
|
||||||
|
|
||||||
|
void* DefaultSpineExtension::spineAlloc(size_t size, const char* file, int line)
|
||||||
|
{
|
||||||
|
return malloc(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* DefaultSpineExtension::spineRealloc(void* ptr, size_t size, const char* file, int line)
|
||||||
|
{
|
||||||
|
return realloc(ptr, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DefaultSpineExtension::spineFree(void* mem)
|
||||||
|
{
|
||||||
|
free(mem);
|
||||||
|
}
|
||||||
|
|
||||||
|
DefaultSpineExtension::DefaultSpineExtension() : SpineExtension()
|
||||||
|
{
|
||||||
|
// Empty
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user