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

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: Use new move semantics 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 "base/move.h"
dcheng 2015/12/08 02:04:51 Remove this #include (see below for more info).
Simon Que 2015/12/08 02:24:16 Done.
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);
dcheng 2015/12/08 02:04:51 This macro is being removed in https://codereview.
Simon Que 2015/12/08 02:24:16 Done.
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<(const Entry& entry) const { return count > entry.count; }
39 };
40
41 // This class uses CustomAllocator to avoid recursive malloc hook invocation
42 // when analyzing allocs and frees.
43 using EntryList = std::list<Entry, STLAllocator<Entry, CustomAllocator>>;
44 using const_iterator = EntryList::const_iterator;
45
46 explicit RankedList(size_t max_size);
47 ~RankedList();
48
49 // For move semantics.
50 RankedList(RankedList&& other);
51 RankedList& operator=(RankedList&& other);
52
53 // Accessors for begin() and end() const iterators.
54 const_iterator begin() const { return entries_.begin(); }
55 const_iterator end() const { return entries_.end(); }
56
57 size_t size() const { return entries_.size(); }
58 size_t max_size() const { return max_size_; }
59
60 // Add a new value-count pair to the list. Does not check for existing entries
61 // with the same value. Is an O(n) operation due to ordering.
62 void Add(const ValueType& value, int count);
63
64 private:
65 // Max and min counts. Returns 0 if the list is empty.
66 int max_count() const {
67 return entries_.empty() ? 0 : entries_.begin()->count;
68 }
69 int min_count() const {
70 return entries_.empty() ? 0 : entries_.rbegin()->count;
71 }
72
73 // Max number of items that can be stored in the list.
74 size_t max_size_;
75
76 // Points to the array of entries.
77 std::list<Entry, STLAllocator<Entry, CustomAllocator>> entries_;
78 };
79
80 } // namespace leak_detector
81 } // namespace metrics
82
83 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698