Homework 3

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:
    • 32 bytes
    • 128 bytes
    • 512 bytes
    • Everything else (>512 bytes)

    Allocations of size 32, 128, 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. Alternatively, you can use mmap to manage the memory regions directly.

  2. Memory request:If no blocks are availabe on the free-list, you must ask kernel for more memory (using sbrk() or mmap 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.

Testing


The malloc 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 for each function wrapper (i.e. malloc.c, free.c, realloc.c, calloc.c) that contains definitions for malloc, free, realloc, and calloc. These file must not have main() and shouldn't have any debugging output. There should be separate header/C files for all utility functions. For example, all code to manage a linked list should be in a separate .c file with a corresponding .h file. 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

Conventions

  • One file per wrapper (malloc.c, free.c, etc.)
  • Makefile that allows compiling individual units.

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