Simple Malloc and GC
Implementing a simple malloc() before implementing the GC
Each chunk in free list begins with a header:
typedef struct header {
unsigned int size; // Size of the chunk
struct header* next; // Next free block of memory
} header_t;
Maintain a used list to keep track of blocks in use. Items added to used list when removed
from the free list and vice-versa.
The heap starts at a low addreess from BSS and then grows till the “break”. To request more
memory from the kernel we call the “sbrk” system call to extend the “break” by the argument
given to the sys call and it returns the pointer to the previous break.
Two functions : morecore() and add_to_free_list()
moecore() will be called to request more memory from the kernel. Since the sys calls are
expensive, it will be done in page size chunks. Page is the smallest unit of virtual mem
that can be mapped to phsyical memory.
add_to_free_list() is used to add the newly requested/freed up memory to our free list
….
bp->size += p->next->size;
bp->next = p->next->next;