aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-01-11 18:05:00 -0700
committerChristian Cunningham <cc@localhost>2022-01-11 18:05:00 -0700
commit5b3ceca69d5432f976ac487c63a75f8c6275028b (patch)
treec932890424d8e30ea61a7a406967f8d6e2daf39f /src
parent516df5812ac64d87ac2e32ebf0211534ea0abaa7 (diff)
Memory freeing is optimized
Fixed Uart Bug temporarily
Diffstat (limited to 'src')
-rw-r--r--src/cpu/irq.c3
-rw-r--r--src/drivers/uart.S2
-rw-r--r--src/lib/mem.c98
-rw-r--r--src/sys/timer.c3
4 files changed, 59 insertions, 47 deletions
diff --git a/src/cpu/irq.c b/src/cpu/irq.c
index aa7c375..c0fd367 100644
--- a/src/cpu/irq.c
+++ b/src/cpu/irq.c
@@ -30,7 +30,7 @@ void c_irq_handler(void)
write_string(&g_Drawer, "TIMER: ");
if(timer_status == 0) {
cntfrq = read_cntfrq();
- write_cntv_tval(cntfrq);
+ write_cntv_tval(cntfrq/CPS);
enablecntv();
write_cstring(&g_Drawer, "Enabled ", 0x00FF00);
} else {
@@ -67,6 +67,7 @@ void c_irq_handler(void)
} else if (data == 0x61) {
cmd[off] = (char) data;
off += 1;
+ heap_info_u();
// Else output
} else {
cmd[off] = (char) data;
diff --git a/src/drivers/uart.S b/src/drivers/uart.S
index cde6c11..731e160 100644
--- a/src/drivers/uart.S
+++ b/src/drivers/uart.S
@@ -49,5 +49,5 @@ uart_hex.hloop:
uart_hex.print:
str r1, [r2]
subs r3, #1
- bge uart_hex.loop // Jump back to wait for availablilty
+ bge uart_hex.hloop // Jump back to wait for availablilty
pop {r4, pc}
diff --git a/src/lib/mem.c b/src/lib/mem.c
index ed6d23f..fcaea4b 100644
--- a/src/lib/mem.c
+++ b/src/lib/mem.c
@@ -53,54 +53,63 @@ 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;
+ 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;
}
// Update top of heap
if (mem[i] == 0)
- rpi_heap_top = (void*)&mem[i+2+size];
+ 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+1] = 1;
- return (void*)&mem[i+2];
+ mem[i + MEM_USE_OFFSET] = 1;
+ return (void*)&mem[i + MEM_BASE_SIZE];
}
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(1) {
- unsigned long diff = (unsigned long)mem + i + 2;
+ unsigned long diff = (unsigned long)mem + i + MEM_BASE_SIZE;
diff %= amnt;
diff = amnt - diff;
diff %= amnt;
- if((mem[i] == size) && mem[i+1]==0) {
+ if((mem[i + MEM_SIZE_OFFSET] == size) && mem[i + MEM_USE_OFFSET]==0) {
if(diff == 0) {
- mem[i] = size;
- mem[i+1] = 1;
- return (void*)&mem[i+2];
+ 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 || diff == amnt) {
- mem[i] = size;
- mem[i+1] = 1;
- rpi_heap_top = (void*)&mem[i+2+size];
- return (void*)&mem[i+2];
+ 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 {
- if(diff <= 2) {
+ while (diff <= MEM_BASE_SIZE) {
diff += amnt;
}
- mem[i] = diff-2;
+ unsigned long empty_size = diff - MEM_META_SIZE;
+ mem[i + MEM_SIZE_OFFSET] = empty_size;
+ mem[i + MEM_BASE_SIZE + empty_size] = empty_size;
i += diff;
- mem[i] = size;
- mem[i+1] = 1;
- rpi_heap_top = (void*)&mem[i+2+size];
- return (void*)&mem[i+2];
+ 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]+2;
+ i += mem[i] + MEM_META_SIZE;
}
}
@@ -109,18 +118,23 @@ 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;
+ unsigned char* base = memloc - MEM_BASE_SIZE;
+ unsigned char size = base[MEM_SIZE_OFFSET];
// TODO: Use Null PID
- base[1] = 0;
+ base[MEM_USE_OFFSET] = 0;
// Clear out old memory
for(unsigned int i = 0; i < size; i++) {
- base[i+2] = 0;
+ base[i + MEM_BASE_SIZE] = 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;
+ 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;
+ }
}
}
@@ -138,24 +152,24 @@ void heap_info(void)
{
unsigned char* base = rpi_heap;
while ((void*)base < rpi_heap_top) {
- unsigned char size = *base;
- if(base[1] == 0) {
+ unsigned char size = base[MEM_SIZE_OFFSET];
+ if(base[MEM_USE_OFFSET] == 0) {
uart_char('F');
uart_char(' ');
}
- uart_hex((unsigned long)(base+2));
+ 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[2+i]>>4)&0xF;
+ temp = (base[MEM_BASE_SIZE + i]>>4)&0xF;
if(temp > 9)
temp += 7;
temp += 0x30;
data[0] = temp;
- temp = (base[2+i])&0xF;
+ temp = (base[MEM_BASE_SIZE + i])&0xF;
if(temp > 9)
temp += 7;
temp += 0x30;
@@ -163,7 +177,7 @@ void heap_info(void)
uart_string(data);
}
uart_char('\n');
- base += size + 2;
+ base += size + MEM_META_SIZE;
}
uart_char('\n');
}
@@ -172,24 +186,24 @@ 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;
+ unsigned char size = base[MEM_SIZE_OFFSET];
+ if(base[MEM_USE_OFFSET] == 0) {
+ base += MEM_META_SIZE;
continue;
}
- uart_hex((unsigned long)(base+2));
+ 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[2+i]>>4)&0xF;
+ temp = (base[MEM_BASE_SIZE + i]>>4)&0xF;
if(temp > 9)
temp += 7;
temp += 0x30;
data[0] = temp;
- temp = (base[2+i])&0xF;
+ temp = (base[MEM_BASE_SIZE + i])&0xF;
if(temp > 9)
temp += 7;
temp += 0x30;
@@ -197,7 +211,7 @@ void heap_info_u(void)
uart_string(data);
}
uart_char('\n');
- base += size + 2;
+ base += size + MEM_META_SIZE;
}
uart_char('\n');
}
diff --git a/src/sys/timer.c b/src/sys/timer.c
index d35ef83..4ccdf5d 100644
--- a/src/sys/timer.c
+++ b/src/sys/timer.c
@@ -8,9 +8,6 @@
#include <util/time.h>
#include <symbols.h>
-/// Cycles Per Second
-#define CPS 100
-
#define SYS_TIMER_C
static unsigned long exe_cnt = 0;
struct Mutex exe_cnt_m = {.addr = &exe_cnt, .pid = NULL_PID};