Docsity

Project 5: Developing a Dynamic Memory Allocator (Malloc) in COMP 221, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
COMP 221: Introduction to Computer Systems
Project 5: Malloc Dynamic Memory Allocator
Assigned: 10/23/08, Due: 11/11/08
Important: This project may be done individually or in pairs. Be sure to carefully read the course
policies for assignments (including the honor code policy) on the assignments page of the course web site:
http://www.owlnet.rice.edu/˜comp221/2008/html/assignments.html
Overview
In this lab you will be writing a dynamic memory 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.
Project Description
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 given you implements a simple, but still functionally correct, malloc package.
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.
The driver will call mm init before running each trace (and after resetting the brk pointer). There-
fore, your mm init function should be able to reinitialize all state in your allocator each time it is
called. In other words, you should not assume that it will only be called once.
•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 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 8 bytes, your
malloc implementation should do likewise and always return 8-byte aligned pointers.
1
pf3
pf4
pf5

Partial preview of the text

Download Project 5: Developing a Dynamic Memory Allocator (Malloc) in COMP 221 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

COMP 221: Introduction to Computer Systems

Project 5: Malloc Dynamic Memory Allocator

Assigned: 10/23/08, Due: 11/11/

Important: This project may be done individually or in pairs. Be sure to carefully read the course policies for assignments (including the honor code policy) on the assignments page of the course web site:

http://www.owlnet.rice.edu/˜comp221/2008/html/assignments.html

Overview

In this lab you will be writing a dynamic memory 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.

Project Description

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 given you implements a simple, but still functionally correct, malloc package. 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. The driver will call mm init before running each trace (and after resetting the brk pointer). There- fore, your mm init function should be able to reinitialize all state in your allocator each time it is called. In other words, you should not assume that it will only be called once.
  • 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 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 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 implementation, 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 bytes of the old block and the last 4 bytes are uninitialized. 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.

These semantics match the the semantics of the corresponding libc malloc, realloc, and free routines. Type man malloc 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 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:

  • Is every block in the free list marked as free?
  • Are there any contiguous free blocks that somehow escaped coalescing?
  • Is every free block actually in the free list?
  • Do the pointers in the free list point to valid free blocks?
  • Do any allocated blocks overlap?
  • Do the pointers in a heap block point to valid heap addresses? 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.
  • -h: Print a summary of the command line arguments.
  • -l: Run and measure libc malloc in addition to your 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. The test harness simulates an address space by initially malloc-ing a single 20MB array. However, on Owlnet, your accounts default to a maximum data segment size smaller than that. So, before testing, use the following shell commands (based on which shell you are using):
  • sh, ksh, bash: ulimit -d unlimited
  • csh, tcsh: unlimit datasize

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. Therefore, you may not use 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.

Hints

  • Use the mdriver -f option. During initial development, using tiny trace files will simplify debug- ging and testing. We have included two such trace files (short1,2-bal.rep) that you can use for initial debugging.
  • Use the mdriver -v and -V options. The -v option will give you a detailed summary for each trace file. The -V will also indicate when each trace file is read, which will help you isolate errors.
  • Compile with gcc -g and use a debugger. A debugger will help you isolate and identify out of bounds memory references.
  • Understand every line of the malloc implementation in the textbook. The textbook has a detailed example of a simple allocator based on an implicit free list. Use this is a point of departure. Don’t start working on your allocator until you understand everything about the simple implicit list allocator.
  • Encapsulate your pointer arithmetic in C preprocessor macros. Pointer arithmetic in memory man- agers is confusing and error-prone because of all the casting that is necessary. You can reduce the complexity significantly by writing macros for your pointer operations. See the text for examples.
  • Do your implementation in stages. The first 9 traces contain requests to malloc and free. The last 2 traces contain requests for realloc, malloc, and free. We recommend that you start by getting your malloc and free routines working correctly and efficiently on the first 9 traces. Only then should you turn your attention to the realloc implementation. For starters, you can build realloc by calling your existing malloc and free routines. But to get really good performance, you will need to build a stand-alone realloc.
  • Use a profiler. You may find the gprof tool helpful for optimizing performance.
  • Start early! It is possible to write an efficient malloc package with a few pages of code. However, we can guarantee that it will be some of the most difficult and sophisticated code you have written so far in your career. So start early, and good luck!

Evaluation

To turn in your code, you should run the ˜comp221/bin/proj5submit program in the directory that contains the files you wish to submit. Be sure that your code and writeup are contained in files named mm.c, and writeup.txt. Note that we will only use your mm.c along with the original files that were handed out to test your code, so you should not add or modify code in any other file (except during testing, as necessary). Note that the proj5submit program submits your entire directory. If there are additional files in the directory, that is okay, but we will not use them for grading purposes. The project will be graded as follows: You will receive zero points for correctness and performance if you break any of the programming rules or your code is buggy and crashes the driver. Otherwise, your grade will be calculated as follows:

  • Correctness (30 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 (40 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 (^1) The value for Tlibc is a constant in the driver (3400 Kops/s) that is based on the libc allocator performance on long-eared.