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 <stdint.h> |
| 9 |
| 10 #include <unordered_set> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "components/metrics/leak_detector/custom_allocator.h" |
| 14 #include "components/metrics/leak_detector/stl_allocator.h" |
| 15 |
| 16 namespace metrics { |
| 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 |
| 27 // Maintains and owns all unique call stack objects. |
| 28 class CallStackManager { |
| 29 public: |
| 30 CallStackManager(); |
| 31 ~CallStackManager(); |
| 32 |
| 33 // Returns a CallStack object for a given call stack. Each unique call stack |
| 34 // has its own CallStack object. If the given call stack has already been |
| 35 // created by a previous call to this function, return a pointer to that same |
| 36 // call stack object. |
| 37 // |
| 38 // Returns the call stacks as const pointers because no caller should take |
| 39 // ownership of them and modify or delete them. |
| 40 const CallStack* GetCallStack(int depth, const void* const stack[]); |
| 41 |
| 42 size_t size() const { |
| 43 return call_stacks_.size(); |
| 44 } |
| 45 |
| 46 private: |
| 47 // Allocator class for unique call stacks. |
| 48 using CallStackPointerAllocator = STLAllocator<CallStack*, CustomAllocator>; |
| 49 |
| 50 // Hash operator for call stack object given as a pointer. |
| 51 // Does not actually compute the hash. Instead, returns the already computed |
| 52 // hash value stored in a CallStack object. |
| 53 struct CallStackPointerStoredHash { |
| 54 size_t operator() (const CallStack* call_stack) const { |
| 55 return call_stack->hash; |
| 56 } |
| 57 }; |
| 58 |
| 59 // Equality comparator for call stack objects given as pointers. Compares |
| 60 // their stack trace contents. |
| 61 struct CallStackPointerEqual { |
| 62 bool operator() (const CallStack* c1, const CallStack* c2) const; |
| 63 }; |
| 64 |
| 65 // Holds all call stack objects. Each object is allocated elsewhere and stored |
| 66 // as a pointer because the container may rearrange itself internally. |
| 67 std::unordered_set<CallStack*, |
| 68 CallStackPointerStoredHash, |
| 69 CallStackPointerEqual, |
| 70 CallStackPointerAllocator> call_stacks_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(CallStackManager); |
| 73 }; |
| 74 |
| 75 } // namespace leak_detector |
| 76 } // namespace metrics |
| 77 |
| 78 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_MANAGER_H_ |
OLD | NEW |