From 10bb8a05088f1d3bb24f7167f609b5f6fb0ba026 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Wed, 30 Oct 2024 22:58:03 +0100 Subject: bootstrap project Signed-off-by: Johnny Richard --- include/obe/arena.h | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 include/obe/arena.h (limited to 'include/obe/arena.h') 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 + * + * 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 . + */ +#ifndef OBE_ARENA_H +#define OBE_ARENA_H + +#include +#include + +#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 */ -- cgit v1.2.3