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

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

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Alexei's comments Created 5 years, 1 month 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 <string.h> // For memset.
8
9 #include <algorithm> // For std::copy.
10 #include <new>
11
12 #include "base/hash.h"
13 #include "components/metrics/leak_detector/custom_allocator.h"
14
15 namespace metrics {
16 namespace leak_detector {
17
18 CallStackManager::CallStackManager() {}
19
20 CallStackManager::~CallStackManager() {
21 for (CallStack* call_stack : call_stacks_) {
22 CustomAllocator::Free(call_stack->stack,
23 call_stack->depth * sizeof(*call_stack->stack));
jar (doing other things) 2015/11/14 02:58:35 nit: Perhaps a tiny comment indicating why the str
Simon Que 2015/11/17 00:28:47 They are just addresses into the stack segment --
24 CustomAllocator::Free(call_stack, sizeof(CallStack));
25 }
26 call_stacks_.clear();
27 }
28
29 const CallStack* CallStackManager::GetCallStack(
30 int depth, const void* const stack[]) {
31 // Temporarily create a call stack object for lookup in |call_stacks_|.
32 CallStack temp;
33 temp.depth = depth;
34 temp.stack = const_cast<const void**>(stack);
jar (doing other things) 2015/11/14 02:58:36 nit: perhaps it would be nicer to have declared th
Simon Que 2015/11/17 00:28:48 The problem is that we wouldn't be able to make a
35 // This is the only place where the call stack's hash is computed. This value
36 // can be reused in the created object to avoid further hash computation.
37 temp.hash =
38 base::Hash(reinterpret_cast<const char*>(stack), sizeof(*stack) * depth);
39
40 auto iter = call_stacks_.find(&temp);
41 if (iter != call_stacks_.end())
42 return *iter;
43
44 // Since |call_stacks_| stores CallStack pointers rather than actual objects,
45 // create new call objects manually here.
46 CallStack* call_stack =
jar (doing other things) 2015/11/14 02:58:36 nit: Would it have been nicer to declare CallStack
Simon Que 2015/11/17 00:28:47 Actually the memset is not necessary, removed.
47 new(CustomAllocator::Allocate(sizeof(CallStack))) CallStack;
48 memset(call_stack, 0, sizeof(*call_stack));
49 call_stack->depth = depth;
50 call_stack->hash = temp.hash; // Don't run the hash function again.
51 call_stack->stack =
52 reinterpret_cast<const void**>(
53 CustomAllocator::Allocate(sizeof(*stack) * depth));
54 std::copy(stack, stack + depth, call_stack->stack);
55
56 call_stacks_.insert(call_stack);
57 return call_stack;
58 }
59
60 bool CallStackManager::CallStackPointerEqual::operator() (
61 const CallStack* c1, const CallStack* c2) const {
62 return c1->depth == c2->depth &&
jar (doing other things) 2015/11/14 02:58:36 nit: Is it worth also validating that the hash mat
Simon Que 2015/11/17 00:28:48 Done.
63 std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
64 }
65
66 } // namespace leak_detector
67 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698