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

Side by Side 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 comments about lack of thread safety 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_
7
8 #include <stddef.h>
9
10 #include <list>
11
12 #include "base/macros.h"
13 #include "base/move.h"
14 #include "components/metrics/leak_detector/custom_allocator.h"
15 #include "components/metrics/leak_detector/leak_detector_value_type.h"
16 #include "components/metrics/leak_detector/stl_allocator.h"
17
18 namespace metrics {
19 namespace leak_detector {
20
21 // RankedList lets you add entries consisting of a value-count pair, and
22 // automatically sorts them internally by count in descending order. This allows
23 // for the user of this list to put value-count pairs into this list without
24 // having to explicitly sort them by count.
25 class RankedList {
26 MOVE_ONLY_TYPE_FOR_CPP_03(RankedList, RValue);
27
28 public:
29 using ValueType = LeakDetectorValueType;
30
31 // A single entry in the RankedList. The RankedList sorts entries by |count|
32 // in descending order.
33 struct Entry {
34 ValueType value;
35 int count;
36
37 // Create a < comparator for reverse sorting.
38 bool operator<(Entry& entry) const { return count > entry.count; }
39 };
40
41 using EntryList = std::list<Entry, STLAllocator<Entry, CustomAllocator>>;
42 using const_iterator = EntryList::const_iterator;
43
44 explicit RankedList(size_t max_size) : max_size_(max_size) {}
45
46 // For move semantics.
47 RankedList(RValue other);
48 RankedList& operator=(RValue other);
49
50 // Accessors for begin() and end() const iterators.
51 const_iterator begin() const { return entries_.begin(); }
52 const_iterator end() const { return entries_.end(); }
53
54 size_t size() const { return entries_.size(); }
55 size_t max_size() const { return max_size_; }
56
57 // Add a new value-count pair to the list. Does not check for existing entries
58 // with the same value. Is an O(n) operation due to ordering.
59 void Add(const ValueType& value, int count);
60
61 private:
62 // Max and min counts. Returns 0 if the list is empty.
63 const int max_count() const {
64 return entries_.empty() ? 0 : entries_.begin()->count;
65 }
66 const int min_count() const {
67 return entries_.empty() ? 0 : entries_.rbegin()->count;
68 }
69
70 // Max number of items that can be stored in the list.
71 size_t max_size_;
72
73 // Points to the array of entries.
74 std::list<Entry, STLAllocator<Entry, CustomAllocator>> entries_;
75 };
76
77 } // namespace leak_detector
78 } // namespace metrics
79
80 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698