change to engine init; little fix

This commit is contained in:
aap 2020-11-25 22:47:26 +01:00
parent d9def88c46
commit e8990d5b3d
3 changed files with 44 additions and 22 deletions

View File

@ -125,6 +125,15 @@ free_managed(void *p)
free(mem->origPtr);
}
void
printleaks(void)
{
FORLIST(lnk, allocations){
MemoryBlock *mem = LLLinkGetData(lnk, MemoryBlock, inAllocList);
printf("sz %d hint %X\n %s\n", mem->sz, mem->hint, mem->codeline);
}
}
// TODO: make the debug out configurable
void *mustmalloc_h(size_t sz, uint32 hint)
{
@ -147,11 +156,25 @@ void *mustrealloc_h(void *p, size_t sz, uint32 hint)
return nil;
}
//#define TRACK_ALLOCATIONS
MemoryFunctions defaultMemfuncs = {
malloc_h,
realloc_h,
free,
nil,
nil
};
MemoryFunctions managedMemfuncs = {
malloc_managed,
realloc_managed,
free_managed,
nil,
nil
};
// This function mainly registers engine plugins
bool32
Engine::init(void)
Engine::init(MemoryFunctions *memfuncs)
{
if(engine || Engine::state != Dead){
RWERROR((ERR_ENGINEINIT));
@ -161,19 +184,15 @@ Engine::init(void)
totalMemoryAllocated = 0;
allocations.init();
// TODO: make this an argument
#ifdef TRACK_ALLOCATIONS
memfuncs.rwmalloc = malloc_managed;
memfuncs.rwrealloc = realloc_managed;
memfuncs.rwfree = free_managed;
#else
memfuncs.rwmalloc = malloc_h;
memfuncs.rwrealloc = realloc_h;
memfuncs.rwfree = free;
#endif
if(memfuncs)
Engine::memfuncs = *memfuncs;
else
Engine::memfuncs = defaultMemfuncs;
memfuncs.rwmustmalloc = mustmalloc_h;
memfuncs.rwmustrealloc = mustrealloc_h;
if(Engine::memfuncs.rwmustmalloc == nil)
Engine::memfuncs.rwmustmalloc = mustmalloc_h;
if(Engine::memfuncs.rwmustrealloc == nil)
Engine::memfuncs.rwmustrealloc = mustrealloc_h;
PluginList::open();
@ -303,13 +322,6 @@ Engine::term(void)
// TODO: maybe reset more stuff here?
d3d::nativeRasterOffset = 0;
#ifdef TRACK_ALLOCATIONS
FORLIST(lnk, allocations){
MemoryBlock *mem = LLLinkGetData(lnk, MemoryBlock, inAllocList);
printf("sz %d hint %X\n %s\n", mem->sz, mem->hint, mem->codeline);
}
#endif
Engine::state = Dead;
}

View File

@ -83,6 +83,9 @@ closeIm2D(void)
{
glDeleteBuffers(1, &im2DIbo);
glDeleteBuffers(1, &im2DVbo);
#ifdef RW_GL_USE_VAOS
glDeleteVertexArrays(1, &im2DVao);
#endif
im2dShader->destroy();
im2dShader = nil;
}
@ -238,6 +241,9 @@ closeIm3D(void)
{
glDeleteBuffers(1, &im3DIbo);
glDeleteBuffers(1, &im3DVbo);
#ifdef RW_GL_USE_VAOS
glDeleteVertexArrays(1, &im3DVao);
#endif
im3dShader->destroy();
im3dShader = nil;
}

View File

@ -159,7 +159,7 @@ struct Engine
static MemoryFunctions memfuncs;
static State state;
static bool32 init(void);
static bool32 init(MemoryFunctions *memfuncs = nil);
static bool32 open(EngineOpenParams*);
static bool32 start(void);
static void term(void);
@ -207,6 +207,10 @@ inline void *mustrealloc_LOC(void *p, size_t sz, uint32 hint, const char *here)
#define rwResize(p, s, h) rw::mustrealloc_LOC(p,s,h,RWHERE)
#define rwResizeT(t, p, s, h) (t*)rw::mustrealloc_LOC(p,(s)*sizeof(t),h,RWHERE)
extern MemoryFunctions defaultMemfuncs;
extern MemoryFunctions managedMemfuncs;
void printleaks(void); // when using managed mem funcs
namespace null {
void beginUpdate(Camera*);
void endUpdate(Camera*);