allocator.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2010-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-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_DEBUG
  12. # define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, 0, __FILE__, __LINE__)
  13. # define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size, 0, __FILE__, __LINE__)
  14. # define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr, 0, __FILE__, __LINE__)
  15. # define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alloc(_allocator, _size, _align, __FILE__, __LINE__)
  16. # define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align, __FILE__, __LINE__)
  17. # define BX_ALIGNED_FREE(_allocator, _ptr, _align) bx::free(_allocator, _ptr, _align, __FILE__, __LINE__)
  18. # define BX_NEW(_allocator, _type) ::new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
  19. # define BX_DELETE(_allocator, _ptr) bx::deleteObject(_allocator, _ptr, 0, __FILE__, __LINE__)
  20. # define BX_ALIGNED_NEW(_allocator, _type, _align) ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
  21. # define BX_ALIGNED_DELETE(_allocator, _ptr, _align) bx::deleteObject(_allocator, _ptr, _align, __FILE__, __LINE__)
  22. #else
  23. # define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, 0)
  24. # define BX_REALLOC(_allocator, _ptr, _size) bx::realloc(_allocator, _ptr, _size, 0)
  25. # define BX_FREE(_allocator, _ptr) bx::free(_allocator, _ptr, 0)
  26. # define BX_ALIGNED_ALLOC(_allocator, _size, _align) bx::alloc(_allocator, _size, _align)
  27. # define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align)
  28. # define BX_ALIGNED_FREE(_allocator, _ptr, _align) bx::free(_allocator, _ptr, _align)
  29. # define BX_NEW(_allocator, _type) ::new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
  30. # define BX_DELETE(_allocator, _ptr) bx::deleteObject(_allocator, _ptr, 0)
  31. # define BX_ALIGNED_NEW(_allocator, _type, _align) ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
  32. # define BX_ALIGNED_DELETE(_allocator, _ptr, _align) bx::deleteObject(_allocator, _ptr, _align)
  33. #endif // BX_CONFIG_DEBUG_ALLOC
  34. #ifndef BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT
  35. # define BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT 8
  36. #endif // BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT
  37. namespace bx
  38. {
  39. /// Aligns pointer to nearest next aligned address. _align must be power of two.
  40. inline void* alignPtr(void* _ptr, size_t _extra, size_t _align = BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT)
  41. {
  42. union { void* ptr; size_t addr; } un;
  43. un.ptr = _ptr;
  44. size_t unaligned = un.addr + _extra; // space for header
  45. size_t mask = _align-1;
  46. size_t aligned = BX_ALIGN_MASK(unaligned, mask);
  47. un.addr = aligned;
  48. return un.ptr;
  49. }
  50. struct BX_NO_VTABLE AllocatorI
  51. {
  52. virtual ~AllocatorI() = 0;
  53. /// Allocated, resizes memory block or frees memory.
  54. ///
  55. /// @param[in] _ptr If _ptr is NULL new block will be allocated.
  56. /// @param[in] _size If _ptr is set, and _size is 0, memory will be freed.
  57. /// @param[in] _align Alignment.
  58. /// @param[in] _file Debug file path info.
  59. /// @param[in] _line Debug file line info.
  60. virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) = 0;
  61. };
  62. inline AllocatorI::~AllocatorI()
  63. {
  64. }
  65. inline void* alloc(AllocatorI* _allocator, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
  66. {
  67. return _allocator->realloc(NULL, _size, _align, _file, _line);
  68. }
  69. inline void free(AllocatorI* _allocator, void* _ptr, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
  70. {
  71. _allocator->realloc(_ptr, 0, _align, _file, _line);
  72. }
  73. inline void* realloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
  74. {
  75. return _allocator->realloc(_ptr, _size, _align, _file, _line);
  76. }
  77. static inline void* alignedAlloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
  78. {
  79. size_t total = _size + _align;
  80. uint8_t* ptr = (uint8_t*)alloc(_allocator, total, 0, _file, _line);
  81. uint8_t* aligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), _align);
  82. uint32_t* header = (uint32_t*)aligned - 1;
  83. *header = uint32_t(aligned - ptr);
  84. return aligned;
  85. }
  86. static inline void alignedFree(AllocatorI* _allocator, void* _ptr, size_t /*_align*/, const char* _file = NULL, uint32_t _line = 0)
  87. {
  88. uint8_t* aligned = (uint8_t*)_ptr;
  89. uint32_t* header = (uint32_t*)aligned - 1;
  90. uint8_t* ptr = aligned - *header;
  91. free(_allocator, ptr, 0, _file, _line);
  92. }
  93. static inline void* alignedRealloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
  94. {
  95. if (NULL == _ptr)
  96. {
  97. return alignedAlloc(_allocator, _size, _align, _file, _line);
  98. }
  99. uint8_t* aligned = (uint8_t*)_ptr;
  100. uint32_t offset = *( (uint32_t*)aligned - 1);
  101. uint8_t* ptr = aligned - offset;
  102. size_t total = _size + _align;
  103. ptr = (uint8_t*)realloc(_allocator, ptr, total, 0, _file, _line);
  104. uint8_t* newAligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), _align);
  105. if (newAligned == aligned)
  106. {
  107. return aligned;
  108. }
  109. aligned = ptr + offset;
  110. ::memmove(newAligned, aligned, _size);
  111. uint32_t* header = (uint32_t*)newAligned - 1;
  112. *header = uint32_t(newAligned - ptr);
  113. return newAligned;
  114. }
  115. template <typename ObjectT>
  116. inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
  117. {
  118. if (NULL != _object)
  119. {
  120. _object->~ObjectT();
  121. free(_allocator, _object, _align, _file, _line);
  122. }
  123. }
  124. } // namespace bx
  125. #endif // BX_ALLOCATOR_H_HEADER_GUARD