1#pragma once
 2
 3#include <stddef.h>
 4#include <stdio.h>
 5
 6#include <libpussy/alignment.h>
 7
 8#ifdef __cplusplus
 9extern "C" {
10#endif
11
12typedef struct _Arena Arena;
13/*
14 * Arena is a singly linked list of regions allocated with mmap.
15 */
16
17Arena* create_arena(unsigned capacity);
18/*
19 * Create new arena with one region of desired capacity.
20 *
21 * The capacity of subsequent regions will have
22 * the same capacity unless adjusted with `set_region_capacity`.
23 */
24
25void delete_arena(Arena* arena);
26/*
27 * Free arena and all its regions.
28 */
29
30void set_region_capacity(Arena* arena, unsigned capacity);
31/*
32 * Set desired capacity for newly created regions.
33 */
34
35void* _arena_alloc(Arena* arena, unsigned size, unsigned alignment);
36/*
37 * Allocate `size` bytes from the last region aligned at `alignment` boundary.
38 * If the last region has no space available, allocate new region.
39 *
40 * This is a low-level function because its arguments
41 * tell how to allocate a block.
42 *
43 * The following macro is a wrapper to allocate things of specific type:
44 */
45
46#define arena_alloc(arena, num_elements, element_type) \
47    _arena_alloc((arena), (num_elements) * sizeof(element_type), alignof(element_type))
48
49void* _arena_fit(Arena* arena, unsigned size, unsigned alignment);
50/*
51 * Try to find a region with sufficient free space
52 * and allocate from it.
53 * If no region has space available, allocate new region.
54 *
55 * This is a low-level function because its arguments
56 * tell how to allocate a block.
57 *
58 * The following macro gets what to allocate:
59 */
60
61#define arena_fit(arena, num_elements, element_type) \
62    _arena_fit((arena), (num_elements) * sizeof(element_type), alignof(element_type))
63
64void arena_print(FILE* fp, Arena* arena);
65
66#ifdef __cplusplus
67}
68#endif