Machine Problem 5: Implementing a Dynamic Memory Allocator

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.

Preliminaries

For this machine problem you'll be working in the mps/05 directory.

As before, don't forget to commit your previous work and pull the latest changes from the central repository before starting!

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.)

Implementation Details

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

The memlib.c package simulates 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 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.

The driver mdriver.c 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:

Hints