Skip to content

Commit 0090e7e

Browse files
committed
Extract hash functions into header
1 parent aef0d5e commit 0090e7e

2 files changed

Lines changed: 42 additions & 33 deletions

File tree

src/sentry/logging/sentry_godot_logger.cpp

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "sentry/logging/state.h"
55
#include "sentry/sentry_options.h"
66
#include "sentry/sentry_sdk.h"
7+
#include "sentry/util/hash.h"
78

89
#include <godot_cpp/classes/engine.hpp>
910
#include <godot_cpp/classes/resource_loader.hpp>
@@ -162,36 +163,6 @@ Vector<SentryEvent::StackFrame> _extract_error_stack_frames_from_backtraces(
162163
return frames;
163164
}
164165

165-
// FNV-1a hash - portable hash implementation that doesn't rely on std::hash<*>.
166-
// See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
167-
inline size_t _fnv1a_hash(const char *p_data, size_t p_len) {
168-
// FNV-1a constants for 64-bit or 32-bit depending on platform
169-
// See table: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV_hash_parameters
170-
constexpr size_t FNV_OFFSET_BASIS = sizeof(size_t) == 8 ? 14695981039346656037ULL : 2166136261U;
171-
constexpr size_t FNV_PRIME = sizeof(size_t) == 8 ? 1099511628211ULL : 16777619U;
172-
173-
size_t hash = FNV_OFFSET_BASIS;
174-
for (size_t i = 0; i < p_len; i++) {
175-
hash ^= static_cast<uint64_t>(static_cast<unsigned char>(p_data[i]));
176-
hash *= FNV_PRIME;
177-
}
178-
return hash;
179-
}
180-
181-
inline size_t _hash(std::string_view p_value) {
182-
return _fnv1a_hash(p_value.data(), p_value.size());
183-
}
184-
185-
inline size_t _hash(int p_value) {
186-
return p_value;
187-
}
188-
189-
template <typename T>
190-
inline void _hash_combine(std::size_t &p_hash, T p_value) {
191-
// NOTE: Hash combining technique, originally from boost.
192-
p_hash ^= _hash(p_value) + 0x9e3779b9 + (p_hash << 6) + (p_hash >> 2);
193-
}
194-
195166
String _strip_invisible(const String &p_text) {
196167
String result;
197168

@@ -240,9 +211,9 @@ std::size_t SentryGodotLogger::ErrorKeyHash::operator()(const ErrorKey &p_key) c
240211
std::string_view message_sv{ message_cstr.get_data() };
241212
std::string_view filename_sv{ filename_cstr.get_data() };
242213

243-
size_t hash_value = _hash(message_sv);
244-
_hash_combine(hash_value, filename_sv);
245-
_hash_combine(hash_value, p_key.line);
214+
size_t hash_value = sentry::util::hash(message_sv);
215+
sentry::util::hash_combine(hash_value, filename_sv);
216+
sentry::util::hash_combine(hash_value, p_key.line);
246217
return hash_value;
247218
}
248219

src/sentry/util/hash.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
#include <godot_cpp/core/defs.hpp>
4+
#include <string_view>
5+
6+
namespace sentry::util {
7+
8+
// FNV-1a hash - portable hash implementation that doesn't rely on std::hash<*>.
9+
// See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
10+
_FORCE_INLINE_ size_t fnv1a_hash(const char *p_data, size_t p_len) {
11+
// FNV-1a constants for 64-bit or 32-bit depending on platform
12+
// See table: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV_hash_parameters
13+
constexpr size_t FNV_OFFSET_BASIS = sizeof(size_t) == 8 ? 14695981039346656037ULL : 2166136261U;
14+
constexpr size_t FNV_PRIME = sizeof(size_t) == 8 ? 1099511628211ULL : 16777619U;
15+
16+
size_t hash = FNV_OFFSET_BASIS;
17+
for (size_t i = 0; i < p_len; i++) {
18+
hash ^= static_cast<uint64_t>(static_cast<unsigned char>(p_data[i]));
19+
hash *= FNV_PRIME;
20+
}
21+
return hash;
22+
}
23+
24+
_FORCE_INLINE_ size_t hash(std::string_view p_value) {
25+
return fnv1a_hash(p_value.data(), p_value.size());
26+
}
27+
28+
_FORCE_INLINE_ size_t hash(int p_value) {
29+
return p_value;
30+
}
31+
32+
template <class T>
33+
_FORCE_INLINE_ void hash_combine(std::size_t &p_hash, const T &p_value) {
34+
// NOTE: Hash combining technique, originally from boost.
35+
p_hash ^= hash(p_value) + 0x9e3779b9 + (p_hash << 6) + (p_hash >> 2);
36+
}
37+
38+
} //namespace sentry::util

0 commit comments

Comments
 (0)