allocator.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2010-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef BX_ALLOCATOR_H_HEADER_GUARD
  6. #define BX_ALLOCATOR_H_HEADER_GUARD
  7. #include "bx.h"
  8. #include <memory.h>
  9. #include <string.h> //::memmove
  10. #include <new>
  11. #if BX_CONFIG_ALLOCATOR_CRT
  12. # include <malloc.h>
  13. #endif // BX_CONFIG_ALLOCATOR_CRT
  14. #if BX_CONFIG_ALLOCATOR_DEBUG
  15. # define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, 0, __FILE__, __LINE__)
  16. # define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size, 0, __FILE__, __LINE__)
  17. # define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr, 0, __FILE__, __LINE__)
  18. # define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alloc(_allocator, _size, _align, __FILE__, __LINE__)
  19. # define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align, __FILE__, __LINE__)
  20. # define BX_ALIGNED_FREE(_allocator, _ptr, _align) bx::free(_allocator, _ptr, _align, __FILE__, __LINE__)
  21. # define BX_NEW(_allocator, _type) ::new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
  22. # define BX_DELETE(_allocator, _ptr) bx::deleteObject(_allocator, _ptr, 0, __FILE__, __LINE__)
  23. # define BX_ALIGNED_NEW(_allocator, _type, _align) ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
  24. # define BX_ALIGNED_DELETE(_allocator, _ptr, _align) bx::deleteObject(_allocator, _ptr, _align, __FILE__, __LINE__)
  25. #else
  26. # define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, 0)
  27. # define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size, 0)
  28. # define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr, 0)
  29. # define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alloc(_allocator, _size, _align)
  30. # define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align)
  31. # define BX_ALIGNED_FREE(_allocator, _ptr, _align) bx::free(_allocator, _ptr, _align)
  32. # define BX_NEW(_allocator, _type) ::new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
  33. # define BX_DELETE(_allocator, _ptr) bx::deleteObject(_allocator, _ptr, 0)
  34. # define BX_ALIGNED_NEW(_allocator, _type, _align) ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
  35. # define BX_ALIGNED_DELETE(_allocator, _ptr, _align) bx::deleteObject(_allocator, _ptr, _align)
  36. #endif // BX_CONFIG_DEBUG_ALLOC
  37. #ifndef BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT
  38. # define BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT 8
  39. #endif // BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT
  40. namespace bx
  41. {
  42. /// Aligns pointer to nearest next aligned address. _align must be power of two.
  43. inline void* alignPtr(void* _ptr, size_t _extra, size_t _align = BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT)
  44. {
  45. union { void* ptr; size_t addr; } un;
  46. un.ptr = _ptr;
  47. size_t unaligned = un.addr + _extra; // space for header
  48. size_t mask = _align-1;
  49. size_t aligned = BX_ALIGN_MASK(unaligned, mask);
  50. un.addr = aligned;
  51. return un.ptr;
  52. }
  53. struct BX_NO_VTABLE AllocatorI
  54. {
  55. virtual ~AllocatorI() = 0;
  56. /// Allocated, resizes memory block or frees memory.
  57. ///
  58. /// @param[in] _ptr If _ptr is NULL new block will be allocated.
  59. /// @param[in] _size If _ptr is set, and _size is 0, memory will be freed.
  60. /// @param[in] _align Alignment.
  61. /// @param[in] _file Debug file path info.
  62. /// @param[in] _line Debug file line info.
  63. virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) = 0;
  64. };
  65. inline AllocatorI::~AllocatorI()
  66. {
  67. }
  68. inline void* alloc(AllocatorI* _allocator, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
  69. {
  70. return _allocator->realloc(NULL, _size, _align, _file, _line);
  71. }
  72. inline void free(AllocatorI* _allocator, void* _ptr, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
  73. {
  74. _allocator->realloc(_ptr, 0, _align, _file, _line);
  75. }
  76. inline void* realloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
  77. {
  78. return _allocator->realloc(_ptr, _size, _align, _file, _line);
  79. }
  80. static inline void* alignedAlloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
  81. {
  82. size_t total = _size + _align;
  83. uint8_t* ptr = (uint8_t*)alloc(_allocator, total, 0, _file, _line);
  84. uint8_t* aligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), _align);
  85. uint32_t* header = (uint32_t*)aligned - 1;
  86. *header = uint32_t(aligned - ptr);
  87. return aligned;
  88. }
  89. static inline void alignedFree(AllocatorI* _allocator, void* _ptr, size_t /*_align*/, const char* _file = NULL, uint32_t _line = 0)
  90. {
  91. uint8_t* aligned = (uint8_t*)_ptr;
  92. uint32_t* header = (uint32_t*)aligned - 1;
  93. uint8_t* ptr = aligned - *header;
  94. free(_allocator, ptr, 0, _file, _line);
  95. }
  96. static inline void* alignedRealloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
  97. {
  98. if (NULL == _ptr)
  99. {
  100. return alignedAlloc(_allocator, _size, _align, _file, _line);
  101. }
  102. uint8_t* aligned = (uint8_t*)_ptr;
  103. uint32_t offset = *( (uint32_t*)aligned - 1);
  104. uint8_t* ptr = aligned - offset;
  105. size_t total = _size + _align;
  106. ptr = (uint8_t*)realloc(_allocator, ptr, total, 0, _file, _line);
  107. uint8_t* newAligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), _align);
  108. if (newAligned == aligned)
  109. {
  110. return aligned;
  111. }
  112. aligned = ptr + offset;
  113. ::memmove(newAligned, aligned, _size);
  114. uint32_t* header = (uint32_t*)newAligned - 1;
  115. *header = uint32_t(newAligned - ptr);
  116. return newAligned;
  117. }
  118. template <typename ObjectT>
  119. inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
  120. {
  121. if (NULL != _object)
  122. {
  123. _object->~ObjectT();
  124. free(_allocator, _object, _align, _file, _line);
  125. }
  126. }
  127. #if BX_CONFIG_ALLOCATOR_CRT
  128. class CrtAllocator : public AllocatorI
  129. {
  130. public:
  131. CrtAllocator()
  132. {
  133. }
  134. virtual ~CrtAllocator()
  135. {
  136. }
  137. virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  138. {
  139. if (0 == _size)
  140. {
  141. if (NULL != _ptr)
  142. {
  143. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  144. {
  145. ::free(_ptr);
  146. return NULL;
  147. }
  148. # if BX_COMPILER_MSVC
  149. BX_UNUSED(_file, _line);
  150. _aligned_free(_ptr);
  151. # else
  152. bx::alignedFree(this, _ptr, _align, _file, _line);
  153. # endif // BX_
  154. }
  155. return NULL;
  156. }
  157. else if (NULL == _ptr)
  158. {
  159. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  160. {
  161. return ::malloc(_size);
  162. }
  163. # if BX_COMPILER_MSVC
  164. BX_UNUSED(_file, _line);
  165. return _aligned_malloc(_size, _align);
  166. # else
  167. return bx::alignedAlloc(this, _size, _align, _file, _line);
  168. # endif // BX_
  169. }
  170. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  171. {
  172. return ::realloc(_ptr, _size);
  173. }
  174. # if BX_COMPILER_MSVC
  175. BX_UNUSED(_file, _line);
  176. return _aligned_realloc(_ptr, _size, _align);
  177. # else
  178. return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line);
  179. # endif // BX_
  180. }
  181. };
  182. #endif // BX_CONFIG_ALLOCATOR_CRT
  183. } // namespace bx
  184. #endif // BX_ALLOCATOR_H_HEADER_GUARD