19#include "pw_allocator/allocator.h"
20#include "pw_allocator/first_fit.h"
21#include "pw_allocator/hardening.h"
22#include "pw_allocator/metrics.h"
23#include "pw_allocator/tracking_allocator.h"
24#include "pw_assert/assert.h"
25#include "pw_bytes/span.h"
26#include "pw_result/result.h"
27#include "pw_status/status.h"
28#include "pw_tokenizer/tokenize.h"
29#include "pw_unit_test/framework.h"
31namespace pw::allocator::test {
35static_assert(Hardening::kIncludesDebugChecks,
36 "Tests must use a config that enables strict validation");
42template <
typename BlockType>
43void FreeAll(
typename BlockType::Range range) {
44 BlockType* block = *(range.begin());
45 if (block ==
nullptr) {
50 BlockType* prev = block->Prev();
51 while (prev !=
nullptr) {
57 while (block !=
nullptr) {
58 if (!block->IsFree()) {
59 auto result = BlockType::Free(std::move(block));
60 block = result.block();
62 block = block->Next();
67template <
size_t kBufferSize,
72 using BlockType = BlockType_;
73 using MetricsType = MetricsType_;
78 static constexpr size_t kMinSize = BlockType::kAlignment;
81 :
Allocator(AllocatorType::kCapabilities),
83 tracker_(kToken, allocator_) {
89 typename BlockType::Range blocks()
const {
return allocator_.
blocks(); }
90 typename BlockType::Range blocks() {
return allocator_.
blocks(); }
92 const metric::Group& metric_group()
const {
return tracker_.metric_group(); }
93 metric::Group& metric_group() {
return tracker_.metric_group(); }
95 const MetricsType& metrics()
const {
return tracker_.metrics(); }
97 size_t allocate_size()
const {
return allocate_size_; }
98 void* deallocate_ptr()
const {
return deallocate_ptr_; }
99 size_t deallocate_size()
const {
return deallocate_size_; }
100 void* resize_ptr()
const {
return resize_ptr_; }
101 size_t resize_old_size()
const {
return resize_old_size_; }
102 size_t resize_new_size()
const {
return resize_new_size_; }
107 deallocate_ptr_ =
nullptr;
108 deallocate_size_ = 0;
109 resize_ptr_ =
nullptr;
110 resize_old_size_ = 0;
111 resize_new_size_ = 0;
116 for (
auto* block : allocator_.
blocks()) {
117 if (block->IsFree()) {
118 auto result = BlockType::AllocLast(std::move(block),
119 Layout(block->InnerSize(), 1));
120 PW_ASSERT(result.status() ==
OkStatus());
122 using Prev = internal::GenericBlockResult::Prev;
123 PW_ASSERT(result.prev() == Prev::kUnchanged);
125 using Next = internal::GenericBlockResult::Next;
126 PW_ASSERT(result.next() == Next::kUnchanged);
149 allocate_size_ = layout.size();
150 void* ptr = tracker_.Allocate(layout);
157 deallocate_ptr_ = ptr;
158 deallocate_size_ = requested.
ok() ? requested->size() : 0;
159 tracker_.Deallocate(ptr);
163 bool DoResize(
void* ptr,
size_t new_size)
override {
166 resize_old_size_ = requested.
ok() ? requested->size() : 0;
167 resize_new_size_ = new_size;
168 return tracker_.Resize(ptr, new_size);
176 return GetInfo(tracker_, info_type, ptr);
179 alignas(BlockType::kAlignment) std::array<std::byte, kBufferSize> buffer_{};
180 AllocatorType allocator_;
182 size_t allocate_size_;
183 void* deallocate_ptr_;
184 size_t deallocate_size_;
186 size_t resize_old_size_;
187 size_t resize_new_size_;
Definition: allocator.h:42
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
constexpr bool ok() const
Definition: result.h:451
Fragmentation MeasureFragmentation() const
Returns fragmentation information for the block allocator's memory region.
Definition: block_allocator.h:478
Range blocks() const
Returns a Range of blocks tracking the memory of this allocator.
Definition: block_allocator.h:301
Definition: detailed_block.h:88
Definition: tracking_allocator.h:55
An AllocatorForTest that is automatically initialized on construction.
Definition: testing.h:70
size_t DoGetAllocated() const override
Definition: testing.h:172
std::optional< allocator::Fragmentation > DoMeasureFragmentation() const override
Definition: testing.h:141
void ResetParameters()
Resets the recorded parameters to an initial state.
Definition: testing.h:105
void DoDeallocate(void *ptr) override
Definition: testing.h:155
void * DoAllocate(Layout layout) override
Definition: testing.h:148
void Exhaust()
Allocates all the memory from this object.
Definition: testing.h:115
Fragmentation MeasureFragmentation() const
Returns fragmentation information for the block allocator's memory region.
Definition: testing.h:132
bool DoResize(void *ptr, size_t new_size) override
Definition: testing.h:163
TrackingAllocator< MetricsType > & GetTracker()
Returns the underlying tracking allocator.
Definition: testing.h:138
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: testing.h:175
void FreeAll(typename BlockType::Range range)
Free all the blocks reachable by the given block. Useful for test cleanup.
Definition: testing.h:43
constexpr Status OkStatus()
Definition: status.h:450
#define PW_TOKENIZE_STRING(...)
Definition: tokenize.h:67
Definition: fragmentation.h:46
Definition: metrics.h:173