{"id":18257919,"url":"https://github.com/linkdd/larena","last_synced_at":"2026-02-27T12:14:53.685Z","repository":{"id":246916599,"uuid":"824671239","full_name":"linkdd/larena","owner":"linkdd","description":"Yet another simple header only arena allocator for C11","archived":false,"fork":false,"pushed_at":"2024-07-05T21:09:32.000Z","size":25,"stargazers_count":41,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-20T17:04:39.678Z","etag":null,"topics":["arena-allocator","c","c11","header-only","memory-management"],"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/linkdd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2024-07-05T16:44:58.000Z","updated_at":"2025-02-06T18:32:31.000Z","dependencies_parsed_at":"2024-09-24T04:30:24.816Z","dependency_job_id":null,"html_url":"https://github.com/linkdd/larena","commit_stats":null,"previous_names":["linkdd/larena"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linkdd%2Flarena","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linkdd%2Flarena/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linkdd%2Flarena/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linkdd%2Flarena/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/linkdd","download_url":"https://codeload.github.com/linkdd/larena/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247229374,"owners_count":20905039,"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":["arena-allocator","c","c11","header-only","memory-management"],"created_at":"2024-11-05T10:28:08.753Z","updated_at":"2026-02-27T12:14:48.649Z","avatar_url":"https://github.com/linkdd.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Little Arena\n\n![tests](https://img.shields.io/github/actions/workflow/status/linkdd/larena/tests.yml?style=flat-square\u0026logo=github\u0026label=tests)\n![license](https://img.shields.io/github/license/linkdd/larena?style=flat-square\u0026color=blue)\n![version](https://img.shields.io/github/v/release/linkdd/larena?style=flat-square\u0026color=red)\n\nThis project is a C11 header only library providing an Arena implementation.\n\n## Installation\n\nCopy the `larena.h` file in your source tree, then in a single `.c` file:\n\n```c\n#define LARENA_IMPLEMENTATION\n#include \"larena.h\"\n```\n\n## Usage\n\nTo create an arena, you first need an allocator:\n\n```c\nlallocator myallocator = {0};\nlallocator_init_stdlib(\u0026myallocator);\n```\n\nThis will initialize (on the stack) an allocator using `stdlib.h`'s `malloc`,\n`realloc` and `free` functions.\n\nTo create a custom allocator:\n\n```c\nvoid *myallocator_alloc(size_t sz, void *ctx) {\n  // ...\n}\n\nvoid *myallocator_realloc(void *ptr, size_t oldsz, size_t newsz, void *ctx) {\n  // ...\n}\n\nvoid myallocator_dealloc(void *ptr, size_t sz, void *ctx) {\n  // ...\n}\n```\n\n```c\nlallocator myallocator = {\n  .alloc   = myallocator_alloc,\n  .realloc = myallocator_realloc,\n  .dealloc = myallocator_dealloc,\n  .ctx     = NULL\n};\n```\n\nOnce your allocator is set-up, you will be able to create an arena:\n\n```c\nlarena myarena = {0};\nlarena_init(\u0026myarena, \u0026myallocator);\n```\n\nYou can then allocate objects with your arena:\n\n```c\nlobject myobj = {0};\n\nif (larena_alloc(\u0026myarena, sizeof(int), \u0026myobj) != 0) {\n  // allocation failed\n}\n```\n\nThe possible error codes are:\n\n| Code | Description |\n| --- | --- |\n| `0` | The allocation was successful |\n| `EOVERFLOW` | The size of the allocation would produce an integer overflow |\n| `ENOMEM` | The heap allocation, to extend the arena, failed |\n\n\u003e **NB:** Error codes are found in the header `errno.h`\n\nAn object is a simple structure containing a reference to the arena used to\nallocate said object, and a pointer relative to the base of the arena.\n\nIf the arena does not have enough capacity, the allocator's `realloc` method\nwill be called to increase the available memory. Because `realloc` can move the\nmemory, old pointers would be invalidated. This is why we store in the object\na **relative** pointer.\n\n\u003e **NB:** The returned memory has been zero'd.\n\nTo access the allocated memory, we need to dereference the object:\n\n```c\nint *mymem = lobject_deref(\u0026myobj);\n```\n\nThe **absolute** pointer will be resolved by summing the arena's base with the\n**relative** pointer contained in the object. This way, there is no pointer\ninvalidation when `realloc` moves the memory.\n\nThe library also provide a `calloc` interface to allocate multiple chunks of\ncontiguous memory at once:\n\n```c\nlobject myobj = {0};\n\nif (larena_calloc(\u0026myarena, 5, sizeof(int), \u0026myobj) != 0) {\n  // allocation failed\n}\n\nint *array = lobject_deref(myobj);\narray[0] = 1;\narray[1] = 2;\narray[2] = 3;\narray[3] = 4;\narray[4] = 5;\n```\n\nOnce you're done with your arena, you can free all the memory at once:\n\n```c\nlarena_free(\u0026myarena);\n// all lobjects are now invalid\n```\n\nBut if you wish to reuse the arena, for example each frame of a game loop, you\ncan simply clear it instead:\n\n```c\nlarena_clear(\u0026myarena);\n```\n\nThe arena's offset is reset to 0, but the allocated memory has not been free'd.\nThis is especially useful for loops so that the arena grows to its maximum\ncapacity only once:\n\n```c\nlarena myarena = {0};\nlarena_init(\u0026myarena);\n\nwhile (true) {\n  // do work, and some allocations\n\n  larena_clear(\u0026myarena);\n}\n\nlarena_free(\u0026myarena);\n```\n\n## License\n\nThis library is distributed under the terms of the [MIT License](./LICENSE.txt).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinkdd%2Flarena","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinkdd%2Flarena","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinkdd%2Flarena/lists"}