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 "base/containers/hash_tables.h" |
| 11 #include "base/macros.h" |
| 12 #include "components/metrics/leak_detector/custom_allocator.h" |
| 13 #include "components/metrics/leak_detector/stl_allocator.h" |
| 14 |
| 15 // Summary of structures: |
| 16 // |
| 17 // struct CallStack: |
| 18 // Represents a unique call stack, defined by its raw call stack (array of |
| 19 // pointers), and hash value. All CallStack objects are owned by class |
| 20 // CallStackManager. Other classes may hold pointers to them but should not |
| 21 // attempt to create or modify any CallStack objects. |
| 22 // |
| 23 // class CallStackManager: |
| 24 // Creates CallStack objects to represent unique call stacks. Owns all |
| 25 // CallStack objects that it creates, storing them internally. |
| 26 // |
| 27 // Returns the call stacks as const pointers because no caller should take |
| 28 // ownership of them and modify or delete them. The lifetime of all CallStack |
| 29 // objects is limited to that of the CallStackManager object that created |
| 30 // them. When the CallStackManager is destroyed, the CallStack objects will be |
| 31 // invalidated. Hence the caller should make sure that it does not use |
| 32 // CallStack objects beyond the lifetime of the CallStackManager that created |
| 33 // them. |
| 34 |
| 35 namespace metrics { |
| 36 namespace leak_detector { |
| 37 |
| 38 // Struct to represent a call stack. |
| 39 struct CallStack { |
| 40 // Call stack as an array of pointers, |stack|. The array length is stored in |
| 41 // |depth|. There is no null terminator. |
| 42 const void** stack; |
| 43 size_t depth; |
| 44 |
| 45 // Hash of call stack. It is cached here so that it doesn not have to be |
| 46 // recomputed each time. |
| 47 size_t hash; |
| 48 }; |
| 49 |
| 50 // Maintains and owns all unique call stack objects. |
| 51 // Not thread-safe. |
| 52 class CallStackManager { |
| 53 public: |
| 54 CallStackManager(); |
| 55 ~CallStackManager(); |
| 56 |
| 57 // Returns a CallStack object for a given raw call stack. The first time a |
| 58 // particular raw call stack is passed into this function, it will create a |
| 59 // new CallStack object to hold the raw call stack data, and then return it. |
| 60 // The CallStack object is stored internally. |
| 61 // |
| 62 // On subsequent calls with the same raw call stack, this function will return |
| 63 // the previously created CallStack object. |
| 64 const CallStack* GetCallStack(size_t depth, const void* const stack[]); |
| 65 |
| 66 size_t size() const { return call_stacks_.size(); } |
| 67 |
| 68 private: |
| 69 // Allocator class for unique call stacks. |
| 70 using CallStackPointerAllocator = STLAllocator<CallStack*, CustomAllocator>; |
| 71 |
| 72 // Hash operator for call stack object given as a pointer. |
| 73 // Does not actually compute the hash. Instead, returns the already computed |
| 74 // hash value stored in a CallStack object. |
| 75 struct CallStackPointerStoredHash { |
| 76 size_t operator()(const CallStack* call_stack) const { |
| 77 return call_stack->hash; |
| 78 } |
| 79 }; |
| 80 |
| 81 // Equality comparator for call stack objects given as pointers. Compares |
| 82 // their stack trace contents. |
| 83 struct CallStackPointerEqual { |
| 84 bool operator()(const CallStack* c1, const CallStack* c2) const; |
| 85 }; |
| 86 |
| 87 // Holds all call stack objects. Each object is allocated elsewhere and stored |
| 88 // as a pointer because the container may rearrange itself internally. |
| 89 base::hash_set<CallStack*, |
| 90 CallStackPointerStoredHash, |
| 91 CallStackPointerEqual, |
| 92 CallStackPointerAllocator> call_stacks_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(CallStackManager); |
| 95 }; |
| 96 |
| 97 } // namespace leak_detector |
| 98 } // namespace metrics |
| 99 |
| 100 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_MANAGER_H_ |
OLD | NEW |