Small code refactoring.

This commit is contained in:
LoRd_MuldeR 2022-11-23 02:03:48 +01:00
parent 0feea09215
commit 8bb56b5a2a

View File

@ -34,21 +34,31 @@
unsigned char __stdcall RtlGenRandom(void *buffer, uint32_t length); unsigned char __stdcall RtlGenRandom(void *buffer, uint32_t length);
#endif #endif
static INLINE uint64_t random_uint64(void) typedef struct
{ {
static size_t offset = SIZE_MAX; size_t offset;
static uint64_t buffer[16U]; uint64_t buffer[16U];
}
random_t;
if (offset >= ARRAY_SIZE(buffer)) static INLINE void random_init(random_t *const rnd)
{
memset(rnd, 0, sizeof(random_t));
rnd->offset = SIZE_MAX;
}
static INLINE uint64_t random_next(random_t *const rnd)
{
if (rnd->offset >= ARRAY_SIZE(rnd->buffer))
{ {
offset = 0U; rnd->offset = 0U;
if (getentropy(&buffer, sizeof(buffer)) < 0) if (getentropy(rnd->buffer, sizeof(rnd->buffer)) < 0)
{ {
abort(); abort();
} }
} }
return buffer[offset++]; return rnd->buffer[rnd->offset++];
} }
#define PRINT_SET_INFO() do \ #define PRINT_SET_INFO() do \
@ -186,9 +196,12 @@ static int test_function_2(hash_set_t *const hash_set)
uint8_t spinner = 0U; uint8_t spinner = 0U;
clock_t last_update = clock(); clock_t last_update = clock();
random_t random;
random_init(&random);
for (;;) for (;;)
{ {
const uint64_t rnd = random_uint64() & UINT64_C(0x3FFFFFFFFFFFFFF); const uint64_t rnd = random_next(&random) & UINT64_C(0x3FFFFFFFFFFFFFF);
const errno_t error = hash_set_insert(hash_set, rnd); const errno_t error = hash_set_insert(hash_set, rnd);
if (error) if (error)
{ {