OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/heap_profiler_allocation_context.h" | 5 #include "base/trace_event/heap_profiler_allocation_context.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 | 8 |
9 #include "base/hash.h" | 9 #include "base/hash.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 namespace trace_event { | 13 namespace trace_event { |
14 | 14 |
15 bool operator < (const StackFrame& lhs, const StackFrame& rhs) { | |
16 return std::memcmp(&lhs, &rhs, sizeof(StackFrame)) < 0; | |
Primiano Tucci (use gerrit)
2016/04/14 10:15:33
can we just compare the value? We'll never have tw
| |
17 } | |
18 | |
19 bool operator == (const StackFrame& lhs, const StackFrame& rhs) { | |
20 return std::memcmp(&lhs, &rhs, sizeof(StackFrame)) == 0; | |
Primiano Tucci (use gerrit)
2016/04/14 10:15:33
same here
| |
21 } | |
22 | |
23 bool operator != (const StackFrame& lhs, const StackFrame& rhs) { | |
24 return !(lhs.value == rhs.value); | |
25 } | |
26 | |
15 // Constructor that does not initialize members. | 27 // Constructor that does not initialize members. |
16 AllocationContext::AllocationContext() {} | 28 AllocationContext::AllocationContext() {} |
17 | 29 |
18 // static | 30 // static |
19 AllocationContext AllocationContext::Empty() { | 31 AllocationContext AllocationContext::Empty() { |
20 AllocationContext ctx; | 32 AllocationContext ctx; |
21 | 33 |
22 for (size_t i = 0; i < arraysize(ctx.backtrace.frames); i++) | 34 ctx.backtrace.frame_count = 0; |
23 ctx.backtrace.frames[i] = nullptr; | |
Primiano Tucci (use gerrit)
2016/04/14 10:15:33
if you omit the null initialization of frames here
Dmitry Skiba
2016/04/15 07:01:39
Equality operator was changed to compare only fram
| |
24 | |
25 ctx.type_name = nullptr; | 35 ctx.type_name = nullptr; |
26 | 36 |
27 return ctx; | 37 return ctx; |
28 } | 38 } |
29 | 39 |
30 bool operator==(const Backtrace& lhs, const Backtrace& rhs) { | 40 bool operator==(const Backtrace& lhs, const Backtrace& rhs) { |
31 // Pointer equality of the stack frames is assumed, so instead of doing a deep | 41 if (lhs.frame_count != rhs.frame_count) return false; |
32 // string comparison on all of the frames, a |memcmp| suffices. | 42 return std::memcmp(lhs.frames, |
33 return std::memcmp(lhs.frames, rhs.frames, sizeof(lhs.frames)) == 0; | 43 rhs.frames, |
44 lhs.frame_count * sizeof(StackFrame)) == 0; | |
34 } | 45 } |
35 | 46 |
36 bool operator==(const AllocationContext& lhs, const AllocationContext& rhs) { | 47 bool operator==(const AllocationContext& lhs, const AllocationContext& rhs) { |
37 return (lhs.backtrace == rhs.backtrace) && (lhs.type_name == rhs.type_name); | 48 return (lhs.backtrace == rhs.backtrace) && (lhs.type_name == rhs.type_name); |
38 } | 49 } |
39 | 50 |
40 } // namespace trace_event | 51 } // namespace trace_event |
41 } // namespace base | 52 } // namespace base |
42 | 53 |
43 namespace BASE_HASH_NAMESPACE { | 54 namespace BASE_HASH_NAMESPACE { |
44 using base::trace_event::AllocationContext; | 55 using base::trace_event::AllocationContext; |
45 using base::trace_event::Backtrace; | 56 using base::trace_event::Backtrace; |
57 using base::trace_event::StackFrame; | |
58 | |
59 size_t hash<StackFrame>::operator()(const StackFrame& frame) const { | |
60 return base::SuperFastHash(reinterpret_cast<const char*>(&frame), | |
Primiano Tucci (use gerrit)
2016/04/14 10:15:33
as above, here i'd just return std::hash of the |v
| |
61 sizeof(StackFrame)); | |
62 } | |
46 | 63 |
47 size_t hash<Backtrace>::operator()(const Backtrace& backtrace) const { | 64 size_t hash<Backtrace>::operator()(const Backtrace& backtrace) const { |
48 return base::SuperFastHash(reinterpret_cast<const char*>(backtrace.frames), | 65 return base::SuperFastHash(reinterpret_cast<const char*>(backtrace.frames), |
49 sizeof(backtrace.frames)); | 66 backtrace.frame_count * sizeof(StackFrame)); |
50 } | 67 } |
51 | 68 |
52 size_t hash<AllocationContext>::operator()(const AllocationContext& ctx) const { | 69 size_t hash<AllocationContext>::operator()(const AllocationContext& ctx) const { |
53 size_t backtrace_hash = hash<Backtrace>()(ctx.backtrace); | 70 size_t backtrace_hash = hash<Backtrace>()(ctx.backtrace); |
54 | 71 |
55 // Multiplicative hash from [Knuth 1998]. Works best if |size_t| is 32 bits, | 72 // Multiplicative hash from [Knuth 1998]. Works best if |size_t| is 32 bits, |
56 // because the magic number is a prime very close to 2^32 / golden ratio, but | 73 // because the magic number is a prime very close to 2^32 / golden ratio, but |
57 // will still redistribute keys bijectively on 64-bit architectures because | 74 // will still redistribute keys bijectively on 64-bit architectures because |
58 // the magic number is coprime to 2^64. | 75 // the magic number is coprime to 2^64. |
59 size_t type_hash = reinterpret_cast<size_t>(ctx.type_name) * 2654435761; | 76 size_t type_hash = reinterpret_cast<size_t>(ctx.type_name) * 2654435761; |
60 | 77 |
61 // Multiply one side to break the commutativity of +. Multiplication with a | 78 // Multiply one side to break the commutativity of +. Multiplication with a |
62 // number coprime to |numeric_limits<size_t>::max() + 1| is bijective so | 79 // number coprime to |numeric_limits<size_t>::max() + 1| is bijective so |
63 // randomness is preserved. | 80 // randomness is preserved. |
64 return (backtrace_hash * 3) + type_hash; | 81 return (backtrace_hash * 3) + type_hash; |
65 } | 82 } |
66 | 83 |
67 } // BASE_HASH_NAMESPACE | 84 } // BASE_HASH_NAMESPACE |
OLD | NEW |