Branimir Karadžić 10 лет назад
Родитель
Сommit
792e1458fc
2 измененных файлов с 32 добавлено и 34 удалено
  1. 30 32
      include/bx/allocator.h
  2. 2 2
      include/bx/readerwriter.h

+ 30 - 32
include/bx/allocator.h

@@ -61,30 +61,24 @@ namespace bx
 	struct BX_NO_VTABLE AllocatorI
 	{
 		virtual ~AllocatorI() = 0;
-		virtual void* alloc(size_t _size, size_t _align, const char* _file, uint32_t _line) = 0;
-		virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) = 0;
+		virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) = 0;
 	};
 
 	inline AllocatorI::~AllocatorI()
 	{
 	}
 
-	struct BX_NO_VTABLE ReallocatorI : public AllocatorI
-	{
-		virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) = 0;
-	};
-
 	inline void* alloc(AllocatorI* _allocator, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
 	{
-		return _allocator->alloc(_size, _align, _file, _line);
+		return _allocator->realloc(NULL, _size, _align, _file, _line);
 	}
 
 	inline void free(AllocatorI* _allocator, void* _ptr, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
 	{
-		_allocator->free(_ptr, _align, _file, _line);
+		_allocator->realloc(_ptr, 0, _align, _file, _line);
 	}
 
-	inline void* realloc(ReallocatorI* _allocator, void* _ptr, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
+	inline void* realloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
 	{
 		return _allocator->realloc(_ptr, _size, _align, _file, _line);
 	}
@@ -107,7 +101,7 @@ namespace bx
 		free(_allocator, ptr, 0, _file, _line);
 	}
 
-	static inline void* alignedRealloc(ReallocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
+	static inline void* alignedRealloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
 	{
 		if (NULL == _ptr)
 		{
@@ -144,7 +138,7 @@ namespace bx
 	}
 
 #if BX_CONFIG_ALLOCATOR_CRT
-	class CrtAllocator : public ReallocatorI
+	class CrtAllocator : public AllocatorI
 	{
 	public:
 		CrtAllocator()
@@ -155,39 +149,43 @@ namespace bx
 		{
 		}
 
-		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)
 			{
-				return ::malloc(_size);
-			}
+				if (NULL != _ptr)
+				{
+					if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
+					{
+						::free(_ptr);
+						return NULL;
+					}
 
 #	if BX_COMPILER_MSVC
-			BX_UNUSED(_file, _line);
-			return _aligned_malloc(_size, _align);
+					BX_UNUSED(_file, _line);
+					_aligned_free(_ptr);
 #	else
-			return bx::alignedAlloc(this, _size, _align, _file, _line);
+					bx::alignedFree(this, _ptr, _align, _file, _line);
 #	endif // BX_
-		}
+				}
 
-		virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
-		{
-			if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
-			{
-				::free(_ptr);
-				return;
+				return NULL;
 			}
+			else if (NULL == _ptr)
+			{
+				if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
+				{
+					return ::malloc(_size);
+				}
 
 #	if BX_COMPILER_MSVC
-			BX_UNUSED(_file, _line);
-			_aligned_free(_ptr);
+				BX_UNUSED(_file, _line);
+				return _aligned_malloc(_size, _align);
 #	else
-			bx::alignedFree(this, _ptr, _align, _file, _line);
+				return bx::alignedAlloc(this, _size, _align, _file, _line);
 #	endif // BX_
-		}
+			}
 
-		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)
 			{
 				return ::realloc(_ptr, _size);

+ 2 - 2
include/bx/readerwriter.h

@@ -265,7 +265,7 @@ namespace bx
 	class MemoryBlock : public MemoryBlockI
 	{
 	public:
-		MemoryBlock(ReallocatorI* _allocator)
+		MemoryBlock(AllocatorI* _allocator)
 			: m_allocator(_allocator)
 			, m_data(NULL)
 			, m_size(0)
@@ -294,7 +294,7 @@ namespace bx
 		}
 
 	private:
-		ReallocatorI* m_allocator;
+		AllocatorI* m_allocator;
 		void* m_data;
 		uint32_t m_size;
 	};