Download Assignment 1 for Dynamic Storage Allocator - Computer Organization | CSCE 230 and more Assignments Computer Architecture and Organization in PDF only on Docsity!
CSCE 230J, Spring 2004
Final Project: Malloc Lab: Writing a Dynamic Storage Allocator
Assigned: Thurs Apr. 8, Due: Friday Apr. 30, 09:00PM
Byron Blunk ([email protected]) is the lead person for this assignment.
1 Introduction
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.
2 Logistics
You may work in a group of up to three people. Any clarifications and revisions to the assignment will be posted on the course Web page.
3 Hand Out Instructions
Start by copying malloclab-handout.tar to a protected directory in which you plan to do your work. Then give the command: tar xvf malloclab-handout.tar. This will cause a number 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 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.
4 How to Work on the Lab
Your dynamic storage 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 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:
- mm init: Before calling mm malloc mm realloc or mm free, the application program (i.e., the trace-driven driver program that you will use to evaluate your implementation) calls mm init to perform any necessary initializations, such as allocating the initial heap area. The return value should be -1 if there was a problem in performing the initialization, 0 otherwise.
- mm malloc: The mm malloc routine returns a pointer to an allocated block payload of at least size bytes. The entire allocated block should lie within the heap region and should not overlap with any other allocated chunk. We will comparing 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 8 bytes, your malloc implementation should do likewise and always return 8-byte aligned pointers.
- mm free: The mm free routine frees the block pointed to by ptr. It returns nothing. This rou- tine is only guaranteed to work when the passed pointer (ptr) was returned by an earlier call to mm malloc or mm realloc and has not yet been freed.
- mm realloc: The mm realloc routine returns a pointer to an allocated region of at least size bytes with the following constraints. - if ptr is NULL, the call is equivalent to mm malloc(size); - if size is equal to zero, the call is equivalent to mm free(ptr); - if ptr is not NULL, it must have been returned by an earlier call to mm malloc or mm realloc. The call to mm realloc changes the size of the memory block pointed to by ptr (the old block ) to size bytes and returns the address of the new block. Notice that the address of the new block might be the same as the old block, or it might be different, depending on your imple- mentation, the amount of internal fragmentation in the old block, and the size of the realloc request. The contents of the new block are the same as those of the old ptr 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
- void *mem heap lo(void): Returns a generic pointer to the first byte in the heap.
- void *mem heap hi(void): Returns a generic pointer to the last byte in the heap.
- size t mem heapsize(void): Returns the current size of the heap in bytes.
- size t mem pagesize(void): Returns the system’s page size in bytes (4K on Linux systems).
7 The Trace-driven Driver Program
The driver program mdriver.c in the malloclab-handout.tar distribution tests your mm.c pack- age for correctness, space utilization, and throughput. The driver program is controlled by a set of trace files that are included in the malloclab-handout.tar distribution. 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 and the trace files are the same ones we will use when we grade your handin mm.c file.
The driver mdriver.c accepts the following command line arguments:
- -t : Look for the default trace files in directory tracedir instead of the default directory defined in config.h.
- -f : Use one particular tracefile for testing instead of the default set of trace- files.
- -h: Print a summary of the command line arguments.
- -l: Run and measure libc malloc in addition to the student’s malloc package.
- -v: Verbose output. Print a performance breakdown for each tracefile in a compact table.
- -V: More verbose output. Prints additional diagnostic information as each trace file is processed. Useful during debugging for determining which trace file is causing your malloc package to fail.
8 Programming Rules
- You should not change any of the interfaces in mm.c.
- You should not invoke any memory-management related library calls or system calls. This excludes the use of malloc, calloc, free, realloc, sbrk, brk or any variants of these calls in your code.
- You are not allowed to define any global or static compound data structures such as arrays, structs, trees, or lists in your mm.c program. However, you are allowed to declare global scalar variables such as integers, floats, and pointers in mm.c.
- For consistency with the libc malloc package, which returns blocks aligned on 8-byte boundaries, your allocator must always return pointers that are aligned to 8-byte boundaries. The driver will enforce this requirement for you.
9 Evaluation
You will receive zero points if you break any of the rules or your code is buggy and crashes the driver. Otherwise, your grade will be calculated as follows:
- Correctness (20 points). You will receive full points if your solution passes the correctness tests performed by the driver program. You will receive partial credit for each correct trace.
- Performance (35 points). Two performance metrics will be used to evaluate your solution: - Space utilization : The peak ratio between the aggregate amount of memory used by the driver (i.e., allocated via mm malloc or mm realloc but not yet freed via mm free) and the size of the heap used by your allocator. The optimal ratio equals to 1. You should find good policies to minimize fragmentation in order to make this ratio as close as possible to the optimal. - Throughput : The average number of operations completed per second.
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.^1 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.
- Style (10 points). - Your code should be decomposed into functions and use as few global variables as possible. - Your code should begin with a header comment that describes the structure of your free and allocated blocks, the organization of the free list, and how your allocator manipulates the free list. each function should be preceeded by a header comment that describes what the function does. (^1) The value for Tlibc is a constant in the driver (600 Kops/s) that your instructor established when they configured the program.