Machine problem 5: Dynamic memory allocation

Overview

In this lab you will be writing a dynamic storage allocator for C programs, i.e., your own version of the malloc and free routines. You are encouraged to explore the design space creatively and implement an allocator that is correct, efficient and fast.

Implementation Details

For this machine problem you'll be working in the "05-malloc" directory.

The only file you will be modifying is "mm.c". The "mdriver.c" program is a driver program that allows you to evaluate the performance of your solution. Use the command make to generate the driver code and run it with the command ./mdriver -V. (The -V flag displays helpful summary information.)

Your dynamic storage allocator will consist of the following four functions, which are declared in "mm.h" and defined in "mm.c".

int   mm_init(void);
void *mm_malloc(size_t size);
void  mm_free(void *ptr);
void *mm_realloc(void *ptr, size_t size);

The "mm.c" file we have given you implements the simplest but still functionally correct malloc package that we could think of. Using this as a starting place, modify these functions (and possibly define other private static functions), so that they obey the following semantics:

These semantics match the the semantics of the corresponding libc malloc, realloc, and free routines. Refer to the malloc manpage for complete documentation.

Heap Consistency Checker

Dynamic memory allocators are notoriously tricky beasts to program correctly and efficiently. They are difficult to program correctly because they involve a lot of untyped pointer manipulation. You will find it very helpful to write a heap checker that scans the heap and checks it for consistency.

Some examples of what a heap checker might check are:

Your heap checker will consist of the function int mm_check(void) in "mm.c". It will check any invariants or consistency conditions you consider prudent. It returns a nonzero value if and only if your heap is consistent. You are not limited to the listed suggestions nor are you required to check all of them. You are encouraged to print out error messages when mm_check fails.

This consistency checker is for your own debugging during development. When you submit "mm.c", make sure to remove any calls to mm_check as they will slow down your throughput.

Support Routines

Functions in "memlib.c" simulate the memory system for your dynamic memory allocator. You can invoke the following functions in "memlib.c":

The Trace-driven Driver Program

The driver program in "mdriver.c" tests your "mm.c" package for correctness, space utilization, and throughput. The driver program is controlled by a set of trace files, each of which contains a sequence of allocate, reallocate, and free directions that instruct the driver to call your mm_malloc, mm_realloc, and mm_free routines in some sequence. The driver and the trace files are the same ones we will use when we grade your submitted "mm.c" file.

mdriver accepts the following command line arguments:

Programming Rules

Grading

You will receive zero points if you break any of the rules or your code is buggy and crashes the driver. Otherwise, your grade will be calculated as follows:

Submission

To submit your work, commit all your changes to "malloc.c" and push to Github. Note that we will not be using any of the other files in your repository to evaluate your work (i.e., we will use a fresh set of supporting files), so be sure you're not relying on changes made outside "malloc.c"!

Hints