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

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: 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 #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 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 // This is the only place where the call stack's hash is computed. This value
35 // can be reused in the created object to avoid further hash computation.
36 temp.hash =
37 base::Hash(reinterpret_cast<const char*>(stack), sizeof(*stack) * depth);
38
39 auto iter = call_stacks_.find(&temp);
40 if (iter != call_stacks_.end())
41 return *iter;
42
43 // Since |call_stacks_| stores CallStack pointers rather than actual objects,
44 // create new call objects manually here.
45 CallStack* call_stack =
46 new(CustomAllocator::Allocate(sizeof(CallStack))) CallStack;
47 memset(call_stack, 0, sizeof(*call_stack));
48 call_stack->depth = depth;
49 call_stack->hash = temp.hash; // Don't run the hash function again.
50 call_stack->stack =
51 reinterpret_cast<const void**>(
52 CustomAllocator::Allocate(sizeof(*stack) * depth));
53 std::copy(stack, stack + depth, call_stack->stack);
54
55 call_stacks_.insert(call_stack);
56 return call_stack;
57 }
58
59 bool CallStackManager::CallStackPointerEqual::operator() (
60 const CallStack* c1, const CallStack* c2) const {
61 return c1->depth == c2->depth &&
62 std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
63 }
64
65 } // namespace leak_detector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698