|
6 | 6 | #include <stdbool.h>
|
7 | 7 | #include <stddef.h>
|
8 | 8 | #include <stdint.h>
|
| 9 | +#include <string.h> |
9 | 10 | #include <sys/mman.h>
|
10 | 11 | #include <sys/syscall.h>
|
11 | 12 | #include <sys/types.h>
|
@@ -57,7 +58,7 @@ static void tiny_c_print(int32_t file_handle, const char *data) {
|
57 | 58 | static void tiny_c_newline(int32_t file_handle) {
|
58 | 59 | struct SysArgs args = (struct SysArgs){
|
59 | 60 | .param_one = (size_t)file_handle,
|
60 |
| - .param_two = (size_t)"\n", |
| 61 | + .param_two = (size_t) "\n", |
61 | 62 | .param_three = 1,
|
62 | 63 | };
|
63 | 64 | tiny_c_syscall(SYS_write, &args);
|
@@ -410,23 +411,39 @@ const char *tinyc_strerror(int32_t err_number) {
|
410 | 411 |
|
411 | 412 | // @todo: alignment
|
412 | 413 | void *tinyc_malloc_arena(size_t n) {
|
413 |
| - const size_t PAGE_SIZE = 0x1000; |
414 |
| - |
415 |
| - if (tinyc_heap_start == 0) { |
416 |
| - tinyc_heap_start = tinyc_sys_brk(0); |
417 |
| - tinyc_heap_end = tinyc_heap_start; |
418 |
| - tinyc_heap_index = tinyc_heap_start; |
419 |
| - } |
420 |
| - if (tinyc_heap_index + n > tinyc_heap_end) { |
421 |
| - size_t extend_size = PAGE_SIZE * (n / PAGE_SIZE) + PAGE_SIZE; |
422 |
| - tinyc_heap_end = tinyc_sys_brk(tinyc_heap_end + extend_size); |
423 |
| - if (tinyc_heap_end <= tinyc_heap_start) { |
424 |
| - return NULL; |
425 |
| - } |
| 414 | + // const size_t PAGE_SIZE = 0x1000; |
| 415 | + |
| 416 | + // if (tinyc_heap_start == 0) { |
| 417 | + size_t start = tinyc_sys_brk(0); |
| 418 | + size_t end = start; |
| 419 | + size_t index = start; |
| 420 | + // tinyc_heap_start = tinyc_sys_brk(0); |
| 421 | + // tinyc_heap_end = tinyc_heap_start; |
| 422 | + // tinyc_heap_index = tinyc_heap_start; |
| 423 | + // } |
| 424 | + // if (tinyc_heap_index + n > tinyc_heap_end) { |
| 425 | + // size_t extend_size = PAGE_SIZE * (n / PAGE_SIZE) + PAGE_SIZE; |
| 426 | + // tinyc_heap_end = tinyc_sys_brk(tinyc_heap_end + extend_size); |
| 427 | + // tinyc_heap_start = tinyc_sys_brk(0); |
| 428 | + // tinyc_heap_end = tinyc_sys_brk(tinyc_heap_end + 0x2000); |
| 429 | + // size_t temp_start = tinyc_sys_brk(0); |
| 430 | + // size_t temp_end = tinyc_sys_brk(temp_start + 0x2000); |
| 431 | + // if (temp_end <= temp_start) { |
| 432 | + // return NULL; |
| 433 | + // } |
| 434 | + end = tinyc_sys_brk(start + 0x2000); |
| 435 | + |
| 436 | + tinyc_heap_start = start; |
| 437 | + tinyc_heap_end = end; |
| 438 | + tinyc_heap_index = index; |
| 439 | + |
| 440 | + if (end <= start) { |
| 441 | + return NULL; |
426 | 442 | }
|
| 443 | + // } |
427 | 444 |
|
428 |
| - void *address = (void *)tinyc_heap_index; |
429 |
| - tinyc_heap_index += n; |
| 445 | + void *address = (void *)index; |
| 446 | + // tinyc_heap_index += n; |
430 | 447 |
|
431 | 448 | return address;
|
432 | 449 | }
|
|
0 commit comments