Leak.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef LIBMEMUNREACHABLE_LEAK_H_
  17. #define LIBMEMUNREACHABLE_LEAK_H_
  18. #include <functional>
  19. #include <vector>
  20. #include "memunreachable/memunreachable.h"
  21. // Custom std::hash specialization so that Leak::Backtrace can be used
  22. // as a key in std::unordered_map.
  23. namespace std {
  24. template <>
  25. struct hash<android::Leak::Backtrace> {
  26. std::size_t operator()(const android::Leak::Backtrace& key) const {
  27. std::size_t seed = 0;
  28. hash_combine(seed, key.num_frames);
  29. for (size_t i = 0; i < key.num_frames; i++) {
  30. hash_combine(seed, key.frames[i]);
  31. }
  32. return seed;
  33. }
  34. private:
  35. template <typename T>
  36. inline void hash_combine(std::size_t& seed, const T& v) const {
  37. std::hash<T> hasher;
  38. seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  39. }
  40. };
  41. } // namespace std
  42. namespace android {
  43. static bool operator==(const Leak::Backtrace& lhs, const Leak::Backtrace& rhs) {
  44. return (lhs.num_frames == rhs.num_frames) &&
  45. memcmp(lhs.frames, rhs.frames, lhs.num_frames * sizeof(lhs.frames[0])) == 0;
  46. }
  47. }
  48. #endif