From a8d6a2ee568283a25f5d979c1516554eaac19d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sat, 14 Jan 2017 18:21:23 -0800 Subject: [PATCH] Cleanup. --- include/bx/allocator.h | 142 ++++++++++++++++++--------------------- include/bx/allocator.inl | 97 ++++++++++++++++++++++++++ 2 files changed, 163 insertions(+), 76 deletions(-) create mode 100644 include/bx/allocator.inl diff --git a/include/bx/allocator.h b/include/bx/allocator.h index 059a740..03b4994 100644 --- a/include/bx/allocator.h +++ b/include/bx/allocator.h @@ -41,18 +41,6 @@ namespace bx { - /// Aligns pointer to nearest next aligned address. _align must be power of two. - inline void* alignPtr(void* _ptr, size_t _extra, size_t _align = BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT) - { - union { void* ptr; size_t addr; } un; - un.ptr = _ptr; - size_t unaligned = un.addr + _extra; // space for header - size_t mask = _align-1; - size_t aligned = BX_ALIGN_MASK(unaligned, mask); - un.addr = aligned; - return un.ptr; - } - struct BX_NO_VTABLE AllocatorI { virtual ~AllocatorI() = 0; @@ -67,79 +55,81 @@ namespace bx virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) = 0; }; - inline AllocatorI::~AllocatorI() - { - } + /// Aligns pointer to nearest next aligned address. _align must be power of two. + void* alignPtr( + void* _ptr + , size_t _extra + , size_t _align = BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT + ); - inline void* alloc(AllocatorI* _allocator, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0) - { - return _allocator->realloc(NULL, _size, _align, _file, _line); - } + /// + void* alloc( + AllocatorI* _allocator + , size_t _size + , size_t _align = 0 + , 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->realloc(_ptr, 0, _align, _file, _line); - } + /// + void free( + AllocatorI* _allocator + , void* _ptr + , 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); - } + /// + void* realloc( + AllocatorI* _allocator + , void* _ptr + , size_t _size + , size_t _align = 0 + , const char* _file = NULL + , uint32_t _line = 0 + ); - 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, 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; - } + /// + void* alignedAlloc( + AllocatorI* _allocator + , size_t _size + , size_t _align + , 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, 0, _file, _line); - } + /// + void alignedFree( + AllocatorI* _allocator + , void* _ptr + , 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) - { - return alignedAlloc(_allocator, _size, _align, _file, _line); - } - - uint8_t* aligned = (uint8_t*)_ptr; - uint32_t offset = *( (uint32_t*)aligned - 1); - uint8_t* ptr = aligned - offset; - size_t total = _size + _align; - ptr = (uint8_t*)realloc(_allocator, ptr, total, 0, _file, _line); - uint8_t* newAligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), _align); - - if (newAligned == aligned) - { - return aligned; - } - - aligned = ptr + offset; - ::memmove(newAligned, aligned, _size); - uint32_t* header = (uint32_t*)newAligned - 1; - *header = uint32_t(newAligned - ptr); - return newAligned; - } + /// + void* alignedRealloc( + AllocatorI* _allocator + , void* _ptr + , size_t _size + , size_t _align + , const char* _file = NULL + , uint32_t _line = 0 + ); + /// template - inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0) - { - if (NULL != _object) - { - _object->~ObjectT(); - free(_allocator, _object, _align, _file, _line); - } - } + void deleteObject( + AllocatorI* _allocator + , ObjectT* _object + , size_t _align = 0 + , const char* _file = NULL + , uint32_t _line = 0 + ); } // namespace bx +#include "allocator.inl" + #endif // BX_ALLOCATOR_H_HEADER_GUARD diff --git a/include/bx/allocator.inl b/include/bx/allocator.inl new file mode 100644 index 0000000..3bef88f --- /dev/null +++ b/include/bx/allocator.inl @@ -0,0 +1,97 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#ifndef BX_ALLOCATOR_H_HEADER_GUARD +# error "Must be included from bx/allocator.h" +#endif // BX_ALLOCATOR_H_HEADER_GUARD + +namespace bx +{ + inline AllocatorI::~AllocatorI() + { + } + + inline void* alignPtr(void* _ptr, size_t _extra, size_t _align) + { + union { void* ptr; size_t addr; } un; + un.ptr = _ptr; + size_t unaligned = un.addr + _extra; // space for header + size_t mask = _align-1; + size_t aligned = BX_ALIGN_MASK(unaligned, mask); + un.addr = aligned; + return un.ptr; + } + + inline void* alloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file, uint32_t _line) + { + return _allocator->realloc(NULL, _size, _align, _file, _line); + } + + inline void free(AllocatorI* _allocator, void* _ptr, size_t _align, const char* _file, uint32_t _line) + { + _allocator->realloc(_ptr, 0, _align, _file, _line); + } + + inline void* realloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) + { + return _allocator->realloc(_ptr, _size, _align, _file, _line); + } + + inline void* alignedAlloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file, uint32_t _line) + { + size_t total = _size + _align; + 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; + } + + inline void alignedFree(AllocatorI* _allocator, void* _ptr, size_t _align, const char* _file, uint32_t _line) + { + BX_UNUSED(_align); + uint8_t* aligned = (uint8_t*)_ptr; + uint32_t* header = (uint32_t*)aligned - 1; + uint8_t* ptr = aligned - *header; + free(_allocator, ptr, 0, _file, _line); + } + + inline void* alignedRealloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) + { + if (NULL == _ptr) + { + return alignedAlloc(_allocator, _size, _align, _file, _line); + } + + uint8_t* aligned = (uint8_t*)_ptr; + uint32_t offset = *( (uint32_t*)aligned - 1); + uint8_t* ptr = aligned - offset; + size_t total = _size + _align; + ptr = (uint8_t*)realloc(_allocator, ptr, total, 0, _file, _line); + uint8_t* newAligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), _align); + + if (newAligned == aligned) + { + return aligned; + } + + aligned = ptr + offset; + ::memmove(newAligned, aligned, _size); + uint32_t* header = (uint32_t*)newAligned - 1; + *header = uint32_t(newAligned - ptr); + return newAligned; + } + + template + inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align, const char* _file, uint32_t _line) + { + if (NULL != _object) + { + _object->~ObjectT(); + free(_allocator, _object, _align, _file, _line); + } + } + +} // namespace bx