18#include "pw_allocator/allocator.h"
19#include "pw_allocator/block.h"
20#include "pw_allocator/buffer.h"
21#include "pw_allocator/first_fit_block_allocator.h"
22#include "pw_allocator/metrics.h"
23#include "pw_allocator/tracking_allocator.h"
24#include "pw_bytes/span.h"
25#include "pw_result/result.h"
26#include "pw_status/status.h"
27#include "pw_sync/interrupt_spin_lock.h"
28#include "pw_tokenizer/tokenize.h"
29#include "pw_unit_test/framework.h"
31namespace pw::allocator {
35constexpr pw::tokenizer::Token kToken = PW_TOKENIZE_STRING(
"test");
38template <
size_t kBufferSize,
typename MetricsType =
internal::AllMetrics>
42 using BlockType = AllocatorType::BlockType;
45 :
Allocator(AllocatorType::kCapabilities), tracker_(kToken, *allocator_) {
47 allocator_->Init(allocator_.as_bytes());
51 for (
auto* block : allocator_->blocks()) {
52 BlockType::Free(block);
57 AllocatorType::Range blocks()
const {
return allocator_->blocks(); }
58 AllocatorType::Range blocks() {
return allocator_->blocks(); }
60 const metric::Group& metric_group()
const {
return tracker_.metric_group(); }
61 metric::Group& metric_group() {
return tracker_.metric_group(); }
63 const MetricsType& metrics()
const {
return tracker_.metrics(); }
65 size_t allocate_size()
const {
return allocate_size_; }
66 void* deallocate_ptr()
const {
return deallocate_ptr_; }
67 size_t deallocate_size()
const {
return deallocate_size_; }
68 void* resize_ptr()
const {
return resize_ptr_; }
69 size_t resize_old_size()
const {
return resize_old_size_; }
70 size_t resize_new_size()
const {
return resize_new_size_; }
75 deallocate_ptr_ =
nullptr;
77 resize_ptr_ =
nullptr;
84 for (
auto* block : allocator_->blocks()) {
91 void* DoAllocate(
Layout layout)
override {
92 allocate_size_ = layout.size();
93 return tracker_.Allocate(layout);
97 void DoDeallocate(
void* ptr)
override {
98 Result<Layout> requested = GetRequestedLayout(tracker_, ptr);
99 deallocate_ptr_ = ptr;
100 deallocate_size_ = requested.ok() ? requested->size() : 0;
101 tracker_.Deallocate(ptr);
105 void DoDeallocate(
void* ptr, Layout)
override { DoDeallocate(ptr); }
108 bool DoResize(
void* ptr,
size_t new_size)
override {
109 Result<Layout> requested = GetRequestedLayout(tracker_, ptr);
111 resize_old_size_ = requested.ok() ? requested->size() : 0;
112 resize_new_size_ = new_size;
113 return tracker_.Resize(ptr, new_size);
117 Result<Layout> DoGetInfo(InfoType info_type,
const void* ptr)
const override {
118 return GetInfo(tracker_, info_type, ptr);
121 WithBuffer<AllocatorType, kBufferSize> allocator_;
122 TrackingAllocator<MetricsType> tracker_;
123 size_t allocate_size_;
124 void* deallocate_ptr_;
125 size_t deallocate_size_;
127 size_t resize_old_size_;
128 size_t resize_new_size_;
133template <
size_t kBufferSize,
typename MetricsType =
internal::AllMetrics>
139 void* DoAllocate(
Layout layout)
override {
140 std::lock_guard lock(lock_);
145 void DoDeallocate(
void* ptr)
override {
146 std::lock_guard lock(lock_);
151 void DoDeallocate(
void* ptr,
Layout)
override { DoDeallocate(ptr); }
154 bool DoResize(
void* ptr,
size_t new_size)
override {
155 std::lock_guard lock(lock_);
156 return base_.
Resize(ptr, new_size);
160 Result<Layout> DoGetInfo(InfoType info_type,
const void* ptr)
const override {
161 std::lock_guard lock(lock_);
162 return GetInfo(base_, info_type, ptr);
Definition: allocator.h:32
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:81
void * Allocate(Layout layout)
Definition: allocator.h:40
void Deallocate(void *ptr)
Definition: deallocator.h:47
Definition: first_fit_block_allocator.h:33
An AllocatorForTest that is automatically initialized on construction.
Definition: testing.h:39
void ResetParameters()
Resets the recorded parameters to an initial state.
Definition: testing.h:73
void Exhaust()
Allocates all the memory from this object.
Definition: testing.h:83
Definition: testing.h:134
Definition: interrupt_spin_lock.h:48