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<Fragmentation> DoMeasureFragmentation() const override {
98 return allocator_.MeasureFragmentation();
99 }
100
102 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override {
103 return GetInfo(allocator_, info_type, ptr);
104 }
105
106 Allocator& allocator_;
107 mutable internal::Metrics<MetricsType> metrics_;
108};
109
110// Template method implementation.
111
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());
120 return nullptr;
121 }
122 metrics_.IncrementAllocations();
123 metrics_.ModifyRequested(requested.size(), 0);
124 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
125 return new_ptr;
126 } else {
127 return allocator_.Allocate(layout);
128 }
129}
130
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);
140 } else {
141 allocator_.Deallocate(ptr);
142 }
143}
144
145template <typename MetricsType>
146bool TrackingAllocator<MetricsType>::DoResize(void* ptr, size_t new_size) {
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);
152 return false;
153 }
154 metrics_.IncrementResizes();
155 metrics_.ModifyRequested(new_size, requested.size());
156 metrics_.ModifyAllocated(allocator_.GetAllocated(), allocated);
157 return true;
158 } else {
159 return allocator_.Resize(ptr, new_size);
160 }
161}
162
163template <typename MetricsType>
165 Layout new_layout) {
166 if constexpr (internal::AnyEnabled<MetricsType>()) {
167 // Check if possible to resize in place with no additional overhead.
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);
175 return ptr;
176 }
177
178 // Retrieve the allocated layout of the old pointer to calculate the peak
179 // memory usage during reallocation if a move occurs. Note that if the
180 // underlying allocator does not have the `kImplementsGetAllocatedLayout`
181 // capability, this will return an error and the peak metric may be lower
182 // than the actual peak allocation value.
183 Result<Layout> old_allocated_layout = GetAllocatedLayout(ptr);
184
185 void* new_ptr = allocator_.Reallocate(ptr, new_layout);
186 if (new_ptr == nullptr) {
187 metrics_.RecordFailure(new_size);
188 return nullptr;
189 }
190 metrics_.IncrementReallocations();
191 metrics_.ModifyRequested(new_size, requested.size());
192
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);
198 } else {
199 metrics_.ModifyAllocated(current_allocated, allocated);
200 }
201 return new_ptr;
202 } else {
203 return allocator_.Reallocate(ptr, new_layout);
204 }
205}
206
208
209} // namespace pw::allocator
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
Definition: result.h:145
constexpr bool ok() const
Definition: result.h:451
Definition: layout.h:64
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