I am trying to write a custom allocator for debugging purposes (as an exercise) in C, where I will be using a single linked list to hold together the free list of memory using the First Fit Algorithm. I've shown below the structure I would like to create in an "Empty Memory Node".
How do I write the header block (a union to be specific) at the first few bytes of the memory, I obtain (I am using malloc() to initially get a chunk of memory) so that the remaining bytes are free?
This is the union I am using:
/*Define Header Structure for proper alignment*/
union header {
struct{
union header* next;
unsigned size ; /*Make it size_t*/
}s;
double dummy_align_var;
};
-------------------------------------------------------------------------------
|Next |Size of |16Byte| User is concerned only about |16Byte| |
|Free Memory |Allocated|Header| this portion of memory |Footer|Checksum |
|Address |Block |Picket| and has no knowledge of rest |Picket| |
-------------------------------------------------------------------------------
|-------Header---------| ^Address Returned to user
^------User Requested Size-----^
^-------------Memory Obtained From The Operating System-----------------------^
*/
[EDIT] Changed block structure according to suggestions provided.