CSCI 237

Computer Organization

Home | Schedule | Assignments | Links | Williams CS

Lab 6: Writing a Dynamic Storage Allocator

Assigned May 6, 2026
Prelim Due Date May 12, 2026 at 10:00pm. Submit your latest version of mm.c.
Final Due Date May 17, 2026 at 10:00pm. Submit your final version of mm.c.
Submissions Submit your solutions using submit237 6 mm.c.

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. This is a classic implementation problem with many interesting algorithms and opportunities to put several of the skills you have learned in this course to good use. It is tricky! Start early!

You may discuss your work with a partner on this lab. If you choose to discuss your work with a partner, make sure that both members of the group are contributing equally to the discussion. Moreover, each student must submit their own individual code that they wrote entirely by themselves.

Learning Objectives

Instructions

Repository Organization

Inside your repository, you will find a number of files. The only file you will modify and turn in is mm.c. However, you may find several of the files, including the short README.md file, quite useful to read.

Your dynamic storage allocator will consist of the following three functions (and several helper 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 starter mm.c file in your repository partially implements an allocator using an explicit free list. Your job is to complete this implementation by filling out mm_malloc and mm_free. The three main memory management functions should work as follows:

Your malloc implementation should always return 8-byte-aligned pointers.

Provided Code

The BlockInfo struct is designed to be used as a node in a doubly-linked explicit free list, and the following functions are available for manipulating free lists:

In addition, mm_init is already implemented, along with two helper functions that implement important parts of the allocator:

Finally, there are a number of C preprocessor macros to extract common pieces of code (constants, annoying casts/pointer manipulation) that might be prone to error. Each is documented in the code. You are welcome to create your own macros as well, though the ones already included in mm.c are the only ones used in our sample solutions, so it's possible without more. For more info on macros, check the GCC manual.

Additionally, for debugging purposes, you may want to print the contents of the heap. You can use the included examine_heap function to do that.

Memory System

The memlib.c package simulates the memory system for your dynamic memory allocator. In your allocator, you can call the following functions (if you use the provided code for an explicit free list, most uses of the memory system calls are already covered).

The Trace-driven Driver Program

The driver program mdriver.c tests your mm.c package for correctness, space utilization, and throughput. Use the command make to generate the driver code and run it with the command ./mdriver -V (the -V flag displays helpful summary information as described below).

The driver program is controlled by a set of trace files that it will expect to find in a subdirectory called traces. Your private repository's traces subdirectory is at the correct location relative to the driver. (If you want to move the trace files around, you can update the TRACEDIR path in config.h). Each trace file contains a sequence of allocate and free directions that instruct the driver to call your mm_malloc and mm_free routines in some sequence. The driver and the trace files are the same ones used when grading your submitted mm.c file.

The mdriver executable accepts the following command line arguments:

Programming Rules

Evaluation

Your grade will be calculated (as a percentage) out of a total of 55 points as follows:

Hints

Getting Started

Debugging