This post is part of a series on implementing the malloc()
and free()
functions. Previously, we implemented a rather simplistic approach that almost doesn’t free any memory: a pointer points to the last allocated block, enabling free()
to deallocate it, but only it.
A better option is to make the last block point to the second-to-last, the second-to-last block to the third-to-last, and so on, forming a linked list. To achieve this, we create a struct that will serve as the header of the blocks, containing a pointer to the previous block:
typedef struct Header {
struct Header *previous;
} Header;
Additionally, the pointer to the last block, which used to be void*
, is now of type Header*
:
Header *last = NULL;
To use these headers, abmalloc()
reserves enough memory to store both the header and the requested size:
void *abmalloc(size_t size) {
Header *header = sbrk(sizeof(Header) + size);
In this way, we use the beginning of the block to store necessary information, such as a pointer to the last allocated block before the new one:
header->previous = last;
Then, we update last
to point to the new block:
last = header;
Finally, we return a pointer to the memory that the user can use. Since header
points to the metadata, we cannot simply return it. Otherwise, all header information would be overwritten when the user used the pointer! Instead, we return a pointer to just after the header. This pointer is easy to calculate: it is the memory address of the header plus the size of the header:
return header + 1;
}
Note how we increment the header
pointer by 1. Since the pointer type is Header*
, the increment is actually the number of bytes of the Header
struct, not just one byte. The type of the pointer is very relevant in pointer arithmetic.
Now that our memory blocks have metadata at the beginning, we need to take this into account when deallocating. free()
receives a pointer not to the start of the block but to the memory made available to the user. Therefore, we need to find the start of the block from the pointer the user passed. Nothing that a little pointer arithmetic can’t solve:
void abfree(void *ptr) {
Header *header = (Header*) ptr - 1;
If header
points to the last allocated block, the previous block will become the last. In this case, we can return memory from the heap to the operating system through brk()
:
if (header == last) {
last = header->previous;
brk(header);
}
}
Here are our new malloc()
and free()
functions:
typedef struct Header {
struct Header *previous;
} Header;
Header *last = NULL;
void *abmalloc(size_t size) {
Header *header = sbrk(sizeof(Header) + size);
header->previous = last;
last = header;
return header + 1;
}
void abfree(void *ptr) {
Header *header = (Header*) ptr - 1;
if (header == last) {
last = header->previous;
brk(header);
}
}
abmalloc()
and abfree()
may be slightly more memory-efficient now, but not by much. Dynamically allocated memory rarely behaves like a stack, where the oldest block is always deallocated first. In the next post, we will see how to use the memory of older blocks that are no longer in use.
(This post is a translation of Implementando malloc() e free() — adicionando metadados aos blocos de memória, from Suspensão de Descrença.)