man 3
malloc
):
#includeYou have to implement a library that implements these functions to allocate and free dynamic memory.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 will need to know about memset
and memcpy
to
implememt calloc and realloc respectively.
free
put a block back on
the free-list for the corresponding bin.
There should be four separate bins for the following allocation sizes:
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.
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;
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).
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.
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.
You must print the following statistics for each arena when the
program calls malloc_stats()
(see the manpage for more
details):
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: