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 = {};
48template <
typename MetricsType>
52 :
Allocator(allocator.capabilities() | kImplementsGetRequestedLayout),
53 allocator_(allocator),
56 template <
typename OtherMetrics>
61 parent.metric_group().Add(metric_group());
64 const metric::Group& metric_group()
const {
return metrics_.group(); }
65 metric::Group& metric_group() {
return metrics_.group(); }
67 const MetricsType& metrics()
const {
return metrics_.metrics(); }
85 bool DoResize(
void* ptr,
size_t new_size)
override;
95 return GetInfo(allocator_, info_type, ptr);
104template <
typename MetricsType>
106 if constexpr (internal::AnyEnabled<MetricsType>()) {
107 Layout requested = layout;
108 size_t allocated = allocator_.GetAllocated();
109 void* new_ptr = allocator_.Allocate(requested);
110 if (new_ptr ==
nullptr) {
111 metrics_.RecordFailure(requested.size());
114 metrics_.IncrementAllocations();
115 metrics_.ModifyRequested(requested.size(), 0);
116 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
119 return allocator_.Allocate(layout);
123template <
typename MetricsType>
125 if constexpr (internal::AnyEnabled<MetricsType>()) {
126 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
127 size_t allocated = allocator_.GetAllocated();
128 allocator_.Deallocate(ptr);
129 metrics_.IncrementDeallocations();
130 metrics_.ModifyRequested(0, requested.size());
131 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
133 allocator_.Deallocate(ptr);
137template <
typename MetricsType>
139 if constexpr (internal::AnyEnabled<MetricsType>()) {
140 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
141 size_t allocated = allocator_.GetAllocated();
142 if (!allocator_.Resize(ptr, new_size)) {
143 metrics_.RecordFailure(new_size);
146 metrics_.IncrementResizes();
147 metrics_.ModifyRequested(new_size, requested.size());
148 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
151 return allocator_.Resize(ptr, new_size);
155template <
typename MetricsType>
158 if constexpr (internal::AnyEnabled<MetricsType>()) {
160 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
161 size_t allocated = allocator_.GetAllocated();
162 size_t new_size = new_layout.size();
163 if (allocator_.Resize(ptr, new_size)) {
164 metrics_.IncrementReallocations();
165 metrics_.ModifyRequested(new_size, requested.size());
166 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
175 if (!old_layout.ok()) {
176 metrics_.RecordFailure(new_size);
179 void* new_ptr = allocator_.Allocate(new_layout);
180 if (new_ptr ==
nullptr) {
181 metrics_.RecordFailure(new_size);
185 size_t transient_allocated = allocator_.GetAllocated();
186 metrics_.ModifyAllocated(transient_allocated, allocated);
187 if (ptr !=
nullptr) {
188 std::memcpy(new_ptr, ptr, std::min(new_size, old_layout->size()));
189 allocator_.Deallocate(ptr);
191 metrics_.IncrementReallocations();
192 metrics_.ModifyRequested(new_size, requested.size());
193 metrics_.ModifyAllocated(allocator_.GetAllocated(), transient_allocated);
196 return allocator_.Reallocate(ptr, new_layout);
212template <
typename MetricsType>
Definition: allocator.h:36
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
size_t GetAllocated() const
Definition: allocator.h:346
Definition: tracking_allocator.h:49
void UpdateDeferred() const
Definition: tracking_allocator.h:72
size_t DoGetAllocated() const override
Definition: tracking_allocator.h:91
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: tracking_allocator.h:94
void DoDeallocate(void *ptr, Layout) override
Definition: tracking_allocator.h:82
Definition: metrics.h:195
bool DoResize(void *ptr, size_t new_size) override
Definition: tracking_allocator.h:138
void DoDeallocate(void *ptr) override
Definition: tracking_allocator.h:124
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: tracking_allocator.h:156
void * DoAllocate(Layout layout) override
Definition: tracking_allocator.h:105
Definition: tracking_allocator.h:37