#include <syscall.h>

void syscall_malloc(cpu_status_t* status){
    printf("\e[32mmalloc %ul bytes\e[39m\n", status->r[4]);

    uint32_t size = status->r[4];
    if(status->malloc.allocs == 0){
        status->r[0] = status->malloc.lo_mem;
    }
    else{
        int addr = status->malloc.lo_mem;
        int hi = 0;
        int hi_id = 0;
        for(int i=0; i < status->malloc.allocs; i++){
            if(status->malloc.mallocs[i].addr > hi){
                hi = status->malloc.mallocs[i].addr;
                hi_id = i;
            }
        }
        addr = hi + status->malloc.mallocs[hi_id].size + status->malloc.margin;
        status->r[0] = addr; 
    }
    status->malloc.allocs++;
    status->malloc.mallocs = realloc(status->malloc.mallocs, status->malloc.allocs * sizeof(malloc_info_t));
    status->malloc.mallocs[status->malloc.allocs - 1] = (malloc_info_t){size,status->r[0]};
}