Reflect dynamically allocate block pointers instead of using heap

This commit is contained in:
Peter van der Perk
2021-05-03 19:10:29 +02:00
committed by Daniel Agar
parent cd2aceb363
commit ecc5154a44
+21 -6
View File
@@ -52,16 +52,16 @@ __EXPORT int reflect_main(int argc, char *argv[]);
// memory corruption checking // memory corruption checking
#define MAX_BLOCKS 1000 #define MAX_BLOCKS 1000
static uint32_t nblocks;
struct block { struct block {
uint32_t v[256]; uint32_t v[256];
}; };
static struct block *blocks[MAX_BLOCKS];
#define VALUE(i) ((i*7) ^ 0xDEADBEEF) #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) { while (nblocks < MAX_BLOCKS) {
blocks[nblocks] = calloc(1, sizeof(struct block)); blocks[nblocks] = calloc(1, sizeof(struct block));
@@ -77,9 +77,11 @@ static void allocate_blocks(void)
} }
printf("Allocated %u blocks\n", nblocks); 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 n = 0; n < nblocks; n++) {
for (uint32_t i = 0; i < sizeof(blocks[nblocks]->v) / sizeof(uint32_t); i++) { 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[]) reflect_main(int argc, char *argv[])
{ {
uint32_t total = 0; uint32_t total = 0;
uint32_t nblocks = 0;
printf("Starting reflector\n"); 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) { while (true) {
char buf[128]; char buf[128];
@@ -111,7 +126,7 @@ reflect_main(int argc, char *argv[])
total += n; total += n;
if (total > 1024000) { if (total > 1024000) {
check_blocks(); check_blocks(blocks, nblocks);
total = 0; total = 0;
} }
} }