59static inline void arena_init(
struct arena *self,
const size_t len,
unsigned char *backing_buf)
66 assert(len >= padding);
68 self->
buf_ptr = &backing_buf[padding];
105 const bool has_space_left =
align(alignment, size, &ptr, &space_left);
106 if (!has_space_left) {
110 const uintptr_t relative_offset = (uintptr_t)((
unsigned char *)ptr - &self->
buf_ptr[0]);
115 memset(ptr, 0, size);
137static inline void *internal_arena_try_optimizing_w_prev_offset(
struct arena *self,
unsigned char *old_ptr,
138 const size_t old_size,
const size_t new_size)
146 if (new_size > old_size) {
147 const size_t diff = new_size - old_size;
171 const size_t old_size,
const size_t new_size)
176 unsigned char *old_ptr = (
unsigned char *)old_ptr_;
178 const bool misc_input = old_ptr == NULL || old_size == 0 || new_size == 0;
179 const bool inside_arena_buf = &self->
buf_ptr[0] <= old_ptr && old_ptr <= &self->
buf_ptr[self->
buf_len - 1];
180 if (misc_input || !inside_arena_buf) {
184 const bool has_optimized_w_prev_buf =
185 internal_arena_try_optimizing_w_prev_offset(self, old_ptr, old_size, new_size);
186 if (has_optimized_w_prev_buf) {
190 const size_t copy_size = old_size < new_size ? old_size : new_size;
194 memmove(new_mem, old_ptr, copy_size);
211static inline void *
arena_reallocate(
struct arena *self,
void *old_ptr,
const size_t old_size,
const size_t new_size)
static uintptr_t calc_alignment_padding(const size_t alignment, const uintptr_t ptr)
Calculate the alignment padding required to align a pointer.
Definition align.h:91
static void * align(const size_t alignment, const size_t size, void **ptr_ptr, size_t *space_ptr)
Align pointer to the next alignment boundary.
Definition align.h:43
static void arena_deallocate_all(struct arena *self)
Deallocate all allocations in the arena.
Definition arena.h:79
static void * arena_reallocate(struct arena *self, void *old_ptr, const size_t old_size, const size_t new_size)
Reallocate a previously allocated chunk in the arena.
Definition arena.h:211
static void * arena_allocate_aligned(struct arena *self, const size_t alignment, const size_t size)
Get the pointer to a chunk of the arena. With specific alignment.
Definition arena.h:97
static void * arena_reallocate_aligned(struct arena *self, void *old_ptr_, const size_t alignment, const size_t old_size, const size_t new_size)
Reallocate a previously allocated chunk in the arena. With specific aligment.
Definition arena.h:170
static void arena_init(struct arena *self, const size_t len, unsigned char *backing_buf)
Initialize the arena.
Definition arena.h:59
static void temp_arena_state_restore(struct temp_arena_state prev_state)
Restore the arena state.
Definition arena.h:237
static struct temp_arena_state temp_arena_state_save(struct arena *arena_ptr)
Save the arena state temporarily.
Definition arena.h:223
static void * arena_allocate(struct arena *self, const size_t size)
Get the pointer to a chunk of the arena.
Definition arena.h:129
Check if a number is a power of two.
static size_t is_pow2(const size_t x)
Check if a number is a power of two.
Definition is_pow2.h:34
Arena data struct.
Definition arena.h:36
unsigned char * buf_ptr
Underlying buffer pointer.
Definition arena.h:40
size_t prev_offset
Previous offset relative to buf_ptr.
Definition arena.h:38
size_t buf_len
Underlying buffer length.
Definition arena.h:37
size_t curr_offset
Current offset relative to buf_ptr.
Definition arena.h:39
Tempory arena state struct.
Definition arena.h:46
size_t curr_offset
Arena curr offset.
Definition arena.h:49
struct arena * arena_ptr
Arena pointer.
Definition arena.h:47
size_t prev_offset
Arena prev offset.
Definition arena.h:48