OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 _CALL_STACK_TABLE_H_ |
| 6 #define _CALL_STACK_TABLE_H_ |
| 7 |
| 8 #include <unordered_map> |
| 9 #include <functional> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/custom_allocator.h" |
| 13 #include "base/stl_allocator.h" |
| 14 #include "heap-profile-stats.h" |
| 15 #include "leak_analyzer.h" |
| 16 |
| 17 namespace leak_detector { |
| 18 |
| 19 // Contains a hash table where the key is the call stack and the value is the |
| 20 // number of allocations from that call stack. |
| 21 class CallStackTable { |
| 22 public: |
| 23 // Represents a unique call stack. |
| 24 using CallStack = HeapProfileBucket; |
| 25 |
| 26 struct Hash { |
| 27 long operator() (const CallStack* call_stack) const { |
| 28 // The call stack object should already have a hash computed when it was |
| 29 // created. |
| 30 return call_stack->hash; |
| 31 } |
| 32 }; |
| 33 |
| 34 CallStackTable(); |
| 35 ~CallStackTable(); |
| 36 |
| 37 // Add/Remove an allocation for the given call stack. |
| 38 void Add(const CallStack* call_stack); |
| 39 void Remove(const CallStack* call_stack); |
| 40 |
| 41 // Dump contents to log buffer |buffer| of size |size|. |
| 42 int Dump(char* buffer, int size) const; |
| 43 |
| 44 // Check for leak patterns in the allocation data. |
| 45 void TestForLeaks(); |
| 46 |
| 47 const LeakAnalyzer& leak_analyzer() const { |
| 48 return leak_analyzer_; |
| 49 } |
| 50 |
| 51 private: |
| 52 // Hash table entry used to track number of allocs and frees for a call stack. |
| 53 // Do not use the fields of struct HeapProfileBucket for this purpose; one |
| 54 // call stack object can have different entries in multiple CallStackTables. |
| 55 struct Entry { |
| 56 // Number of allocs/frees for the call stack. |
| 57 uint32 num_allocs; |
| 58 uint32 num_frees; |
| 59 }; |
| 60 |
| 61 // The total number of recorded allocations when this object was created. This |
| 62 // acts as a timestamp of sorts. |
| 63 uint64_t total_num_allocs_at_creation_; |
| 64 |
| 65 // Total number of allocs and frees in this table. |
| 66 uint32 num_allocs_; |
| 67 uint32 num_frees_; |
| 68 |
| 69 // Hash table containing entries. |
| 70 using TableEntryAllocator = |
| 71 STL_Allocator<std::pair<const CallStack*, Entry>, CustomAllocator>; |
| 72 std::unordered_map<const CallStack*, |
| 73 Entry, |
| 74 Hash, |
| 75 std::equal_to<const CallStack*>, |
| 76 TableEntryAllocator> entry_map_; |
| 77 |
| 78 // For detecting leak patterns in incoming allocations. |
| 79 LeakAnalyzer leak_analyzer_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(CallStackTable); |
| 82 }; |
| 83 |
| 84 } // namespace leak_detector |
| 85 |
| 86 #endif // _CALL_STACK_TABLE_H_ |
OLD | NEW |