Simplified allocator interface.

This commit is contained in:
Branimir Karadžić 2014-05-01 10:15:41 -07:00
parent 9970637bb4
commit 6dc6dd6148
2 changed files with 81 additions and 53 deletions

View file

@ -325,9 +325,10 @@ public:
{ {
} }
virtual void* alloc(size_t _size, const char* _file, uint32_t _line) BX_OVERRIDE virtual void* alloc(size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
{
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{ {
BX_UNUSED(_file, _line);
void* ptr = ::malloc(_size); void* ptr = ::malloc(_size);
dbgPrintf("%s(%d): ALLOC %p of %d byte(s)\n", _file, _line, ptr, _size); dbgPrintf("%s(%d): ALLOC %p of %d byte(s)\n", _file, _line, ptr, _size);
++m_numBlocks; ++m_numBlocks;
@ -335,20 +336,30 @@ public:
return ptr; return ptr;
} }
virtual void free(void* _ptr, const char* _file, uint32_t _line) BX_OVERRIDE return bx::alignedAlloc(this, _size, _align, _file, _line);
}
virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
{ {
if (NULL != _ptr) if (NULL != _ptr)
{
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{ {
dbgPrintf("%s(%d): FREE %p\n", _file, _line, _ptr); dbgPrintf("%s(%d): FREE %p\n", _file, _line, _ptr);
BX_UNUSED(_file, _line);
::free(_ptr); ::free(_ptr);
--m_numBlocks; --m_numBlocks;
} }
else
{
bx::alignedFree(this, _ptr, _align, _file, _line);
}
}
} }
virtual void* realloc(void* _ptr, size_t _size, const char* _file, uint32_t _line) BX_OVERRIDE virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
{
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{ {
BX_UNUSED(_file, _line);
void* ptr = ::realloc(_ptr, _size); void* ptr = ::realloc(_ptr, _size);
dbgPrintf("%s(%d): REALLOC %p (old %p) of %d byte(s)\n", _file, _line, ptr, _ptr, _size); dbgPrintf("%s(%d): REALLOC %p (old %p) of %d byte(s)\n", _file, _line, ptr, _ptr, _size);
@ -361,6 +372,9 @@ public:
return ptr; return ptr;
} }
return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line);
}
void dumpStats() const void dumpStats() const
{ {
dbgPrintf("Allocator stats: num blocks %d (peak: %d)\n", m_numBlocks, m_maxBlocks); dbgPrintf("Allocator stats: num blocks %d (peak: %d)\n", m_numBlocks, m_maxBlocks);
@ -380,7 +394,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
uint32_t height = 720; uint32_t height = 720;
bgfx::init(&callback, &allocator); bgfx::init(&callback, &allocator);
bgfx::reset(width, height, BGFX_RESET_CAPTURE); bgfx::reset(width, height, BGFX_RESET_CAPTURE|BGFX_RESET_MSAA_X16);
// Enable debug text. // Enable debug text.
bgfx::setDebug(BGFX_DEBUG_TEXT); bgfx::setDebug(BGFX_DEBUG_TEXT);

View file

@ -144,7 +144,9 @@ namespace bgfx
{ {
} }
virtual void* alloc(size_t _size, const char* _file, uint32_t _line) BX_OVERRIDE virtual void* alloc(size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
{
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{ {
#if BGFX_CONFIG_MEMORY_TRACKING #if BGFX_CONFIG_MEMORY_TRACKING
{ {
@ -154,14 +156,18 @@ namespace bgfx
} }
#endif // BGFX_CONFIG_MEMORY_TRACKING #endif // BGFX_CONFIG_MEMORY_TRACKING
BX_UNUSED(_file, _line);
return ::malloc(_size); return ::malloc(_size);
} }
virtual void free(void* _ptr, const char* _file, uint32_t _line) BX_OVERRIDE return bx::alignedAlloc(this, _size, _align, _file, _line);
}
virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
{ {
if (NULL != _ptr) if (NULL != _ptr)
{ {
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{
#if BGFX_CONFIG_MEMORY_TRACKING #if BGFX_CONFIG_MEMORY_TRACKING
{ {
bx::LwMutexScope scope(m_mutex); bx::LwMutexScope scope(m_mutex);
@ -170,12 +176,18 @@ namespace bgfx
} }
#endif // BGFX_CONFIG_MEMORY_TRACKING #endif // BGFX_CONFIG_MEMORY_TRACKING
BX_UNUSED(_file, _line);
::free(_ptr); ::free(_ptr);
} }
else
{
bx::alignedFree(this, _ptr, _align, _file, _line);
}
}
} }
virtual void* realloc(void* _ptr, size_t _size, const char* _file, uint32_t _line) BX_OVERRIDE virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
{
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{ {
#if BGFX_CONFIG_MEMORY_TRACKING #if BGFX_CONFIG_MEMORY_TRACKING
if (NULL == _ptr) if (NULL == _ptr)
@ -186,10 +198,12 @@ namespace bgfx
} }
#endif // BGFX_CONFIG_MEMORY_TRACKING #endif // BGFX_CONFIG_MEMORY_TRACKING
BX_UNUSED(_file, _line);
return ::realloc(_ptr, _size); return ::realloc(_ptr, _size);
} }
return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line);
}
void checkLeaks() void checkLeaks()
{ {
#if BGFX_CONFIG_MEMORY_TRACKING #if BGFX_CONFIG_MEMORY_TRACKING
@ -762,7 +776,7 @@ namespace bgfx
s_threadIndex = BGFX_MAIN_THREAD_MAGIC; s_threadIndex = BGFX_MAIN_THREAD_MAGIC;
s_ctx = BX_ALIGNED_NEW(g_allocator, 16, Context); s_ctx = BX_ALIGNED_NEW(g_allocator, Context, 16);
s_ctx->init(); s_ctx->init();
BX_TRACE("Init complete."); BX_TRACE("Init complete.");
@ -776,7 +790,7 @@ namespace bgfx
Context* ctx = s_ctx; // it's going to be NULLd inside shutdown. Context* ctx = s_ctx; // it's going to be NULLd inside shutdown.
ctx->shutdown(); ctx->shutdown();
BX_ALIGNED_DELETE(g_allocator, 16, ctx); BX_ALIGNED_DELETE(g_allocator, ctx, 16);
if (NULL != s_callbackStub) if (NULL != s_callbackStub)
{ {