C memory management implementation problem

Im pretty much trying to implement the 4 default C memory management functions
malloc, calloc, realloc and free, through the use of a freelist and memory chunks. However im having trouble figuring out how im meant to implement free. The idea behind my code is that when malloc/calloc is called, memory of desired size is allocated through a single or multiple chunks of variable size (64, 128, 256 etc. up to 4096 byte chunks with a maximum total of 1GB of memory for the entire heap) (And calloc initializes everything to 0) before these chunks lies a small amount of metadata which contains information regarding the chunk. If malloc/realloc is called and there is still memory available from the previous call, malloc/calloc will instead return the address of the first free available memory within that chunk. My freelist is a doubly linked list and keeps track of all the chunks allocated and when one is used up, they are removed from the freelist. Each chunk contains the address of where it begins (not where the metadata begins), its size, pointers to the next and prior chunks as well as a variable holding the size of the metadata. The issue im having, is i cannot figure out a way to implement free, because for free, its one single parameter is the address of something that was previously malloced/calloced and for something that was malloced and is at the beginning of the chunk, i can simply move the pointer back the exact amount of length to the beginning of the metadata as i know the size of the metadata and the metadata will contain information regarding the chunk. However if i have previously malloced something and there is still space remaining in the chunk, and i store something else within the chunk (kind of like storing a couple of strings in a chunk “side by side”) if i free any of the given strings, how do i know where in the chunk I am? How can i make my way to the metadata at the beginning of the chunk from where i can figure out how much i need to free
here are screenshots of my current implementations of the freelist and of the chunks, i havent started my free implementation as i have no idea where to really begin
(i cannot upload more than one image, so i have uploaded them to imgur, hopefully this link works)


Apologies for the poor structure of the topic, I’ve never written one of these before

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.