From ecc5154a443f21ac6a99df4548f33a8a128ab802 Mon Sep 17 00:00:00 2001 From: Peter van der Perk Date: Mon, 3 May 2021 19:10:29 +0200 Subject: [PATCH] Reflect dynamically allocate block pointers instead of using heap --- src/systemcmds/reflect/reflect.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/systemcmds/reflect/reflect.c b/src/systemcmds/reflect/reflect.c index 9e1b2a1f94..4a89edff44 100644 --- a/src/systemcmds/reflect/reflect.c +++ b/src/systemcmds/reflect/reflect.c @@ -52,16 +52,16 @@ __EXPORT int reflect_main(int argc, char *argv[]); // memory corruption checking #define MAX_BLOCKS 1000 -static uint32_t nblocks; struct block { uint32_t v[256]; }; -static struct block *blocks[MAX_BLOCKS]; #define VALUE(i) ((i*7) ^ 0xDEADBEEF) -static void allocate_blocks(void) +static uint32_t allocate_blocks(struct block **blocks) { + uint32_t nblocks = 0; + while (nblocks < MAX_BLOCKS) { blocks[nblocks] = calloc(1, sizeof(struct block)); @@ -77,9 +77,11 @@ static void allocate_blocks(void) } printf("Allocated %u blocks\n", nblocks); + + return nblocks; } -static void check_blocks(void) +static void check_blocks(struct block **blocks, uint32_t nblocks) { for (uint32_t n = 0; n < nblocks; n++) { for (uint32_t i = 0; i < sizeof(blocks[nblocks]->v) / sizeof(uint32_t); i++) { @@ -92,9 +94,22 @@ int reflect_main(int argc, char *argv[]) { uint32_t total = 0; + uint32_t nblocks = 0; printf("Starting reflector\n"); - allocate_blocks(); + struct block **blocks = NULL; + blocks = malloc(sizeof(struct block *) * MAX_BLOCKS); + + if (blocks == NULL) { + return -1; + } + + while (nblocks < MAX_BLOCKS) { + blocks[nblocks] = NULL; + nblocks++; + } + + nblocks = allocate_blocks(blocks); while (true) { char buf[128]; @@ -111,7 +126,7 @@ reflect_main(int argc, char *argv[]) total += n; if (total > 1024000) { - check_blocks(); + check_blocks(blocks, nblocks); total = 0; } }