-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratchmips-essentials.c
More file actions
323 lines (279 loc) · 6.7 KB
/
scratchmips-essentials.c
File metadata and controls
323 lines (279 loc) · 6.7 KB
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
#define NULL ((void *)0)
#define bool int
#define true 1
#define false 0
#define HEAP_CAPACITY 65536
#define _printf(...) __printf(__VA_ARGS__)
void _putchar(char c) {
__asm__(
"li $v0, 11\n"
"syscall\n"
:
: "a"(c)
);
}
int _puts(const char *s) {
__asm__(
"li $v0, 4\n"
"syscall\n"
:
: "a"(s)
);
return 0;
}
void putsym(int sym) {
__asm__(
"li $v0, 3\n"
"syscall\n"
:
: "a"(sym)
);
}
void __exit(int status) {
__asm__(
"li $v0, 10\n"
"syscall\n"
:
: "a"(status)
);
}
void _putint(int n) {
if (n < 0) {
_putchar('-');
n *= -1;
}
int d = n / 10;
if (d != 0) {
_putint(d);
}
_putchar('0' + n % 10);
}
void *_sbrk(int inc) {
void *result;
__asm__(
"li $v0, 9\n"
"syscall\n"
: "=r"(result)
: "a"(inc)
);
return result;
}
char *_gets(char *buf, int size) {
__asm__(
"li $v0, 8\n"
"syscall\n"
:
: "a"(buf), "b"(size)
);
return buf;
}
char _getchar() {
char c;
__asm__(
"li $v0, 12\n"
"syscall\n"
: "=r"(c)
);
return c;
}
void __printf(char *fmt, ...) {
char *s = fmt;
int *args = (int *)&fmt + 1;
while (*s) {
if (*s == '%') {
s++;
if (*s == 'd') {
_putint(*(args++));
} else if (*s == 's') {
_puts((char *)(long)(*(args++)));
} else if (*s == 'c') {
_putchar(*(args++));
} else if (*s == '%') {
_putchar('%');
}
} else {
_putchar(*s);
}
s++;
}
}
void _cls() {
__asm__(
"li $v0, 0\n"
"syscall\n"
);
}
void *_memset(void *s, int c, int n) {
char *p = (char *)s;
while (n--) {
*(p++) = c;
}
return s;
}
void *_memcpy(void *dest, const void *src, int n) {
char *d = (char *)dest;
const char *s = (const char *)src;
while (n--) {
*(d++) = *(s++);
}
return dest;
}
int _strlen(const char *s) {
int len = 0;
while (*(s++)) {
len++;
}
return len;
}
int _strcmp(const char *s1, const char *s2) {
while (*s1 && *s2 && *(s1++) == *(s2++));
return *s1 - *s2;
}
int rand() {
int out;
__asm__(
"li $v0, 41\n"
"syscall\n"
: "=r"(out)
);
return out;
}
typedef struct {
int meta;
} heap_meta;
heap_meta *heap_start = (heap_meta *)NULL; // { size: 0, free: false, start: true, end: false }
heap_meta *heap_end = (heap_meta *)NULL; // { size: 0, free: true, start: false, end: true }
int heap_chunk_size(heap_meta *header) {
return (header->meta >> 3) << 3;
}
int heap_chunk_is_free(heap_meta *footer) {
return footer->meta & 1;
}
int heap_chunk_is_start(heap_meta *footer) {
return footer->meta & 2;
}
int heap_chunk_is_end(heap_meta *footer) {
return footer->meta & 4;
}
heap_meta *heap_meta_header_to_footer(heap_meta *header) {
return (heap_meta *)((char *)header + heap_chunk_size(header) + sizeof(heap_meta));
}
heap_meta *heap_meta_footer_to_header(heap_meta *footer) {
return (heap_meta *)(footer - heap_chunk_size(footer) - sizeof(heap_meta));
}
heap_meta *heap_meta_header_next(heap_meta *header) { // returns the header of the next chunk
return (heap_meta *)((char *)header + heap_chunk_size(header) + 2 * sizeof(heap_meta));
}
heap_meta *heap_meta_header_prev(heap_meta *header) { // returns the header of the previous chunk
heap_meta *footer = (heap_meta *)((char *)header - sizeof(heap_meta));
return heap_meta_footer_to_header(footer);
}
heap_meta *heap_meta_header_update(heap_meta *header, int size, bool free, bool start, bool end) {
header->meta = (size >> 3 << 3) | (free << 0) | (start << 1) | (end << 2);
heap_meta *footer = heap_meta_header_to_footer(header);
footer->meta = (size >> 3 << 3) | (free << 0) | (start << 1) | (end << 2);
return header;
}
heap_meta *heap_chunk_append(heap_meta *footer, int size, bool free, bool start, bool end) { // appends a new chunka after the given footer
heap_meta *new_header = (heap_meta *)((char *)footer + sizeof(heap_meta));
return heap_meta_header_update(new_header, size, free, start, end);
}
heap_meta *heap_chunk_append_end(heap_meta *footer) {
heap_meta *end = heap_chunk_append(footer, 0, true, false, true);
heap_end = end;
return end;
}
void heap_init() {
heap_meta *start = (heap_meta *)_sbrk(HEAP_CAPACITY);
heap_meta_header_update(start, 0, false, true, false);
heap_meta *end = heap_chunk_append_end(heap_meta_header_to_footer(start));
heap_start = start;
heap_end = end;
}
void *_malloc(unsigned int size) {
if (heap_start == NULL) {
heap_init();
}
if (size <= 0) {
return NULL;
}
size = ((size / 8) + 1) * 8; // round up to the nearest multiple of 8
heap_meta *chunk = heap_start;
while (1) {
chunk = heap_meta_header_next(chunk); // chunk header
if (!heap_chunk_is_free(chunk)) { // note: the end chunk is always free
continue;
}
bool is_end = heap_chunk_is_end(chunk);
if (is_end) { // append chunk to the end of the heap
heap_meta_header_update(chunk, size, false, false, false); // initialize the new chunk
heap_meta *footer = heap_meta_header_to_footer(chunk);
heap_chunk_append_end(footer); // initialize the end chunk
return (void *)((char *)chunk + sizeof(heap_meta));
}
int curr_chunk_size = heap_chunk_size(chunk);
if (curr_chunk_size == size) { // if the chunk is exactly the right size
heap_meta_header_update(chunk, size, false, false, false);
return (void *)((char *)chunk + sizeof(heap_meta));
}
if (curr_chunk_size >= size + sizeof(heap_meta) * 2) { // if there is enough space for a header and footer in between
heap_meta_header_update(chunk, size, false, false, false); // initialize the new chunk
heap_meta *footer = heap_meta_header_to_footer(chunk);
heap_meta *next_chunk = heap_chunk_append(footer, curr_chunk_size - size - sizeof(heap_meta) * 2, true, false, false);
return (void *)((char *)chunk + sizeof(heap_meta));
}
}
return NULL;
}
void _free(void *ptr) {
if (ptr == NULL) {
return;
}
heap_meta *header = (heap_meta *)((char *)ptr - sizeof(heap_meta));
heap_meta_header_update(header, heap_chunk_size(header), true, false, false);
heap_meta *next = heap_meta_header_next(header);
if (heap_chunk_is_free(next) && !heap_chunk_is_end(next)) {
int size = heap_chunk_size(header) + heap_chunk_size(next) + sizeof(heap_meta) * 2;
heap_meta_header_update(header, size, true, false, false);
}
heap_meta *prev = heap_meta_header_prev(header);
if (heap_chunk_is_free(prev)) {
int size = heap_chunk_size(prev) + heap_chunk_size(header) + sizeof(heap_meta) * 2;
heap_meta_header_update(prev, size, true, false, false);
}
}
int atoi(char *s) {
int n = 0;
char *c = s;
if (*s == '-') {
s++;
}
while (*s) {
n = n * 10 + (*s - '0');
s++;
}
return n * (c[0] == '-' ? -1 : 1);
}
void itoa(int n, char *s) {
if (n < 0) {
*(s++) = '-';
n = -n;
}
int d = n / 10;
if (d != 0) {
itoa(d, s);
while (*s) {
s++;
}
}
*(s++) = '0' + n % 10;
*s = '\0';
}
void sleep(unsigned int ms) {
__asm__(
"li $v0, 32\n"
"syscall\n"
:
: "a"(ms)
);
}