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