allocator.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2010-2023 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #ifndef BX_ALLOCATOR_H_HEADER_GUARD
  6. #define BX_ALLOCATOR_H_HEADER_GUARD
  7. #include "bx.h"
  8. #include "uint32_t.h"
  9. #define BX_NEW(_allocator, _type) BX_PLACEMENT_NEW(bx::alloc(_allocator, sizeof(_type) ), _type)
  10. #define BX_ALIGNED_NEW(_allocator, _type, _align) BX_PLACEMENT_NEW(bx::alloc(_allocator, sizeof(_type), _align), _type)
  11. #define BX_PLACEMENT_NEW(_ptr, _type) ::new(bx::PlacementNew, _ptr) _type
  12. void* operator new(size_t, bx::PlacementNewTag, void* _ptr);
  13. void operator delete(void*, bx::PlacementNewTag, void*) throw();
  14. namespace bx
  15. {
  16. /// Abstract allocator interface.
  17. ///
  18. struct BX_NO_VTABLE AllocatorI
  19. {
  20. ///
  21. virtual ~AllocatorI() = 0;
  22. /// Allocates, resizes, or frees memory block.
  23. ///
  24. /// @remark
  25. /// - Allocate memory block: _ptr == NULL && size > 0
  26. /// - Resize memory block: _ptr != NULL && size > 0
  27. /// - Free memory block: _ptr != NULL && size == 0
  28. ///
  29. /// @param[in] _ptr If _ptr is NULL new block will be allocated. If _ptr is not-NULL, and
  30. /// _size is not 0, memory block will be resized.
  31. /// @param[in] _size If _ptr is set, and _size is 0, memory will be freed.
  32. /// @param[in] _align Alignment.
  33. /// @param[in] _filePath Debug file path info.
  34. /// @param[in] _line Debug file line info.
  35. ///
  36. virtual void* realloc(
  37. void* _ptr
  38. , size_t _size
  39. , size_t _align
  40. , const char* _filePath
  41. , uint32_t _line
  42. ) = 0;
  43. };
  44. ///
  45. class DefaultAllocator : public AllocatorI
  46. {
  47. public:
  48. ///
  49. DefaultAllocator();
  50. ///
  51. virtual ~DefaultAllocator();
  52. ///
  53. virtual void* realloc(
  54. void* _ptr
  55. , size_t _size
  56. , size_t _align
  57. , const char* _filePath
  58. , uint32_t _line
  59. ) override;
  60. };
  61. /// Aligns pointer to nearest next aligned address. _align must be power of two.
  62. void* alignPtr(
  63. void* _ptr
  64. , size_t _extra
  65. , size_t _align = 0
  66. );
  67. /// Allocate memory.
  68. void* alloc(
  69. AllocatorI* _allocator
  70. , size_t _size
  71. , size_t _align = 0
  72. , const Location& _location = Location::current()
  73. );
  74. /// Free memory.
  75. void free(
  76. AllocatorI* _allocator
  77. , void* _ptr
  78. , size_t _align = 0
  79. , const Location& _location = Location::current()
  80. );
  81. /// Resize memory block.
  82. void* realloc(
  83. AllocatorI* _allocator
  84. , void* _ptr
  85. , size_t _size
  86. , size_t _align = 0
  87. , const Location& _location = Location::current()
  88. );
  89. /// Allocate memory with specific alignment.
  90. void* alignedAlloc(
  91. AllocatorI* _allocator
  92. , size_t _size
  93. , size_t _align
  94. , const Location& _location = Location::current()
  95. );
  96. /// Free memory that was allocated with aligned allocator.
  97. void alignedFree(
  98. AllocatorI* _allocator
  99. , void* _ptr
  100. , size_t /*_align*/
  101. , const Location& _location = Location::current()
  102. );
  103. /// Resize memory block that was allocated with aligned allocator.
  104. void* alignedRealloc(
  105. AllocatorI* _allocator
  106. , void* _ptr
  107. , size_t _size
  108. , size_t _align
  109. , const Location& _location = Location::current()
  110. );
  111. /// Delete object with specific allocator.
  112. template <typename ObjectT>
  113. void deleteObject(
  114. AllocatorI* _allocator
  115. , ObjectT* _object
  116. , size_t _align = 0
  117. , const Location& _location = Location::current()
  118. );
  119. } // namespace bx
  120. #include "inline/allocator.inl"
  121. #endif // BX_ALLOCATOR_H_HEADER_GUARD