Homework 3 (Due Feb. 29, 2016)

Please check back regularly for updates!

  • 02/17/16: Updated Testing section with an skeleton implementation.

Implement a Malloc Library


For this assignment, you have to write a malloc library that provides the following dynamic memory allocation routines (as defined in man 3 malloc):

       #include 

       void *malloc(size_t size);
       void free(void *ptr);
       void *calloc(size_t nmemb, size_t size);
       void *realloc(void *ptr, size_t size);
      
You have to implement a library that implements these functions to allocate and free dynamic memory.

You will need to know about memset and memcpy to implememt calloc and realloc respectively.

Allocation


  1. Bins: You should use separate bins for different allocation size. The idea is to then use the best-fit approach to find the right bin. You have to implement a free-list that contains a list of unallocated blocks. Once a block is allocated, it should be removed from the free-list. A call to free put a block back on the free-list for the corresponding bin. There should be four separate bins for the following allocation sizes:
    • 8 bytes
    • 64 bytes
    • 512 bytes
    • Everything else (>512 bytes)

    Allocations of size 8, 64, and 512 bytes should be done from within the heap, whereas, allocations greater than 512 bytes should be done using mmap system call. To manipulate heap, you can use the sbrk interface.

  2. Memory request:If no blocks are availabe on the free-list, you must ask kernel for more memory (using sbrk() systemcall). The memory request must always be multiple of PAGE_SIZE (use the sysconf(_SC_PAGESIZE) syscall). If sbrk fails, you must set errno to ENOMEM and return NULL;
  3. Alignment: The returned address from malloc, calloc and realloc must be aligned at 8-byte boundary.

Thread Safety


The library should be thread-safe, i.e., it should use proper locks, etc., to allow multiple threads to allocation/free memory.

Per-Thread Malloc Arenas: Each thread allocates from it's own arena. That is, if there are four threads, there must be four separate arena (with their own bins).

Support for fork


If a thread calls fork() to create a child process, can your library handle it? Try running a process with one thread calling fork() while other is calling malloc() continuously.

Testing


The hw3 directory contains a skeleton malloc.c, Makefile, and a basic test case.

Once you have everything ready, you can use the following benchmarks/tools to test out your malloc library.

Print Malloc Statistics


You must print the following statistics for each arena when the program calls malloc_stats() (see the manpage for more details):

  • Total size of arena allocated with sbrk/mmap
  • Total number of bins
  • For each bin:
    • Total number of blocks
    • Used blocks
    • Free blocks
    • Total allocation requests
    • Total free requests

Deliverables


Your submission should contain a file malloc.c that contains definitions for malloc, free, realloc, and calloc. This file must not have main() and shouldn't have any debugging output. A Makefile should be provided with a rule lib that generates libmalloc.so from this file.

You must also provide a README file that contains:

  • Design overview: A brief description of your overall code structure; important data structures (e.g., the free-node list); approach for managing arenas; allocation scheme; and any other important details.
  • Design decisions that you made.
  • Known bugs and errors

Extra credit


  • Implement buddy allocation for coalescing free memory within a bucket. For example, if a 16-byte memory request is followed by a 20-byte request, can you support them using just one block from the 64-byte bin?
  • Implement Malloc hooks.
  • Implement Heap Consistency Checking.

Debugging, Hints and Resources


Memory debuggers:


See Also


  • alloca
  • Lock-free data structures