| 1 |
/// @file |
| 2 |
/// @brief Region-based memory allocator |
| 3 |
/// |
| 4 |
/// The API below is for interacting with a basic bump-pointer allocator.¹ |
| 5 |
/// |
| 6 |
/// ¹ https://en.wikipedia.org/wiki/Region-based_memory_management |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include <stdalign.h> |
| 11 |
#include <stddef.h> |
| 12 |
#include <util/api.h> |
| 13 |
|
| 14 |
#ifdef __cplusplus |
| 15 |
extern "C" { |
| 16 |
#endif |
| 17 |
|
| 18 |
/// a block of backing memory |
| 19 |
/// |
| 20 |
/// This is a private implementation detail and should not be used outside of |
| 21 |
/// arena.c |
| 22 |
typedef struct arena_chunk arena_chunk_t; |
| 23 |
|
| 24 |
/// an arena from which dynamic memory can be allocated |
| 25 |
/// |
| 26 |
/// This structure is intended to be zero-initialized: |
| 27 |
/// |
| 28 |
/// arena_t new_arena = {0}; |
| 29 |
/// |
| 30 |
/// All fields are considered private and should not be used outside of arena.c. |
| 31 |
typedef struct { |
| 32 |
arena_chunk_t *source; ///< current chunk being allocated out of |
| 33 |
size_t remaining; ///< number of free bytes remaining in `source` |
| 34 |
} arena_t; |
| 35 |
|
| 36 |
/// allocate new memory |
| 37 |
/// |
| 38 |
/// The requested `alignment` must be a power of two. The returned memory is |
| 39 |
/// zero initialized. |
| 40 |
/// |
| 41 |
/// @param arena Arena to allocate from |
| 42 |
/// @param alignment Required pointer alignment for the resulting allocation |
| 43 |
/// @param size Number of bytes requested |
| 44 |
/// @return Pointer to allocated memory |
| 45 |
UTIL_API void *gv_arena_alloc(arena_t *arena, size_t alignment, size_t size); |
| 46 |
|
| 47 |
/// allocate new memory for a typed value |
| 48 |
/// |
| 49 |
/// This is shorthand for when you are allocating memory for a specific type. |
| 50 |
/// The returned memory is zero initialized. |
| 51 |
/// |
| 52 |
/// @param arena Arena to allocate from |
| 53 |
/// @param type Type of value that will be later stored in this memory |
| 54 |
/// @return Pointer to allocated memory |
| 55 |
#define ARENA_NEW(arena, type) \ |
| 56 |
gv_arena_alloc((arena), alignof(type), sizeof(type)) |
| 57 |
|
| 58 |
/// copy a string into new dynamic memory |
| 59 |
/// |
| 60 |
/// @param arena Arena to allocate from |
| 61 |
/// @param s Source string to copy |
| 62 |
/// @return A copy of the string, hosted in arena-allocated memory |
| 63 |
UTIL_API char *gv_arena_strdup(arena_t *arena, const char *s); |
| 64 |
|
| 65 |
/// deallocate memory |
| 66 |
/// |
| 67 |
/// @param arena Arena that was previously used to allocate this memory |
| 68 |
/// @param ptr Pointer to memory to release |
| 69 |
/// @param size Number of bytes of the original allocation |
| 70 |
UTIL_API void gv_arena_free(arena_t *arena, void *ptr, size_t size); |
| 71 |
|
| 72 |
/// deallocate all memory |
| 73 |
/// |
| 74 |
/// Calling this restores the arena to an empty state from which it can be used |
| 75 |
/// for more allocations. |
| 76 |
/// |
| 77 |
/// @param arena Arena to reset |
| 78 |
UTIL_API void gv_arena_reset(arena_t *arena); |
| 79 |
|
| 80 |
#ifdef __cplusplus |
| 81 |
} |
| 82 |
#endif |