MeMs is a custom memory management system implemented in C, utilizing mmap
and munmap
system calls for efficient memory allocation and deallocation. The system employs a 2-D free list data structure to track memory usage, manage virtual and physical address mappings, and handle memory fragmentation.
MeMs employs a 2-D doubly linked list (free list) data structure to manage memory allocation and deallocation efficiently. The structure organizes memory segments into nodes, which are categorized as either:
- PROCESS: Representing allocated memory segments.
- HOLE: Representing free memory segments available for allocation.
-
Step 1: Traverse the Free List
The free list is traversed to locate a HOLE node that can satisfy the requested memory size. -
Step 2: Splitting Nodes
- If a HOLE is larger than the requested size, it is split into two nodes:
- One node allocated as a PROCESS segment.
- The remaining portion remains as a HOLE.
- The free list pointers are updated accordingly.
- If a HOLE is larger than the requested size, it is split into two nodes:
-
Step 3: Expanding Memory
If no suitable HOLE is found,mmap
is used to allocate additional memory. The new memory segment is added to the list as a PROCESS node.
-
Step 1: Locate the Segment
The free list is traversed to find the node corresponding to the given MeMS virtual address. -
Step 2: Mark as HOLE
The node is marked as a HOLE, making it available for future allocations. -
Step 3: Coalesce Adjacent HOLEs
If adjacent nodes are also HOLEs, they are merged into a single node to reduce fragmentation and optimize the free list structure.
- Efficient Allocation: Reuses memory segments effectively to minimize overhead.
- Dynamic Memory Management: Handles varying allocation sizes with minimal fragmentation.
- Fast Deallocation: Updates the structure and coalesces nodes in constant or linear time, depending on the node's position.
- Memory Allocation (
mems_malloc
): Allocates memory of the requested size by reusing free segments or expanding memory viammap
if necessary. Returns a MeMS virtual address. - Memory Deallocation (
mems_free
): Frees allocated memory and marks it as reusable by updating the free list structure. - Address Translation (
mems_get
): Converts a MeMS virtual address to the corresponding physical address, ensuring memory consistency. - Statistics Printing (
mems_print_stats
): Provides insights into memory usage, including the number of mapped pages, unused memory, and details about allocated and free segments.
-
void* mems_malloc(size_t size)
Allocates memory of the specified size. If no suitable free segment exists, allocates new memory usingmmap
.
Parameters: Size of the memory to allocate.
Returns: MeMS virtual address of the allocated memory. -
void mems_free(void* ptr)
Frees the memory pointed to byptr
and marks it as available in the free list.
Parameters: MeMS virtual address to free.
Returns: None. -
void* mems_get(void* v_ptr)
Retrieves the physical address corresponding to a given MeMS virtual address.
Parameters: MeMS virtual address.
Returns: Physical address mapped to the virtual address. -
void mems_print_stats()
Prints detailed statistics about memory usage, including:- Total mapped pages (
mmap
calls) - Unused memory in bytes
- Details of nodes and segments (PROCESS or HOLE)
Parameters: None.
Returns: None (prints toSTDOUT
). - Total mapped pages (
- 2-D Free List:
- Tracks memory allocation and deallocation with a doubly linked list for efficient management.
- Each node represents a segment of memory marked as either
PROCESS
(allocated) orHOLE
(free).
- Virtual Memory Address: Represents the contiguous address space for processes in MeMs. Virtual addresses are meaningful within MeMs but not externally.
- Physical Memory Address: Points to the actual memory location in the system. Physical addresses may be non-contiguous and are tracked to prevent memory overlap between processes.
MeMs provides a lightweight, customizable solution for memory management in C programs. By leveraging system calls and a robust data structure, it ensures efficient allocation, deallocation, and memory usage statistics. This project demonstrates a deeper understanding of memory management techniques and virtual-to-physical address translation.
Feel free to clone this repository and experiment with the provided functions to enhance your understanding of custom memory management systems!