I need to make a self made malloc simplified a bit. The memory pool is just an array of unsigned chars. I'm planning on having a struct that is a header, containing the size of the memory block, a flag ('A' for allocated, 'F' for free) and a pointer to the next header (though I guess this isn't necessary because you already have the length of the block)
Anywho, I have no idea how to easily store the struct inside the memory pool. This is my current code:
#include <stdlib.h>
#include <stdio.h>
#include "ex1.h"
#define POOL_SIZE 500
unsigned char mem_pool[POOL_SIZE];
unsigned char *ptr = &mem_pool[0];
typedef struct header Header;
Header h;
struct header{
int size;
char flag;
Header * next;
};
void ma_init(){
h = (Header){ .size = POOL_SIZE - sizeof(Header), .flag = 'F', .next = NULL};
}
Now ofcourse the h Header is not inside the mem_pool. Logically, int the initialisation it should be mem_pool[0] = (Header){ ..., since this is the first field of the mem_pool. Of course I could put pointers inside the array, but that would sort of ruin the aspec tof having a mem_pool.
So how should I store a struct (or whatever) in a certain location of the array?