For this assignment, you will be writing a memory allocation library called libmalloc. This library should provide basic heap memory management features.
The required interface for your library is as follows,
void *allocate(size_t) Returns a pointer to an available region of heap memory of a specified number of bytes. If the allocation fails for some reason, return NULL instead. This function should use the sbrk syscall to grow the heap as needed, and should prioritize allocating memory from the free list if possible. Returned pointers must be aligned to a 16 byte boundary and the allocated region must be within the programs heap. void release(void*) Frees a region of memory allocated by the allocate function by adding it to the free list. This function should also handle coalescing contiguous memory regions on the free list at the time of release. You may assume that the input argument is a pointer that has been returned by allocate, and has not already been released. typedef struct freelist_hdr freelist_hdr; A struct for the header of memory regions on the free list. This type should *not* be opaque externally to the library, and can be used for testing freelist_hdr *list_head() Returns a pointer to the head of the free list--can be used for testing
Beyond the required interface, there are a few constraints on how your implementation must function.
sbrk(0).init() function in its API, you will need to piggyback initialization on the first call to allocate(). Consider using a global variable to track whether the library has been initialized yet.Submissions will be graded on the following criteria,
| Criteria | Points |
| Basic allocation and release work correctly, without account for reuse of released blocks | 15 |
| Blocks are correctly reused from the free list when possible | 7 |
| Blocks in the free list are coalesced when possible | 3 |
| Blocks in the free list are split when possible | 5 |
| Criteria | Points |
| Testing code exists and runs | 2 |
| Testing code exists that leverages the list_head() function to validate the free list | 3 |
| Testing code validates allocating and releasing memory | 5 |
| Criteria | Points |
| Library is well organized, non-public functions and data are declared as static | 3 |
| Code is well documented using a combination of meaningful variable and function names, and comments | 2 |
| Include file contains comments describing the behavior of each function in the interface. | 5 |