blob: de64b6410b231bd12f62d5776a64b04e4f576de6 [file] [log] [blame]
Colin Cross783edb02016-03-04 16:36:12 -08001/*
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.
27namespace std {
28
Colin Cross401319a2017-06-22 10:50:05 -070029template <>
Colin Cross1fa81f52017-06-21 13:13:00 -070030struct hash<android::Leak::Backtrace> {
31 std::size_t operator()(const android::Leak::Backtrace& key) const {
Colin Cross783edb02016-03-04 16:36:12 -080032 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 Cross401319a2017-06-22 10:50:05 -070043 template <typename T>
Colin Cross783edb02016-03-04 16:36:12 -080044 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 Cross1fa81f52017-06-21 13:13:00 -070052namespace android {
53
Colin Cross783edb02016-03-04 16:36:12 -080054static bool operator==(const Leak::Backtrace& lhs, const Leak::Backtrace& rhs) {
55 return (lhs.num_frames == rhs.num_frames) &&
Colin Cross401319a2017-06-22 10:50:05 -070056 memcmp(lhs.frames, rhs.frames, lhs.num_frames * sizeof(lhs.frames[0])) == 0;
Colin Cross783edb02016-03-04 16:36:12 -080057}
Colin Cross1fa81f52017-06-21 13:13:00 -070058}
Colin Cross783edb02016-03-04 16:36:12 -080059
60#endif