CSCI 237

Computer Organization

Home | Lectures | Labs | CS@Williams

Lab 6: Writing a Dynamic Storage Allocator

Assigned Apr 24/25, 2024
Prelim Due Date Apr 30/May 1, 2024 at 11:59pm. Submit your latest version of mm.c.
Final Due Date May 7/8, 2024 at 11:59pm. Submit your final version of mm.c.
Files lab6.tar
Submissions Submit your solutions using submit237 6 mm.c. If you work with a partner, only one submission per pair is required.

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 work with a partner on this lab. If you choose to work with a partner, make sure that both members of the group are contributing equally.

Learning Objectives

Instructions

Start by extracting lab6.tar to a directory where you plan to do your work:

	$ wget http://www.cs.williams.edu/~jeannie/cs237/labs/lab6/lab6.tar
	$ tar xvf lab6.tar

This will cause a number of files to be unpacked in a directory called lab6. The only file you will modify and turn in is mm.c. You may find the short README file 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 mm.c file I have given you 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

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

In addition, I implement mm_init and provide two helper functions implementing important parts of the allocator:

Finally, I use 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 I used in my sample solution, 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 in the lab6.tar distribution 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. The tar file provided to you should unpack into a directory structure that places the traces subdirectory in 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 I will use when I grade 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

Resources