about summary refs log tree commit diff stats
path: root/src/arena.c
blob: e38ad20dd84086ce8813e371c6e008bab79064c9 (plain) (blame)
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
/*
 * arena.c
 * arena allocator i wrote during a meeting in 30 mins
 * imogen sorindeia thoms 2025
 * */

#include <stdlib.h>
#include <stdint.h>

#include "arena.h"

/* initialize an arena with given capacity */
mogi_arena_t mogi_arena_init(size_t size) {
	mogi_arena_t a = {
			.buffer = calloc(size, 1),
			.capacity = size
	};
	return a;
}

/* get rid of entire arena */
void mogi_arena_dispose(mogi_arena_t *a) {
	if(NULL != a->buffer) {
		free(a->buffer);
	}
}

/* allocate space on arena
 * TODO: alignment l m f a o
 */
uintptr_t mogi_arena_allocate(mogi_arena_t *a, size_t size) {
	if((NULL == a->buffer) || ((a->posn + size) > a->capacity)) {
		return (uintptr_t)0;
	}
	uintptr_t p = (uintptr_t)(a->buffer + a->posn);
	a->posn += (uintptr_t)size;
	return p;
}