From: Michael Tremer Date: Thu, 19 Sep 2024 08:47:10 +0000 (+0000) Subject: random: Refactor to use a statically allocated pool X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=d73a985d9adcdbd73f8338e928e9b2310cbe3815;p=fireperf.git random: Refactor to use a statically allocated pool Signed-off-by: Michael Tremer --- diff --git a/src/ctx.c b/src/ctx.c index 8f3277b..69f8892 100644 --- a/src/ctx.c +++ b/src/ctx.c @@ -28,7 +28,6 @@ #include #include "ctx.h" -#include "logging.h" #include "random.h" static int parse_address(const char* string, struct in6_addr* address6) { @@ -262,12 +261,9 @@ int fireperf_ctx_create(struct fireperf_ctx** ctx, int argc, char* argv[]) { // Initialize random pool if (!c->zero) { - c->pool = fireperf_random_pool_create(c, DEFAULT_RANDOM_POOL_SIZE); - if (!c->pool) { - ERROR(c, "Could not allocate random data\n"); - r = 1; + r = fireperf_random_pool_init(c); + if (r) goto ERROR; - } } // Return the context @@ -283,8 +279,5 @@ ERROR: } void fireperf_ctx_free(struct fireperf_ctx* ctx) { - if (ctx->pool) - fireperf_random_pool_free(ctx->pool); - free(ctx); } diff --git a/src/ctx.h b/src/ctx.h index a069f9f..28a2546 100644 --- a/src/ctx.h +++ b/src/ctx.h @@ -26,7 +26,6 @@ #include "constants.h" // Forward declarations -struct fireperf_random_pool; struct fireperf_worker; struct fireperf_ctx { @@ -42,7 +41,6 @@ struct fireperf_ctx { int keepalive_only; int keepalive_count; int keepalive_interval; - struct fireperf_random_pool* pool; int port; unsigned int listening_sockets; unsigned long parallel; @@ -50,6 +48,9 @@ struct fireperf_ctx { int close; int zero; + // Random Pool + char pool[DEFAULT_RANDOM_POOL_SIZE]; + // Workers struct fireperf_workers* workers[MAX_WORKERS]; unsigned int num_workers; diff --git a/src/random.c b/src/random.c index 86a99ab..da7b8d7 100644 --- a/src/random.c +++ b/src/random.c @@ -25,47 +25,25 @@ #include "logging.h" #include "random.h" -struct fireperf_random_pool* fireperf_random_pool_create(struct fireperf_ctx* ctx, size_t size) { - struct fireperf_random_pool* pool = calloc(1, sizeof(*pool)); - if (!pool) - return NULL; - - pool->size = size; - - // Allocate the data array - pool->data = malloc(pool->size); - if (!pool->data) - goto ERROR; - +int fireperf_random_pool_init(struct fireperf_ctx* ctx) { size_t offset = 0; - while (offset < pool->size) { - offset += getrandom(pool->data + offset, pool->size - offset, 0); - } - DEBUG(ctx, "Allocated random pool of %zu bytes(s)\n", pool->size); - - return pool; - -ERROR: - fireperf_random_pool_free(pool); - - return NULL; -} + while (offset < sizeof(ctx->pool)) { + offset += getrandom(ctx->pool + offset, sizeof(ctx->pool) - offset, 0); + } -void fireperf_random_pool_free(struct fireperf_random_pool* pool) { - if (pool->data) - free(pool->data); + DEBUG(ctx, "Allocated random pool of %zu bytes(s)\n", sizeof(ctx->pool)); - free(pool); + return 0; } -const char* fireperf_random_pool_get_slice(struct fireperf_random_pool* pool, size_t size) { - if (size > pool->size) +const char* fireperf_random_pool_get_slice(struct fireperf_ctx* ctx, size_t size) { + if (size > sizeof(ctx->pool)) return NULL; // Find a random value between the start and end of // the data region that is at least size bytes long. - off_t offset = random() % (pool->size - size); + off_t offset = random() % (sizeof(ctx->pool) - size); - return pool->data + offset; + return ctx->pool + offset; } diff --git a/src/random.h b/src/random.h index 1fd8919..1f8f1a6 100644 --- a/src/random.h +++ b/src/random.h @@ -21,17 +21,10 @@ #ifndef FIREPERF_RANDOM_H #define FIREPERF_RANDOM_H -struct fireperf_random_pool { - char* data; - size_t size; -}; - #include "ctx.h" -struct fireperf_random_pool* fireperf_random_pool_create( - struct fireperf_ctx* ctx, size_t size); -void fireperf_random_pool_free(struct fireperf_random_pool* pool); +int fireperf_random_pool_init(struct fireperf_ctx* ctx); -const char* fireperf_random_pool_get_slice(struct fireperf_random_pool* pool, size_t size); +const char* fireperf_random_pool_get_slice(struct fireperf_ctx* ctx, size_t size); #endif /* FIREPERF_RANDOM_H */