Browse Source

Simplifed allocator.

Branimir Karadžić 10 years ago
parent
commit
40d526744a

+ 26 - 25
examples/07-callback/callback.cpp

@@ -291,7 +291,7 @@ struct BgfxCallback : public bgfx::CallbackI
 	AviWriter* m_writer;
 };
 
-class BgfxAllocator : public bx::ReallocatorI
+class BgfxAllocator : public bx::AllocatorI
 {
 public:
 	BgfxAllocator()
@@ -304,39 +304,40 @@ public:
 	{
 	}
 
-	virtual void* alloc(size_t _size, size_t _align, 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 (0 == _size)
 		{
-			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);
-	}
+			if (NULL != _ptr)
+			{
+				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 free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
-	{
-		if (NULL != _ptr)
+			return NULL;
+		}
+		else if (NULL == _ptr)
 		{
 			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);
+				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* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
-	{
 		if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
 		{
 			void* ptr = ::realloc(_ptr, _size);

+ 4 - 4
examples/common/bgfx_utils.cpp

@@ -22,7 +22,7 @@ namespace stl = tinystl;
 
 #include "bgfx_utils.h"
 
-void* load(bx::FileReaderI* _reader, bx::ReallocatorI* _allocator, const char* _filePath, uint32_t* _size)
+void* load(bx::FileReaderI* _reader, bx::AllocatorI* _allocator, const char* _filePath, uint32_t* _size)
 {
 	if (0 == bx::open(_reader, _filePath) )
 	{
@@ -69,7 +69,7 @@ static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const char* _filePa
 	return NULL;
 }
 
-static void* loadMem(bx::FileReaderI* _reader, bx::ReallocatorI* _allocator, const char* _filePath, uint32_t* _size)
+static void* loadMem(bx::FileReaderI* _reader, bx::AllocatorI* _allocator, const char* _filePath, uint32_t* _size)
 {
 	if (0 == bx::open(_reader, _filePath) )
 	{
@@ -175,7 +175,7 @@ bgfx::TextureHandle loadTexture(bx::FileReaderI* _reader, const char* _name, uin
 	}
 
 	bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
-	bx::ReallocatorI* allocator = entry::getAllocator();
+	bx::AllocatorI* allocator = entry::getAllocator();
 
 	uint32_t size = 0;
 	void* data = loadMem(_reader, allocator, filePath, &size);
@@ -399,7 +399,7 @@ struct Mesh
 
 		Group group;
 
-		bx::ReallocatorI* allocator = entry::getAllocator();
+		bx::AllocatorI* allocator = entry::getAllocator();
 
 		uint32_t chunk;
 		while (4 == bx::read(_reader, chunk) )

+ 4 - 4
examples/common/entry/entry.cpp

@@ -27,11 +27,11 @@ namespace entry
 	static bx::FileReaderI* s_fileReader = NULL;
 	static bx::FileWriterI* s_fileWriter = NULL;
 
-	extern bx::ReallocatorI* getDefaultAllocator();
-	static bx::ReallocatorI* s_allocator = getDefaultAllocator();
+	extern bx::AllocatorI* getDefaultAllocator();
+	static bx::AllocatorI* s_allocator = getDefaultAllocator();
 
 #if ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
-	bx::ReallocatorI* getDefaultAllocator()
+	bx::AllocatorI* getDefaultAllocator()
 	{
 BX_PRAGMA_DIAGNOSTIC_PUSH();
 BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4459); // warning C4459: declaration of 's_allocator' hides global declaration
@@ -666,7 +666,7 @@ BX_PRAGMA_DIAGNOSTIC_POP();
 		return s_fileWriter;
 	}
 
-	bx::ReallocatorI* getAllocator()
+	bx::AllocatorI* getAllocator()
 	{
 		return s_allocator;
 	}

+ 2 - 2
examples/common/entry/entry.h

@@ -10,7 +10,7 @@
 #include <string.h> // memset
 #include <bx/bx.h>
 
-namespace bx { struct FileReaderI; struct FileWriterI; struct ReallocatorI; }
+namespace bx { struct FileReaderI; struct FileWriterI; struct AllocatorI; }
 
 extern "C" int _main_(int _argc, char** _argv);
 
@@ -236,7 +236,7 @@ namespace entry
 
 	bx::FileReaderI* getFileReader();
 	bx::FileWriterI* getFileWriter();
-	bx::ReallocatorI* getAllocator();
+	bx::AllocatorI*  getAllocator();
 
 	WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags = ENTRY_WINDOW_FLAG_NONE, const char* _title = "");
 	void destroyWindow(WindowHandle _handle);

+ 2 - 2
include/bgfx/bgfx.h

@@ -19,7 +19,7 @@
 
 #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
 
-namespace bx { struct ReallocatorI; }
+namespace bx { struct AllocatorI; }
 
 /// BGFX
 namespace bgfx
@@ -769,7 +769,7 @@ namespace bgfx
 	///
 	/// @attention C99 equivalent is `bgfx_init`.
 	///
-	bool init(RendererType::Enum _type = RendererType::Count, uint16_t _vendorId = BGFX_PCI_ID_NONE, uint16_t _deviceId = 0, CallbackI* _callback = NULL, bx::ReallocatorI* _reallocator = NULL);
+	bool init(RendererType::Enum _type = RendererType::Count, uint16_t _vendorId = BGFX_PCI_ID_NONE, uint16_t _deviceId = 0, CallbackI* _callback = NULL, bx::AllocatorI* _reallocator = NULL);
 
 	/// Shutdown bgfx library.
 	///

+ 1 - 1
include/bgfx/bgfxdefines.h

@@ -6,7 +6,7 @@
 #ifndef BGFX_DEFINES_H_HEADER_GUARD
 #define BGFX_DEFINES_H_HEADER_GUARD
 
-#define BGFX_API_VERSION UINT32_C(1)
+#define BGFX_API_VERSION UINT32_C(2)
 
 ///
 #define BGFX_STATE_RGB_WRITE             UINT64_C(0x0000000000000001) //!< Enable RGB write.

+ 8 - 10
include/bgfx/c99/bgfx.h

@@ -416,20 +416,18 @@ typedef struct bgfx_callback_vtbl
 } bgfx_callback_vtbl_t;
 
 /**/
-typedef struct bgfx_reallocator_interface
+typedef struct bgfx_allocator_interface
 {
-    const struct bgfx_reallocator_vtbl* vtbl;
+    const struct bgfx_allocator_vtbl* vtbl;
 
-} bgfx_reallocator_interface_t;
+} bgfx_allocator_interface_t;
 
 /**/
-typedef struct bgfx_reallocator_vtbl
+typedef struct bgfx_allocator_vtbl
 {
-    void* (*alloc)(bgfx_reallocator_interface_t* _this, size_t _size, size_t _align, const char* _file, uint32_t _line);
-    void  (*free)(bgfx_reallocator_interface_t* _this, void* _ptr, size_t _align, const char* _file, uint32_t _line);
-    void* (*realloc)(bgfx_reallocator_interface_t* _this, void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line);
+    void* (*realloc)(bgfx_allocator_interface_t* _this, void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line);
 
-} bgfx_reallocator_vtbl_t;
+} bgfx_allocator_vtbl_t;
 
 /**/
 typedef struct bgfx_interface_vtbl
@@ -448,7 +446,7 @@ typedef struct bgfx_interface_vtbl
     void (*image_rgba8_downsample_2x2)(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
     uint8_t (*get_supported_renderers)(bgfx_renderer_type_t _enum[BGFX_RENDERER_TYPE_COUNT]);
     const char* (*get_renderer_name)(bgfx_renderer_type_t _type);
-    bool (*init)(bgfx_renderer_type_t _type, uint16_t _vendorId, uint16_t _deviceId, bgfx_callback_interface_t* _callback, bgfx_reallocator_interface_t* _allocator);
+    bool (*init)(bgfx_renderer_type_t _type, uint16_t _vendorId, uint16_t _deviceId, bgfx_callback_interface_t* _callback, bgfx_allocator_interface_t* _allocator);
     void (*shutdown)();
     void (*reset)(uint32_t _width, uint32_t _height, uint32_t _flags);
     uint32_t (*frame)();
@@ -602,7 +600,7 @@ BGFX_C_API uint8_t bgfx_get_supported_renderers(bgfx_renderer_type_t _enum[BGFX_
 BGFX_C_API const char* bgfx_get_renderer_name(bgfx_renderer_type_t _type);
 
 /**/
-BGFX_C_API bool bgfx_init(bgfx_renderer_type_t _type, uint16_t _vendorId, uint16_t _deviceId, bgfx_callback_interface_t* _callback, bgfx_reallocator_interface_t* _allocator);
+BGFX_C_API bool bgfx_init(bgfx_renderer_type_t _type, uint16_t _vendorId, uint16_t _deviceId, bgfx_callback_interface_t* _callback, bgfx_allocator_interface_t* _allocator);
 
 /**/
 BGFX_C_API void bgfx_shutdown();

+ 32 - 41
src/bgfx.cpp

@@ -125,7 +125,7 @@ namespace bgfx
 #	define BGFX_CONFIG_MEMORY_TRACKING (BGFX_CONFIG_DEBUG && BX_CONFIG_SUPPORTS_THREADING)
 #endif // BGFX_CONFIG_MEMORY_TRACKING
 
-	class AllocatorStub : public bx::ReallocatorI
+	class AllocatorStub : public bx::AllocatorI
 	{
 	public:
 		AllocatorStub()
@@ -136,49 +136,50 @@ namespace bgfx
 		{
 		}
 
-		virtual void* alloc(size_t _size, size_t _align, 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 (0 == _size)
 			{
-#if BGFX_CONFIG_MEMORY_TRACKING
+				if (NULL != _ptr)
 				{
-					bx::LwMutexScope scope(m_mutex);
-					++m_numBlocks;
-					m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
-				}
+					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
 
-				return ::malloc(_size);
-			}
-
-			return bx::alignedAlloc(this, _size, _align, _file, _line);
-		}
+						::free(_ptr);
+					}
+					else
+					{
+						bx::alignedFree(this, _ptr, _align, _file, _line);
+					}
+				}
 
-		virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
-		{
-			if (NULL != _ptr)
+				return NULL;
+			}
+			else 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
 
-					::free(_ptr);
-				}
-				else
-				{
-					bx::alignedFree(this, _ptr, _align, _file, _line);
+					return ::malloc(_size);
 				}
+
+				return bx::alignedAlloc(this, _size, _align, _file, _line);
 			}
-		}
 
-		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
@@ -211,7 +212,7 @@ namespace bgfx
 	static bool s_graphicsDebuggerPresent = false;
 
 	CallbackI* g_callback = NULL;
-	bx::ReallocatorI* g_allocator = NULL;
+	bx::AllocatorI* g_allocator = NULL;
 
 	Caps g_caps;
 
@@ -2275,7 +2276,7 @@ again:
 		return s_rendererCreator[_type].name;
 	}
 
-	bool init(RendererType::Enum _type, uint16_t _vendorId, uint16_t _deviceId, CallbackI* _callback, bx::ReallocatorI* _allocator)
+	bool init(RendererType::Enum _type, uint16_t _vendorId, uint16_t _deviceId, CallbackI* _callback, bx::AllocatorI* _allocator)
 	{
 		BX_CHECK(NULL == s_ctx, "bgfx is already initialized.");
 
@@ -3512,29 +3513,19 @@ namespace bgfx
 		bgfx_callback_interface_t* m_interface;
 	};
 
-	class AllocatorC99 : public bx::ReallocatorI
+	class AllocatorC99 : public bx::AllocatorI
 	{
 	public:
 		virtual ~AllocatorC99()
 		{
 		}
 
-		virtual void* alloc(size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
-		{
-			return m_interface->vtbl->alloc(m_interface, _size, _align, _file, _line);
-		}
-
-		virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
-		{
-			m_interface->vtbl->free(m_interface, _ptr, _align, _file, _line);
-		}
-
 		virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
 		{
 			return m_interface->vtbl->realloc(m_interface, _ptr, _size, _align, _file, _line);
 		}
 
-		bgfx_reallocator_interface_t* m_interface;
+		bgfx_allocator_interface_t* m_interface;
 	};
 
 } // namespace bgfx
@@ -3613,7 +3604,7 @@ BGFX_C_API const char* bgfx_get_renderer_name(bgfx_renderer_type_t _type)
 	return bgfx::getRendererName(bgfx::RendererType::Enum(_type) );
 }
 
-BGFX_C_API bool bgfx_init(bgfx_renderer_type_t _type, uint16_t _vendorId, uint16_t _deviceId, bgfx_callback_interface_t* _callback, bgfx_reallocator_interface_t* _allocator)
+BGFX_C_API bool bgfx_init(bgfx_renderer_type_t _type, uint16_t _vendorId, uint16_t _deviceId, bgfx_callback_interface_t* _callback, bgfx_allocator_interface_t* _allocator)
 {
 	static bgfx::CallbackC99 s_callback;
 	s_callback.m_interface = _callback;

+ 1 - 1
src/bgfx_p.h

@@ -295,7 +295,7 @@ namespace bgfx
 
 	extern const uint32_t g_uniformTypeSize[UniformType::Count+1];
 	extern CallbackI* g_callback;
-	extern bx::ReallocatorI* g_allocator;
+	extern bx::AllocatorI* g_allocator;
 	extern Caps g_caps;
 
 	void setGraphicsDebuggerPresent(bool _present);