20#include "pw_allocator/allocator.h"
21#include "pw_allocator/capability.h"
22#include "pw_allocator/metrics.h"
23#include "pw_assert/assert.h"
24#include "pw_metric/metric.h"
25#include "pw_preprocessor/compiler.h"
26#include "pw_result/result.h"
27#include "pw_status/status.h"
28#include "pw_status/status_with_size.h"
30namespace pw::allocator {
38} kAddTrackingAllocatorAsChild = {};
54template <
typename MetricsType>
58 :
Allocator(allocator.capabilities() | kImplementsGetRequestedLayout),
59 allocator_(allocator),
62 template <
typename OtherMetrics>
67 parent.metric_group().Add(metric_group());
70 const metric::Group& metric_group()
const {
return metrics_.group(); }
71 metric::Group& metric_group() {
return metrics_.group(); }
73 const MetricsType& metrics()
const {
return metrics_.metrics(); }
91 bool DoResize(
void* ptr,
size_t new_size)
override;
107 return GetInfo(allocator_, info_type, ptr);
116template <
typename MetricsType>
118 if constexpr (internal::AnyEnabled<MetricsType>()) {
119 Layout requested = layout;
120 size_t allocated = allocator_.GetAllocated();
121 void* new_ptr = allocator_.Allocate(requested);
122 if (new_ptr ==
nullptr) {
123 metrics_.RecordFailure(requested.size());
126 metrics_.IncrementAllocations();
127 metrics_.ModifyRequested(requested.size(), 0);
128 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
131 return allocator_.Allocate(layout);
135template <
typename MetricsType>
137 if constexpr (internal::AnyEnabled<MetricsType>()) {
138 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
139 size_t allocated = allocator_.GetAllocated();
140 allocator_.Deallocate(ptr);
141 metrics_.IncrementDeallocations();
142 metrics_.ModifyRequested(0, requested.size());
143 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
145 allocator_.Deallocate(ptr);
149template <
typename MetricsType>
151 if constexpr (internal::AnyEnabled<MetricsType>()) {
152 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
153 size_t allocated = allocator_.GetAllocated();
154 if (!allocator_.Resize(ptr, new_size)) {
155 metrics_.RecordFailure(new_size);
158 metrics_.IncrementResizes();
159 metrics_.ModifyRequested(new_size, requested.size());
160 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
163 return allocator_.Resize(ptr, new_size);
167template <
typename MetricsType>
170 if constexpr (internal::AnyEnabled<MetricsType>()) {
172 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
173 size_t allocated = allocator_.GetAllocated();
174 size_t new_size = new_layout.size();
175 if (allocator_.Resize(ptr, new_size)) {
176 metrics_.IncrementReallocations();
177 metrics_.ModifyRequested(new_size, requested.size());
178 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
189 void* new_ptr = allocator_.Reallocate(ptr, new_layout);
190 if (new_ptr ==
nullptr) {
191 metrics_.RecordFailure(new_size);
194 metrics_.IncrementReallocations();
195 metrics_.ModifyRequested(new_size, requested.size());
197 size_t current_allocated = allocator_.GetAllocated();
198 if (new_ptr != ptr && old_allocated_layout.
ok()) {
199 size_t peak_allocated = current_allocated + old_allocated_layout->size();
200 metrics_.ModifyAllocated(peak_allocated, allocated);
201 metrics_.ModifyAllocated(current_allocated, peak_allocated);
203 metrics_.ModifyAllocated(current_allocated, allocated);
207 return allocator_.Reallocate(ptr, new_layout);
223template <
typename MetricsType>
Definition: allocator.h:45
std::optional< allocator::Fragmentation > MeasureFragmentation() const
Returns fragmentation information for the allocator's memory region.
Definition: allocator.h:360
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
size_t GetAllocated() const
Definition: allocator.h:355
constexpr bool ok() const
Definition: result.h:447
Definition: tracking_allocator.h:55
void UpdateDeferred() const
Definition: tracking_allocator.h:78
std::optional< allocator::Fragmentation > DoMeasureFragmentation() const override
Definition: tracking_allocator.h:100
size_t DoGetAllocated() const override
Definition: tracking_allocator.h:97
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: tracking_allocator.h:106
void DoDeallocate(void *ptr, Layout) override
Definition: tracking_allocator.h:88
Definition: metrics.h:195
bool DoResize(void *ptr, size_t new_size) override
Definition: tracking_allocator.h:150
void DoDeallocate(void *ptr) override
Definition: tracking_allocator.h:136
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: tracking_allocator.h:168
void * DoAllocate(Layout layout) override
Definition: tracking_allocator.h:117
Definition: tracking_allocator.h:37