C/C++ API Reference
Loading...
Searching...
No Matches
tracking_allocator.h
1// Copyright 2023 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17#include <cstdint>
18#include <cstring>
19
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"
29
30namespace pw::allocator {
31
33
37static constexpr struct AddTrackingAllocatorAsChild {
38} kAddTrackingAllocatorAsChild = {};
39
54template <typename MetricsType>
56 public:
57 TrackingAllocator(metric::Token token, Allocator& allocator)
58 : Allocator(allocator.capabilities() | kImplementsGetRequestedLayout),
59 allocator_(allocator),
60 metrics_(token) {}
61
62 template <typename OtherMetrics>
63 TrackingAllocator(metric::Token token,
66 : TrackingAllocator(token, parent) {
67 parent.metric_group().Add(metric_group());
68 }
69
70 const metric::Group& metric_group() const { return metrics_.group(); }
71 metric::Group& metric_group() { return metrics_.group(); }
72
73 const MetricsType& metrics() const { return metrics_.metrics(); }
74
78 void UpdateDeferred() const { metrics_.UpdateDeferred(allocator_); }
79
80 private:
82 void* DoAllocate(Layout layout) override;
83
85 void DoDeallocate(void* ptr) override;
86
88 bool DoResize(void* ptr, size_t new_size) override;
89
91 void* DoReallocate(void* ptr, Layout new_layout) override;
92
94 size_t DoGetAllocated() const override { return allocator_.GetAllocated(); }
95
97 std::optional<allocator::Fragmentation> DoMeasureFragmentation()
98 const override {
99 return allocator_.MeasureFragmentation();
100 }
101
103 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override {
104 return GetInfo(allocator_, info_type, ptr);
105 }
106
107 Allocator& allocator_;
108 mutable internal::Metrics<MetricsType> metrics_;
109};
110
111// Template method implementation.
112
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());
121 return nullptr;
122 }
123 metrics_.IncrementAllocations();
124 metrics_.ModifyRequested(requested.size(), 0);
125 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
126 return new_ptr;
127 } else {
128 return allocator_.Allocate(layout);
129 }
130}
131
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);
141 } else {
142 allocator_.Deallocate(ptr);
143 }
144}
145
146template <typename MetricsType>
147bool TrackingAllocator<MetricsType>::DoResize(void* ptr, size_t new_size) {
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);
153 return false;
154 }
155 metrics_.IncrementResizes();
156 metrics_.ModifyRequested(new_size, requested.size());
157 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
158 return true;
159 } else {
160 return allocator_.Resize(ptr, new_size);
161 }
162}
163
164template <typename MetricsType>
166 Layout new_layout) {
167 if constexpr (internal::AnyEnabled<MetricsType>()) {
168 // Check if possible to resize in place with no additional overhead.
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);
176 return ptr;
177 }
178
179 // Retrieve the allocated layout of the old pointer to calculate the peak
180 // memory usage during reallocation if a move occurs. Note that if the
181 // underlying allocator does not have the `kImplementsGetAllocatedLayout`
182 // capability, this will return an error and the peak metric may be lower
183 // than the actual peak allocation value.
184 Result<Layout> old_allocated_layout = GetAllocatedLayout(ptr);
185
186 void* new_ptr = allocator_.Reallocate(ptr, new_layout);
187 if (new_ptr == nullptr) {
188 metrics_.RecordFailure(new_size);
189 return nullptr;
190 }
191 metrics_.IncrementReallocations();
192 metrics_.ModifyRequested(new_size, requested.size());
193
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);
199 } else {
200 metrics_.ModifyAllocated(current_allocated, allocated);
201 }
202 return new_ptr;
203 } else {
204 return allocator_.Reallocate(ptr, new_layout);
205 }
206}
207
209
210} // namespace pw::allocator
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
Definition: result.h:145
constexpr bool ok() const
Definition: result.h:451
Definition: layout.h:58
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