xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
beast
hash
xxhasher.h
1
#pragma once
2
3
#include <boost/endian/conversion.hpp>
4
5
#include <xxhash.h>
6
7
#include <
array
>
8
#include <
cstddef
>
9
#include <
cstdint
>
10
#include <
cstring
>
11
#include <
new
>
12
#include <
optional
>
13
#include <
span
>
14
#include <
type_traits
>
15
16
namespace
beast
{
17
18
class
Xxhasher
19
{
20
public
:
21
using
result_type
=
std::size_t
;
22
23
private
:
24
static_assert
(
sizeof
(
std::size_t
) == 8,
"requires 64-bit std::size_t"
);
25
// Have an internal buffer to avoid the streaming API
26
// A 64-byte buffer should to be big enough for us
27
static
constexpr
std::size_t
kInternalBufferSize
= 64;
28
29
alignas
(64)
std::array<std::uint8_t, kInternalBufferSize>
buffer_
{};
30
std::span<std::uint8_t>
readBuffer_
;
31
std::span<std::uint8_t>
writeBuffer_
;
32
33
std::optional<XXH64_hash_t>
seed_
;
34
XXH3_state_t*
state_
=
nullptr
;
35
36
void
37
resetBuffers
()
38
{
39
writeBuffer_
=
std::span
{
buffer_
};
40
readBuffer_
= {};
41
}
42
43
void
44
updateHash
(
void
const
* data,
std::size_t
len)
45
{
46
if
(
writeBuffer_
.size() < len)
47
{
48
flushToState
(data, len);
49
}
50
else
51
{
52
std::memcpy
(
writeBuffer_
.data(), data, len);
53
writeBuffer_
=
writeBuffer_
.subspan(len);
54
readBuffer_
=
std::span
{
std::begin
(
buffer_
),
buffer_
.size() -
writeBuffer_
.size()};
55
}
56
}
57
58
static
XXH3_state_t*
59
allocState
()
60
{
61
auto
ret = XXH3_createState();
62
if
(ret ==
nullptr
)
63
throw
std::bad_alloc
();
// LCOV_EXCL_LINE
64
return
ret;
65
}
66
67
void
68
flushToState
(
void
const
* data,
std::size_t
len)
69
{
70
if
(
state_
==
nullptr
)
71
{
72
state_
=
allocState
();
73
if
(
seed_
.has_value())
74
{
75
XXH3_64bits_reset_withSeed(
state_
, *
seed_
);
76
}
77
else
78
{
79
XXH3_64bits_reset(
state_
);
80
}
81
}
82
XXH3_64bits_update(
state_
,
readBuffer_
.data(),
readBuffer_
.size());
83
resetBuffers
();
84
if
((data !=
nullptr
) && (len != 0u))
85
{
86
XXH3_64bits_update(
state_
, data, len);
87
}
88
}
89
90
result_type
91
retrieveHash
()
92
{
93
if
(
state_
!=
nullptr
)
94
{
95
flushToState
(
nullptr
, 0);
96
return
XXH3_64bits_digest(
state_
);
97
}
98
99
if
(
seed_
.has_value())
100
{
101
return
XXH3_64bits_withSeed(
readBuffer_
.data(),
readBuffer_
.size(), *
seed_
);
102
}
103
104
return
XXH3_64bits(
readBuffer_
.data(),
readBuffer_
.size());
105
}
106
107
public
:
108
static
constexpr
auto
kEndian
= boost::endian::order::native;
109
110
Xxhasher
(
Xxhasher
const
&) =
delete
;
111
Xxhasher
&
112
operator=
(
Xxhasher
const
&) =
delete
;
113
114
Xxhasher
()
115
{
116
resetBuffers
();
117
}
118
119
~Xxhasher
() noexcept
120
{
121
if
(
state_
!=
nullptr
)
122
{
123
XXH3_freeState(
state_
);
124
}
125
}
126
127
template
<
class
Seed>
128
explicit
Xxhasher
(Seed seed)
129
requires
(
std::is_unsigned_v<Seed>
)
130
:
seed_
(seed)
131
{
132
resetBuffers
();
133
}
134
135
template
<
class
Seed>
136
Xxhasher
(Seed seed, Seed)
137
requires
(
std::is_unsigned_v<Seed>
)
138
:
seed_
(seed)
139
{
140
resetBuffers
();
141
}
142
143
void
144
operator()
(
void
const
* key,
std::size_t
len)
noexcept
145
{
146
updateHash
(key, len);
147
}
148
149
explicit
150
operator
result_type
() noexcept
151
{
152
return
retrieveHash
();
153
}
154
};
155
156
}
// namespace beast
array
std::bad_alloc
std::begin
T begin(T... args)
beast::Xxhasher::Xxhasher
Xxhasher()
Definition
xxhasher.h:114
beast::Xxhasher::kEndian
static constexpr auto kEndian
Definition
xxhasher.h:108
beast::Xxhasher::resetBuffers
void resetBuffers()
Definition
xxhasher.h:37
beast::Xxhasher::state_
XXH3_state_t * state_
Definition
xxhasher.h:34
beast::Xxhasher::writeBuffer_
std::span< std::uint8_t > writeBuffer_
Definition
xxhasher.h:31
beast::Xxhasher::buffer_
std::array< std::uint8_t, kInternalBufferSize > buffer_
Definition
xxhasher.h:29
beast::Xxhasher::readBuffer_
std::span< std::uint8_t > readBuffer_
Definition
xxhasher.h:30
beast::Xxhasher::seed_
std::optional< XXH64_hash_t > seed_
Definition
xxhasher.h:33
beast::Xxhasher::Xxhasher
Xxhasher(Seed seed)
Definition
xxhasher.h:128
beast::Xxhasher::retrieveHash
result_type retrieveHash()
Definition
xxhasher.h:91
beast::Xxhasher::operator=
Xxhasher & operator=(Xxhasher const &)=delete
beast::Xxhasher::updateHash
void updateHash(void const *data, std::size_t len)
Definition
xxhasher.h:44
beast::Xxhasher::Xxhasher
Xxhasher(Seed seed, Seed)
Definition
xxhasher.h:136
beast::Xxhasher::result_type
std::size_t result_type
Definition
xxhasher.h:21
beast::Xxhasher::Xxhasher
Xxhasher(Xxhasher const &)=delete
beast::Xxhasher::flushToState
void flushToState(void const *data, std::size_t len)
Definition
xxhasher.h:68
beast::Xxhasher::allocState
static XXH3_state_t * allocState()
Definition
xxhasher.h:59
beast::Xxhasher::kInternalBufferSize
static constexpr std::size_t kInternalBufferSize
Definition
xxhasher.h:27
beast::Xxhasher::~Xxhasher
~Xxhasher() noexcept
Definition
xxhasher.h:119
beast::Xxhasher::operator()
void operator()(void const *key, std::size_t len) noexcept
Definition
xxhasher.h:144
cstddef
cstdint
cstring
std::is_unsigned_v
T is_unsigned_v
std::memcpy
T memcpy(T... args)
beast
Definition
base_uint.h:697
new
optional
std::size_t
span
beast::result_type
type_traits
Generated by
1.17.0