{"id":13710591,"url":"https://github.com/dweiller/zig-composable-allocators","last_synced_at":"2025-05-06T19:32:13.063Z","repository":{"id":77415042,"uuid":"585819298","full_name":"dweiller/zig-composable-allocators","owner":"dweiller","description":"Comptime-generic composable allocators","archived":false,"fork":false,"pushed_at":"2023-07-04T06:22:48.000Z","size":25,"stargazers_count":18,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-13T21:44:31.115Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Zig","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/dweiller.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-01-06T06:48:15.000Z","updated_at":"2024-10-26T04:07:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"051b1a48-ff22-4454-b766-16d5a5525801","html_url":"https://github.com/dweiller/zig-composable-allocators","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/dweiller%2Fzig-composable-allocators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dweiller%2Fzig-composable-allocators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dweiller%2Fzig-composable-allocators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dweiller%2Fzig-composable-allocators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dweiller","download_url":"https://codeload.github.com/dweiller/zig-composable-allocators/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252753620,"owners_count":21798983,"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-08-02T23:00:58.690Z","updated_at":"2025-05-06T19:32:08.030Z","avatar_url":"https://github.com/dweiller.png","language":"Zig","readme":"# Composable allocators\n\nThis is an implementation in Zig of the ideas [presented](https://www.youtube.com/watch?v=LIb3L4vKZ7U) by Andrei Alexandrescu about composable allocators.\n\nThe allocators are (mostly) implemented as comptime generics conforming to an interface that is (optionally) a superset of `std.mem.Allocator`, except that concrete types are used instead of `*anyopaque`.\n\nThe following allocators are either currently implemented (those with a tick) or are planned to be:\n\n  - [x] `Std` (for wrapping a `std.mem.Allocator`)\n  - [x] `Null`\n  - [x] `FixedBuffer` (bump allocator with fixed memory buffer)\n  - [x] `Fallback`\n  - [x] `Stack` (a thin wrapper around `FixedBuffer` putting the buffer on the stack)\n  - [ ] `Affix` (add optional extra data before/after each allocation)\n  - [x] `FreeList` (allocates blocks of a specific size; non-thread-safe)\n  - [ ] `ThreadSafeFreeList` (a thread-safe version of `FreeList`, allowing other threads to free, but not allocate)\n  - [ ] `BitMapped` (allocates blocks of a specific size, tracking occupancy with a bitmap)\n  - [ ] `Cascading` (holds a collection of allocators in use (all the same type), adding a new one when they are all full)\n  - [x] `Segregated` (chooses between two allocators based on a size threshold)\n  - [ ] `Bucket` (like `Segregated`, but has multiple size classes)\n\nAll of these (except for the first three) are generic over other allocator types that they wrap, allowing them to be composed to create complex allocation strategies in a relatively simple way; for example, an allocator that allocates on the stack (as a bump allocator) but falls back to a `std.heap.GeneralPurposeAllocator` can be implemented as:\n```zig\nconst ca = @import(\"/path/to/composable-allocator/lib.zig\");\n\npub fn main() void {\n    var gpa = std.heap.GeneralPurposeAllocator(.{}){};\n    var test_allocator: ca.Fallback(ca.Stack(1024), ca.Std) = undefined;\n    test_allocator.primary.initInPlaceExtra(.{gpa.allocator()});\n    const a = ca.allocator(\u0026test_allocator);\n}\n```\n\nIn the above, `a` is a `std.mem.Allocator` that will first try to use a bump allocator with 1024 bytes on the stack before reverting to using `std.mem.GeneralPurposeAllocator(.{})`.\n\n## WIP\nIf you have suggestions, would like to contribute some allocators, or have issues using anything issues and PRs are welcome.\n","funding_links":[],"categories":["Language Essentials"],"sub_categories":["Memory Allocator and Management"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdweiller%2Fzig-composable-allocators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdweiller%2Fzig-composable-allocators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdweiller%2Fzig-composable-allocators/lists"}