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 {
36} kAddTrackingAllocatorAsChild = {};
46template <
typename MetricsType>
50 :
Allocator(allocator.capabilities() | kImplementsGetRequestedLayout),
51 allocator_(allocator),
54 template <
typename OtherMetrics>
59 parent.metric_group().Add(metric_group());
62 const metric::Group& metric_group()
const {
return metrics_.group(); }
63 metric::Group& metric_group() {
return metrics_.group(); }
65 const MetricsType& metrics()
const {
return metrics_.metrics(); }
83 bool DoResize(
void* ptr,
size_t new_size)
override;
92 Result<Layout>
DoGetInfo(InfoType info_type,
const void* ptr)
const override {
93 return GetInfo(allocator_, info_type, ptr);
102template <
typename MetricsType>
104 if constexpr (internal::AnyEnabled<MetricsType>()) {
105 Layout requested = layout;
106 size_t allocated = allocator_.GetAllocated();
107 void* new_ptr = allocator_.Allocate(requested);
108 if (new_ptr ==
nullptr) {
109 metrics_.RecordFailure(requested.size());
112 metrics_.IncrementAllocations();
113 metrics_.ModifyRequested(requested.size(), 0);
114 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
117 return allocator_.Allocate(layout);
121template <
typename MetricsType>
123 if constexpr (internal::AnyEnabled<MetricsType>()) {
124 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
125 size_t allocated = allocator_.GetAllocated();
126 allocator_.Deallocate(ptr);
127 metrics_.IncrementDeallocations();
128 metrics_.ModifyRequested(0, requested.size());
129 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
131 allocator_.Deallocate(ptr);
135template <
typename MetricsType>
137 if constexpr (internal::AnyEnabled<MetricsType>()) {
138 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
139 size_t allocated = allocator_.GetAllocated();
140 if (!allocator_.Resize(ptr, new_size)) {
141 metrics_.RecordFailure(new_size);
144 metrics_.IncrementResizes();
145 metrics_.ModifyRequested(new_size, requested.size());
146 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
149 return allocator_.Resize(ptr, new_size);
153template <
typename MetricsType>
156 if constexpr (internal::AnyEnabled<MetricsType>()) {
158 Layout requested = Layout::Unwrap(GetRequestedLayout(ptr));
159 size_t allocated = allocator_.GetAllocated();
160 size_t new_size = new_layout.size();
161 if (allocator_.Resize(ptr, new_size)) {
162 metrics_.IncrementReallocations();
163 metrics_.ModifyRequested(new_size, requested.size());
164 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
172 Result<Layout> old_layout = GetUsableLayout(ptr);
173 if (!old_layout.ok()) {
174 metrics_.RecordFailure(new_size);
177 void* new_ptr = allocator_.Allocate(new_layout);
178 if (new_ptr ==
nullptr) {
179 metrics_.RecordFailure(new_size);
183 size_t transient_allocated = allocator_.GetAllocated();
184 metrics_.ModifyAllocated(transient_allocated, allocated);
185 if (ptr !=
nullptr) {
186 std::memcpy(new_ptr, ptr, std::min(new_size, old_layout->size()));
187 allocator_.Deallocate(ptr);
189 metrics_.IncrementReallocations();
190 metrics_.ModifyRequested(new_size, requested.size());
191 metrics_.ModifyAllocated(allocator_.GetAllocated(), transient_allocated);
194 return allocator_.Reallocate(ptr, new_layout);
210template <
typename MetricsType>
Definition: allocator.h:34
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
size_t GetAllocated() const
Definition: allocator.h:320
Definition: tracking_allocator.h:47
void UpdateDeferred() const
Definition: tracking_allocator.h:70
bool DoResize(void *ptr, size_t new_size) override
Definition: tracking_allocator.h:136
void DoDeallocate(void *ptr) override
Definition: tracking_allocator.h:122
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: tracking_allocator.h:154
size_t DoGetAllocated() const override
Definition: tracking_allocator.h:89
void * DoAllocate(Layout layout) override
Definition: tracking_allocator.h:103
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: tracking_allocator.h:92
void DoDeallocate(void *ptr, Layout) override
Definition: tracking_allocator.h:80
Definition: metrics.h:193
Definition: tracking_allocator.h:35