mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-18 04:53:06 +01:00
Simplifed allocator.
This commit is contained in:
@@ -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
|
||||
{
|
||||
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
|
||||
{
|
||||
return ::malloc(_size);
|
||||
}
|
||||
|
||||
# if BX_COMPILER_MSVC
|
||||
BX_UNUSED(_file, _line);
|
||||
return _aligned_malloc(_size, _align);
|
||||
# else
|
||||
return bx::alignedAlloc(this, _size, _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;
|
||||
}
|
||||
|
||||
# if BX_COMPILER_MSVC
|
||||
BX_UNUSED(_file, _line);
|
||||
_aligned_free(_ptr);
|
||||
# else
|
||||
bx::alignedFree(this, _ptr, _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 (0 == _size)
|
||||
{
|
||||
if (NULL != _ptr)
|
||||
{
|
||||
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
|
||||
{
|
||||
::free(_ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
# if BX_COMPILER_MSVC
|
||||
BX_UNUSED(_file, _line);
|
||||
_aligned_free(_ptr);
|
||||
# else
|
||||
bx::alignedFree(this, _ptr, _align, _file, _line);
|
||||
# endif // BX_
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
else if (NULL == _ptr)
|
||||
{
|
||||
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
|
||||
{
|
||||
return ::malloc(_size);
|
||||
}
|
||||
|
||||
# if BX_COMPILER_MSVC
|
||||
BX_UNUSED(_file, _line);
|
||||
return _aligned_malloc(_size, _align);
|
||||
# else
|
||||
return bx::alignedAlloc(this, _size, _align, _file, _line);
|
||||
# endif // BX_
|
||||
}
|
||||
|
||||
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
|
||||
{
|
||||
return ::realloc(_ptr, _size);
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user