Computer Systems Programming

ECE454, Fall 2025
University of Toronto
Instructors: Ashvin Goel, Ding Yuan

    Computer Systems Programming

Lab 3: Dynamic Memory Allocation

Assigned: Oct 9, Due: Oct 26, 11:59 PM

The TAs for this lab are: Hang Yan, Kai Shen

Introduction

OptsRus is doing really well now and has been asked to create a dynamic memory allocator (e.g., malloc, free and realloc routines) for C programs for a new experimental operating system. You are asked to explore the design of allocators and then implement an allocator that is correct, efficient and fast. This is a difficult lab and you must plan your time wisely.

Setup

Start by copying the ece454-lab3.tar.gz file from the shared directory /cad2/ece454f/hw3/ on the UG machines into a protected directory within your UG home directory. Then run the command:

tar xzvf ece454-lab3.tar.gz

The mdriver program is a driver program that allows you to evaluate the performance of your solution. Use the make command to generate the driver code and run it with the command ./mdriver -V. The -V flag displays helpful summary information.

The only file you will be modifying and submitting is mm.c. In this file, please add the requested information in the team C structure so that the automarker can identify you. Do this right away so you don't forget.

Allocator API

Your dynamic memory 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 provided implements the simplest versions of these functions that are functionally correct. You need to modify these functions (and possibly define other private static functions), so that they obey the following semantics:

These semantics match the semantics of the corresponding malloc, realloc, and free routines in libc. Type man malloc in the shell for complete documentation.

Heap Consistency Checker

Dynamic memory allocators are notoriously tricky to program correctly and efficiently. They are difficult to program correctly because they involve a lot of untyped pointer manipulation. You will find it 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 should check any invariants or consistency conditions you consider important. It returns a non-zero value if and only if your heap is consistent. You are not limited to the listed suggestions and you are not 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 submitmm.c, make sure to comment out any calls to mm_check as they will lower throughput.

Support Routines

The memlib.c file simulates the memory system for your dynamic memory allocator. You can invoke the following functions declared in memlib.h:

The Driver Program

The mdriver driver program tests your mm.c implementation for correctness, memory utilization, and throughput. The driver program takes one or more trace files as input. A trace file contains a sequence of allocate, reallocate, and free requests that instruct the driver to call your mm_malloc, mm_realloc, and mm_free routines. The mdriver program accepts the following command line arguments:

Programming Rules

For more details, read the Lab 3 FAQ.

Evaluation

The total grade for this assignment is 100 points. You will receive zero points if you break any of the rules described above. Otherwise, your grade will be calculated based on two performance metrics:

The mdriver program outputs the performance of your allocator in terms of a weighted sum of memory utilization and throughput. It computes a performance index that lies between [0, 100] as follows:

P=⌊wU⌋ + ⌊(100-w) * min(1, T/Tc)⌋

where w is a weight, U is memory utilization, T is throughput, and Tc is the estimated throughput of the libc malloc implementation on your system for the default traces. We set w=60 to slightly favor memory utilization over throughput.

The driver computes the performance index P for each trace that passes validation. This value is then scaled down (linearly) by the fraction of traces that pass validation. For example, if half of the traces pass validation, P will be divided by two.

If your code is buggy and crashes the driver when running all traces together (i.e., without the -f option), you will get a zero mark. So make sure that your code does not break the driver.

Submission

Submit your assignment by typing submitece454f 3 mm.con one of the UG EECG machines. Do not submit any other files since we do not use them.

We will use an automarker for this lab to mark your lab (similar to Lab 2). The automarker URL will be provided on Piazza within a week of releasing this lab. The automarker may give a mark close to 100% but not 100%. Therefore, after the lab is done, we will increase everyone's mark by a constant score so that the top student gets 100% in this lab.