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

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: Add OWNERS file Created 5 years, 1 month 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..a3b0b122c6b188b0c35ebbabeb3a31d2182301a0
--- /dev/null
+++ b/components/metrics/leak_detector/ranked_list.h
@@ -0,0 +1,90 @@
+// 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 <stddef.h>
+
+#include <list>
+
+#include "base/macros.h"
+#include "base/move.h"
+#include "components/metrics/leak_detector/custom_allocator.h"
+#include "components/metrics/leak_detector/leak_detector_value_type.h"
+#include "components/metrics/leak_detector/stl_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 metrics {
+namespace leak_detector {
+
+class RankedList {
Alexei Svitkine (slow) 2015/11/13 20:51:27 Add a comment explaing what this class is for.
Simon Que 2015/11/13 23:44:57 Done.
+ MOVE_ONLY_TYPE_FOR_CPP_03(RankedList, RValue);
+
+ public:
+ using ValueType = LeakDetectorValueType;
+
+ // A single entry in the RankedList. The RankedList sorts entries by |count|
+ // in descending order.
+ struct Entry {
+ ValueType value;
+ int count;
+
+ // Create a < comparator for reverse sorting.
+ bool operator< (Entry& entry) const {
+ return count > entry.count;
+ }
+ };
+
+ using EntryList = std::list<Entry, STLAllocator<Entry, CustomAllocator>>;
+ using const_iterator = EntryList::const_iterator;
+
+ explicit RankedList(size_t max_size) : max_size_(max_size) {}
+
+ // For move semantics.
+ RankedList(RValue other);
+ RankedList& operator= (RValue other);
+
+ // Accessors for begin() and end() const iterators.
+ const_iterator begin() const {
+ return entries_.begin();
+ }
+ const_iterator end() const {
+ return entries_.end();
+ }
+
+ size_t size() const {
+ return entries_.size();
+ }
+ size_t max_size() const {
+ return max_size_;
+ }
+
+ // Add a new value-count pair to the list. Does not check for existing entries
+ // with the same value. Is an O(n) operation due to ordering.
+ void Add(const ValueType& value, int count);
+
+ private:
+ // Max and min counts. Returns 0 if the list is empty.
+ const int max_count() const {
+ return entries_.empty() ? 0 : entries_.begin()->count;
+ }
+ const int min_count() const {
+ return entries_.empty() ? 0 : entries_.rbegin()->count;
+ }
+
+ // Max number of items that can be stored in the list.
+ size_t max_size_;
+
+ // Points to the array of entries.
+ std::list<Entry, STLAllocator<Entry, CustomAllocator>> entries_;
+};
+
+} // namespace leak_detector
+} // namespace metrics
+
+#endif // COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_

Powered by Google App Engine
This is Rietveld 408576698