summaryrefslogtreecommitdiff
path: root/base.cpp
blob: 95135a0642e3894f92c4af1e6d44db23aa06cf0f (plain)
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
// this file includes functions, etc. used just about everywhere

// allocates aligned temporary memory 
static u8 *tmp_alloc(State *state, size_t bytes) {
	u32 used = state->tmp_mem_used;
	u32 max_aligns_needed = (u32)(bytes + sizeof(MaxAlign) - 1) / (u32)sizeof(MaxAlign); // = ceil(bytes / sizeof(MaxAlign)) 
	MaxAlign *ret = state->tmp_mem + used;
	if (bytes == 0) {
		return NULL;
	}
	if (used + max_aligns_needed > arr_count(state->tmp_mem)) {
		printf("%zu\n",bytes);
		assert(0);
		return NULL;
	}
	state->tmp_mem_used += max_aligns_needed;
	memset(ret, 0, bytes);
	return (u8 *)ret;
}

/* 
these functions save and restore the state of the temporary memory.
*/
static u32 tmp_push(State *state) {
	return state->tmp_mem_used;
}

static void tmp_pop(State *state, u32 mark) {
	state->tmp_mem_used = mark;
}

#define tmp_alloc_object(state, type) ((type *)tmp_alloc((state), sizeof(type)))
#define tmp_alloc_arr(state, type, n) ((type *)tmp_alloc((state), (n) * sizeof(type)))
#define calloc_object(type) ((type *)calloc(1, sizeof(type)))
#define calloc_arr(type, n) ((type *)calloc((n), sizeof(type)))