CS 105 Malloc Lab Writing a Dynamic Storage Allocator See Web page for due date 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 As usual you must work in pairs Any clarifications and revisions to the assignment will be e mailed to the class list or posted on the course Web page 3 Handout Instructions Start by downloading malloclab handout tar from the Web page 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 As with other labs mm c contains a C structure named team which you should fill in with information about your programming team Do this right away so you don t forget When you have completed the lab you will submit 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 1 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 initialization such as allocating the initial heap area The return value should be 0 if all was OK and 1 if there was a problem during initialization Note mm init may be called more than once by the driver It should not remember any external state and should not assume that it is only called once Each call to mm init should reset the allocator forgetting everything that has gone before mm malloc The mm malloc routine returns a pointer to an allocated block of at least size bytes The entire allocated block should lie within the heap region and should not overlap with any other allocated chunk Note that the returned value should point to the payload the area available for use by the caller rather than to whatever header you might choose to put on the block We will comparing your implementation to the version of malloc supplied in the standard C library libc The libc malloc always returns payload pointers that are aligned to 8 bytes which is excessive on Linux your malloc implementation can gain a few utilization points by aligning only to 4 bytes In the Linux specification it is legal to allocate 0 bytes your implementation should behave gracefully in this case It is up to you whether to return NULL or to return a non NULL pointer that can legally be passed to mm free 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 In the Linux specification NULL can be passed to free You can write mm free to accept NULL pointers but the test driver will never exercise this case 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 either 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 2 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 to the shell for complete documentation 5 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 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 6 Support Routines The …
View Full Document
Unlocking...