This library provides a custom implementation of memory allocation (my_malloc
) and deallocation (my_free
) using a doubly-linked list of memory blocks. The primary goal is to manage memory dynamically, allowing users to allocate and free memory as needed.
-
Memory Allocation: The
my_malloc
function allocates memory blocks, searching for a suitable free block or splitting a larger block using the first-fit algorithm. -
Memory Deallocation: The
my_free
function marks a previously allocated block as free and coalseces adjacent free blocks to optimize memory usage. -
Initialization: The
mem_init
function initializes the custom memory allocation library by allocating a main memory block and intializing the linked list.
The memory allocation strategy used in this code is the "first-fit" algorithm, where the first available block that satisfies the allocation size is chosen. This strategy is a simple methodology where the suitable free block in the list is used, as opposed to the best-fit, improving runtime at the cost of slight external fragmentation.
In this example diagram, an item of size 500 is attempting to be allocated within a memory block featuring three open blocks of sizes 1000, 2000, and 550. The first fit algorithm selects the first available block with sufficient space (in this case, the 1000-sized block) for the requested item. Any extra space will be split into a new free block.
Coalescing is a memory management technique used to merge adjacent free blocks into a single, larger free block. This helps prevent external memory fragmentation and ensures more efficient use of available memory. As the inital memory block is used up however, coalescing becomes less effective.
The 'Before Coalescing' diagram depicts a memory layout with alternating allocated and free blocks, in the strucutre of a linked list. After applying memory coalescing, represented in the 'After Coalescing' diagram, adjacent free blocks are merged to create a more contiguous and efficient memory space. This process is done everytime my_free
is called, minimizing external fragmentation.
-
Clone the repository to your local machine:
git clone https://github.com/Daksh2060/custom-malloc-free-c.git
-
If using the provided test file, simply run using the included make file:
make ./mem_test
To use in your own program, include the library header in your C file:
#include "my_malloc.h"
-
To adjust the maximum size of the allocated block, adjust
TOTAL_BLOCK
value withinmy_malloc.h
:#define TOTAL_BLOCK 1000
-
Initiate the library within main():
mem_init();
-
To use allocated memory, call as you would
malloc
, for example, to allocate a singleint
:int* int1 = (int*)my_malloc(sizeof(int));
-
To free memory, call as you would
free
:my_free(int1);
-
Free the inital memory block at the very end of your program to clean up:
free_block();
Feel free to reach out if you have any questions, suggestions, or feedback:
- Email: dpa45@sfu.ca
- LinkedIn: @Daksh Patel