Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions score/launch_manager/src/daemon/src/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cc_library(
include_prefix = "score/mw/launch_manager/common",
strip_include_prefix = "/score/launch_manager/src/daemon/src/common",
visibility = ["//score:__subpackages__"],
deps = ["@score_baselibs//score/language/futurecpp"],
)

lm_cc_test(
Expand Down
35 changes: 4 additions & 31 deletions score/launch_manager/src/daemon/src/common/identifier_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
********************************************************************************/

#include "score/mw/launch_manager/common/identifier_hash.hpp"
#include <score/hash.hpp>
#include <cstdint>
#include <mutex>
#include <unordered_map>
Expand Down Expand Up @@ -51,37 +52,9 @@ namespace score::mw::lifecycle
// - The probability of h(a) == h(b) for a != b should approach 1.0 / std::numeric_limits<std::size_t>::max().
// - This is small enough for initial implementation.

namespace
{

// Portable hash function FNV-1a https://www.rfc-editor.org/info/rfc9923/

#if SIZE_MAX == UINT32_MAX
constexpr std::size_t kFnvOffsetBasis = 2166136261U;
constexpr std::size_t kFnvPrime = 16777619U;
#elif SIZE_MAX == UINT64_MAX
constexpr std::size_t kFnvOffsetBasis = 14695981039346656037ULL;
constexpr std::size_t kFnvPrime = 1099511628211ULL;
#else
#error "FNV-1a constants are only defined here for 32-bit or 64-bit std::size_t"
#endif

std::size_t Fnv1aHash(std::string_view data) noexcept
{
std::size_t hash = kFnvOffsetBasis;
for (const unsigned char byte : data)
{
hash ^= static_cast<std::size_t>(byte);
hash *= kFnvPrime;
}
return hash;
}

} // namespace

IdentifierHash::IdentifierHash(std::string_view id)
{
hash_id_ = Fnv1aHash(id);
hash_id_ = score::cpp::hash_bytes_fnv1a<std::size_t>(id.data(), id.size());
Comment thread
MaciejKaszynski marked this conversation as resolved.
const std::lock_guard<std::mutex> lock(get_registry_mutex());
get_registry()[hash_id_] = id;
}
Expand All @@ -93,7 +66,7 @@ IdentifierHash::IdentifierHash(std::size_t hash_id)

std::optional<IdentifierHash> IdentifierHash::if_exists(std::string_view id)
{
const std::size_t hash_id = Fnv1aHash(id);
const std::size_t hash_id = score::cpp::hash_bytes_fnv1a<std::size_t>(id.data(), id.size());

const std::lock_guard<std::mutex> lock(get_registry_mutex());
const std::unordered_map<std::size_t, std::string>& registry = get_registry();
Expand Down Expand Up @@ -135,7 +108,7 @@ bool IdentifierHash::operator<(const IdentifierHash& other) const

IdentifierHash::IdentifierHash()
{
hash_id_ = Fnv1aHash(std::string_view(""));
hash_id_ = score::cpp::hash_bytes_fnv1a<std::size_t>("", 0U);
const std::lock_guard<std::mutex> lock(get_registry_mutex());
get_registry()[hash_id_] = "";
}
Expand Down
Loading