OLD | NEW |
| (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 <stdint.h> | |
9 | |
10 #include <functional> // For std::equal_to. | |
11 | |
12 #include "base/containers/hash_tables.h" | |
13 #include "base/macros.h" | |
14 #include "components/metrics/leak_detector/custom_allocator.h" | |
15 #include "components/metrics/leak_detector/leak_analyzer.h" | |
16 #include "components/metrics/leak_detector/stl_allocator.h" | |
17 | |
18 namespace metrics { | |
19 namespace leak_detector { | |
20 | |
21 struct CallStack; | |
22 | |
23 // Contains a hash table where the key is the call stack and the value is the | |
24 // number of allocations from that call stack. | |
25 // Not thread-safe. | |
26 class CallStackTable { | |
27 public: | |
28 struct StoredHash { | |
29 size_t operator()(const CallStack* call_stack) const; | |
30 }; | |
31 | |
32 explicit CallStackTable(int call_stack_suspicion_threshold); | |
33 ~CallStackTable(); | |
34 | |
35 // Add/Remove an allocation for the given call stack. | |
36 // Note that this class does NOT own the CallStack objects. Instead, it | |
37 // identifies different CallStacks by their hashes. | |
38 void Add(const CallStack* call_stack); | |
39 void Remove(const CallStack* call_stack); | |
40 | |
41 // Check for leak patterns in the allocation data. | |
42 void TestForLeaks(); | |
43 | |
44 const LeakAnalyzer& leak_analyzer() const { return leak_analyzer_; } | |
45 | |
46 size_t size() const { return entry_map_.size(); } | |
47 bool empty() const { return entry_map_.empty(); } | |
48 | |
49 uint32_t num_allocs() const { return num_allocs_; } | |
50 uint32_t num_frees() const { return num_frees_; } | |
51 | |
52 private: | |
53 // Hash table entry used to track allocation stats for a given call stack. | |
54 struct Entry { | |
55 // Net number of allocs (allocs minus frees). | |
56 uint32_t net_num_allocs; | |
57 }; | |
58 | |
59 // Total number of allocs and frees in this table. | |
60 uint32_t num_allocs_; | |
61 uint32_t num_frees_; | |
62 | |
63 // Hash table containing entries. | |
64 using TableEntryAllocator = | |
65 STLAllocator<std::pair<const CallStack*, Entry>, CustomAllocator>; | |
66 base::hash_map<const CallStack*, | |
67 Entry, | |
68 StoredHash, | |
69 std::equal_to<const CallStack*>, | |
70 TableEntryAllocator> entry_map_; | |
71 | |
72 // For detecting leak patterns in incoming allocations. | |
73 LeakAnalyzer leak_analyzer_; | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(CallStackTable); | |
76 }; | |
77 | |
78 } // namespace leak_detector | |
79 } // namespace metrics | |
80 | |
81 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_ | |
OLD | NEW |