From 221f4ad7ea257d7d65598e89799cb389d6d5932d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 19 Dec 2024 15:16:33 -0500 Subject: [PATCH] Default RNG now uses static, thread-local memory instead of heap allocated --- stdlib/rng.c | 4 ++-- stdlib/rng.h | 2 +- stdlib/stdlib.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/rng.c b/stdlib/rng.c index c6bcfa5..8e98aa8 100644 --- a/stdlib/rng.c +++ b/stdlib/rng.c @@ -18,14 +18,14 @@ #include "chacha.h" -public RNG_t default_rng = NULL; - struct RNGState_t { chacha_ctx chacha; size_t unused_bytes; uint8_t random_bytes[1024]; }; +public _Thread_local RNG_t default_rng = (struct RNGState_t[1]){}; + PUREFUNC static Text_t RNG$as_text(const void *rng, bool colorize, const TypeInfo_t*) { if (!rng) return Text("RNG"); diff --git a/stdlib/rng.h b/stdlib/rng.h index 89b5350..5bc4794 100644 --- a/stdlib/rng.h +++ b/stdlib/rng.h @@ -26,6 +26,6 @@ Num_t RNG$num(RNG_t rng, Num_t min, Num_t max); Num32_t RNG$num32(RNG_t rng, Num32_t min, Num32_t max); extern const TypeInfo_t RNG$info; -extern RNG_t default_rng; +extern _Thread_local RNG_t default_rng; // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/stdlib/stdlib.c b/stdlib/stdlib.c index c7f1c13..cb9d221 100644 --- a/stdlib/stdlib.c +++ b/stdlib/stdlib.c @@ -54,7 +54,7 @@ public void tomo_init(void) if (read(rng_fd, (void*)random_bytes, 40) < 40) fail("Couldn't read from /dev/urandom"); Array_t rng_seed = {.length=40, .data=random_bytes, .stride=1, .atomic=1}; - default_rng = RNG$new(rng_seed); + RNG$set_seed(default_rng, rng_seed); if (register_printf_specifier('k', printf_text, printf_text_size)) errx(1, "Couldn't set printf specifier");