| Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 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 |  | 
 | 17 | #ifndef LIBMEMUNREACHABLE_LEAK_H_ | 
 | 18 | #define LIBMEMUNREACHABLE_LEAK_H_ | 
 | 19 |  | 
 | 20 | #include <functional> | 
 | 21 | #include <vector> | 
 | 22 |  | 
 | 23 | #include "memunreachable/memunreachable.h" | 
 | 24 |  | 
 | 25 | // Custom std::hash specialization so that Leak::Backtrace can be used | 
 | 26 | // as a key in std::unordered_map. | 
 | 27 | namespace std { | 
 | 28 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 29 | template <> | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 30 | struct hash<android::Leak::Backtrace> { | 
 | 31 |   std::size_t operator()(const android::Leak::Backtrace& key) const { | 
| Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 32 |     std::size_t seed = 0; | 
 | 33 |  | 
 | 34 |     hash_combine(seed, key.num_frames); | 
 | 35 |     for (size_t i = 0; i < key.num_frames; i++) { | 
 | 36 |       hash_combine(seed, key.frames[i]); | 
 | 37 |     } | 
 | 38 |  | 
 | 39 |     return seed; | 
 | 40 |   } | 
 | 41 |  | 
 | 42 |  private: | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 43 |   template <typename T> | 
| Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 44 |   inline void hash_combine(std::size_t& seed, const T& v) const { | 
 | 45 |     std::hash<T> hasher; | 
 | 46 |     seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); | 
 | 47 |   } | 
 | 48 | }; | 
 | 49 |  | 
 | 50 | }  // namespace std | 
 | 51 |  | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 52 | namespace android { | 
 | 53 |  | 
| Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 54 | static bool operator==(const Leak::Backtrace& lhs, const Leak::Backtrace& rhs) { | 
 | 55 |   return (lhs.num_frames == rhs.num_frames) && | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 56 |          memcmp(lhs.frames, rhs.frames, lhs.num_frames * sizeof(lhs.frames[0])) == 0; | 
| Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 57 | } | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 58 | } | 
| Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 59 |  | 
 | 60 | #endif |