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(); }
88 bool DoResize(
void* ptr,
size_t new_size)
override;
104 return GetInfo(allocator_, info_type, ptr);
113template <
typename MetricsType>
115 if constexpr (internal::AnyEnabled<MetricsType>()) {
116 Layout requested = layout;
117 size_t allocated = allocator_.GetAllocated();
118 void* new_ptr = allocator_.Allocate(requested);
119 if (new_ptr ==
nullptr) {
120 metrics_.RecordFailure(requested.size());
123 metrics_.IncrementAllocations();
124 metrics_.ModifyRequested(requested.size(), 0);
125 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
128 return allocator_.Allocate(layout);
132template <
typename MetricsType>
134 if constexpr (internal::AnyEnabled<MetricsType>()) {
135 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
136 size_t allocated = allocator_.GetAllocated();
137 allocator_.Deallocate(ptr);
138 metrics_.IncrementDeallocations();
139 metrics_.ModifyRequested(0, requested.size());
140 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
142 allocator_.Deallocate(ptr);
146template <
typename MetricsType>
148 if constexpr (internal::AnyEnabled<MetricsType>()) {
149 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
150 size_t allocated = allocator_.GetAllocated();
151 if (!allocator_.Resize(ptr, new_size)) {
152 metrics_.RecordFailure(new_size);
155 metrics_.IncrementResizes();
156 metrics_.ModifyRequested(new_size, requested.size());
157 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
160 return allocator_.Resize(ptr, new_size);
164template <
typename MetricsType>
167 if constexpr (internal::AnyEnabled<MetricsType>()) {
169 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
170 size_t allocated = allocator_.GetAllocated();
171 size_t new_size = new_layout.size();
172 if (allocator_.Resize(ptr, new_size)) {
173 metrics_.IncrementReallocations();
174 metrics_.ModifyRequested(new_size, requested.size());
175 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
186 void* new_ptr = allocator_.Reallocate(ptr, new_layout);
187 if (new_ptr ==
nullptr) {
188 metrics_.RecordFailure(new_size);
191 metrics_.IncrementReallocations();
192 metrics_.ModifyRequested(new_size, requested.size());
194 size_t current_allocated = allocator_.GetAllocated();
195 if (new_ptr != ptr && old_allocated_layout.
ok()) {
196 size_t peak_allocated = current_allocated + old_allocated_layout->size();
197 metrics_.ModifyAllocated(peak_allocated, allocated);
198 metrics_.ModifyAllocated(current_allocated, peak_allocated);
200 metrics_.ModifyAllocated(current_allocated, allocated);
204 return allocator_.Reallocate(ptr, new_layout);
Definition: allocator.h:42
std::optional< allocator::Fragmentation > MeasureFragmentation() const
Returns fragmentation information for the allocator's memory region.
Definition: allocator.h:301
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
size_t GetAllocated() const
Definition: allocator.h:296
constexpr bool ok() const
Definition: result.h:451
Definition: tracking_allocator.h:55
void UpdateDeferred() const
Definition: tracking_allocator.h:78
std::optional< allocator::Fragmentation > DoMeasureFragmentation() const override
Definition: tracking_allocator.h:97
size_t DoGetAllocated() const override
Definition: tracking_allocator.h:94
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: tracking_allocator.h:103
Definition: metrics.h:195
bool DoResize(void *ptr, size_t new_size) override
Definition: tracking_allocator.h:147
void DoDeallocate(void *ptr) override
Definition: tracking_allocator.h:133
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: tracking_allocator.h:165
void * DoAllocate(Layout layout) override
Definition: tracking_allocator.h:114
Definition: tracking_allocator.h:37