xrpld
Loading...
Searching...
No Matches
rngfill.h
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include <cstring>
6
7namespace beast {
8
9template <class Generator>
10void
11rngfill(void* const buffer, std::size_t const bytes, Generator& g)
12{
13 using result_type = Generator::result_type;
14 constexpr std::size_t kResultSize = sizeof(result_type);
15
16 auto* const bufferStart = static_cast<std::uint8_t*>(buffer);
17 std::size_t const completeIterations = bytes / kResultSize;
18 std::size_t const bytesRemaining = bytes % kResultSize;
19
20 for (std::size_t count = 0; count < completeIterations; ++count)
21 {
22 result_type const v = g();
23 std::size_t const offset = count * kResultSize;
24 std::memcpy(bufferStart + offset, &v, kResultSize);
25 }
26
27 if (bytesRemaining > 0)
28 {
29 result_type const v = g();
30 std::size_t const offset = completeIterations * kResultSize;
31 std::memcpy(bufferStart + offset, &v, bytesRemaining);
32 }
33}
34
35template <class Generator, std::size_t N>
36void
38 requires(N % sizeof(typename Generator::result_type) == 0)
39{
40 using result_type = Generator::result_type;
41 auto i = N / sizeof(result_type);
42 auto* p = reinterpret_cast<result_type*>(a.data());
43 while (i--)
44 *p++ = g();
45}
46
47} // namespace beast
T memcpy(T... args)
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
Definition rngfill.h:11