{"id":22123070,"url":"https://github.com/bcrist/zig-tempallocator","last_synced_at":"2025-04-10T12:43:18.202Z","repository":{"id":49753313,"uuid":"517929484","full_name":"bcrist/Zig-TempAllocator","owner":"bcrist","description":"Arena allocator for interactive programs and simulations","archived":false,"fork":false,"pushed_at":"2025-03-22T07:09:59.000Z","size":41,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-22T08:19:48.725Z","etag":null,"topics":["allocator","utilities","utility-library","zig","zig-library","zig-package","ziglang"],"latest_commit_sha":null,"homepage":"","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/bcrist.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-07-26T05:46:09.000Z","updated_at":"2025-03-22T07:10:02.000Z","dependencies_parsed_at":"2024-01-13T07:49:04.563Z","dependency_job_id":"9cfad25b-9f02-483f-b046-85af0ac416ea","html_url":"https://github.com/bcrist/Zig-TempAllocator","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/bcrist%2FZig-TempAllocator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcrist%2FZig-TempAllocator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcrist%2FZig-TempAllocator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcrist%2FZig-TempAllocator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcrist","download_url":"https://codeload.github.com/bcrist/Zig-TempAllocator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217160,"owners_count":21066634,"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":["allocator","utilities","utility-library","zig","zig-library","zig-package","ziglang"],"created_at":"2024-12-01T15:29:08.187Z","updated_at":"2025-04-10T12:43:18.186Z","avatar_url":"https://github.com/bcrist.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Zig-TempAllocator\n\nA stack allocator similar to std.heap.ArenaAllocator, except:\n\n- You can reset the whole allocator without releasing the underlying memory back to the OS.\n- You can take a snapshot at any time and later invalidate/free all allocations after that point, but preserving allocations before it.\n- It will track how much memory is usually used before being reset, and release some if it remains significantly lower than the current committed capacity.\n- The inner \"child allocator\" is not configurable; internally it uses OS memory management facilities directly.\n\nThe primary use-cases for TempAllocator are real-time interactive programs and simulations (games, GUIs, etc.), but it can be useful for anything where work is done sequentially in a main loop, and it's easy to guarantee that memory allocated from it won't be held across resets.\n\n## Usage Example\n\n```zig\nconst std = @import(\"std\");\nconst TempAllocator = @import(\"temp_allocator.zig\");\nconst app = @import(\"myApp.zig\");\n\npub fn main() void {\n    var tempalloc = TempAllocator.init(1024*1024*1024); // 1GB of virtual address space\n    defer tempalloc.deinit();\n\n    var n: usize = 0;\n    while (!app.shouldExit()) {\n        tempalloc.reset();\n        n += 1;\n\n        var temp: []u8 = std.fmt.allocPrint(tempalloc.allocator(), \"number {} is {s}\", .{ n, \"Something\" });\n        app.doSomethingWithAString(temp);\n    }\n}\n```\n\n## Zig Version\n\nLast updated for `zig 0.12.0-dev.1591+3fc6a2f11`; use with significantly older or newer versions may require adjustments\n\n## Implementation Notes\n\nTempAllocator utilizes a fixed chunk of virtual address space to allocate from.  The size of this chunk must be specified when initializing the allocator, and can't be changed while the allocator is in use.  But the maximum size may be enormous (up to several terabytes on windows, and possibly even more on other systems).  This is because the full virtual address chunk won't be \"committed\" to physical memory and/or swap pages until it's actually used.\n\n### Windows\nThe full virtual address chunk is allocated with:\n\n    VirtualAlloc(null, capacity, MEM_RESERVE, PAGE_NOACCESS)\n\nThen when regions need to be used, they're committed with:\n\n    VirtualAlloc(ptr, len, MEM_COMMIT, PAGE_READWRITE)\n\nLater, those regions may be decommitted with:\n\n    VirtualFree(ptr, len, MEM_DECOMMIT)\n\n### Linux/MacOS\nThe full virtual address chunk is allocated with:\n\n    mmap(null, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE)\n\nThe use of `MAP_NORESERVE` means we open up the possibility of getting segfaults later when the allocator's memory is written to, if the system runs out of physical memory.  But linux's default `vm.overcommit_memory` sysctl means that can already happen for pretty much any allocation anyway.  Zig's use of errors to try to handle allocation failures is more or less useless on linux.\n\nThe full virtual address range is `mmap`ed as writable, but real pages won't be assigned until they're written to.  But we still keep track of \"committed\" pages so that later we can mark them unused when we would normally decommit them on Windows:\n\n    madvise(ptr, len, MADV_DONTNEED)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcrist%2Fzig-tempallocator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcrist%2Fzig-tempallocator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcrist%2Fzig-tempallocator/lists"}