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;
103 return GetInfo(allocator_, info_type, ptr);
112template <
typename MetricsType>
114 if constexpr (internal::AnyEnabled<MetricsType>()) {
115 Layout requested = layout;
116 size_t allocated = allocator_.GetAllocated();
117 void* new_ptr = allocator_.Allocate(requested);
118 if (new_ptr ==
nullptr) {
119 metrics_.RecordFailure(requested.size());
122 metrics_.IncrementAllocations();
123 metrics_.ModifyRequested(requested.size(), 0);
124 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
127 return allocator_.Allocate(layout);
131template <
typename MetricsType>
133 if constexpr (internal::AnyEnabled<MetricsType>()) {
134 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
135 size_t allocated = allocator_.GetAllocated();
136 allocator_.Deallocate(ptr);
137 metrics_.IncrementDeallocations();
138 metrics_.ModifyRequested(0, requested.size());
139 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
141 allocator_.Deallocate(ptr);
145template <
typename MetricsType>
147 if constexpr (internal::AnyEnabled<MetricsType>()) {
148 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
149 size_t allocated = allocator_.GetAllocated();
150 if (!allocator_.Resize(ptr, new_size)) {
151 metrics_.RecordFailure(new_size);
154 metrics_.IncrementResizes();
155 metrics_.ModifyRequested(new_size, requested.size());
156 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
159 return allocator_.Resize(ptr, new_size);
163template <
typename MetricsType>
166 if constexpr (internal::AnyEnabled<MetricsType>()) {
168 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
169 size_t allocated = allocator_.GetAllocated();
170 size_t new_size = new_layout.size();
171 if (allocator_.Resize(ptr, new_size)) {
172 metrics_.IncrementReallocations();
173 metrics_.ModifyRequested(new_size, requested.size());
174 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
185 void* new_ptr = allocator_.Reallocate(ptr, new_layout);
186 if (new_ptr ==
nullptr) {
187 metrics_.RecordFailure(new_size);
190 metrics_.IncrementReallocations();
191 metrics_.ModifyRequested(new_size, requested.size());
193 size_t current_allocated = allocator_.GetAllocated();
194 if (new_ptr != ptr && old_allocated_layout.
ok()) {
195 size_t peak_allocated = current_allocated + old_allocated_layout->size();
196 metrics_.ModifyAllocated(peak_allocated, allocated);
197 metrics_.ModifyAllocated(current_allocated, peak_allocated);
199 metrics_.ModifyAllocated(current_allocated, allocated);
203 return allocator_.Reallocate(ptr, new_layout);
Definition: allocator.h:42
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
size_t GetAllocated() const
Definition: allocator.h:277
std::optional< Fragmentation > MeasureFragmentation() const
Returns fragmentation information for the allocator's memory region.
Definition: allocator.h:280
constexpr bool ok() const
Definition: result.h:451
Definition: tracking_allocator.h:55
std::optional< Fragmentation > DoMeasureFragmentation() const override
Returns fragmentation information for the allocator's memory region.
Definition: tracking_allocator.h:97
void UpdateDeferred() const
Definition: tracking_allocator.h:78
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:102
Definition: metrics.h:195
bool DoResize(void *ptr, size_t new_size) override
Definition: tracking_allocator.h:146
void DoDeallocate(void *ptr) override
Definition: tracking_allocator.h:132
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: tracking_allocator.h:164
void * DoAllocate(Layout layout) override
Definition: tracking_allocator.h:113
Definition: tracking_allocator.h:37