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

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: Add missing dependency of unit tests on //base to BUILD.gn 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 #include <vector>
10
11 #include "base/hash.h"
12 #include "components/metrics/leak_detector/custom_allocator.h"
13
14 namespace metrics {
15 namespace leak_detector {
16
17 CallStackManager::CallStackManager() {}
18
19 CallStackManager::~CallStackManager() {
20 // Free all call stack objects and clear |call_stacks_|. Unfortunately,
21 // freeing an element of |call_stacks_| while iterating over it may result in
22 // undefined behavior since |call_stacks_| needs to access the memory pointed
23 // to by its members.
24
25 // Instead, copy all call stack objects' pointers into a local vector and
26 // clear |call_stacks_|. Then free the objects using the pointers stored in
27 // the local vector.
28 std::vector<CallStack*, CallStackPointerAllocator> temp_call_stacks;
29 temp_call_stacks.reserve(call_stacks_.size());
30 temp_call_stacks.assign(call_stacks_.begin(), call_stacks_.end());
31 call_stacks_.clear();
32
33 for (CallStack* call_stack : temp_call_stacks) {
34 CustomAllocator::Free(call_stack->stack,
35 call_stack->depth * sizeof(*call_stack->stack));
36 call_stack->stack = nullptr;
37 call_stack->depth = 0;
38
39 CustomAllocator::Free(call_stack, sizeof(CallStack));
40 }
41 }
42
43 const CallStack* CallStackManager::GetCallStack(size_t depth,
44 const void* const stack[]) {
45 // Temporarily create a call stack object for lookup in |call_stacks_|.
46 CallStack temp;
47 temp.depth = depth;
48 temp.stack = const_cast<const void**>(stack);
49 // This is the only place where the call stack's hash is computed. This value
50 // can be reused in the created object to avoid further hash computation.
51 temp.hash =
52 base::Hash(reinterpret_cast<const char*>(stack), sizeof(*stack) * depth);
53
54 auto iter = call_stacks_.find(&temp);
55 if (iter != call_stacks_.end())
56 return *iter;
57
58 // Since |call_stacks_| stores CallStack pointers rather than actual objects,
59 // create new call objects manually here.
60 CallStack* call_stack =
61 new (CustomAllocator::Allocate(sizeof(CallStack))) CallStack;
62 call_stack->depth = depth;
63 call_stack->hash = temp.hash; // Don't run the hash function again.
64 call_stack->stack = reinterpret_cast<const void**>(
65 CustomAllocator::Allocate(sizeof(*stack) * depth));
66 std::copy(stack, stack + depth, call_stack->stack);
67
68 call_stacks_.insert(call_stack);
69 return call_stack;
70 }
71
72 bool CallStackManager::CallStackPointerEqual::operator()(
73 const CallStack* c1,
74 const CallStack* c2) const {
75 return c1->depth == c2->depth && c1->hash == c2->hash &&
76 std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
77 }
78
79 } // namespace leak_detector
80 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698