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

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

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: LeakDetectorImpl stores addrs as uintptr_t; Implement move semantics for RankedList 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 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_CALL_STACK_TABLE_H_
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_
7
8 #include <gperftools/custom_allocator.h>
9 #include <stdint.h>
10
11 #include <functional>
12 #include <unordered_map>
13
14 #include "components/metrics/leak_detector/leak_analyzer.h"
15 #include "components/metrics/leak_detector/stl_allocator.h"
16
17 namespace leak_detector {
18
19 // Struct to represent a call stack.
20 struct CallStack {
21 uint32_t depth; // Depth of current call stack.
22 const void** stack; // Call stack as an array of addrs.
23
24 size_t hash; // Hash of call stack.
25
26 // Generate hash from call stack.
27 struct ComputeHash {
28 size_t operator() (const CallStack* call_stack) const;
29 };
30 };
31
32 // Contains a hash table where the key is the call stack and the value is the
33 // number of allocations from that call stack.
34 class CallStackTable {
35 public:
36 struct StoredHash {
37 size_t operator() (const CallStack* call_stack) const {
38 // The call stack object should already have a hash computed when it was
39 // created.
40 //
41 // This is NOT the actual hash computation function for a new call stack.
42 return call_stack->hash;
43 }
44 };
45
46 explicit CallStackTable(int call_stack_suspicion_threshold);
47 ~CallStackTable();
48
49 // Add/Remove an allocation for the given call stack.
50 // Note that this class does NOT own the CallStack objects. Instead, it
51 // identifies different CallStacks by their hashes.
52 void Add(const CallStack* call_stack);
53 void Remove(const CallStack* call_stack);
54
55 // Dump contents to log buffer |buffer| of size |size|. Returns the number of
56 // bytes remaining in the buffer after writing to it. The number of bytes
57 // remaining includes the zero terminator that was just written, so this will
58 // always return at least 1, unless |size| == 0.
59 size_t Dump(const size_t buffer_size, char* buffer) const;
60
61 // Check for leak patterns in the allocation data.
62 void TestForLeaks();
63
64 const LeakAnalyzer& leak_analyzer() const {
65 return leak_analyzer_;
66 }
67
68 size_t size() const {
69 return entry_map_.size();
70 }
71 bool empty() const {
72 return entry_map_.empty();
73 }
74
75 uint32_t num_allocs() const {
76 return num_allocs_;
77 }
78 uint32_t num_frees() const {
79 return num_frees_;
80 }
81
82 private:
83 // Hash table entry used to track number of allocs and frees for a call stack.
84 struct Entry {
85 // Number of allocs minus number of frees for a given call stack.
86 uint32_t net_num_allocs;
87 };
88
89 // Total number of allocs and frees in this table.
90 uint32_t num_allocs_;
91 uint32_t num_frees_;
92
93 // Hash table containing entries.
94 using TableEntryAllocator =
95 STL_Allocator<std::pair<const CallStack*, Entry>, CustomAllocator>;
96 std::unordered_map<const CallStack*,
97 Entry,
98 StoredHash,
99 std::equal_to<const CallStack*>,
100 TableEntryAllocator> entry_map_;
101
102 // For detecting leak patterns in incoming allocations.
103 LeakAnalyzer leak_analyzer_;
104
105 DISALLOW_COPY_AND_ASSIGN(CallStackTable);
106 };
107
108 } // namespace leak_detector
109
110 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698