blob: eaeeea7cfdd01499447d130e97dbb5b33793e308 [file] [log] [blame]
Colin Cross7a22e812016-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
29template<>
30struct hash<Leak::Backtrace> {
31 std::size_t operator()(const Leak::Backtrace& key) const {
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:
43 template<typename T>
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
52static bool operator==(const Leak::Backtrace& lhs, const Leak::Backtrace& rhs) {
53 return (lhs.num_frames == rhs.num_frames) &&
54 memcmp(lhs.frames, rhs.frames, lhs.num_frames * sizeof(lhs.frames[0])) == 0;
55}
56
57#endif