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

Side by Side Diff: components/metrics/leak_detector/ranked_list.h

Issue 1472763003: Reland of: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years 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 "components/metrics/leak_detector/custom_allocator.h"
14 #include "components/metrics/leak_detector/leak_detector_value_type.h"
15 #include "components/metrics/leak_detector/stl_allocator.h"
16
17 namespace metrics {
18 namespace leak_detector {
19
20 // RankedList lets you add entries consisting of a value-count pair, and
21 // automatically sorts them internally by count in descending order. This allows
22 // for the user of this list to put value-count pairs into this list without
23 // having to explicitly sort them by count.
24 class RankedList {
25 public:
26 using ValueType = LeakDetectorValueType;
27
28 // A single entry in the RankedList. The RankedList sorts entries by |count|
29 // in descending order.
30 struct Entry {
31 ValueType value;
32 int count;
33
34 // Create a < comparator for reverse sorting.
35 bool operator<(const Entry& entry) const { return count > entry.count; }
36 };
37
38 // This class uses CustomAllocator to avoid recursive malloc hook invocation
39 // when analyzing allocs and frees.
40 using EntryList = std::list<Entry, STLAllocator<Entry, CustomAllocator>>;
41 using const_iterator = EntryList::const_iterator;
42
43 explicit RankedList(size_t max_size);
44 ~RankedList();
45
46 // For move semantics.
47 RankedList(RankedList&& other);
48 RankedList& operator=(RankedList&& 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 int max_count() const {
64 return entries_.empty() ? 0 : entries_.begin()->count;
65 }
66 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 DISALLOW_COPY_AND_ASSIGN(RankedList);
77 };
78
79 } // namespace leak_detector
80 } // namespace metrics
81
82 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_
OLDNEW
« no previous file with comments | « components/metrics/leak_detector/leak_detector_value_type.cc ('k') | components/metrics/leak_detector/ranked_list.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698