aboutsummaryrefslogtreecommitdiff
path: root/src/lib/mem.c
blob: 2f50d3d8ac8a36fc3c099d9286ac74aae1256977 (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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <drivers/uart.h>
#include <globals.h>
#include <graphics/lfb.h>
#include <lib/mem.h>
#include <sys/schedule.h>

void memcpyrot(unsigned char* src, struct RotBuffer* rb, unsigned int n)
{
	if (n > rb->size)
		return;
	unsigned char* base = rb->base;
	unsigned int offset = rb->woffset;
	for (unsigned int i = 0; i < n; i++) {
		if (offset >= rb->size)
			offset = 0;
		base[offset] = src[i];
		offset++;
	}
	rb->woffset = offset;
}

void memshow(unsigned char* addr, unsigned int n)
{
	unsigned char temp;
	for(unsigned int i = 0; i < n; i++) {
		temp = addr[i] >> 4;
		temp += 0x30;
		if (temp > 0x39)
			temp += 7;
		uart_char(temp);
		temp = addr[i];
		temp += 0x30;
		if (temp > 0x39)
			temp += 7;
		uart_char(temp);
		if (i+1 != n)
			uart_char(0x20);
	}
	uart_char(0x0a);
}

void memshow32(unsigned long* addr, unsigned int n)
{
	for(unsigned int i = 0; i < n; i++) {
		uart_hex(addr[i]);
		if (i+1 != n)
			uart_char(0x20);
	}
	uart_char(0x0a);
}

void memset(unsigned char* dest, unsigned char value, unsigned int n)
{
	for(unsigned int i = 0; i < n; i++) {
		dest[i] = value;
	}
}

void memcpy(unsigned char* src, unsigned char* dest, unsigned int n)
{
	for(unsigned int i = 0; i < n; i++) {
		dest[i] = src[i];
	}
}

unsigned char memcmp(unsigned char* a, unsigned char* b, unsigned int n)
{
	for(unsigned int i = 0; i < n; i++) {
		if (a[i] != b[i])
			return 0;
	}
	return 1;
}

void memset32(unsigned long* dest, unsigned long value, unsigned int n)
{
	for(unsigned int i = 0; i < n; i++) {
		dest[i] = value;
	}
}

void memcpy32(unsigned long* src, unsigned long* dest, unsigned int n)
{
	for(unsigned int i = 0; i < n; i++) {
		dest[i] = src[i];
	}
}

unsigned char memcmp32(unsigned long* a, unsigned long* b, unsigned int n)
{
	for(unsigned int i = 0; i < n; i++) {
		if (a[i] != b[i])
			return 0;
	}
	return 1;
}


void* malloc(unsigned char size)
{
	unsigned char* mem = (unsigned char*)rpi_heap;
	unsigned long i = 0;
	// TODO: Use Null PID
	while (((void*)(mem + i) < rpi_heap_top) && !(mem[i + MEM_SIZE_OFFSET] == size && mem[i + MEM_USE_OFFSET]==0)) {
		i += mem[i] + MEM_META_SIZE;
	}
	if (mem + i >= (unsigned char*)(rpi_heap_top + MAX_MM))
		return NULL;
	// Update top of heap
	if (mem[i] == 0)
		rpi_heap_top = (void*)&mem[i + MEM_META_SIZE + size];
	mem[i] = size;
	mem[i + MEM_BASE_SIZE + size] = size;
	// Use allocator's PID
	mem[i + MEM_USE_OFFSET] = 1;
	return (void*)&mem[i + MEM_BASE_SIZE];
}

void* calloc(unsigned char size)
{
	void* addr = malloc(size);
	if (addr != NULL) {
		memset(addr, 0, size);
		return addr;
	}
	return NULL;
}
void* realloc(void* old, unsigned char size)
{
	unsigned char osize = *(unsigned char*)(old - MEM_BASE_SIZE);
	if (size <= osize)
		return old;
	void* new = malloc(size);
	if (new != NULL) {
		free(old);
		return new;
	}
	return NULL;
}

void* malloca(unsigned char size, unsigned char amnt)
{
	// Return malloc if alignment size is 0 or 1 - trivial alignment
	if(amnt == 0 || amnt == 1)
		return malloc(size);
	unsigned char* mem = (unsigned char*)rpi_heap;
	unsigned long i = 0;
	// TODO: Use Null PID
	while(mem + i < rpi_heap + MAX_MM) {
		unsigned long diff = (unsigned long)mem + i + MEM_BASE_SIZE;
		diff %= amnt;
		diff = amnt - diff;
		diff %= amnt;
		if((mem[i + MEM_SIZE_OFFSET] == size) && mem[i + MEM_USE_OFFSET]==0) {
			if(diff == 0) {
				mem[i + MEM_SIZE_OFFSET] = size;
				mem[i + MEM_BASE_SIZE + size] = size;
				mem[i + MEM_USE_OFFSET] = 1;
				return (void*)&mem[i + MEM_BASE_SIZE];
			}
		} else if (mem[i] == 0) {
			if(diff == 0) {
				mem[i + MEM_SIZE_OFFSET] = size;
				mem[i + MEM_BASE_SIZE + size] = size;
				mem[i + MEM_USE_OFFSET] = 1;
				rpi_heap_top = (void*)&mem[i + MEM_META_SIZE + size];
				return (void*)&mem[i + MEM_BASE_SIZE];
			} else {
				while (diff <= MEM_BASE_SIZE) {
					diff += amnt;
				}
				unsigned long empty_size = diff - MEM_META_SIZE;
				if(empty_size == 0) {
					empty_size += amnt;
					diff += amnt;
				}
				mem[i + MEM_SIZE_OFFSET] = empty_size;
				mem[i + MEM_BASE_SIZE + empty_size] = empty_size;
				i += diff;
				mem[i + MEM_SIZE_OFFSET] = size;
				mem[i + MEM_BASE_SIZE + size] = size;
				mem[i + MEM_USE_OFFSET] = 1;
				rpi_heap_top = (void*)&mem[i + MEM_META_SIZE + size];
				return (void*)&mem[i + MEM_BASE_SIZE];
			}
		}

		i += mem[i] + MEM_META_SIZE;
	}
	return NULL;
}

void* calloca(unsigned char size, unsigned char amnt)
{
	void* addr = malloca(size, amnt);
	if (addr != NULL) {
		memset(addr, 0, size);
		return addr;
	}
	return NULL;
}

void* realloca(void* old, unsigned char size, unsigned char amnt)
{
	unsigned char osize = *(unsigned char*)(old - MEM_BASE_SIZE);
	if (size <= osize)
		return old;
	void* new = malloca(size, amnt);
	if (new != NULL) {
		free(old);
		return new;
	}
	return NULL;
}

void free(void* memloc)
{
	// Don't try to free memory outside of heap
	if(!(((void*)rpi_heap <= memloc) && (memloc < rpi_heap_top)))
		return;
	unsigned char* base = memloc - MEM_BASE_SIZE;
	unsigned char size = base[MEM_SIZE_OFFSET];
	// TODO: Use Null PID
	base[MEM_USE_OFFSET] = 0;
	// Clear out old memory
	for(unsigned int i = 0; i < size; i++) {
		base[i + MEM_BASE_SIZE] = 0;
	}
	// If it is the last entry, clear it and move the heap top down
	if (base + size + MEM_META_SIZE == rpi_heap_top) {
		while(base[MEM_USE_OFFSET] == 0 && base >= rpi_heap) {
			base[MEM_SIZE_OFFSET] = 0;
			base[MEM_BASE_SIZE + size] = 0;
			rpi_heap_top = base;
			unsigned long psize = *(base - 1);
			base -= psize + MEM_META_SIZE;
		}
	}
}

void* heap_base(void)
{
	return (void*)rpi_heap;
}

void* heap_top(void)
{
	return rpi_heap_top;
}

void vheap_info(void)
{
	unsigned char* base = rpi_heap;
	unsigned long ioff = STACK_DRAW_WIDTH*STACK_DRAW_SIZE;
	unsigned long yoff = STACK_DRAW_YOFF;
	draw_cbox(ioff, yoff, STACK_DRAW_SIZE*STACK_DRAW_WIDTH, STACK_DRAW_SIZE * 8, 0x0);
	while ((void*)base < rpi_heap_top) {
		unsigned char size = base[MEM_SIZE_OFFSET];
		if(base[MEM_USE_OFFSET] == 0) {
			draw_cbox(ioff, yoff, STACK_DRAW_SIZE, STACK_DRAW_SIZE, 0x00FF00);
		} else {
			draw_cbox(ioff, yoff, STACK_DRAW_SIZE, STACK_DRAW_SIZE, 0xFF0000);
		}
		ioff += STACK_DRAW_SIZE;
		if(ioff % STACK_DRAW_WIDTH == 0) {
			yoff += STACK_DRAW_SIZE;
			ioff = STACK_DRAW_WIDTH*STACK_DRAW_SIZE;
		}
		base += size + MEM_META_SIZE;
	}
}

void heap_info(void)
{
	unsigned char* base = rpi_heap;
	while ((void*)base < rpi_heap_top) {
		unsigned char size = base[MEM_SIZE_OFFSET];
		if(base[MEM_USE_OFFSET] == 0) {
			uart_char('F');
			uart_char(' ');
		} else {
		}
		uart_hex((unsigned long)(base + MEM_BASE_SIZE));
		uart_string(" Size: ");
		uart_10(size);
		uart_string("\n");
		static char* data = "00 \0";
		static unsigned char temp = 0;
		for(unsigned int i = 0; i < size; i++) {
			temp = (base[MEM_BASE_SIZE + i]>>4)&0xF;
			if(temp > 9)
				temp += 7;
			temp += 0x30;
			data[0] = temp;
			temp = (base[MEM_BASE_SIZE + i])&0xF;
			if(temp > 9)
				temp += 7;
			temp += 0x30;
			data[1] = temp;
			uart_string(data);
		}
		uart_char('\n');
		base += size + MEM_META_SIZE;
	}
	uart_char('\n');
}

void heap_info_u(void)
{
	unsigned char* base = rpi_heap;
	while ((void*)base < rpi_heap_top) {
		unsigned char size = base[MEM_SIZE_OFFSET];
		if(base[MEM_USE_OFFSET] == 0) {
			base += size + MEM_META_SIZE;
			continue;
		}
		uart_hex((unsigned long)(base + MEM_BASE_SIZE));
		uart_string(" Size: ");
		uart_10(size);
		uart_string("\n");
		static char* data = "00 \0";
		static unsigned char temp = 0;
		for(unsigned int i = 0; i < size; i++) {
			temp = (base[MEM_BASE_SIZE + i]>>4)&0xF;
			if(temp > 9)
				temp += 7;
			temp += 0x30;
			data[0] = temp;
			temp = (base[MEM_BASE_SIZE + i])&0xF;
			if(temp > 9)
				temp += 7;
			temp += 0x30;
			data[1] = temp;
			uart_string(data);
		}
		uart_char('\n');
		base += size + MEM_META_SIZE;
	}
	uart_char('\n');
}