In this lab you will be writing a dynamic storage allocator for C programs, i.e., your own version of the malloc, free, and realloc routines. You are encouraged to explore the design space creatively and implement an allocator that is correct, efficient and fast.
You may work in a group of up to two people. Any clarifications and revisions to the assignment will be posted to Blackboard.
Start by copying /u/cs252/labs14/lab6/malloclab-handout.tar to a protected directory in which you plan to do your work. (You can also download it here.) Then give the command:
tar xvf malloclab-handout.tarThis will cause a bunch of files to be unpacked into the directory. 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 the command make to generate the driver code and run it with the command ./mdriver –V. (The –V flag displays helpful summary information.)
Looking at the file mm.c you’ll notice a C structure team into which you should insert the requested identifying information about the one or two individuals comprising your programming team. Do this right away so you don’t forget.
When you have completed the lab, you will hand in only one file (mm.c), which contains your solution.
WARNING: If you look, you can find solutions to this assignment on the web. So can we. Before you go looking, please review the (recently updated and clarified) course rules on academic honesty. If you would never think of cheating, please accept my apologies for including this note in the assignment. If you’re temped, please note that copied code has a very good chance of being noticed, at which point you will find yourself before a hearing of the College Board on Academic Honesty. The typical penalty for a first infraction is a zero on the assignment plus an additional reduction in the end-of-semester grade. The typical penalty for a second infraction is academic suspension.
Your dynamic storage allocator will consist of the following four functions, which are declared in mm.h and defined in mm.c.
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:
We will compare your implementation to the version of malloc supplied in the standard C library (libc). Since the libc malloc always returns payload pointers that are aligned to an 8 byte boundary, your malloc implementation should do likewise.
The contents of the new block are the same as those of the old block, up to the minimum of the old and new sizes. Everything else is uninitialized. For example, if the old block is 8 bytes and the new block is 12 bytes, then the first 8 bytes of the new block are identical to the first 8 bytes of the old block and the last 4 bytes are uninitialized (garbage). Similarly, if the old block is 8 bytes and the new block is 4 bytes, then the contents of the new block are identical to the first 4 bytes of the old block.
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. Style points will be given for your mm_check function. Make sure to put in comments and document what you are checking.
The memlib.c package simulates the OS portion of the memory system for your dynamic memory allocator. You can invoke the following functions in memlib.c:
-1on an allocation error, mem_sbrk returns
The driver program mdriver.c in the malloclab-handout.tar distribution tests your mm.c package for correctness, space utilization, and throughput. The driver program is controlled by a set of trace files. Some small traces are included in the malloclab-handout.tar distribution. The larger traces that we will test your file with are located at /u/cs252/labs14/malloctraces/. These larger traces are automatically run if you execute mdriver without a -f argument. Each trace file 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 mdriver.c accepts the following command line arguments:
The driver program summarizes the performance of your allocator by computing a performance index, P, which is a weighted sum of the space utilization and throughput
P = wU + (1−w) min (1, T / Tlibc)
where U is your space utilization, T is your throughput, and Tlibc is the estimated throughput of libc malloc on your system on the default traces. (The value for Tlibc is a constant in the driver (4000 Kops/s) that the TA established when he configured the release). The performance index favors space utilization over throughput, with a default of w = 0.6.
Observing that both memory and CPU cycles are expensive system resources, we adopt this formula to encourage balanced optimization of both memory utilization and throughput. Ideally, the performance index will reach P = w + (1−w) = 1 or 100%. Since each metric will contribute at most w and 1−w to the performance index, respectively, you should not go to extremes to optimize either the memory utilization or the throughput only. To receive a good score, you must achieve a balance between utilization and throughput.
You will be awarded 5 points for a good heap consistency checker and 5 points for good program structure and comments.
Before 11am on Thursday, April 17, send email to containing answers to the following questions (a single email per team is acceptable). Please use the subject
for your email.
[cs252] Assignment 7 Trivia - uname1, uname2
mdriver -V -f short2-bal.rep
The “trivia” assignment will be submitted via email. The main assignment will be submitted using the script /u/cs252/bin/TURN_IN.
You only need to turn in your completed mm.c. Watch Blackboard for details, and for any clarifications or revisions to the assignment.
Before running the TURN_IN script, be sure that you have
For the “trivia” assignment: noon, Thursday, April 17th.
For the main assignment: 11:59pm, Friday, May 2nd.