diff options
Diffstat (limited to 'include/obe/arena.h')
| -rw-r--r-- | include/obe/arena.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/include/obe/arena.h b/include/obe/arena.h new file mode 100644 index 0000000..5b3490a --- /dev/null +++ b/include/obe/arena.h @@ -0,0 +1,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 */ |
