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