General Instructions
1. You will work individually on this the project.
2. The code for this assignment should be developed and tested in a UNIX-based environment, specifically the Duke Linux environment available at login.oit.duke.edu.
3. You must follow this assignment spec carefully, and turn in everything that is asked (and in the proper formats, as described). Due to the large class size, this is required to make grading more efficient.
4. You should plan to start early on this project and make steady progress over time. It will take time and careful though to work through the assignment.
Implementation of malloc library
For this assignment, you will implement your own version of several memory allocation functions from the C standard library (actually you will have the chance to implement and study several different versions as described below). Your implementation is to be done in C code.
The C standard library includes 4 malloc-related library functions: malloc(), free(), calloc(), and realloc(). In this assignment, you only need to implement versions of malloc() and free():
void *malloc(size_t size); void free(void *ptr);
Please refer to the man pages for full descriptions of the expected operation for these functions. Essentially, malloc() takes in a size (number of bytes) for a memory allocation, locates an address in the program’s data region where there is enough space to fit the specified number of bytes, and returns this address for use by the calling program. The free() function takes an address (that was returned by a previous malloc operation) and marks that data region as available again for use.
As you work through implementing malloc() and free(), you will discover that as memory allocations and deallocations happen, you will sometimes free a region of memory that is adjacent to other also free memory region(s). Your implementation is required to coalesce in this situation by merging the adjacent free regions into a single free region of memory.
Hint: For implementing malloc(), you should become familiar with the sbrk() system call. This system call is useful for: 1) returning the address that represents the current end of the processes data segment (called program break), and 2) growing the size of the processes data segment by the amount specified by “increment”.
void *sbrk(intptr_t increment);
Hint: A common way to implement malloc() / free() and manage the memory space is to keep a data structure that represents list of free memory regions. This collection of free memory ranges would change as malloc() and free() are called to allocate and release regions of memory in the process data segment. You may design and implement your malloc and free using structures and state tracking as you see best fit.
In this assignment, you will develop a malloc implementation and study different allocation policies. In Homework 2, you will make this implementation thread-safe.
Study of Memory Allocation Policies
Your task is to implement 2 versions of malloc and free, each based on a different strategy for determining the memory region to allocate. The two strategies are:
1. First Fit: Examine the free space tracker (e.g. free list), and allocate an address from the first free region with enough space to fit the requested allocation size.
2. Best Fit: Examine all of the free space information, and allocate an address from the free region which has the smallest number of bytes greater than or equal to the requested allocation size.
The following picture illustrates how each strategy would operate (assuming free regions are traversed in a left to right order) for a malloc() request of 2 bytes:
Requirement: Malloc implementations
To implement your allocation strategies, you will create 4 functions:
//First Fit malloc/free void *ff_malloc(size_t size); void ff_free(void *ptr);
//Best Fit malloc/free void *bf_malloc(size_t size); void bf_free(void *ptr);
Note, that in all cases, a policy to minimize the size of the process’s data segment should be used. In other words, if there is no free space that fits an allocation request, then sbrk() should be used to create that space. However, you do not need to perform any type of garbage collection (e.g. reducing the size of the process’s data segment, even if allocations at the top of the data segment have been freed).
On free(), your implementation is required to merge the newly freed region with any currently free adjacent regions. In other words, your bookkeeping data structure should not contain multiple adjacent free regions, as this would lead to poor region selection during malloc.
Requirement: Performance study report
In addition to implementing these malloc functions, you are tasked to conduct a performance study of the malloc() performance with different allocation policies. Several programs for experimentation will be provided that perform malloc() and free() requests with different patterns (e.g. frequencies, sizes). The metrics of interest will be: 1) the run-time of the program as the implementation of different allocation policies may result in different amounts of memory allocation overhead, and 2) fragmentation (i.e. the amount of allocated data segment space divided by total data segment space). In order to capture #2, you should also implement two additional library functions:
unsigned long get_data_segment_size(); //in bytes unsigned long get_data_segment_free_space_size(); //in bytes
The functions must include the spaced used for metadata also.
get_data_segment_size() = entire heap memory (this includes memory used to save metadata) get_free_space_segment_size() = size of the "free list" = actual usable free space + space occupied by metadata(unusable)[1]
Then, using these functions, you can use the included test programs as well as test programs of your own creation to evaluate and compare the algorithms.
The Starter Kit
A starter kit is included in a file on the web: homework1-kit.tgz
This archive can be retrieved on the command-line using wget and extracted using
“tar xvzf homework1-kit.tgz”.
The kit includes:
● Makefile: A sample Makefile for libmymalloc.so.
● general_tests/: General correctness test, see README.txt for details.
● alloc_policy_tests/: Allocation policy test cases, see README.txt for details.
● alloc_policy_tests_osx/: Same thing adapted for MacOS.
NOTE: Mac OS is not the grading platform; these files are provided as a convenience. Additionally, you may need to change #include "time.h" to #include "sys/time.h"
Testing your system
Code is provided for minimal testing, but the provided materials will not exhaustively evaluate the correctness of your implementation. It is recommended to create your own test software that uses your library, both to aid during development and to ensure correctness in a variety of situations. If you’d like a general introduction to developing test cases, see Software Testing by Sarah Heckman.