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_MANAGER_H_ |
| 6 #define COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_MANAGER_H_ |
| 7 |
| 8 #include <gperftools/custom_allocator.h> |
| 9 #include <stdint.h> |
| 10 |
| 11 #include <unordered_set> |
| 12 |
| 13 #include "base/macros.h" |
| 14 #include "components/metrics/leak_detector/stl_allocator.h" |
| 15 |
| 16 namespace leak_detector { |
| 17 |
| 18 // Struct to represent a call stack. |
| 19 struct CallStack { |
| 20 uint32_t depth; // Depth of current call stack. |
| 21 const void** stack; // Call stack as an array of addrs. |
| 22 |
| 23 size_t hash; // Hash of call stack. |
| 24 }; |
| 25 |
| 26 // Maintains and owns all unique call stack objects. |
| 27 class CallStackManager { |
| 28 public: |
| 29 CallStackManager(); |
| 30 ~CallStackManager(); |
| 31 |
| 32 // Returns a CallStack object for a given call stack. Each unique call stack |
| 33 // has its own CallStack object. If the given call stack has already been |
| 34 // created by a previous call to this function, return a pointer to that same |
| 35 // call stack object. |
| 36 // |
| 37 // Returns the call stacks as const pointers because no caller should take |
| 38 // ownership of them and modify or delete them. |
| 39 const CallStack* GetCallStack(int depth, const void* const stack[]); |
| 40 |
| 41 size_t size() const { |
| 42 return call_stacks_.size(); |
| 43 } |
| 44 |
| 45 private: |
| 46 // Allocator class for unique call stacks. |
| 47 using CallStackPointerAllocator = STL_Allocator<CallStack*, CustomAllocator>; |
| 48 |
| 49 // Used to compute hashes from call stack objects. Takes a pointer to a call |
| 50 // stack object as an argument. |
| 51 struct CallStackPointerHash { |
| 52 size_t operator() (const CallStack* call_stack) const; |
| 53 }; |
| 54 |
| 55 // Equality comparator for call stack objects. Takes pointers to call stack |
| 56 // objects as arguments. |
| 57 struct CallStackPointerEqual { |
| 58 bool operator() (const CallStack* c1, const CallStack* c2) const; |
| 59 }; |
| 60 |
| 61 // Holds all call stack objects. |
| 62 std::unordered_set<CallStack*, |
| 63 CallStackPointerHash, |
| 64 CallStackPointerEqual, |
| 65 CallStackPointerAllocator> call_stacks_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(CallStackManager); |
| 68 }; |
| 69 |
| 70 } // namespace leak_detector |
| 71 |
| 72 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_MANAGER_H_ |
OLD | NEW |