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

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: Create CallStackManager out of LeakDetectorImpl Created 5 years, 3 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 #include "components/metrics/leak_detector/call_stack_manager.h"
6
7 #include <gperftools/custom_allocator.h>
8 #include <string.h> // For memset.
9
10 #include <algorithm> // For std::copy.
11 #include <new>
12
13 #include "base/hash.h"
14
15 namespace leak_detector {
16
17 CallStackManager::CallStackManager() {}
18
19 CallStackManager::~CallStackManager() {
20 for (CallStack* call_stack : call_stacks_) {
21 CustomAllocator::Free(call_stack->stack,
22 call_stack->depth * sizeof(*call_stack->stack));
23 CustomAllocator::Free(call_stack, sizeof(CallStack));
24 }
25 call_stacks_.clear();
26 }
27
28 const CallStack* CallStackManager::GetCallStack(
29 int depth, const void* const stack[]) {
30 // Temporarily create a call stack object for lookup in |call_stacks_|.
31 CallStack temp;
32 temp.depth = depth;
33 temp.stack = const_cast<const void**>(stack);
34
35 auto iter = call_stacks_.find(&temp);
36 if (iter != call_stacks_.end())
37 return *iter;
38
39 // Since |call_stacks_| stores CallStack pointers rather than actual objects,
40 // create new call objects manually here.
41 CallStack* call_stack =
42 new(CustomAllocator::Allocate(sizeof(CallStack))) CallStack;
43 memset(call_stack, 0, sizeof(*call_stack));
44 call_stack->depth = depth;
45 call_stack->hash = call_stacks_.hash_function()(&temp);
46 call_stack->stack =
47 reinterpret_cast<const void**>(
48 CustomAllocator::Allocate(sizeof(*stack) * depth));
49 std::copy(stack, stack + depth, call_stack->stack);
50
51 call_stacks_.insert(call_stack);
52 return call_stack;
53 }
54
55 size_t CallStackManager::CallStackPointerHash::operator() (
56 const CallStack* call_stack) const {
57 return base::Hash(reinterpret_cast<const char*>(call_stack->stack),
58 sizeof(*(call_stack->stack)) * call_stack->depth);
59 }
60
61 bool CallStackManager::CallStackPointerEqual::operator() (
62 const CallStack* c1, const CallStack* c2) const {
63 return c1->depth == c2->depth &&
64 std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
65 }
66
67 } // namespace leak_detector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698