From 6dc6dd6148cb4d0c93771c8203faed04fb549174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 1 May 2014 10:15:41 -0700 Subject: [PATCH] Simplified allocator interface. --- examples/07-callback/callback.cpp | 58 ++++++++++++++--------- src/bgfx.cpp | 76 ++++++++++++++++++------------- 2 files changed, 81 insertions(+), 53 deletions(-) diff --git a/examples/07-callback/callback.cpp b/examples/07-callback/callback.cpp index 55f89bf81..47f1876ed 100644 --- a/examples/07-callback/callback.cpp +++ b/examples/07-callback/callback.cpp @@ -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); - void* ptr = ::malloc(_size); - dbgPrintf("%s(%d): ALLOC %p of %d byte(s)\n", _file, _line, ptr, _size); - ++m_numBlocks; - m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks); - return ptr; + 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); + ++m_numBlocks; + 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) { - dbgPrintf("%s(%d): FREE %p\n", _file, _line, _ptr); - BX_UNUSED(_file, _line); - ::free(_ptr); - --m_numBlocks; + if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) + { + dbgPrintf("%s(%d): FREE %p\n", _file, _line, _ptr); + ::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); - 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) + if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) { - ++m_numBlocks; - m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks); + 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; + 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 @@ -380,7 +394,7 @@ int _main_(int /*_argc*/, char** /*_argv*/) uint32_t height = 720; 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. bgfx::setDebug(BGFX_DEBUG_TEXT); diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 5016a5856..0a6842213 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -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 - { - 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 (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; + ++m_numBlocks; + m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks); } #endif // BGFX_CONFIG_MEMORY_TRACKING - BX_UNUSED(_file, _line); - ::free(_ptr); + return ::malloc(_size); + } + + 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 (NULL == _ptr) + if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) { - bx::LwMutexScope scope(m_mutex); - ++m_numBlocks; - m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks); - } +#if BGFX_CONFIG_MEMORY_TRACKING + if (NULL == _ptr) + { + 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 ::realloc(_ptr, _size); + return ::realloc(_ptr, _size); + } + + return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line); } void checkLeaks() @@ -762,7 +776,7 @@ namespace bgfx 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(); BX_TRACE("Init complete."); @@ -776,7 +790,7 @@ namespace bgfx Context* ctx = s_ctx; // it's going to be NULLd inside shutdown. ctx->shutdown(); - BX_ALIGNED_DELETE(g_allocator, 16, ctx); + BX_ALIGNED_DELETE(g_allocator, ctx, 16); if (NULL != s_callbackStub) {