#ifndef LLVM_LIBC_SRC___SUPPORT_HASHTABLE_RANDOMNESS_H
#define LLVM_LIBC_SRC___SUPPORT_HASHTABLE_RANDOMNESS_H
#include "src/__support/common.h"
#include "src/__support/hash.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
#if defined(LIBC_HASHTABLE_USE_GETRANDOM)
#include "src/errno/libc_errno.h"
#include "src/sys/random/getrandom.h"
#endif
namespace LIBC_NAMESPACE_DECL {
namespace internal {
namespace randomness {
LIBC_INLINE_VAR thread_local HashState state = {
0x38049a7ea6f5a79b, 0x45cb02147c3f718a, 0x53eb431c12770718,
0x5b55742bd20a2fcb};
LIBC_INLINE_VAR thread_local uint64_t counter = 0;
LIBC_INLINE_VAR constexpr uint64_t RESEED_PERIOD = 1024;
LIBC_INLINE uint64_t next_random_seed() {
if (counter % RESEED_PERIOD == 0) {
uint64_t entropy[2];
entropy[0] = reinterpret_cast<uint64_t>(&entropy);
entropy[1] = reinterpret_cast<uint64_t>(&state);
#if defined(LIBC_HASHTABLE_USE_GETRANDOM)
int errno_backup = libc_errno;
size_t count = sizeof(entropy);
uint8_t *buffer = reinterpret_cast<uint8_t *>(entropy);
while (count > 0) {
ssize_t len = getrandom(buffer, count, 0);
if (len == -1) {
if (libc_errno == ENOSYS)
break;
continue;
}
count -= len;
buffer += len;
}
libc_errno = errno_backup;
#endif
state.update(&entropy, sizeof(entropy));
}
state.update(&counter, sizeof(counter));
counter++;
return state.finish();
}
}
}
}
#endif