tlsf_allocator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #pragma once
  2. #include "Internal/tlsf_allocator.inl.h"
  3. #include "heap_allocator.h"
  4. namespace baselib
  5. {
  6. BASELIB_CPP_INTERFACE
  7. {
  8. // tlsf_allocator (Two-Level Segregated Fit)
  9. // Lockless, dynamic-sized allocator capable of handling multiple concurrent allocations and deallocations (unless otherwise stated).
  10. // The cost (in processor instructions) allocating from the pool is O(1).
  11. // Allocating from the pool is lockless, except when capacity is required to increase, in which case the capacity is doubled for the a size range
  12. // of the particular allocation size request (see details below).
  13. //
  14. // Strict segregated fit allocation mechanism is applied, a requested size is rounded up to the next allocator provided size.
  15. // The granularity of provided sizes (size ranges) are defined by the `min_size`, `max_size` and `linear_subdivisions` parameters provided.
  16. // `optimal_size` can be called to obtain the actual/best fit size of an allocation for a certain requested size.
  17. //
  18. // A two-Level segregated fit allocator can be said to have two dimensions, or levels.
  19. // The first level provides size ranges of pow2 segments.
  20. // The second level provides size ranges of the first level pow2 segments size range divided by the `linear_subdivisions` parameter value.
  21. // Size range of a given size is be calculated as follows:
  22. //
  23. // int invSizeMask = ((1 << (int)log2(size)) / linear_subdivisions) - 1; // Inverse subdivision mask based on Pow2 of `size`, which is effectively range
  24. // int lowerBound = (size - 1 & ~invSizeMask) + 1;
  25. // int upperBound = lowerBound + invSizeMask;
  26. //
  27. // As an example, the (internal) size allocated for a requested size of 1500 with linear_subdivisions of 16 is 1536 range(1473-1536).
  28. //
  29. // Notes on performance/memory requirements:
  30. //
  31. // - This implementation is a segregated storage algorithm and does not, unlike a segregated fit algorithm (aka buddy allocator) split and coalesce
  32. // memory blocks. A segregated fit is well suited for a single threaded/lock-based implementation but would require multiple atomic operations to split
  33. // or coalesce blocks.
  34. //
  35. // - All allocators share a common base instance of the backing allocator `Allocator`, which is used for allocation when the capacity is required to
  36. // increase. Memory is only freed up when the tlsf allocator `deallocate_all` or destructor is invoked.
  37. // Furthermore, `deallocate_all` is optional to declare in the backing allocator `Allocator` and is if so invoked (once) instead of multiple `deallocate`
  38. // calls when `deallocate_all` (or destructor) is invoked on the tlsf allocator.
  39. //
  40. // - The allocator is constructed with only as many block allocators required for the selected min-max range with linear_subdivisions.
  41. // I.e. one allocator with (min_size, max_size, linear_subdivisions) 32,1024,8 has the same memory footprint as two 32,512,8 and 513,1024,8.
  42. // If either level allocator only requires a single allocator providing a range, code for calculating allocator indices is optimized away by template
  43. // construction. Additionally, if size is known at compile-time (const or sizeof) lookup can be optimized away by the compiler.
  44. //
  45. // - No overhead per allocation (no header information).
  46. //
  47. // - Internally, all memory block sizes must be rounded up to a multiple of alignment. I.e if alignment is 64, buckets containing 96 byte size allocations
  48. // will in internally use 128 byte blocks. Additionally, smallest size allocated will always be greater than or equal to `linear_subdivisions`.
  49. //
  50. // - The allocator relies on that the free memory pool must be persisted and read/write accessible as link information of free memory blocks are
  51. // read/written to by the allocator operations.
  52. //
  53. // Examples:
  54. // Range is within a single pow2 range, no subdivisions. No lookup code needed.
  55. // using BlockAllocator = tlsf_allocator<17, 32, 1>;
  56. //
  57. // Range is within a single pow2 range with 8 subdivisions, so in this case with linear increments (128/8=16) of bucket sizes. Second level lookup only.
  58. // using SegregatedFitAllocatorLinear = tlsf_allocator<129, 256, 8>;
  59. //
  60. // Range is several pow2 ranges, no subdivisions so pow2 size increments of bucket sizes.
  61. // using SegregatedFitAllocatorPow2 = tlsf_allocator<129, 2048, 1>;
  62. //
  63. // Range is several pow2 ranges, with 32 subdivisions each, so pow2 size increments where each pow2 contains an array of buckets with linear size
  64. // increments (pow2sz/32) of bucket sizes.
  65. // using TLSFAllocator = tlsf_allocator<129, 2048, 32>;
  66. //
  67. //
  68. // tlsf_allocator<size_t min_size, size_t max_size, size_t linear_subdivisions = 1, class Allocator = baselib::heap_allocator<>>
  69. //
  70. // min_size - valid minimum size of allocations.
  71. // max_size - valid maximum size of allocations. Must be less or equal to the size addressable by integral type `size_t` divided by two plus 1.
  72. // linear_subdivisions - number of linear subdivisions of second level allocators (defaults to 1). Must be a power of two and less or equal to `min_size`
  73. // Allocator - Backing memory allocator. Defaults to baselib heap_allocator.
  74. //
  75. template<size_t min_size, size_t max_size, size_t linear_subdivisions = 1, class Allocator = baselib::heap_allocator<> >
  76. class tlsf_allocator : protected detail::tlsf_allocator<min_size, max_size, linear_subdivisions, Allocator>
  77. {
  78. using Impl = detail::tlsf_allocator<min_size, max_size, linear_subdivisions, Allocator>;
  79. static_assert(min_size <= max_size, "min_size > max_size");
  80. static_assert(min_size >= linear_subdivisions, "min_size < linear_subdivisions");
  81. static_assert(max_size <= std::numeric_limits<size_t>::max() / 2 + 1, "max_size > std::numeric_limits<size_t>::max() / 2 + 1");
  82. static_assert(baselib::Algorithm::IsPowerOfTwo(linear_subdivisions), "linear_subdivisions != pow2");
  83. public:
  84. // non-copyable
  85. tlsf_allocator(const tlsf_allocator& other) = delete;
  86. tlsf_allocator& operator=(const tlsf_allocator& other) = delete;
  87. // non-movable (strictly speaking not needed but listed to signal intent)
  88. tlsf_allocator(tlsf_allocator&& other) = delete;
  89. tlsf_allocator& operator=(tlsf_allocator&& other) = delete;
  90. // Allocated memory is guaranteed to always be aligned to at least the value of `alignment`.
  91. static constexpr uint32_t alignment = Impl::alignment;
  92. // Creates a new instance
  93. tlsf_allocator()
  94. {
  95. atomic_thread_fence(memory_order_seq_cst);
  96. }
  97. // Destroy allocator, deallocates any memory allocated.
  98. //
  99. // If there are other threads currently accessing the allocator behavior is undefined.
  100. ~tlsf_allocator() {}
  101. // Allocates a memory block large enough to hold `size` number of bytes if allocation does not require increasing capacity.
  102. //
  103. // \returns Address to memory block of allocated memory or nullptr if failed or outside of size range.
  104. void* try_allocate(size_t size)
  105. {
  106. return owns(nullptr, size) ? Impl::try_allocate(size) : nullptr;
  107. }
  108. // Allocates a memory block large enough to hold `size` number of bytes.
  109. //
  110. // \returns Address to memory block of allocated memory or nullptr if failed or outside of size range
  111. void* allocate(size_t size)
  112. {
  113. return owns(nullptr, size) ? Impl::allocate(size) : nullptr;
  114. }
  115. // Reallocates previously allocated or reallocated memory pointed to by `ptr` from `old_size` to `new_size` number of bytes if reallocation does not
  116. // require increasing capacity. Passing `nullptr` in `ptr` yield the same result as calling `try_allocate`.
  117. //
  118. // \returns Address to memory block of reallocated memory or nullptr if failed or if `new_size` is outside of size range.
  119. void* try_reallocate(void* ptr, size_t old_size, size_t new_size)
  120. {
  121. return owns(nullptr, new_size) ? Impl::try_reallocate(ptr, old_size, new_size) : nullptr;
  122. }
  123. // Reallocates previously allocated or reallocated memory pointed to by `ptr` from `old_size` to `new_size` number of bytes.
  124. // Passing `nullptr` in `ptr` yield the same result as calling `allocate`.
  125. //
  126. // \returns Address to memory block of reallocated memory or nullptr if failed or if `new_size` is outside of size range
  127. void* reallocate(void* ptr, size_t old_size, size_t new_size)
  128. {
  129. return owns(nullptr, new_size) ? Impl::reallocate(ptr, old_size, new_size) : nullptr;
  130. }
  131. // Deallocates memory block previously allocated or reallocated with `size` pointed to by `ptr`.
  132. // Passing `nullptr` in `ptr` result in a no-op.
  133. //
  134. // \returns Always returns `true`
  135. bool deallocate(void* ptr, size_t size)
  136. {
  137. return Impl::deallocate(ptr, size);
  138. }
  139. // Free a linked list of allocations created using `batch_deallocate_link` with `size`.
  140. // `first` to `last` is first and last allocation of a `batch_deallocate_link` series of calls.
  141. //
  142. // \returns Always returns `true`
  143. bool batch_deallocate(void* ptr_first, void* ptr_last, size_t size)
  144. {
  145. return Impl::batch_deallocate(ptr_first, ptr_last, size);
  146. }
  147. // Link previously allocated memory of `size` to another.
  148. //
  149. // Use to create a linked list of allocations for use with `batch_deallocate(first, last, size)`
  150. // Size of linked allocations are required to be equal to `size`.
  151. // `nullptr` is a valid argument for `ptr_next`, but is not needed to terminate a linked list.
  152. // This is implicit transfer of ownership of the memory back to the allocator.
  153. // Memory of the allocation must not be accessed/modified once linked.
  154. void batch_deallocate_link(void* ptr, void* ptr_next, size_t size)
  155. {
  156. Impl::batch_deallocate_link(ptr, ptr_next);
  157. }
  158. // Release all resources and set capacity to zero
  159. //
  160. // Calling this function invalidates any currently allocated memory
  161. // If there are other threads currently accessing the allocator behavior is undefined.
  162. void deallocate_all()
  163. {
  164. Impl::deallocate_all();
  165. }
  166. // Requests that the allocator capacity be at least enough to contain `capacity` for allocations of `size`.
  167. //
  168. // If `capacity` is less or equal to current capacity for allocations of `size`, the capacity is not affected.
  169. // Note that internally, `capacity` is rounded up to the nearest optimal allocation size based on `Allocator` attributes.
  170. //
  171. // \returns true if successful.
  172. bool reserve(size_t size, size_t capacity)
  173. {
  174. return owns(nullptr, size) ? Impl::reserve(size, capacity) : false;
  175. }
  176. // Get the current capacity of allocations with `size`.
  177. size_t capacity(size_t size)
  178. {
  179. return owns(nullptr, size) ? Impl::capacity(size) : 0;
  180. }
  181. // Calculate optimal allocation size given `size`.
  182. //
  183. // \returns Optimal size when allocating memory given `size` or zero if outside size range.
  184. static constexpr size_t optimal_size(const size_t size)
  185. {
  186. return owns(nullptr, size) ? Impl::optimal_size(size) : 0;
  187. }
  188. // Checks for the ownership allocation given `ptr` and `size`
  189. // It is implementation defined if either or both of `ptr` and `size` are considered to determine ownership.
  190. // This allocator does not consider `ptr`.
  191. //
  192. // \returns True if the allocator owns the allocation.
  193. static constexpr bool owns(const void *, size_t size)
  194. {
  195. return size - min_size <= max_size - min_size;
  196. }
  197. };
  198. }
  199. }