summaryrefslogtreecommitdiff
path: root/memory.c
blob: 9dd99c2f92ce184f1573fbd4815a2b78d8a50fc7 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "config.h"
#include "error.h"
#include "memory.h"
#include "plat.h"

#include <stddef.h>
#include <stdint.h>
#if use_system_malloc
#include <stdlib.h>
#endif

static Heap global_heap;
static char* buffer;

static int stack_idx;

void memory_init(void* memory) {
	buffer = (char*)memory;
	stack_idx = 0;
	init_heap(
		&global_heap,
		stack_alloc(memory_heap_size),
		memory_heap_size
	);
}

static void* stack_alloc_impl(int size) {
	char* a;
#ifdef DEBUG
	if (stack_idx + size > memory_size) {
		print_err("Stack exhausted.\n");
		pbreak(error_out_of_memory);
	}
#endif
	a = &buffer[stack_idx];
	stack_idx += size;
	return a;
}

void* stack_alloc(int size) {
	return stack_alloc_aligned(
		size,
		allocation_default_alignment
	);
}

static uintptr_t align_address(
	uintptr_t address,
	size_t align
) {
	size_t m;
	m = align - 1;
	return (address + m) & ~m;
}

void* stack_alloc_aligned(int size, unsigned align) {
	char* p;
	size += (int)align + 1;
	p = stack_alloc_impl(size);
	p = (char*)align_address((uintptr_t)p, (size_t)align);
	return p;
}

void init_heap(Heap* heap, char* buf, int size) {
	heap->buffer = buf;
	heap->size = size;
	heap->blocks = 1;
	heap->usage = 0;
	((int*)buf)[0] = size << 1;
}

static void* heap_alloc_impla(Heap* h, int size) {
	int* header, * nh, s, f, i, as, p, m;
	i = 0;
	as = size + sizeof *header;
	p = 0;
	m = sizeof *header;
	h->usage += as;
	for (i = 0; i < h->blocks; i++) {
		header = (int*)(h->buffer + p);
		s = header[0];
		f = !(s & 1);
		s >>= 1;
		if (f) {
			if (as < s - m) {
				s -= as;
				header[0] = (s << 1);
				nh = (int*)(h->buffer + p + s);
				nh[0] = (as << 1) | 1;
				h->blocks++;
				return nh + 1;
			} else if (s == as) {
				header[0] |= 1;
				return header + 1;
			}
		}
		p += s;
	}
	return 0;
}

static void heap_defrag(Heap* h) {
	int* header, *h2, i, j, s, f, d, n, ns, p;
	p = n = 0;
	for (i = 0; i < h->blocks; i++) {
		header = (int*)(h->buffer + p);
		s = header[0];
		f = !(s & 1);
		s >>= 1;
		if (f) {
			ns = s;
			p += s;
			d = 0;
			for (j = i + 1; j < h->blocks; j++) {
				h2 = (int*)(h->buffer + p);
				s = h2[0];
				f = !(s & 1);
				if (!f) { break; }
				s >>= 1;
				p += s;
				ns += s;
				d++;
			}
			i = j - 1;
			n += d;
			if (d)
				header[0] = ns << 1;
		} else
			p += s;
	}
	h->blocks -= n;
}

static void* heap_alloc_impl(Heap* h, int size) {
	void* ptr = heap_alloc_impla(h, size);
	if (!ptr) {
		heap_defrag(h);
		ptr = heap_alloc_impla(h, size);
		if (!ptr) {
			print_err("Heap exhausted.\n");
			pbreak(error_out_of_memory);
		}
		return ptr;
	}
	return ptr;
}

static void heap_free_impl(Heap* h, void* ptr) {
	int* header, s;
	header = &((int*)ptr)[-1];
	s = header[0] >> 1;
	h->usage -= s;
	*header &= INT32_MAX << 1;
}

void heap_print_blocks(const Heap* h) {
	const int* header;
	int s, f, p, i;
	p = 0;
	print("%8s %8s %s\n", "offset", "size", "free");
	for (i = 0; i < h->blocks; i++) {
		header = (const int*)(h->buffer + p);
		s = header[0];
		f = !(s & 1);
		s >>= 1;
		print("%8d %8d %d\n", p, s, f);
		p += s;
	}
}

void* heap_alloc_aligned(Heap* h, int size, int align) {
	unsigned char* p, * a;
	ptrdiff_t shift;
	size += (int)align;
	p = heap_alloc_impl(h, size);
	if (!p) { return 0; }
	a = (unsigned char*)align_address((uintptr_t)p, align);
	a += align * (unsigned)(p == a);
	shift = a - p;
	a[-1] = shift & 0xff;
	return a;
}

void heap_free_aligned(Heap* h, void* ptr) {
	unsigned char* a;
	ptrdiff_t shift;
	a = ptr;
	shift = a[-1];
	shift += 256 * shift == 0;
	a -= shift;
	heap_free_impl(h, a);
}

void* heap_alloc(Heap* h, int size) {
	return heap_alloc_aligned(
		h,
		size,
		allocation_default_alignment
	);
}

void heap_free(Heap* h, void* ptr) {
	heap_free_aligned(h, ptr);
}

void* galloc(int size) {
#if use_system_malloc
	return malloc(size);
#else
	return heap_alloc(&global_heap, size);
#endif
}

void gfree(void* ptr) {
#if use_system_malloc
	free(ptr);
#else
	heap_free(&global_heap, ptr);
#endif
}

int get_stack_usage() {
	return stack_idx;
}

int get_heap_usage() {
	return global_heap.usage;
}

void init_arena(Arena* arena, char* buffer, int size) {
	arena->buffer = buffer;
	arena->size = size;
	arena->ptr = 0;
}

static void* arena_alloc_impl(Arena* arena, int size) {
	char* a;
#ifdef DEBUG
	if (arena->ptr + size > arena->size) {
		print_err("Arena exhausted.\n");
		pbreak(error_out_of_memory);
	}
#endif
	a = &arena->buffer[arena->ptr];
	arena->ptr += size;
	return a;
}

void* arena_alloc(Arena* arena, int size) {
	return arena_alloc_aligned(
		arena,
		size,
		allocation_default_alignment
	);
}

void* arena_alloc_aligned(
	Arena* arena,
	int size,
	int align
) {
	char* p;
	size += (int)align + 1;
	p = arena_alloc_impl(arena, size);
	p = (char*)align_address((uintptr_t)p, (size_t)align);
	return p;
}