aboutsummaryrefslogtreecommitdiff
path: root/pw_allocator/public/pw_allocator/allocator_metric_proxy.h
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2024-03-12 23:07:32 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2024-03-12 23:07:32 +0000
commit47562fa92998f8f4289ae9a8048349067754d52e (patch)
treec1643be8ab17fc607cea748a8bb1d621a5964873 /pw_allocator/public/pw_allocator/allocator_metric_proxy.h
parenteeec55b65fe2c3c7647bb70ea44b3c839eb1267c (diff)
parent646563934a3e2ee26f50171f94d95173a1662e2c (diff)
downloadpigweed-47562fa92998f8f4289ae9a8048349067754d52e.tar.gz
Snap for 11566117 from 646563934a3e2ee26f50171f94d95173a1662e2c to sdk-releaseplatform-tools-35.0.1
Change-Id: Iec629b181a2c6905754a4c340e334884e13fd3b4
Diffstat (limited to 'pw_allocator/public/pw_allocator/allocator_metric_proxy.h')
-rw-r--r--pw_allocator/public/pw_allocator/allocator_metric_proxy.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/pw_allocator/public/pw_allocator/allocator_metric_proxy.h b/pw_allocator/public/pw_allocator/allocator_metric_proxy.h
new file mode 100644
index 000000000..b4cc3a561
--- /dev/null
+++ b/pw_allocator/public/pw_allocator/allocator_metric_proxy.h
@@ -0,0 +1,71 @@
+// Copyright 2023 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+#pragma once
+
+#include <cstddef>
+
+#include "pw_allocator/allocator.h"
+#include "pw_metric/metric.h"
+#include "pw_status/status.h"
+
+namespace pw::allocator {
+
+/// Wraps an `Allocator` and records details of its usage.
+///
+/// In order for this object to record memory usage metrics correctly, all calls
+/// to, e.g., `Allocate`, `Deallocate`, etc. must be made through it and not the
+/// allocator it wraps.
+///
+/// As a rule, the wrapped allocator is always invoked before any conditions are
+/// asserted by this class, with the exception of checking that a wrapped
+/// allocator has been set via `Initialize`. This allows the wrapped allocator
+/// to issue a more detailed error in case of misuse.
+class AllocatorMetricProxy : public Allocator {
+ public:
+ constexpr explicit AllocatorMetricProxy(metric::Token token)
+ : memusage_(token) {}
+
+ /// Sets the wrapped allocator.
+ ///
+ /// This must be called exactly once and before any other method.
+ ///
+ /// @param[in] allocator The allocator to wrap and record.
+ void Initialize(Allocator& allocator);
+
+ metric::Group& memusage() { return memusage_; }
+ size_t used() const { return used_.value(); }
+ size_t peak() const { return peak_.value(); }
+ size_t count() const { return count_.value(); }
+
+ private:
+ /// @copydoc Allocator::Query
+ Status DoQuery(const void* ptr, Layout layout) const override;
+
+ /// @copydoc Allocator::Allocate
+ void* DoAllocate(Layout layout) override;
+
+ /// @copydoc Allocator::Deallocate
+ void DoDeallocate(void* ptr, Layout layout) override;
+
+ /// @copydoc Allocator::Resize
+ bool DoResize(void* ptr, Layout layout, size_t new_size) override;
+
+ metric::Group memusage_;
+ Allocator* allocator_ = nullptr;
+ PW_METRIC(used_, "used", 0U);
+ PW_METRIC(peak_, "peak", 0U);
+ PW_METRIC(count_, "count", 0U);
+};
+
+} // namespace pw::allocator