This project is a university assignment focused on implementing custom versions of the standard C library functions malloc and free. It utilizes a fixed logical table approach to manage a static memory pool. The memory pool is divided into two main parts:
- A fixed-size table (
_zones) storing metadata about memory zones (address, size, status). - The actual memory storage area (
_mem_pool) where allocated data resides.
This implementation aims to provide a basic understanding of dynamic memory allocation mechanisms.
- Static Memory Pool: A large, fixed-size character array (
_mem_pool) serves as the source of memory (currently 1GB). - Zone-Based Management: Memory is managed using a fixed-size array of
Zonestructures (_zones), each tracking a contiguous block of memory. my_mallocImplementation:- Allocates memory blocks from the pool.
- Uses a First-Fit strategy to find a suitable free zone.
- Supports Zone Splitting: If a found free zone is larger than requested, it's split into an allocated zone and a new smaller free zone.
display_memory_status: A utility function to print the current state of all memory zones (address, size, status - free/allocated) for debugging purposes.- Makefile: Includes rules for easy compilation, execution, and cleaning.
typedef struct {
char *address; // Starting address of the zone
size_t size; // Size of the zone in bytes
int status; // 0 for free, 1 for allocated
} Zone;_mem_pool[POOL_SIZE]: The static memory pool._zones[MAX_ZONE]: The array storing metadata for each zone.
Based on the sujet/todo.md checklist:
- Define
Zonestructure and constants (POOL_SIZE,MAX_ZONE). - Set up the memory pool (
_mem_pool). -
initialize_memory_pool()/initialize_zones(): Initializes the zone table and the first large free zone. -
my_malloc(size_t size): Implements allocation using first-fit and zone splitting. -
my_free(void *ptr): Needs implementation. Should find the corresponding zone, mark it as free, and ideally merge it with adjacent free zones to reduce fragmentation. -
display_memory_status(): Implemented for debugging. - Testing: Requires more comprehensive testing, including edge cases (allocating when full, freeing invalid pointers, allocating more than available), fragmentation scenarios, and stress tests.
- Bonus Features: Consider implementing best-fit allocation or memory alignment.
Ensure you have gcc and make installed. Navigate to the project directory in your terminal and run:
make
# or
make toutThis will compile the source files located in the src/ directory and place the object files in the obj/ directory. The final executable will be named malloc in the project's root directory.
After building the project, run the executable:
make lancer
# or directly
./mallocTo remove the compiled object files and the executable:
make clean- Yacine Hamadouche
- University Gustave Eiffel