aboutsummaryrefslogtreecommitdiff
path: root/src/lib/mem.c
blob: f8a8cc5e165dc1bfed556aaf08ae0b90e1b9901b (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
#include <drivers/uart.h>
#include <lib/mem.h>

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 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 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;
}

#define MAX_MM 0x100000
static unsigned char rpi_heap[MAX_MM] = {0,};
static void* rpi_heap_top = &rpi_heap;

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] == size && mem[i+1]==0)) {
		i += mem[i]+2;
	}
	// Update top of heap
	if (mem[i] == 0)
		rpi_heap_top = (void*)&mem[i+2+size];
	mem[i] = size;
	// Use allocator's PID
	mem[i+1] = 1;
	return (void*)&mem[i+2];
}

void* malloca(unsigned char size, unsigned char amnt)
{
	unsigned char* mem = (unsigned char*)rpi_heap;
	unsigned long i = 0;
	// TODO: Use Null PID
	while(1) {
		unsigned long diff = (unsigned long)mem + i + 2;
		diff %= amnt;
		diff = amnt - diff;
		diff %= amnt;
		if((mem[i] == size) && mem[i+1]==0) {
			if(diff == 0) {
				mem[i] = size;
				mem[i+1] = 1;
				return (void*)&mem[i+2];
			}
		} else if (mem[i] == 0) {
			if(diff == 0 || diff == amnt) {
				mem[i] = size;
				mem[i+1] = 1;
				rpi_heap_top = (void*)&mem[i+2+size];
				return (void*)&mem[i+2];
			} else {
				if(diff <= 2) {
					diff += amnt;
				}
				mem[i] = diff-2;
				i += diff;
				mem[i] = size;
				mem[i+1] = 1;
				rpi_heap_top = (void*)&mem[i+2+size];
				return (void*)&mem[i+2];
			}
		}

		i += mem[i]+2;
	}
}

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 - 2;
	unsigned char size = *base;
	// TODO: Use Null PID
	base[1] = 0;
	// Clear out old memory
	for(unsigned int i = 0; i < size; i++) {
		base[i+2] = 0;
	}
	// If it is the last entry, clear it and move the heap top down
	if (base + size + 2 == rpi_heap_top) {
		base[0] = 0;
		rpi_heap_top = base;
	}
}

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

void* heap_top(void)
{
	return rpi_heap_top;
}

void heap_info(void)
{
	unsigned char* base = rpi_heap;
	while ((void*)base < rpi_heap_top) {
		unsigned char size = *base;
		if(base[1] == 0) {
			uart_char('F');
			uart_char(' ');
		}
		uart_hex((unsigned long)(base+2));
		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[2+i]>>4)&0xF;
			if(temp > 9)
				temp += 7;
			temp += 0x30;
			data[0] = temp;
			temp = (base[2+i])&0xF;
			if(temp > 9)
				temp += 7;
			temp += 0x30;
			data[1] = temp;
			uart_string(data);
		}
		uart_char('\n');
		base += size + 2;
	}
	uart_char('\n');
}

void heap_info_u(void)
{
	unsigned char* base = rpi_heap;
	while ((void*)base < rpi_heap_top) {
		unsigned char size = *base;
		if(base[1] == 0) {
			base += size + 2;
			continue;
		}
		uart_hex((unsigned long)(base+2));
		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[2+i]>>4)&0xF;
			if(temp > 9)
				temp += 7;
			temp += 0x30;
			data[0] = temp;
			temp = (base[2+i])&0xF;
			if(temp > 9)
				temp += 7;
			temp += 0x30;
			data[1] = temp;
			uart_string(data);
		}
		uart_char('\n');
		base += size + 2;
	}
	uart_char('\n');
}