Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: components/metrics/leak_detector/call_stack_manager.h

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Replace tcmalloc's wrappers with pthread_spinlock and standard allocator (no more tcmalloc changes) Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 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 // Hash operator for call stack object given as a pointer.
50 // Does not actually compute the hash. Instead, returns the already computed
51 // hash value stored in a CallStack object.
52 struct CallStackPointerStoredHash {
53 size_t operator() (const CallStack* call_stack) const {
54 return call_stack->hash;
55 }
56 };
57
58 // Equality comparator for call stack objects given as pointers. Compares
59 // their stack trace contents.
60 struct CallStackPointerEqual {
61 bool operator() (const CallStack* c1, const CallStack* c2) const;
62 };
63
64 // Holds all call stack objects. Each object is allocated elsewhere and stored
65 // as a pointer because the container may rearrange itself internally.
66 std::unordered_set<CallStack*,
67 CallStackPointerStoredHash,
68 CallStackPointerEqual,
69 CallStackPointerAllocator> call_stacks_;
70
71 DISALLOW_COPY_AND_ASSIGN(CallStackManager);
72 };
73
74 } // namespace leak_detector
75
76 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698