Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Unified Diff: components/metrics/leak_detector/ranked_list.h

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make log printing more efficient; skip empty containers Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/metrics/leak_detector/ranked_list.h
diff --git a/components/metrics/leak_detector/ranked_list.h b/components/metrics/leak_detector/ranked_list.h
new file mode 100644
index 0000000000000000000000000000000000000000..4672d31af98cc7730e089f237f6a411d4b70f78b
--- /dev/null
+++ b/components/metrics/leak_detector/ranked_list.h
@@ -0,0 +1,72 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_
+#define COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_
+
+#include <stdint.h>
+
+#include <vector>
+
+#include "components/metrics/leak_detector/leak_detector_value_type.h"
+#include "components/metrics/leak_detector/stl_allocator.h"
+#include <gperftools/custom_allocator.h>
+
+// RankedList lets you add entries and automatically sorts them internally, so
+// they can be accessed in sorted order. The entries are stored as a vector
+// array.
+
+namespace leak_detector {
+
+class RankedList {
+ public:
+ using ValueType = LeakDetectorValueType;
+
+ // A single entry in the RankedList. The RankedList sorts entries by |count|.
+ struct Entry {
+ int count;
+ ValueType value;
+ };
+
+ explicit RankedList(size_t max_size) : max_size_(max_size) {
+ entries_.reserve(max_size + 1);
+ }
+ ~RankedList() {}
+
+ // Access individual element. Does not do boundary checking.
+ const Entry& entry(int index) const {
+ return entries_[index];
+ }
+
+ size_t size() const {
+ return entries_.size();
+ }
+
+ // Add a new value-count pair to the list. Does not check for existing entries
+ // with the same value.
+ void Add(const ValueType& value, int count);
+
+ private:
+ // Returns the position to insert a new value. Returns -1 if it cannot be
+ // inserted.
+ int GetInsertionIndex(int count) const;
+
+ // Max and min counts. Returns 0 if the list is empty.
+ const int max_count() const {
+ return entries_.empty() ? 0 : entries_[0].count;
+ }
+ const int min_count() const {
+ return entries_.empty() ? 0 : entries_[size() - 1].count;
+ }
+
+ // Max number of items in the list.
+ size_t max_size_;
+
+ // Points to the array of entries.
+ std::vector<Entry, STL_Allocator<Entry, CustomAllocator>> entries_;
+};
+
+} // namespace leak_detector
+
+#endif // COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_

Powered by Google App Engine
This is Rietveld 408576698