Unformatted text preview:

1 CSE 361S Intro to Systems Software Final Project Due: Tuesday, December 9, 2008. In this project, you will be writing a dynamic storage allocator for C programs (i.e., your own version of malloc, free, and realloc). You are encouraged to explore the design space creatively and implement an allocator that is correct, space efficient, and fast. You may work in a group of up to two people. Logistics Start by downloading the file malloclab-handout.tar from the class web page and putting it in your working directory. 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 one or two individuals comprising your programming team. Do this right away so you do not forget. When you have completed the project, you will hand in only one file (mm.c), which contains your solution. 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 provided mm.c file implements an extremely 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 your2 implementation) calls mm_init to perform any 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. Your mm_malloc implementation should always return 8-byte aligned pointers. • mm_free: The mm_free routine frees the block pointed to by ptr. It returns nothing. This routine 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. o if ptr is NULL, the call is equivalent to mm_malloc(size); o if size is equal to zero, the call is equivalent to mm_free(ptr); o 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 upon your implementation, the amount of internal fragmentation in the old block, and the size of the mm_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 semantics of the corresponding libc malloc, realloc, and free routines. Type man malloc to the shell for complete documentation. Heap Consistency Checker 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: − Is every block in the free list marked as free?3 − 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. Support Routines The memlib.c package simulates the memory system for your dynamic memory allocator. You can invoke the following functions in memlib.c: • void *mem_sbrk(int incr): Expands the heap by incr bytes, where incr is a positive non-zero integer and returns a generic pointer to the first byte of the newly allocated heap area. The semantics are identical to the Unix sbrk function, except that mem_sbrk accepts only a positive non-zero integer argument. • 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). The Trace-driven Driver Program The driver program mdriver.c in


View Full Document

WUSTL CSE 361S - CSE 361S Final Project

Documents in this Course
Load more
Download CSE 361S Final Project
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CSE 361S Final Project and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CSE 361S Final Project 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?