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

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

Issue 1472763003: Reland of: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years 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 #include "components/metrics/leak_detector/call_stack_manager.h"
6
7 #include <algorithm> // For std::copy.
8 #include <new>
9
10 #include "base/hash.h"
11 #include "components/metrics/leak_detector/custom_allocator.h"
12
13 namespace metrics {
14 namespace leak_detector {
15
16 CallStackManager::CallStackManager() {}
17
18 CallStackManager::~CallStackManager() {
19 // Free all call stack objects and clear |call_stacks_|. Make sure to save the
20 // CallStack object pointer and remove it from the container before freeing
21 // the CallStack memory.
22 while (!call_stacks_.empty()) {
23 CallStack* call_stack = *call_stacks_.begin();
24 call_stacks_.erase(call_stacks_.begin());
25
26 CustomAllocator::Free(call_stack->stack,
27 call_stack->depth * sizeof(*call_stack->stack));
28 call_stack->stack = nullptr;
29 call_stack->depth = 0;
30
31 CustomAllocator::Free(call_stack, sizeof(CallStack));
32 }
33 }
34
35 const CallStack* CallStackManager::GetCallStack(size_t depth,
36 const void* const stack[]) {
37 // Temporarily create a call stack object for lookup in |call_stacks_|.
38 CallStack temp;
39 temp.depth = depth;
40 temp.stack = const_cast<const void**>(stack);
41 // This is the only place where the call stack's hash is computed. This value
42 // can be reused in the created object to avoid further hash computation.
43 temp.hash =
44 base::Hash(reinterpret_cast<const char*>(stack), sizeof(*stack) * depth);
45
46 auto iter = call_stacks_.find(&temp);
47 if (iter != call_stacks_.end())
48 return *iter;
49
50 // Since |call_stacks_| stores CallStack pointers rather than actual objects,
51 // create new call objects manually here.
52 CallStack* call_stack =
53 new (CustomAllocator::Allocate(sizeof(CallStack))) CallStack;
54 call_stack->depth = depth;
55 call_stack->hash = temp.hash; // Don't run the hash function again.
56 call_stack->stack = reinterpret_cast<const void**>(
57 CustomAllocator::Allocate(sizeof(*stack) * depth));
58 std::copy(stack, stack + depth, call_stack->stack);
59
60 call_stacks_.insert(call_stack);
61 return call_stack;
62 }
63
64 bool CallStackManager::CallStackPointerEqual::operator()(
65 const CallStack* c1,
66 const CallStack* c2) const {
67 return c1->depth == c2->depth && c1->hash == c2->hash &&
68 std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
69 }
70
71 } // namespace leak_detector
72 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/leak_detector/call_stack_manager.h ('k') | components/metrics/leak_detector/call_stack_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698