{"id":19655529,"url":"https://github.com/ritwickdey/malloc-impl-demo","last_synced_at":"2026-05-10T23:39:04.440Z","repository":{"id":86775830,"uuid":"472014097","full_name":"ritwickdey/malloc-impl-demo","owner":"ritwickdey","description":"Minimal implementation of malloc and free for demo purposes.","archived":false,"fork":false,"pushed_at":"2022-03-21T07:16:20.000Z","size":688,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-10T00:35:22.867Z","etag":null,"topics":["demo","free","knowlage-share","malloc"],"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/ritwickdey.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":"2022-03-20T15:03:06.000Z","updated_at":"2024-11-30T23:02:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"e7ea8331-b291-4857-ab22-91d725272b47","html_url":"https://github.com/ritwickdey/malloc-impl-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ritwickdey%2Fmalloc-impl-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ritwickdey%2Fmalloc-impl-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ritwickdey%2Fmalloc-impl-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ritwickdey%2Fmalloc-impl-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ritwickdey","download_url":"https://codeload.github.com/ritwickdey/malloc-impl-demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240964337,"owners_count":19885730,"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":["demo","free","knowlage-share","malloc"],"created_at":"2024-11-11T15:21:54.131Z","updated_at":"2026-05-10T23:39:04.370Z","avatar_url":"https://github.com/ritwickdey.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"Minimal implementation of `malloc` and `free` using `sbrk()` and `brk()` system calls. \n\n### Context:\nHow does a process *actually* request the kernel to allocate/deallocate memory blocks in runtime?\n\nWhenever we create an object or list, we allocate memory in heap. Unlike `stack`, memory allocation in `heap` happens in runtime. No matter what programming language you choose, the fundamentals remain the same. \n\nIn Clang, there is `malloc()` to create memory in heap and `free()` to release the memory. If you don't know `Clang`, you can relate `malloc()` call as your creating object from Class.\nBut there are no `free()` equivalent functions available in other high level-programming languages as they have automatic garbage collection. Probably garbage collector has some function like `free()`.\n\nBut `malloc()` and `free()` is library (`glibc`) functions, which means anyone can implement those function or tweak those function. Then the question is how `malloc()` or `free()` allocate or deallocate memory. \n\nThere is a few `system call` available to do that. `brk`, `sbrk` and `mmap`, `munmap`. Please check the man page to know more about the functions.\n\nHere I've tried to implement `malloc` and `free` using `brk`, `sbrk` system calls.\n\n-------------\n### Local setup\n```\n\u003e cd \u003cproject dir\u003e\n\u003e mkdir bin obj\n\u003e make build\n\u003e ./bin/mem_test\n```\n-------------\n\n\n### Gist\n!! Try to read **only** the comments.\n\n`my_malloc()` implementation using `brk` and `sbrk`.\n\n\u003cimg src=\"./img/malloc.png?v=2\" height=\"450px\"\u003e\n\n\n`my_free()` implementation using `brk` and `sbrk`.\n\n\u003cimg src=\"./img/free.png?v=2\" height=\"350px\"\u003e\n\n### Reference:\n- https://blog.holbertonschool.com/hack-the-virtual-memory-malloc-the-heap-the-program-break/\n- The Linux Programming Interface by Michael Kerrisk\n- Man page https://man7.org/linux/man-pages/man2/sbrk.2.html\n- https://stackoverflow.com/questions/6988487/what-does-the-brk-system-call-do (https://stackoverflow.com/a/31082353)\n\n---------\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fritwickdey%2Fmalloc-impl-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fritwickdey%2Fmalloc-impl-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fritwickdey%2Fmalloc-impl-demo/lists"}