Add hash. Add hashtable. Add linkedlist. Separate Process. Begin work on Process. Update memory. Update tuple. Update Makefile.

This commit is contained in:
Xnoe 2021-10-27 23:21:37 +01:00
parent b4375d1269
commit 768ceddef0
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
8 changed files with 173 additions and 19 deletions

View File

@ -5,7 +5,7 @@ LDFLAGS =
DISK_IMG_FILES = build/kernel/kernel.bin 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 \ 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/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 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 run: disk.img

View File

@ -0,0 +1,14 @@
#ifndef HASH_H
#define HASH_H
namespace xnoe {
template<class T>
uint32_t hash(T t);
template<>
uint32_t hash<void*>(void* t) {
return (uint32_t) t;
}
}
#endif

View File

@ -0,0 +1,50 @@
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include "hash.h"
#include "linkedlist.h"
#include "memory.h"
namespace xnoe {
template <class key, class value>
class hashtable {
private:
xnoe::linkedlist<xnoe::tuple<key, value>>* table;
public:
hashtable(Allocator* allocator) {
this->table = new(allocator) xnoe::linkedlist<xnoe::tuple<key, value>>[4096];
}
void set(key k, value v) {
xnoe::linkedlist<xnoe::tuple<key, value>> list = table[xnoe::hash<k>(k) % 4096];
xnoe::linkedlistelem<xnoe::tuple<key, value>> current = list.start;
bool exists = false;
while (current->next) {
if (xnoe::get<0>(current->elem) == k) {
exists = true;
break;
}
}
if (exists)
current = xnoe::tuple<key, value>(k, v);
else
list.append(xnoe::tuple<key, value>(k, v));
}
value* get(key k) {
xnoe::linkedlist<xnoe::tuple<key, value>> list = table[xnoe::hash<k>(k) % 4096];
xnoe::linkedlistelem<xnoe::tuple<key, value>> current = list.start;
while (current->next)
if (xnoe::get<0>(current->elem) == k)
return &(xnoe::get<1>(current->elem));
return 0;
}
};
}
#endif

View File

@ -0,0 +1,57 @@
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "memory.h"
namespace xnoe {
template<typename T>
struct linkedlistelem {
T elem;
linkedlistelem<T>* previous;
linkedlistelem<T>* next;
linkedlistelem(T t, linkedlistelem<T>* p, linkedlistelem<T>* n) {
this->elem = t;
this->previous = p;
this->next = n;
}
};
template<typename T>
struct linkedlist {
linkedlistelem<T>* start;
linkedlistelem<T>* end;
void append(linkedlistelem<T>* t) {
this->end->next = t;
t->prev = this->end;
t->next = 0;
this->end = t;
}
void prepend(linkedlistelem<T>* t) {
this->start->previous = t;
this->start = t;
}
void insert(linkedlist<T>* ll, uint32_t index) {
linkedlistelem<T>* current = this->start;
for (int i=0; i<index; i++, current = current->next);
current->next->prev = ll->end;
current->next = ll->start;
}
void remove(Allocator* allocator, uint32_t index) {
linkedlistelem<T>* current = this->start;
for (int i=0; i<index; i++, current = current->next);
current->prev->next = current->next;
current->next->prev = current->prev;
delete(allocator, current);
}
};
}
#endif

View File

@ -18,13 +18,13 @@ namespace xnoe {
tuple(const T& head, const Tail& ... tail) tuple(const T& head, const Tail& ... tail)
: head(head), tail(tail...) {} : head(head), tail(tail...) {}
template<int index>
auto get() {
return tupleGetHelper<index, tuple<T, Tail...>>::get(*this);
}
}; };
template<int index, typename ... Types>
auto get(xnoe::tuple<Types ...> tuple) {
return tupleGetHelper<index, xnoe::tuple<Types ...>>::get(tuple);
}
template<typename T, typename ... Tail> template<typename T, typename ... Tail>
class tupleGetHelper<0, tuple<T, Tail...>> { class tupleGetHelper<0, tuple<T, Tail...>> {
public: public:

View File

@ -86,17 +86,4 @@ void operator delete (void* ptr, Allocator* allocator);
void* operator new[] (uint32_t size, Allocator* allocator); void* operator new[] (uint32_t size, Allocator* allocator);
void operator delete[] (void* ptr, 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 #endif

16
src/kernel/process.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "process.h"
Process::Process(uint32_t PID, xnoe::hashtable<void*, xnoe::tuple<uint32_t, uint32_t>>* 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) {
}

30
src/kernel/process.h Normal file
View File

@ -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<xnoe::tuple<uint32_t, uint32_t>> allocations;
xnoe::hashtable<void*, xnoe::tuple<uint32_t, uint32_t>>* allocmap;
public:
Process(uint32_t PID, xnoe::hashtable<void*, xnoe::tuple<uint32_t, uint32_t>>* 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