heap_profiler_allocation_context.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "base/trace_event/heap_profiler_allocation_context.h"
  5. #include <cstring>
  6. #include "base/hash/hash.h"
  7. namespace base {
  8. namespace trace_event {
  9. bool operator < (const StackFrame& lhs, const StackFrame& rhs) {
  10. return lhs.value < rhs.value;
  11. }
  12. bool operator == (const StackFrame& lhs, const StackFrame& rhs) {
  13. return lhs.value == rhs.value;
  14. }
  15. bool operator != (const StackFrame& lhs, const StackFrame& rhs) {
  16. return !(lhs.value == rhs.value);
  17. }
  18. Backtrace::Backtrace() = default;
  19. bool operator==(const Backtrace& lhs, const Backtrace& rhs) {
  20. if (lhs.frame_count != rhs.frame_count) return false;
  21. return std::equal(lhs.frames, lhs.frames + lhs.frame_count, rhs.frames);
  22. }
  23. bool operator!=(const Backtrace& lhs, const Backtrace& rhs) {
  24. return !(lhs == rhs);
  25. }
  26. AllocationContext::AllocationContext(): type_name(nullptr) {}
  27. AllocationContext::AllocationContext(const Backtrace& backtrace,
  28. const char* type_name)
  29. : backtrace(backtrace), type_name(type_name) {}
  30. bool operator==(const AllocationContext& lhs, const AllocationContext& rhs) {
  31. return (lhs.backtrace == rhs.backtrace) && (lhs.type_name == rhs.type_name);
  32. }
  33. bool operator!=(const AllocationContext& lhs, const AllocationContext& rhs) {
  34. return !(lhs == rhs);
  35. }
  36. } // namespace trace_event
  37. } // namespace base
  38. namespace std {
  39. using base::trace_event::AllocationContext;
  40. using base::trace_event::Backtrace;
  41. using base::trace_event::StackFrame;
  42. size_t hash<StackFrame>::operator()(const StackFrame& frame) const {
  43. return hash<const void*>()(frame.value);
  44. }
  45. size_t hash<Backtrace>::operator()(const Backtrace& backtrace) const {
  46. const void* values[Backtrace::kMaxFrameCount];
  47. for (size_t i = 0; i != backtrace.frame_count; ++i) {
  48. values[i] = backtrace.frames[i].value;
  49. }
  50. return base::PersistentHash(values, backtrace.frame_count * sizeof(*values));
  51. }
  52. size_t hash<AllocationContext>::operator()(const AllocationContext& ctx) const {
  53. size_t backtrace_hash = hash<Backtrace>()(ctx.backtrace);
  54. // Multiplicative hash from [Knuth 1998]. Works best if |size_t| is 32 bits,
  55. // because the magic number is a prime very close to 2^32 / golden ratio, but
  56. // will still redistribute keys bijectively on 64-bit architectures because
  57. // the magic number is coprime to 2^64.
  58. size_t type_hash = reinterpret_cast<size_t>(ctx.type_name) * 2654435761;
  59. // Multiply one side to break the commutativity of +. Multiplication with a
  60. // number coprime to |numeric_limits<size_t>::max() + 1| is bijective so
  61. // randomness is preserved.
  62. return (backtrace_hash * 3) + type_hash;
  63. }
  64. } // namespace std