summaryrefslogtreecommitdiff
path: root/include/obe/arena.h
blob: 5b3490a537d885c5742e5736a8210e7821aa6b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 *
 * This file is part of obe.
 *
 * obe is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * obe is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with obe. If not, see <https://www.gnu.org/licenses/>.
 */
#ifndef OBE_ARENA_H
#define OBE_ARENA_H

#include <stddef.h>
#include <stdint.h>

#define OBE_ARENA_ALIGMENT_BYTES (16)
#define OBE_ARENA_ALIGMENT_MASK (OBE_ARENA_ALIGMENT_BYTES - 1)
#define OBE_ARENA_REGION_DEFAULT_CAPACITY (8 * 1024)
#define OBE_ARENA_PADDING(bytes)                                               \
    ((uint8_t)((OBE_ARENA_ALIGMENT_BYTES - bytes) & OBE_ARENA_ALIGMENT_MASK))

typedef struct obe_arena_region obe_arena_region_t;

struct obe_arena_region
{
    obe_arena_region_t* next;
    size_t offset;
    size_t capacity;
    uint8_t* data;
};

typedef struct obe_arena
{
    obe_arena_region_t* begin;
    obe_arena_region_t* end;
} obe_arena_t;

/* arena */

void*
obe_arena_alloc(obe_arena_t* arena, size_t size);

void*
obe_arena_realloc(obe_arena_t* arena,
                  void* old_ptr,
                  size_t old_size,
                  size_t new_size);

void
obe_arena_release(obe_arena_t* arena);

void
obe_arena_free(obe_arena_t* arena);

/* region */

obe_arena_region_t*
obe_arena_region_new(size_t capacity);

void
obe_arena_region_free(obe_arena_region_t* r);

char*
obe_arena_strdup(obe_arena_t* arena, char* s);

#endif /* OBE_ARENA_H */