For this project, which was made in my CS-453 Operating Systems class, I created a memory management system using the buddy algorithm in the C programming language. When the project was finished, instead of using malloc, calloc, realloc, and free to allocate and deallocate memory, I was able to use my own buddy_malloc, buddy_calloc, buddy_realloc, and buddy_free functions to allocate and deallocate memory.
The way the program works is by first setting aside a large chunk of memory on the machine it is being run on. Then, whenever the user makes a call to budy_malloc, that large chunk of memory is split in half multiple times until there is a chunk of memory that is sized to the nearest power of two that can hold the amount of memory the user wanted to allocate. That memory is then returned to the user in the same way that malloc returns memory to the user.
When the user frees a malloc'd piece of memory, the buddy algorithm I implemented checks if that memory has a buddy that is free, and if so, the two pieces of memory are combined and collapsed up. This process repeats itself until either a block is reached which does not yet have an available buddy or the new chunk of memory made from combining is the same size as the initial large chuck of memory that was set aside in the start of the program.
I think that it should be noted that this was a very complex and time consuming program to create. In the end, however, I was able to receive extra credit on this project by testing my solution of buddy_malloc and buddy_free against the standard malloc and free functions and determine that mine were actually significantly faster than those provided by the C Standard Library.