Simplified allocator interface.

This commit is contained in:
Branimir Karadžić 2014-05-01 10:15:41 -07:00
Родитель 9970637bb4
Коммит 6dc6dd6148
2 изменённых файлов: 81 добавлений и 53 удалений

Просмотреть файл

@ -325,40 +325,54 @@ 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
{ {
BX_UNUSED(_file, _line); if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
void* ptr = ::malloc(_size); {
dbgPrintf("%s(%d): ALLOC %p of %d byte(s)\n", _file, _line, ptr, _size); void* ptr = ::malloc(_size);
++m_numBlocks; dbgPrintf("%s(%d): ALLOC %p of %d byte(s)\n", _file, _line, ptr, _size);
m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks); ++m_numBlocks;
return ptr; m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
return ptr;
}
return bx::alignedAlloc(this, _size, _align, _file, _line);
} }
virtual void free(void* _ptr, const char* _file, uint32_t _line) BX_OVERRIDE virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
{ {
if (NULL != _ptr) if (NULL != _ptr)
{ {
dbgPrintf("%s(%d): FREE %p\n", _file, _line, _ptr); if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
BX_UNUSED(_file, _line); {
::free(_ptr); dbgPrintf("%s(%d): FREE %p\n", _file, _line, _ptr);
--m_numBlocks; ::free(_ptr);
--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
{ {
BX_UNUSED(_file, _line); if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
void* ptr = ::realloc(_ptr, _size);
dbgPrintf("%s(%d): REALLOC %p (old %p) of %d byte(s)\n", _file, _line, ptr, _ptr, _size);
if (NULL == _ptr)
{ {
++m_numBlocks; void* ptr = ::realloc(_ptr, _size);
m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks); dbgPrintf("%s(%d): REALLOC %p (old %p) of %d byte(s)\n", _file, _line, ptr, _ptr, _size);
if (NULL == _ptr)
{
++m_numBlocks;
m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
}
return ptr;
} }
return ptr; return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line);
} }
void dumpStats() const void dumpStats() const
@ -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);

Просмотреть файл

@ -144,50 +144,64 @@ 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 BGFX_CONFIG_MEMORY_TRACKING if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{
bx::LwMutexScope scope(m_mutex);
++m_numBlocks;
m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
}
#endif // BGFX_CONFIG_MEMORY_TRACKING
BX_UNUSED(_file, _line);
return ::malloc(_size);
}
virtual void free(void* _ptr, const char* _file, uint32_t _line) BX_OVERRIDE
{
if (NULL != _ptr)
{ {
#if BGFX_CONFIG_MEMORY_TRACKING #if BGFX_CONFIG_MEMORY_TRACKING
{ {
bx::LwMutexScope scope(m_mutex); bx::LwMutexScope scope(m_mutex);
BX_CHECK(m_numBlocks > 0, "Number of blocks is 0. Possible alloc/free mismatch?"); ++m_numBlocks;
--m_numBlocks; m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
} }
#endif // BGFX_CONFIG_MEMORY_TRACKING #endif // BGFX_CONFIG_MEMORY_TRACKING
BX_UNUSED(_file, _line); return ::malloc(_size);
::free(_ptr); }
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 (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{
#if BGFX_CONFIG_MEMORY_TRACKING
{
bx::LwMutexScope scope(m_mutex);
BX_CHECK(m_numBlocks > 0, "Number of blocks is 0. Possible alloc/free mismatch?");
--m_numBlocks;
}
#endif // BGFX_CONFIG_MEMORY_TRACKING
::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 BGFX_CONFIG_MEMORY_TRACKING if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
if (NULL == _ptr)
{ {
bx::LwMutexScope scope(m_mutex); #if BGFX_CONFIG_MEMORY_TRACKING
++m_numBlocks; if (NULL == _ptr)
m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks); {
} bx::LwMutexScope scope(m_mutex);
++m_numBlocks;
m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
}
#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()
@ -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)
{ {