https://github.com/amateur80lvl/arena
Arena allocator
https://github.com/amateur80lvl/arena
arena arena-allocator
Last synced: 11 months ago
JSON representation
Arena allocator
- Host: GitHub
- URL: https://github.com/amateur80lvl/arena
- Owner: amateur80lvl
- License: lgpl-3.0
- Created: 2024-07-29T08:30:17.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-30T04:17:35.000Z (almost 2 years ago)
- Last Synced: 2024-12-26T03:42:04.602Z (over 1 year ago)
- Topics: arena, arena-allocator
- Language: C
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Arena allocator
## Features
* multiple regions
* allocated values are properly aligned
* `mmap` as underlying allocator
The capacity of arena itself and all its regions is always multiple
of page size.
C23 is required. Just because I love **nullptr**.
Plus, GNU extensions are required for `mmap`.
In notoriously stable Devuan this can be compiled with
```bash
clang-16 --std=gnu2x
```
(as of July 2024)
## How to use
```c
#include
#include "arena.h"
int main(int argc, char* argv[])
{
Arena *arena = create_arena(0);
for(int i = 0; i < 500; i++) {
arena_alloc(arena, 3, long long);
}
arena_fit(arena, 10, char);
arena_fit(arena, 3000, char);
arena_print(arena);
delete_arena(arena);
}
```
## API
```c
Arena* create_arena(size_t capacity);
```
Create new arena with desired capacity
or at least one page.
The capacity of subsequent regions will be
same capacity unless adjusted with `set_region_capacity`.
```c
void delete_arena(Arena* arena);
```
Free arena and all its regions.
```c
void set_region_capacity(Arena* arena, size_t capacity);
```
Set desired capacity for newly created regions.
```c
void* arena_alloc(Arena* arena, size_t num_elements, element_type_name);
```
Allocate properly aligned `num_elements` from the last region.
If the last region has no space available, allocate new region.
```c
void* arena_fit(Arena* arena, size_t num_elements, element_type_name);
```
Try to find a region with sufficient free space
and allocate from it.
If no region has space available, allocate new region.