aboutsummaryrefslogtreecommitdiff
path: root/include/alloc.h
blob: b9e03e2f3f7016778ac2cc53892de589be94c6e2 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef MLIB_ALLOC_H
#define MLIB_ALLOC_H

#include <setjmp.h>
#include <stddef.h>

#include "_attrs.h"

#ifndef MLIB_ARENA_BLKSIZE
#	define MLIB_ARENA_BLKSIZE (8 * 1024)
#endif

struct _region {
	size_t len, cap;
	void *data, *last;
	struct _region *next;
};

typedef struct {
	struct _region *_head;
	size_t _init;
} arena;

/* Heap allocation functions */
[[nodiscard, gnu::returns_nonnull]] void *bufalloc(void *, size_t, size_t);
[[nodiscard]] void *bufalloc_noterm(void *, size_t, size_t);

[[_mlib_pure, _mlib_inline]]
static inline arena
mkarena(size_t n)
{
	return (arena){._init = n ? n : MLIB_ARENA_BLKSIZE};
}

/* Arena allocation functions */
[[nodiscard, gnu::malloc, gnu::alloc_size(2, 3), gnu::alloc_align(4)]]
void *arena_alloc(arena *, size_t, size_t, size_t);
[[nodiscard]]
void *arena_realloc(arena *, void *, size_t, size_t, size_t, size_t);
void arena_zero(arena *);
void arena_free(arena *);

/* Arena allocation macro wrappers */
#define arena_new(a, T, n) ((T *)arena_alloc((a), (n), sizeof(T), alignof(T)))
#define arena_resz(a, T, p, n)                                                 \
	((T *)arena_realloc((a), (p), (n), sizeof(T), alignof(T)))

/* Memory allocator callbacks for memory-allocating functions */
struct arena_ctx {
	arena *a;
	jmp_buf *jmp;
};
struct heap_ctx {
	jmp_buf *jmp;
};

[[nodiscard]] void *alloc_arena(void *, void *, size_t, size_t, size_t);
[[nodiscard]] void *alloc_heap(void *, void *, size_t, size_t, size_t);

#endif /* !MLIB_ALLOC_H */