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