]> git.ipfire.org Git - fireperf.git/commitdiff
random: Refactor to use a statically allocated pool
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 08:47:10 +0000 (08:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 08:47:10 +0000 (08:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/ctx.c
src/ctx.h
src/random.c
src/random.h

index 8f3277b2ebd01ee396f9322d8304b53f95aece2d..69f88926a4f8540d8102e855135e905f67bcc57b 100644 (file)
--- a/src/ctx.c
+++ b/src/ctx.c
@@ -28,7 +28,6 @@
 #include <unistd.h>
 
 #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);
 }
index a069f9ff3b4cf9e68a16375dc0d5de776edf8e63..28a2546aef4116f1dcdb8029a716c0a21bfc39b1 100644 (file)
--- 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;
index 86a99abee5e8f545b05663f1591cdba7f2a8dc52..da7b8d78f22939625d707c06a7d819c67b23bc2e 100644 (file)
 #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;
 }
index 1fd89195b14ffc902a5403a47bcfa0bcd92421a8..1f8f1a67fee0cd0a19d123121bbfc015b64d95ba 100644 (file)
 #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 */