Malloc Library

November 2020

Alexandra Demski

Github Deposit of the project

Generalities

The main goal of the project is to implement my own dynamic allocation functions malloc, free, calloc, realloc which will use the systems calls in order to manipulate the program break limit.

The project

Functionalities

The project will at the very least include the usual functions and a debug tool.

Each allocated block will incorporate a header with administrative information necessary for an efficient malloc and a data space used to actually store informations.

The allocation algorithm will, at first, search for a free and big enough memory block. If it succeeds, it will return the address of this zone and label the block as used. If it fails, it will create a new block using sbrk.

With this minimalist implementation, deallocating memory with the free function will only mark a block as unused.

The debug tool print_stats will display useful information such as the number of allocated block, used and free ones, their size and the quantity of wasted memory space.

Structure

The project is divided in several files. The main code is stored in memory.c with a header memory.h used to include its functions. The various test.c files are used to confirm the effectiveness of the library. The last one called Makefile simply compiles all the code for use : it can be called with the make command which will produce executable files.

The code

Block

A memory block contains useful information which will be stored in a header.

It works as a linked list, each block will point to the next and the previous one. The first block in the list will be called first and because malloc wasn't called yet, it is initialized as NULL.

Malloc

The first step, before allocating any block, is to align the size of it, which will enhance the speed and veracity of the program. In the malloc function we will call align which will return a multiple of 4 based on the given size.

The multiple4 function simply check if a given number is already a multiple and therefor, doesn't need to be aligned.

Obviously, after aligning the size, we check if the given number is greater or equal to zero in order to prevent any abnormal behavior.

After that, I decided to separated two different income : either it's the first time we use malloc and therefor, don't need to search for a free block and directly allocate a new one, or we already allocated some, so we will need to check them.

The _new_block function push the program break with the system call sbrk and return the newly created block.

The _find_block function search through the linked list for the most optimal block. If it doesn't find any, it returns NULL.

Free

As we can't give back to the machine an already allocated zone with sbrk, the free function will only mark a given block as unused. It is important to check the pointer before using it, as it can generate errors.

The _block_ptr function returns the address of the block without its header.

Calloc

The calloc function will use our malloc to allocate a new block and then will set all his bytes to 0 with the functions memset.

Realloc

Just as calloc our realloc function will simply call malloc and free while copying the data from one block to another with memcpy. It's important to check the pointer before any tasks as it can raise an error.

Improvements

In order to improve the library, some additional features were needed. Indeed, the library suffers from fragmentation so in order to correct that, I implemented two improvements.

Split at allocation

Before a free block is given by the function _find_block, if its size is big enough, it will be split into to different ones. In order to do so, we need to add new element in the linked list.

Unit before freeing

The complementary addition will unit free neighboring blocks before setting one to unused. It will work similarly to the previous function but instead of adding a new link, we will erased and incorporate one.