aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-02-04 18:24:31 -0700
committerChristian Cunningham <cc@localhost>2022-02-04 18:24:31 -0700
commit13b2bf797bdf4fab0115675c4d2541fff05e285c (patch)
tree6b622ffe738a8eb76e0c0f7050fab13d1bf1acad /src
parent6e7f93d6a09865937dde1124a6c3f36eebcd9d82 (diff)
Implemented new Real-Time friendly Memory Allocation Scheme
Diffstat (limited to 'src')
-rw-r--r--src/boot.S4
-rw-r--r--src/cpu/irq.c8
-rw-r--r--src/globals.c2
-rw-r--r--src/lib/kmem.c76
-rw-r--r--src/sys/core.c1
5 files changed, 79 insertions, 12 deletions
diff --git a/src/boot.S b/src/boot.S
index 3e8a209..9259874 100644
--- a/src/boot.S
+++ b/src/boot.S
@@ -137,10 +137,6 @@ core0_mbox: .word 0
core1_mbox: .word 0
core2_mbox: .word 0
core3_mbox: .word 0
-core0_msg: .asciz "Powering up, Core 0 Online!\n"
-core1_msg: .asciz "Powering up, Core 1 Online!\n"
-core2_msg: .asciz "Powering up, Core 2 Online!\n"
-core3_msg: .asciz "Powering up, Core 3 Online!\n"
.section .bss.estacks
.align 4
diff --git a/src/cpu/irq.c b/src/cpu/irq.c
index ac08c05..6be4bc7 100644
--- a/src/cpu/irq.c
+++ b/src/cpu/irq.c
@@ -103,13 +103,5 @@ void handle_data(unsigned char data)
add_thread(uart_scheduler, 0, 2);
} else if (data == 0x62) {
add_thread(heap_info, 0, 2);
- } else {
}
- g_Drawer.x = 0;
- g_Drawer.y = 7;
- for(int i = 0; i < 128; i++)
- write_char(&g_Drawer, ' ');
- g_Drawer.x = 0;
- g_Drawer.y = 7;
- write_string(&g_Drawer, "> ");
}
diff --git a/src/globals.c b/src/globals.c
index 6e2e70c..7560d82 100644
--- a/src/globals.c
+++ b/src/globals.c
@@ -10,6 +10,8 @@ char* os_info_v = "?";
char* os_info_v = VERSION;
#endif
+__attribute__((section(".bss.kmem"))) unsigned char kmem_begin[0x2000000];
+__attribute__((section(".bss"))) unsigned char kmem_lookup[0xD000];
__attribute__((section(".bss"))) unsigned long exe_cnt;
__attribute__((section(".bss"))) struct Mutex exe_cnt_m;
__attribute__((section(".bss"))) unsigned long nextpid;
diff --git a/src/lib/kmem.c b/src/lib/kmem.c
new file mode 100644
index 0000000..fc91fc4
--- /dev/null
+++ b/src/lib/kmem.c
@@ -0,0 +1,76 @@
+#include <lib/kmem.h>
+#include <globals.h>
+
+void* kmalloc(unsigned int size)
+{
+ unsigned int sz = 1;
+ while (sz < size && sz < 0x1000)
+ sz *= 2;
+ unsigned long offset = 0x1000*(sz/2);
+ unsigned int exp = 0;
+ unsigned int tmp = sz;
+ while (tmp != 0) {
+ exp++;
+ tmp = tmp >> 1;
+ }
+ unsigned int i = 0;
+ while (i < 0x1000) {
+ if (kmem_lookup[0x1000*exp + i] == 0) {
+ kmem_lookup[0x1000*exp + i] = 1;
+ return (void*)kmem_begin + offset + i*sz;
+ }
+ i++;
+ }
+ return 0;
+}
+
+void* kcalloc(unsigned int size)
+{
+ unsigned char* ptr = kmalloc(size);
+ if (ptr == 0)
+ return 0;
+ for(unsigned int i = 0; i < size; i++)
+ ptr[i] = 0;
+ return ptr;
+}
+
+void* krealloc(void* old, unsigned int size)
+{
+ unsigned long old_size = 1;
+ while (!((unsigned long)kmem_begin + 0x1000*(old_size/2) <= (unsigned long)old && (unsigned long)old < (unsigned long)kmem_begin + 0x1000*old_size))
+ old_size *= 2;
+ if (size <= old_size)
+ return old;
+ void* new = kmalloc(size);
+ if (new == 0)
+ return 0;
+ void* base = (void*)((unsigned long)old - ((unsigned long)old % old_size));
+ void* block_base = kmem_begin + 0x1000*(old_size/2);
+ unsigned int lookup_offset = (base - block_base)/old_size;
+ unsigned int exp = 0;
+ unsigned int tmp = old_size;
+ while (tmp != 0) {
+ exp++;
+ tmp = tmp >> 1;
+ }
+ kmem_lookup[0x1000*exp + lookup_offset] = 0;
+ return new;
+}
+
+void kfree(void* ptr)
+{
+ unsigned long size = 1;
+ while (!((unsigned long)kmem_begin + 0x1000*(size/2) <= (unsigned long)ptr && (unsigned long)ptr < (unsigned long)kmem_begin + 0x1000*size)) {
+ size *= 2;
+ }
+ void* base = (void*)((unsigned long)ptr - ((unsigned long)ptr % size));
+ void* block_base = kmem_begin + 0x1000*(size/2);
+ unsigned int lookup_offset = (base - block_base)/size;
+ unsigned int exp = 0;
+ unsigned int tmp = size;
+ while (tmp != 0) {
+ exp++;
+ tmp = tmp >> 1;
+ }
+ kmem_lookup[0x1000*exp + lookup_offset] = 0;
+}
diff --git a/src/sys/core.c b/src/sys/core.c
index adfbd20..a42b581 100644
--- a/src/sys/core.c
+++ b/src/sys/core.c
@@ -4,6 +4,7 @@
#include <globals.h>
#include <graphics/drawer.h>
#include <graphics/lfb.h>
+#include <lib/kmem.h>
#include <lib/mem.h>
#include <lib/strings.h>
#include <symbols.h>