{"id":20373416,"url":"https://github.com/arxiver/mema","last_synced_at":"2025-11-30T15:04:56.135Z","repository":{"id":195884641,"uuid":"682880419","full_name":"arxiver/mema","owner":"arxiver","description":" Memory Manager for C language to maintain the memory allocation and dellactions and works as a memory pool ","archived":false,"fork":false,"pushed_at":"2023-11-23T09:13:57.000Z","size":23,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T04:15:15.866Z","etag":null,"topics":["c","clang","cpp","cpp17","memory","memory-allocation","memory-management","memory-manager","memory-pool","pool"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arxiver.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}},"created_at":"2023-08-25T04:59:54.000Z","updated_at":"2023-12-31T14:57:46.000Z","dependencies_parsed_at":"2023-09-20T00:57:50.463Z","dependency_job_id":"ff50e8be-7836-49c7-b681-85bf8acd4c12","html_url":"https://github.com/arxiver/mema","commit_stats":null,"previous_names":["rrrokhtar/mema","arxiver/mema"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arxiver%2Fmema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arxiver%2Fmema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arxiver%2Fmema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arxiver%2Fmema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arxiver","download_url":"https://codeload.github.com/arxiver/mema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241921824,"owners_count":20042763,"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","clang","cpp","cpp17","memory","memory-allocation","memory-management","memory-manager","memory-pool","pool"],"created_at":"2024-11-15T01:18:14.958Z","updated_at":"2025-11-30T15:04:56.087Z","avatar_url":"https://github.com/arxiver.png","language":"C","readme":"# mema (MEmory MAnager)\n(Bit9 Memory Manager) Memory Manager for C language to maintain the memory allocation and dellactions and works as a memory pool\n\n### Building blocks\n\nThe building blocks of the app is two data structs\n- Block\n```c\ntypedef struct Block\n{\n  void *ptr;          // pointer to the data chunk\n  size_t size;        // size of the data chunk\n  struct Block *next; // pointer to the next block\n  struct Block *prev; // pointer to the previous block\n  bool poolAllocated; // true if the block is allocated in the pool\n} Block;\n```\n\n- Pool\n```c\ntypedef struct Pool\n{\n  size_t allocatedCount;   // number of allocated blocks\n  size_t deallocatedCount; // number of deallocated blocks\n  size_t poolSize;         // BYTES: unsigned int contains the size of the pool\n  size_t freeSize;         // BYTES: current pool free size\n  bool initialized;        // pool is initialized, true if initialized\n  void *mem;               // pointer to the first byte of the pool\n  struct Block *head;      // head of the list of the allocated blocks\n  struct Block *tail;      // tail of the list of the allocated blocks\n} Pool;\n```\n\n### Diagram of the system\n\n![diagram](./assets/diagram.svg)\n\n### The interface\n\n```c\n// Initialize the pool (1)\nbool initPool(Pool *pool, size_t poolSize);\n// Allocate memory from the pool [chunk] (2)\nvoid *palloc(Pool *pool, size_t size);\n// Free memory from the pool [chunk] (3)\nbool pfree(Pool *pool, void *ptr);\n// Free the pool (destroy) (4)\nbool freePool(Pool *pool);\n```\n\n\n### Strategy\n- Create Pool with size of poolSize\n- Initialize the pool\n  - Reserve memory for the pool `malloc(poolSize);`\n  - Initialize the pool (setting the freeSize to the poolSize)\n- Allocate chunk?\n  - Go to the pool and create a new block\n    - If the block cannot be allocated from the memory?\n      - Allocate from the pool.\n    - Free (to the memory) the requested chunk size from the pool if it is available.\n    - Allocate the requested chunk size from the memory.\n    - Connect the Block which contains a pointer to the allocated mem to the chain of the allocated blocks which is connected to the pool through the (head/tail) ptrs\n- Deallocate chunk?\n  - Go to the pool and free the block from the memory and the chunk as well add the size to the pool and resize the pool to the new available free size\n- Free the pool\n  - Go to the chain and free it\n  - Free the mem of the pool from the memory\n  - Free the pool\n\n### Behind the scenes\nWhy did I decide that approach? I have kept on my mind the problem of memory management and the fragmentation of memory, that for example in case of I got the following order of allocation and deallocation:\nPOOL SIZE = 10\n- a = allocate (2)\n- b = allocate (4)\n- c = allocate (1)\n- dellocate(a)\n\nAt that point I will need to do one of the following:\n\n\n- Make a layer of memory management/access so that I handle the decontinuous allocation and deallocation. It is a bit complex, considering the above example, if I tried to allocate 5 bytes I will not be able to handle it because the free size is 6 (2 at the first bytes) and 4 at the last bytes, connecting them together and returing them is not possible to be done in a simple way.\n\n- Second one is keep tracking of the memory allocated through a linked list that is managed by a manager (the pool) and keep the area of the pool allocated and once there is a chunk of data requested freeing that from the pool and let it be taken from the memory and I make a block on my side that points to it and connect it to the pool chain was what I decided to do after a long time of thinking and actually due to the tightness of the time I decided to do it this way the make the control of the memory allocation and deallocation easier and co-managed (maintained)  by the memory.\n\n### Build\n```sh\nmake \n```\n### Run\n```sh\nmake run\n```\n\n### Test\n```sh\nmake test\n```\n\n### Output\nCheck a snippet from are in OUTPUT.md\n\n### Requirements\n- C\n- gcc \n- make\n\n### Author\nMade for Bitnine by Mohamed Mokhtar [@rrrokhtar](https://github.com/rrrokhtar)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farxiver%2Fmema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farxiver%2Fmema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farxiver%2Fmema/lists"}