20#include "pw_allocator/block/detailed_block.h"
21#include "pw_allocator/block_allocator.h"
22#include "pw_allocator/bucket/fast_sorted.h"
23#include "pw_allocator/bucket/sorted.h"
25namespace pw::allocator {
28template <
typename OffsetType>
29using TlsfBlock = DetailedBlock<OffsetType, GenericFastSortedItem>;
82template <
typename BlockType = TlsfBlock<u
int32_t>,
83 size_t kMinSize = TlsfDefaults::kMinSize,
84 size_t kNumShelves = TlsfDefaults::kNumShelves>
90 static constexpr size_t kNumBucketsPerShelf = 16;
91 static constexpr size_t kBucketBits = cpp20::countr_zero(kNumBucketsPerShelf);
92 using Shelf = std::array<BucketType, kNumBucketsPerShelf>;
94 static_assert(kMinSize >= kNumBucketsPerShelf,
95 "kMinSize must be at least 16.");
98 "kMinSize must be large enough to hold a FastSortedBucket item.");
99 static_assert((kMinSize & (kMinSize - 1)) == 0,
100 "kMinSize must be a power of two.");
102 static_assert(kNumShelves <= 32,
"kNumShelves cannot be larger than 32");
139 void UpdateBitmaps(
const TlsfIndices& indices,
bool empty);
141 uint32_t shelf_bitmap_ = 0;
142 std::array<uint16_t, kNumShelves> bucket_bitmaps_;
143 std::array<Shelf, kNumShelves> shelves_;
149template <
typename BlockType,
size_t kMinSize,
size_t kNumShelves>
151 size_t size = kMinSize;
152 size_t step = kMinSize / kNumBucketsPerShelf;
153 for (Shelf& shelf : shelves_) {
156 bucket.set_max_inner_size(size - 1);
162 BucketType& largest = shelves_[kNumShelves - 1][kNumBucketsPerShelf - 1];
165 bucket_bitmaps_.fill(0);
168template <
typename BlockType,
size_t kMinSize,
size_t kNumShelves>
172 if (layout.size() < small_bucket_.max_inner_size()) {
173 BlockType* block = small_bucket_.RemoveCompatible(layout);
174 if (block !=
nullptr) {
175 return BlockType::AllocFirst(std::move(block), layout);
180 for (
TlsfIndices indices = MapToIndices(layout.size());
181 FindNextAvailable(indices);
184 shelves_[indices.shelf][indices.bucket];
186 if (block !=
nullptr) {
187 UpdateBitmaps(indices, bucket.
empty());
188 return BlockType::AllocFirst(std::move(block), layout);
196template <
typename BlockType,
size_t kMinSize,
size_t kNumShelves>
199 if (block.InnerSize() <=
sizeof(
SortedItem)) {
200 std::ignore = small_bucket_.Remove(block);
203 TlsfIndices indices = MapToIndices(block.InnerSize());
205 shelves_[indices.shelf][indices.bucket];
206 if (large_bucket.
Remove(block)) {
207 UpdateBitmaps(indices, large_bucket.
empty());
211template <
typename BlockType,
size_t kMinSize,
size_t kNumShelves>
214 if (block.InnerSize() <=
sizeof(
SortedItem)) {
215 std::ignore = small_bucket_.Add(block);
218 TlsfIndices indices = MapToIndices(block.InnerSize());
220 shelves_[indices.shelf][indices.bucket];
221 std::ignore = large_bucket.
Add(block);
222 UpdateBitmaps(indices,
false);
225template <
typename BlockType,
size_t kMinSize,
size_t kNumShelves>
228 if (size <= kMinSize) {
233 size_t shelf = cpp20::countr_zero(cpp20::bit_floor(size));
235 auto bucket =
static_cast<uint16_t
>((size >> (shelf - kBucketBits)) & 0xF);
238 shelf -= cpp20::countr_zero(kMinSize);
239 if (shelf >= kNumShelves) {
240 shelf = kNumShelves - 1;
241 bucket = kNumBucketsPerShelf - 1;
243 return TlsfIndices{.shelf =
static_cast<uint32_t
>(shelf), .bucket = bucket};
246template <
typename BlockType,
size_t kMinSize,
size_t kNumShelves>
247bool TlsfAllocator<BlockType, kMinSize, kNumShelves>::FindNextAvailable(
248 TlsfIndices& indices) {
250 if (indices.bucket == kNumBucketsPerShelf) {
256 if (indices.shelf >= kNumShelves) {
261 uint16_t bucket_bitmap =
262 bucket_bitmaps_[indices.shelf] & (~uint32_t(0) << indices.bucket);
263 if (bucket_bitmap != 0) {
266 indices.bucket = cpp20::countr_zero(bucket_bitmap);
273 uint32_t shelf_bitmap = shelf_bitmap_ & (~uint32_t(0) << (indices.shelf + 1));
274 if (shelf_bitmap != 0) {
275 indices.shelf = cpp20::countr_zero(shelf_bitmap);
276 indices.bucket = cpp20::countr_zero(bucket_bitmaps_[indices.shelf]);
284template <
typename BlockType,
size_t kMinSize,
size_t kNumShelves>
285void TlsfAllocator<BlockType, kMinSize, kNumShelves>::UpdateBitmaps(
286 const TlsfIndices& indices,
bool empty) {
287 uint16_t bucket_bitmap = uint32_t(1) << indices.bucket;
289 bucket_bitmaps_[indices.shelf] &= ~bucket_bitmap;
291 bucket_bitmaps_[indices.shelf] |= bucket_bitmap;
294 uint32_t shelf_bitmap = uint32_t(1) << indices.shelf;
295 if (bucket_bitmaps_[indices.shelf] == 0) {
296 shelf_bitmap_ &= ~shelf_bitmap;
298 shelf_bitmap_ |= shelf_bitmap;
Definition: block_allocator.h:104
void Init(ByteSpan region)
Definition: block_allocator.h:281
Definition: fast_sorted.h:63
Definition: tlsf_allocator.h:85
BlockResult< BlockType > ChooseBlock(Layout layout) override
Definition: tlsf_allocator.h:170
TlsfAllocator(ByteSpan region)
Definition: tlsf_allocator.h:113
void ReserveBlock(BlockType &block) override
Definition: tlsf_allocator.h:197
constexpr TlsfAllocator()
Constexpr constructor. Callers must explicitly call Init.
Definition: tlsf_allocator.h:150
void RecycleBlock(BlockType &block) override
Definition: tlsf_allocator.h:212
constexpr void set_max_inner_size(size_t max_inner_size)
Definition: base.h:76
bool Remove(BlockType &block)
Definition: base.h:108
bool Add(BlockType &block)
Definition: base.h:85
constexpr bool empty() const
Returns whether this buckets contains any free blocks.
Definition: base.h:68
BlockType * RemoveCompatible(Layout layout)
Definition: base.h:119
Definition: fast_sorted.h:51
Definition: tlsf_allocator.h:34
static constexpr size_t kMinSize
Definition: tlsf_allocator.h:37
static constexpr size_t kNumShelves
Definition: tlsf_allocator.h:41
Pair used to index a bucket in a two dimensional array.
Definition: tlsf_allocator.h:45