OLD | NEW |
| (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 for (CallStack* call_stack : call_stacks_) { | |
20 CustomAllocator::Free(call_stack->stack, | |
21 call_stack->depth * sizeof(*call_stack->stack)); | |
22 call_stack->stack = nullptr; | |
23 call_stack->depth = 0; | |
24 | |
25 CustomAllocator::Free(call_stack, sizeof(CallStack)); | |
26 } | |
27 call_stacks_.clear(); | |
28 } | |
29 | |
30 const CallStack* CallStackManager::GetCallStack(size_t depth, | |
31 const void* const stack[]) { | |
32 // Temporarily create a call stack object for lookup in |call_stacks_|. | |
33 CallStack temp; | |
34 temp.depth = depth; | |
35 temp.stack = const_cast<const void**>(stack); | |
36 // This is the only place where the call stack's hash is computed. This value | |
37 // can be reused in the created object to avoid further hash computation. | |
38 temp.hash = | |
39 base::Hash(reinterpret_cast<const char*>(stack), sizeof(*stack) * depth); | |
40 | |
41 auto iter = call_stacks_.find(&temp); | |
42 if (iter != call_stacks_.end()) | |
43 return *iter; | |
44 | |
45 // Since |call_stacks_| stores CallStack pointers rather than actual objects, | |
46 // create new call objects manually here. | |
47 CallStack* call_stack = | |
48 new (CustomAllocator::Allocate(sizeof(CallStack))) CallStack; | |
49 call_stack->depth = depth; | |
50 call_stack->hash = temp.hash; // Don't run the hash function again. | |
51 call_stack->stack = 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, | |
61 const CallStack* c2) const { | |
62 return c1->depth == c2->depth && c1->hash == c2->hash && | |
63 std::equal(c1->stack, c1->stack + c1->depth, c2->stack); | |
64 } | |
65 | |
66 } // namespace leak_detector | |
67 } // namespace metrics | |
OLD | NEW |