rippled
Loading...
Searching...
No Matches
xxhasher.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of Beast: https://github.com/vinniefalco/Beast
4 Copyright 2014, Vinnie Falco <vinnie.falco@gmail.com>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef BEAST_HASH_XXHASHER_H_INCLUDED
21#define BEAST_HASH_XXHASHER_H_INCLUDED
22
23#include <boost/endian/conversion.hpp>
24
25#include <xxhash.h>
26
27#include <array>
28#include <cstddef>
29#include <cstdint>
30#include <optional>
31#include <span>
32
33namespace beast {
34
36{
37public:
39
40private:
41 static_assert(sizeof(std::size_t) == 8, "requires 64-bit std::size_t");
42 // Have an internal buffer to avoid the streaming API
43 // A 64-byte buffer should to be big enough for us
44 static constexpr std::size_t INTERNAL_BUFFER_SIZE = 64;
45
49
51 XXH3_state_t* state_ = nullptr;
52
53 void
59
60 void
61 updateHash(void const* data, std::size_t len)
62 {
63 if (writeBuffer_.size() < len)
64 {
65 flushToState(data, len);
66 }
67 else
68 {
69 std::memcpy(writeBuffer_.data(), data, len);
73 }
74 }
75
76 static XXH3_state_t*
78 {
79 auto ret = XXH3_createState();
80 if (ret == nullptr)
81 throw std::bad_alloc(); // LCOV_EXCL_LINE
82 return ret;
83 }
84
85 void
86 flushToState(void const* data, std::size_t len)
87 {
88 if (!state_)
89 {
91 if (seed_.has_value())
92 {
93 XXH3_64bits_reset_withSeed(state_, *seed_);
94 }
95 else
96 {
97 XXH3_64bits_reset(state_);
98 }
99 }
100 XXH3_64bits_update(state_, readBuffer_.data(), readBuffer_.size());
101 resetBuffers();
102 if (data && len)
103 {
104 XXH3_64bits_update(state_, data, len);
105 }
106 }
107
110 {
111 if (state_)
112 {
113 flushToState(nullptr, 0);
114 return XXH3_64bits_digest(state_);
115 }
116 else
117 {
118 if (seed_.has_value())
119 {
120 return XXH3_64bits_withSeed(
122 }
123 else
124 {
125 return XXH3_64bits(readBuffer_.data(), readBuffer_.size());
126 }
127 }
128 }
129
130public:
131 static constexpr auto const endian = boost::endian::order::native;
132
133 xxhasher(xxhasher const&) = delete;
134 xxhasher&
135 operator=(xxhasher const&) = delete;
136
138 {
139 resetBuffers();
140 }
141
142 ~xxhasher() noexcept
143 {
144 if (state_)
145 {
146 XXH3_freeState(state_);
147 }
148 }
149
150 template <
151 class Seed,
153 explicit xxhasher(Seed seed) : seed_(seed)
154 {
155 resetBuffers();
156 }
157
158 template <
159 class Seed,
161 xxhasher(Seed seed, Seed) : seed_(seed)
162 {
163 resetBuffers();
164 }
165
166 void
167 operator()(void const* key, std::size_t len) noexcept
168 {
169 updateHash(key, len);
170 }
171
172 explicit
173 operator result_type() noexcept
174 {
175 return retrieveHash();
176 }
177};
178
179} // namespace beast
180
181#endif
T begin(T... args)
XXH3_state_t * state_
Definition xxhasher.h:51
void flushToState(void const *data, std::size_t len)
Definition xxhasher.h:86
void updateHash(void const *data, std::size_t len)
Definition xxhasher.h:61
xxhasher(Seed seed, Seed)
Definition xxhasher.h:161
static constexpr auto const endian
Definition xxhasher.h:131
xxhasher & operator=(xxhasher const &)=delete
std::span< std::uint8_t > writeBuffer_
Definition xxhasher.h:48
static constexpr std::size_t INTERNAL_BUFFER_SIZE
Definition xxhasher.h:44
static XXH3_state_t * allocState()
Definition xxhasher.h:77
~xxhasher() noexcept
Definition xxhasher.h:142
std::span< std::uint8_t > readBuffer_
Definition xxhasher.h:47
result_type retrieveHash()
Definition xxhasher.h:109
std::size_t result_type
Definition xxhasher.h:38
void operator()(void const *key, std::size_t len) noexcept
Definition xxhasher.h:167
void resetBuffers()
Definition xxhasher.h:54
xxhasher(xxhasher const &)=delete
std::array< std::uint8_t, INTERNAL_BUFFER_SIZE > buffer_
Definition xxhasher.h:46
std::optional< XXH64_hash_t > seed_
Definition xxhasher.h:50
xxhasher(Seed seed)
Definition xxhasher.h:153
T data(T... args)
T memcpy(T... args)
T has_value(T... args)
T size(T... args)
T subspan(T... args)