Simplified allocator interface.

This commit is contained in:
Branimir Karadžić
2014-05-01 10:14:32 -07:00
parent 071be7287a
commit 9f3df4744f

View File

@@ -16,27 +16,27 @@
#endif // BX_CONFIG_ALLOCATOR_CRT
#if BX_CONFIG_ALLOCATOR_DEBUG
# define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, __FILE__, __LINE__)
# define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size, __FILE__, __LINE__)
# define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr, __FILE__, __LINE__)
# define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alignedAlloc(_allocator, _size, _align, __FILE__, __LINE__)
# define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::alignedRealloc(_allocator, _ptr, _size, _align, __FILE__, __LINE__)
# define BX_ALIGNED_FREE(_allocator, _ptr) bx::alignedFree(_allocator, _ptr, __FILE__, __LINE__)
# define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, 0, __FILE__, __LINE__)
# define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size, 0, __FILE__, __LINE__)
# define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr, 0, __FILE__, __LINE__)
# define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alloc(_allocator, _size, _align, __FILE__, __LINE__)
# define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align, __FILE__, __LINE__)
# define BX_ALIGNED_FREE(_allocator, _ptr, _align) bx::free(_allocator, _ptr, _align, __FILE__, __LINE__)
# define BX_NEW(_allocator, _type) ::new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
# define BX_DELETE(_allocator, _ptr) bx::deleteObject(_allocator, _ptr, __FILE__, __LINE__)
# define BX_ALIGNED_NEW(_allocator, _align, _type) ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
# define BX_ALIGNED_DELETE(_allocator, _align, _ptr) bx::alignedDeleteObject(_allocator, _ptr)
# define BX_ALIGNED_NEW(_allocator, _type, _align) ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
# define BX_ALIGNED_DELETE(_allocator, _ptr, _align) bx::alignedDeleteObject(_allocator, _ptr, _align, __FILE__, __LINE__)
#else
# define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size)
# define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size)
# define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr)
# define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alignedAlloc(_allocator, _size, _align)
# define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::alignedRealloc(_allocator, _ptr, _size, _align)
# define BX_ALIGNED_FREE(_allocator, _ptr) bx::alignedFree(_allocator, _ptr)
# define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, 0)
# define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size, 0)
# define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr, 0)
# define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alloc(_allocator, _size, _align)
# define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align)
# define BX_ALIGNED_FREE(_allocator, _ptr, _align) bx::free(_allocator, _ptr, _align)
# define BX_NEW(_allocator, _type) ::new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
# define BX_DELETE(_allocator, _ptr) bx::deleteObject(_allocator, _ptr)
# define BX_ALIGNED_NEW(_allocator, _align, _type) ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
# define BX_ALIGNED_DELETE(_allocator, _align, _ptr) bx::alignedDeleteObject(_allocator, _ptr)
# define BX_ALIGNED_NEW(_allocator, _type, _align) ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
# define BX_ALIGNED_DELETE(_allocator, _ptr, _align) bx::alignedDeleteObject(_allocator, _ptr, _align)
#endif // BX_CONFIG_DEBUG_ALLOC
#ifndef BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT
@@ -68,8 +68,8 @@ namespace bx
struct BX_NO_VTABLE AllocatorI
{
virtual ~AllocatorI() = 0;
virtual void* alloc(size_t _size, const char* _file, uint32_t _line) = 0;
virtual void free(void* _ptr, const char* _file, uint32_t _line) = 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;
};
inline AllocatorI::~AllocatorI()
@@ -78,66 +78,40 @@ namespace bx
struct BX_NO_VTABLE ReallocatorI : public AllocatorI
{
virtual void* realloc(void* _ptr, size_t _size, 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;
};
struct BX_NO_VTABLE AlignedAllocatorI
inline void* alloc(AllocatorI* _allocator, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
{
virtual void* alignedAlloc(size_t _size, size_t _align, const char* _file, uint32_t _line) = 0;
virtual void alignedFree(void* _ptr, const char* _file, uint32_t _line) = 0;
};
struct BX_NO_VTABLE AlignedReallocatorI : public AlignedAllocatorI
{
virtual void* alignedRealloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) = 0;
};
inline void* alloc(AllocatorI* _allocator, size_t _size, const char* _file = NULL, uint32_t _line = 0)
{
return _allocator->alloc(_size, _file, _line);
return _allocator->alloc(_size, _align, _file, _line);
}
inline void free(AllocatorI* _allocator, void* _ptr, const char* _file = NULL, uint32_t _line = 0)
inline void free(AllocatorI* _allocator, void* _ptr, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
{
_allocator->free(_ptr, _file, _line);
_allocator->free(_ptr, _align, _file, _line);
}
inline void* alloc(AlignedAllocatorI* _allocator, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
inline void* realloc(ReallocatorI* _allocator, void* _ptr, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
{
return _allocator->alignedAlloc(_size, _align, _file, _line);
}
inline void free(AlignedAllocatorI* _allocator, void* _ptr, const char* _file = NULL, uint32_t _line = 0)
{
_allocator->alignedFree(_ptr, _file, _line);
}
inline void* realloc(ReallocatorI* _allocator, void* _ptr, size_t _size, const char* _file = NULL, uint32_t _line = 0)
{
return _allocator->realloc(_ptr, _size, _file, _line);
}
inline void* realloc(AlignedReallocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
{
return _allocator->alignedRealloc(_ptr, _size, _align, _file, _line);
return _allocator->realloc(_ptr, _size, _align, _file, _line);
}
static inline void* alignedAlloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
{
size_t total = _size + _align;
uint8_t* ptr = (uint8_t*)alloc(_allocator, total, _file, _line);
uint8_t* ptr = (uint8_t*)alloc(_allocator, total, 0, _file, _line);
uint8_t* aligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), _align);
uint32_t* header = (uint32_t*)aligned - 1;
*header = uint32_t(aligned - ptr);
return aligned;
}
static inline void alignedFree(AllocatorI* _allocator, void* _ptr, const char* _file = NULL, uint32_t _line = 0)
static inline void alignedFree(AllocatorI* _allocator, void* _ptr, size_t /*_align*/, const char* _file = NULL, uint32_t _line = 0)
{
uint8_t* aligned = (uint8_t*)_ptr;
uint32_t* header = (uint32_t*)aligned - 1;
uint8_t* ptr = aligned - *header;
free(_allocator, ptr, _file, _line);
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)
@@ -151,7 +125,7 @@ namespace bx
uint32_t offset = *( (uint32_t*)aligned - 1);
uint8_t* ptr = aligned - offset;
size_t total = _size + _align;
ptr = (uint8_t*)realloc(_allocator, ptr, total, _file, _line);
ptr = (uint8_t*)realloc(_allocator, ptr, total, 0, _file, _line);
uint8_t* newAligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), _align);
if (newAligned == aligned)
@@ -166,43 +140,28 @@ namespace bx
return newAligned;
}
inline void* alignedAlloc(AlignedAllocatorI* _allocator, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
{
return _allocator->alignedAlloc(_size, _align, _file, _line);
}
inline void alignedFree(AlignedAllocatorI* _allocator, void* _ptr, const char* _file = NULL, uint32_t _line = 0)
{
_allocator->alignedFree(_ptr, _file, _line);
}
inline void* alignedRealloc(AlignedReallocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
{
return _allocator->alignedRealloc(_ptr, _size, _align, _file, _line);
}
template <typename ObjectT>
inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, const char* _file = NULL, uint32_t _line = 0)
{
if (NULL != _object)
{
_object->~ObjectT();
free(_allocator, _object, _file, _line);
free(_allocator, _object, 0, _file, _line);
}
}
template <typename ObjectT>
inline void alignedDeleteObject(AllocatorI* _allocator, ObjectT* _object, const char* _file = NULL, uint32_t _line = 0)
inline void alignedDeleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align, const char* _file = NULL, uint32_t _line = 0)
{
if (NULL != _object)
{
_object->~ObjectT();
alignedFree(_allocator, _object, _file, _line);
alignedFree(_allocator, _object, _align, _file, _line);
}
}
#if BX_CONFIG_ALLOCATOR_CRT
class CrtAllocator : public ReallocatorI, public AlignedReallocatorI
class CrtAllocator : public ReallocatorI
{
public:
CrtAllocator()
@@ -213,51 +172,49 @@ namespace bx
{
}
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);
return ::malloc(_size);
}
if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{
return ::malloc(_size);
}
virtual void free(void* _ptr, const char* _file, uint32_t _line) BX_OVERRIDE
{
BX_UNUSED(_file, _line);
::free(_ptr);
}
virtual void* realloc(void* _ptr, size_t _size, const char* _file, uint32_t _line) BX_OVERRIDE
{
BX_UNUSED(_file, _line);
return ::realloc(_ptr, _size);
}
virtual void* alignedAlloc(size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
{
# if BX_COMPILER_MSVC
BX_UNUSED(_file, _line);
return _aligned_malloc(_size, _align);
# else
return bx::alignedAlloc(static_cast<AlignedReallocatorI*>(this), _size, _align, _file, _line);
return bx::alignedAlloc(this, _size, _align, _file, _line);
# endif // BX_
}
virtual void alignedFree(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 (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
{
::free(_ptr);
return;
}
# if BX_COMPILER_MSVC
BX_UNUSED(_file, _line);
return _aligned_free(_ptr);
_aligned_free(_ptr);
# else
return bx::alignedFree(static_cast<AlignedReallocatorI*>(this), _ptr, _file, _line);
bx::alignedFree(this, _ptr, _align, _file, _line);
# endif // BX_
}
virtual void* alignedRealloc(void* _ptr, 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)
{
return ::realloc(_ptr, _size);
}
# if BX_COMPILER_MSVC
BX_UNUSED(_file, _line);
return _aligned_realloc(_ptr, _size, _align);
# else
return bx::alignedRealloc(static_cast<AlignedReallocatorI*>(this), _ptr, _size, _align, _file, _line);
return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line);
# endif // BX_
}
};