From 768ceddef0afef8b2a9ac82c1407d80e6daf8d1d Mon Sep 17 00:00:00 2001 From: Xnoe Date: Wed, 27 Oct 2021 23:21:37 +0100 Subject: [PATCH] Add hash. Add hashtable. Add linkedlist. Separate Process. Begin work on Process. Update memory. Update tuple. Update Makefile. --- Makefile | 2 +- src/kernel/datatypes/hash.h | 14 ++++++++ src/kernel/datatypes/hashtable.h | 50 +++++++++++++++++++++++++++ src/kernel/datatypes/linkedlist.h | 57 +++++++++++++++++++++++++++++++ src/kernel/datatypes/tuple.h | 10 +++--- src/kernel/memory.h | 13 ------- src/kernel/process.cpp | 16 +++++++++ src/kernel/process.h | 30 ++++++++++++++++ 8 files changed, 173 insertions(+), 19 deletions(-) create mode 100644 src/kernel/datatypes/hash.h create mode 100644 src/kernel/datatypes/hashtable.h create mode 100644 src/kernel/datatypes/linkedlist.h create mode 100644 src/kernel/process.cpp create mode 100644 src/kernel/process.h diff --git a/Makefile b/Makefile index 1a786cb..8b5fa40 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LDFLAGS = DISK_IMG_FILES = build/kernel/kernel.bin KERNEL_OBJS = build/kernel/entry.o build/kernel/screenstuff.o build/kernel/io.o build/kernel/idt.o build/kernel/keyboard.o \ build/kernel/strings.o build/kernel/atapio.o build/kernel/kernel.o build/kernel/paging.o build/kernel/allocate.o \ - build/kernel/gdt.o build/kernel/memory.o + build/kernel/gdt.o build/kernel/memory.o build/kernel/process.o STAGE2_OBS = build/c_code_entry.o build/boot_stage2/io.o build/boot_stage2/atapio.o build/boot_stage2/strings.o build/boot_stage2/screenstuff.o build/boot_stage2/stage2.o build/boot_stage2/paging.o run: disk.img diff --git a/src/kernel/datatypes/hash.h b/src/kernel/datatypes/hash.h new file mode 100644 index 0000000..210a342 --- /dev/null +++ b/src/kernel/datatypes/hash.h @@ -0,0 +1,14 @@ +#ifndef HASH_H +#define HASH_H + +namespace xnoe { + template + uint32_t hash(T t); + + template<> + uint32_t hash(void* t) { + return (uint32_t) t; + } +} + +#endif \ No newline at end of file diff --git a/src/kernel/datatypes/hashtable.h b/src/kernel/datatypes/hashtable.h new file mode 100644 index 0000000..3ee1c53 --- /dev/null +++ b/src/kernel/datatypes/hashtable.h @@ -0,0 +1,50 @@ +#ifndef HASHTABLE_H +#define HASHTABLE_H + +#include "hash.h" +#include "linkedlist.h" +#include "memory.h" + +namespace xnoe { + template + class hashtable { + private: + xnoe::linkedlist>* table; + + public: + hashtable(Allocator* allocator) { + this->table = new(allocator) xnoe::linkedlist>[4096]; + } + + void set(key k, value v) { + xnoe::linkedlist> list = table[xnoe::hash(k) % 4096]; + xnoe::linkedlistelem> current = list.start; + + bool exists = false; + + while (current->next) { + if (xnoe::get<0>(current->elem) == k) { + exists = true; + break; + } + } + + if (exists) + current = xnoe::tuple(k, v); + else + list.append(xnoe::tuple(k, v)); + } + + value* get(key k) { + xnoe::linkedlist> list = table[xnoe::hash(k) % 4096]; + xnoe::linkedlistelem> current = list.start; + while (current->next) + if (xnoe::get<0>(current->elem) == k) + return &(xnoe::get<1>(current->elem)); + + return 0; + } + }; +} + +#endif \ No newline at end of file diff --git a/src/kernel/datatypes/linkedlist.h b/src/kernel/datatypes/linkedlist.h new file mode 100644 index 0000000..6ca6d73 --- /dev/null +++ b/src/kernel/datatypes/linkedlist.h @@ -0,0 +1,57 @@ +#ifndef LINKEDLIST_H +#define LINKEDLIST_H + +#include "memory.h" + +namespace xnoe { + template + struct linkedlistelem { + T elem; + linkedlistelem* previous; + linkedlistelem* next; + + linkedlistelem(T t, linkedlistelem* p, linkedlistelem* n) { + this->elem = t; + this->previous = p; + this->next = n; + } + }; + + template + struct linkedlist { + linkedlistelem* start; + linkedlistelem* end; + + void append(linkedlistelem* t) { + this->end->next = t; + t->prev = this->end; + t->next = 0; + this->end = t; + } + + void prepend(linkedlistelem* t) { + this->start->previous = t; + this->start = t; + } + + void insert(linkedlist* ll, uint32_t index) { + linkedlistelem* current = this->start; + for (int i=0; inext); + + current->next->prev = ll->end; + current->next = ll->start; + } + + void remove(Allocator* allocator, uint32_t index) { + linkedlistelem* current = this->start; + for (int i=0; inext); + + current->prev->next = current->next; + current->next->prev = current->prev; + + delete(allocator, current); + } + }; +} + +#endif \ No newline at end of file diff --git a/src/kernel/datatypes/tuple.h b/src/kernel/datatypes/tuple.h index e12495b..4ae0980 100644 --- a/src/kernel/datatypes/tuple.h +++ b/src/kernel/datatypes/tuple.h @@ -18,13 +18,13 @@ namespace xnoe { tuple(const T& head, const Tail& ... tail) : head(head), tail(tail...) {} - - template - auto get() { - return tupleGetHelper>::get(*this); - } }; + template + auto get(xnoe::tuple tuple) { + return tupleGetHelper>::get(tuple); + } + template class tupleGetHelper<0, tuple> { public: diff --git a/src/kernel/memory.h b/src/kernel/memory.h index 49104c8..2bee9fa 100644 --- a/src/kernel/memory.h +++ b/src/kernel/memory.h @@ -86,17 +86,4 @@ void operator delete (void* ptr, Allocator* allocator); void* operator new[] (uint32_t size, Allocator* allocator); void operator delete[] (void* ptr, Allocator* allocator); - -class Process : protected Allocator { -private: - uint32_t PID; - -public: - Process(uint32_t PID); -}; - -class Kernel : private Process { - -}; - #endif \ No newline at end of file diff --git a/src/kernel/process.cpp b/src/kernel/process.cpp new file mode 100644 index 0000000..4f46b81 --- /dev/null +++ b/src/kernel/process.cpp @@ -0,0 +1,16 @@ +#include "process.h" + +Process::Process(uint32_t PID, xnoe::hashtable>* table, PageDirectory* page_directory, PageMap* phys, PageMap* virt, uint32_t virt_alloc_base) +: Allocator(page_directory, phys, virt, virt_alloc_base) { + this->PID = PID; + this->allocmap = table; + this->last_page_pointer = 0; +} + +void* Process::allocate(uint32_t size) { + +} + +void Process::deallocate(uint32_t virt_addr) { + +} \ No newline at end of file diff --git a/src/kernel/process.h b/src/kernel/process.h new file mode 100644 index 0000000..8f53a9a --- /dev/null +++ b/src/kernel/process.h @@ -0,0 +1,30 @@ +#ifndef PROCESS_H +#define PROCESS_H + +#include "types.h" +#include "memory.h" +#include "datatypes/linkedlist.h" +#include "datatypes/hashtable.h" + +class Process : protected Allocator { +private: + uint32_t PID; + + uint32_t last_page_pointer; + uint32_t page_usage; + + xnoe::linkedlist> allocations; + xnoe::hashtable>* allocmap; + +public: + Process(uint32_t PID, xnoe::hashtable>* table, PageDirectory* page_directory, PageMap* phys, PageMap* virt, uint32_t virt_alloc_base); + + void* allocate(uint32_t size) override; + void deallocate(uint32_t virt_addr) override; +}; + +class Kernel : private Process { + +}; + +#endif \ No newline at end of file