{"id":15769643,"url":"https://github.com/thk2b/malloc","last_synced_at":"2025-06-22T03:37:20.149Z","repository":{"id":138600966,"uuid":"162318836","full_name":"thk2b/malloc","owner":"thk2b","description":"A malloc implementation","archived":false,"fork":false,"pushed_at":"2019-08-12T04:11:28.000Z","size":838,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-10-11T14:22:06.448Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thk2b.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2018-12-18T16:49:01.000Z","updated_at":"2019-08-12T04:11:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"cedc2739-269f-47e8-bb39-bc99619e983a","html_url":"https://github.com/thk2b/malloc","commit_stats":{"total_commits":159,"total_committers":1,"mean_commits":159.0,"dds":0.0,"last_synced_commit":"eca495d7a05d63f93044bac69c055aed247bded8"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thk2b%2Fmalloc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thk2b%2Fmalloc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thk2b%2Fmalloc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thk2b%2Fmalloc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thk2b","download_url":"https://codeload.github.com/thk2b/malloc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246458528,"owners_count":20780784,"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":[],"created_at":"2024-10-04T14:03:43.373Z","updated_at":"2025-03-31T11:22:37.036Z","avatar_url":"https://github.com/thk2b.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# malloc\nA malloc implementation\n\nThis screenshots displays the memory managed by the library. Yellow represents block headers, green client data and blue free block headers and trailers.\n![screenshot](https://raw.githubusercontent.com/thk2b/malloc/master/media/screenshot.png)\n\n# API\n```c\nvoid\t*malloc(size_t size);\nvoid\tfree(void *ptr);\nvoid\t*realloc(void *ptr, size_t size);\n\nvoid\tshow_alloc_mem(void); /* print a summary of allocated blocks */\nvoid\thexdump_mem(void);    /* semantic hexdump of the allocated memory */\n```\n\n# Usage \u0026 testing\n\nTo create the library:\n```sh\nmake\n```\n\nTo run a program, dynamically link it by running\n```sh\nscripts/inject.sh COMMAND\n```\n\nOr link with `libft_malloc.so`.\n\n# Strategy\n\n- Memory is managed via `mmap(2)` and `mmunmap(2)`.\n\n- mmaped areas are managed via the `area` struct and `area__*` functions. Areas are a doubly linked list containing individual `blocks`, which are taken from an available `area`.\n\n- `blocks` have a 1 word header encoding size, free status, and free status of the previous block. An additional 3 word minimum size is required to store free list nodes.\n\n- `free_blocks` are doubly-linked list nodes. They have the same header as the underlying `block`, 2 pointers to adjacent free_blocks, an an additional footer encoding the block's size. This footer, in addition to `block.prev_free` allows for linear time coalescing of neighboring blocks.\n\n- Free blocks are stored in an adress-ordered linked list, such that `free_block-\u003eprev \u003c free_block \u003c free_block-\u003enext` is true for all free blocks for a free list. This ensures that the memory close to the begining of the mmaped areas gets reused sooner, reducing memory fragmentation and improving locality.\n\n- When freeing the last `block` of an `area`, the block is destroyed and `area.cur_size` is reduced, effectively destroying the block. This significantly reduces external fragmentation. \n\n- 3 free lists are used. Each list contains blocks of a specific size range (0-255, 256-1023, 1024+).\n\n- Free blocks are selected on a first fit basis.\n\n- Memory is defragmented at allocation size. When searching the free list for a block of the requested size, neighboring free blocks are included in the current blocks's size, and merged if the combined size is sufficient.\n\n# Further improvements\n\n- [ ] Block checksums for fast freeing\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthk2b%2Fmalloc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthk2b%2Fmalloc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthk2b%2Fmalloc/lists"}