Skip to content

yacine20005/Malloc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom Malloc Implementation

Project Overview

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:

  1. A fixed-size table (_zones) storing metadata about memory zones (address, size, status).
  2. The actual memory storage area (_mem_pool) where allocated data resides.

This implementation aims to provide a basic understanding of dynamic memory allocation mechanisms.

Features

  • 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 Zone structures (_zones), each tracking a contiguous block of memory.
  • my_malloc Implementation:
    • 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.

Data Structures

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.

Current Status & To-Do

Based on the sujet/todo.md checklist:

  • Define Zone structure 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.

How to Build

Ensure you have gcc and make installed. Navigate to the project directory in your terminal and run:

make
# or
make tout

This 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.

How to Run

After building the project, run the executable:

make lancer
# or directly
./malloc

How to Clean

To remove the compiled object files and the executable:

make clean

Author

  • Yacine Hamadouche
  • University Gustave Eiffel

About

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.

Resources

Stars

Watchers

Forks

Contributors