{"id":24024626,"url":"https://github.com/adsh16/mems","last_synced_at":"2025-07-07T12:05:50.102Z","repository":{"id":269407821,"uuid":"736674005","full_name":"adsh16/MeMs","owner":"adsh16","description":"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.","archived":false,"fork":false,"pushed_at":"2024-12-23T10:24:04.000Z","size":729,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-07T12:05:45.895Z","etag":null,"topics":["c","freelist","memory-management"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adsh16.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-28T14:49:48.000Z","updated_at":"2024-12-23T10:24:07.000Z","dependencies_parsed_at":"2024-12-23T11:26:20.750Z","dependency_job_id":"3d33f160-31b4-4cb7-9db8-e0cf8b6461ba","html_url":"https://github.com/adsh16/MeMs","commit_stats":null,"previous_names":["adsh16/mems"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/adsh16/MeMs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adsh16%2FMeMs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adsh16%2FMeMs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adsh16%2FMeMs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adsh16%2FMeMs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adsh16","download_url":"https://codeload.github.com/adsh16/MeMs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adsh16%2FMeMs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264075399,"owners_count":23553506,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","freelist","memory-management"],"created_at":"2025-01-08T15:13:13.761Z","updated_at":"2025-07-07T12:05:50.051Z","avatar_url":"https://github.com/adsh16.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Custom Memory Management System (MeMs)\r\n## Overview\r\n\r\nMeMs 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.\r\n\r\n\r\n## Data Structure Used\r\n\r\n![image](https://github.com/user-attachments/assets/cd129633-197f-4097-bf72-40f21b232c57)\r\n\r\n### Overview\r\n\r\nMeMs 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:\r\n- **PROCESS**: Representing allocated memory segments.\r\n- **HOLE**: Representing free memory segments available for allocation.\r\n\r\n### Search Mechanism for Allocation\r\n\r\n- **Step 1: Traverse the Free List**  \r\n  The free list is traversed to locate a HOLE node that can satisfy the requested memory size.\r\n  \r\n- **Step 2: Splitting Nodes**  \r\n  - If a HOLE is larger than the requested size, it is split into two nodes:\r\n    1. One node allocated as a PROCESS segment.\r\n    2. The remaining portion remains as a HOLE.\r\n  - The free list pointers are updated accordingly.\r\n\r\n- **Step 3: Expanding Memory**  \r\n  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.\r\n\r\n### Search Mechanism for Deallocation\r\n\r\n- **Step 1: Locate the Segment**  \r\n  The free list is traversed to find the node corresponding to the given MeMS virtual address.\r\n\r\n- **Step 2: Mark as HOLE**  \r\n  The node is marked as a HOLE, making it available for future allocations.\r\n\r\n- **Step 3: Coalesce Adjacent HOLEs**  \r\n  If adjacent nodes are also HOLEs, they are merged into a single node to reduce fragmentation and optimize the free list structure.\r\n\r\n### Advantages of the Free List Structure\r\n\r\n- **Efficient Allocation**: Reuses memory segments effectively to minimize overhead.  \r\n- **Dynamic Memory Management**: Handles varying allocation sizes with minimal fragmentation.  \r\n- **Fast Deallocation**: Updates the structure and coalesces nodes in constant or linear time, depending on the node's position.\r\n\r\n## Key Features\r\n\r\n- **Memory Allocation (`mems_malloc`)**: Allocates memory of the requested size by reusing free segments or expanding memory via `mmap` if necessary. Returns a MeMS virtual address.\r\n- **Memory Deallocation (`mems_free`)**: Frees allocated memory and marks it as reusable by updating the free list structure.\r\n- **Address Translation (`mems_get`)**: Converts a MeMS virtual address to the corresponding physical address, ensuring memory consistency.\r\n- **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.\r\n\r\n## Functions Provided\r\n\r\n1. **`void* mems_malloc(size_t size)`**  \r\n   Allocates memory of the specified size. If no suitable free segment exists, allocates new memory using `mmap`.  \r\n   **Parameters:** Size of the memory to allocate.  \r\n   **Returns:** MeMS virtual address of the allocated memory.\r\n\r\n2. **`void mems_free(void* ptr)`**  \r\n   Frees the memory pointed to by `ptr` and marks it as available in the free list.  \r\n   **Parameters:** MeMS virtual address to free.  \r\n   **Returns:** None.\r\n\r\n3. **`void* mems_get(void* v_ptr)`**  \r\n   Retrieves the physical address corresponding to a given MeMS virtual address.  \r\n   **Parameters:** MeMS virtual address.  \r\n   **Returns:** Physical address mapped to the virtual address.\r\n\r\n4. **`void mems_print_stats()`**  \r\n   Prints detailed statistics about memory usage, including:  \r\n   - Total mapped pages (`mmap` calls)  \r\n   - Unused memory in bytes  \r\n   - Details of nodes and segments (PROCESS or HOLE)  \r\n\r\n   **Parameters:** None.  \r\n   **Returns:** None (prints to `STDOUT`).\r\n\r\n## Data Structure\r\n\r\n- **2-D Free List**:  \r\n  - Tracks memory allocation and deallocation with a doubly linked list for efficient management.  \r\n  - Each node represents a segment of memory marked as either `PROCESS` (allocated) or `HOLE` (free).\r\n\r\n## Address Management\r\n\r\n- **Virtual Memory Address**: Represents the contiguous address space for processes in MeMs. Virtual addresses are meaningful within MeMs but not externally.  \r\n- **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.\r\n\r\n## Conclusion\r\n\r\nMeMs 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.\r\n\r\n---\r\n\r\nFeel free to clone this repository and experiment with the provided functions to enhance your understanding of custom memory management systems!\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadsh16%2Fmems","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadsh16%2Fmems","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadsh16%2Fmems/lists"}