On this page:
Getting Started
How to Work on the Lab
Support Functions
Tip:   Heap Consistency Checker
Trace-based Driver Program
Programming Rules
Evaluation
More Tips

Malloc Lab

This malloc lab is based on the one by Bryant and O’Hallaron for Computer Systems: A Programmer’s Perspective, Third Edition

Due: Wednesday, November 23, 11:59pm

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

Getting Started

Start by unpacking malloclab-handout.zip. The only file you will be modifying and handing in is "mm.c". The "mdriver.c" program is a driver program that allows you to evaluate the performance of your solution. Use make to generate the driver code and run it as

  $ ./mdriver -V

where the -V flag displays helpful summary information.

When you have completed the lab, you will hand in only one file, "mm.c", which contains your solution.

How to Work on the Lab

Your dynamic storage allocator will consist of the following three 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);

The "mm.c" file that we have given you implements the simplest but still functionally correct malloc implementation 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 corresponding libc malloc and free functions.

Beyond correctness, your goal is to produce an allocator that performs well in time and space. That is, the mm_malloc and mm_free functions should work as quickly as possible, and the total amount of memory used by your allocator should stay as close as possible to the amount of memory needed to hold the payload of mm_malloc calls not yet balanced by mm_free calls.

Support Functions

The "memlib.c" module provides a thin wrapper on the operating system’s virtual-memory system. Your allocator will need to use these functions to obtain pages of memory that it can allocate from. When mdriver runs your allocator, it resets the memory system (i.e., frees all pages) before calling mm_init to start a new benchmark run.

You can invoke the following functions from memlib.c:

While your allocator will obviously need to call mem_map to obtain memory for allocation, space-efficiency for this assignment also means using mem_unmap to avoid retaining pages that are not needed as allocated blocks are freed.

Tip: 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:

You may find it useful to insert a call to your consistency checking just before your mm_malloc or mm_free function returns. Before you submit "mm.c", however, make sure to remove any calls to your checker, since it will slow down throughput.

Trace-based Driver Program

The driver program "mdriver.c" tests your "mm.c" implementation for correctness, space utilization, and throughput. The driver program is controlled by a set of trace files that are included in the malloclab-handout.zip archive. Each trace file contains a sequence of allocate, free, and reallocate (i.e., allocate plus free) directions that instruct the driver to call your mm_malloc and mm_free functions in some sequence. The driver and the trace files are the same ones we will use when we grade your handin "mm.c" file, although we may change the order in which the traces are used.

The provided makefile combines "mdriver.c" with your "mm.c" to create mdriver, which accepts the following command line arguments:

You may find it useful to see the shape of memory use created by a trace file. Run

  $ racket plot.rkt tracefile

to see a plot of allocated memory over time for tracefile.

Programming Rules
Evaluation

Your grade will be calculated as follows:

More Tips