Ver Fonte

Added guard for memory tracking.

bkaradzic há 12 anos atrás
pai
commit
a863185e57
1 ficheiros alterados com 21 adições e 3 exclusões
  1. 21 3
      src/bgfx.cpp

+ 21 - 3
src/bgfx.cpp

@@ -114,15 +114,22 @@ namespace bgfx
 	{
 	{
 	public:
 	public:
 		AllocatorStub()
 		AllocatorStub()
+#if BGFX_CONFIG_DEBUG
 			: m_numBlocks(0)
 			: m_numBlocks(0)
 			, m_maxBlocks(0)
 			, m_maxBlocks(0)
+#endif // BGFX_CONFIG_DEBUG
 		{
 		{
 		}
 		}
 
 
 		virtual void* alloc(size_t _size, const char* _file, uint32_t _line) BX_OVERRIDE
 		virtual void* alloc(size_t _size, const char* _file, uint32_t _line) BX_OVERRIDE
 		{
 		{
-			++m_numBlocks;
-			m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
+#if BGFX_CONFIG_DEBUG
+			{
+				bx::LwMutexScope scope(m_mutex);
+				++m_numBlocks;
+				m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
+			}
+#endif // BGFX_CONFIG_DEBUG
 
 
 			BX_UNUSED(_file, _line);
 			BX_UNUSED(_file, _line);
 			return ::malloc(_size);
 			return ::malloc(_size);
@@ -132,7 +139,12 @@ namespace bgfx
 		{
 		{
 			if (NULL != _ptr)
 			if (NULL != _ptr)
 			{
 			{
-				--m_numBlocks;
+#if BGFX_CONFIG_DEBUG
+				{
+					bx::LwMutexScope scope(m_mutex);
+					--m_numBlocks;
+				}
+#endif // BGFX_CONFIG_DEBUG
 
 
 				BX_UNUSED(_file, _line);
 				BX_UNUSED(_file, _line);
 				::free(_ptr);
 				::free(_ptr);
@@ -141,11 +153,14 @@ namespace bgfx
 
 
 		virtual void* realloc(void* _ptr, size_t _size, const char* _file, uint32_t _line) BX_OVERRIDE
 		virtual void* realloc(void* _ptr, size_t _size, const char* _file, uint32_t _line) BX_OVERRIDE
 		{
 		{
+#if BGFX_CONFIG_DEBUG
 			if (NULL == _ptr)
 			if (NULL == _ptr)
 			{
 			{
+				bx::LwMutexScope scope(m_mutex);
 				++m_numBlocks;
 				++m_numBlocks;
 				m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
 				m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
 			}
 			}
+#endif // BGFX_CONFIG_DEBUG
 
 
 			BX_UNUSED(_file, _line);
 			BX_UNUSED(_file, _line);
 			return ::realloc(_ptr, _size);
 			return ::realloc(_ptr, _size);
@@ -157,8 +172,11 @@ namespace bgfx
 		}
 		}
 
 
 	private:
 	private:
+#if BGFX_CONFIG_DEBUG
+		bx::LwMutex m_mutex;
 		uint32_t m_numBlocks;
 		uint32_t m_numBlocks;
 		uint32_t m_maxBlocks;
 		uint32_t m_maxBlocks;
+#endif // BGFX_CONFIG_DEBUG
 	};
 	};
 
 
 	static CallbackStub* s_callbackStub = NULL;
 	static CallbackStub* s_callbackStub = NULL;