Kernel: Add basic fixed width allocator for userspace

We have to move process stacks to the general heap and maybe map
kernel to higher half.
This commit is contained in:
Bananymous
2023-05-06 18:10:38 +03:00
parent 9c07add00f
commit bcfd838131
10 changed files with 267 additions and 9 deletions

View File

@@ -0,0 +1,48 @@
#pragma once
#include <kernel/Memory/Heap.h>
namespace Kernel
{
class Process;
class FixedWidthAllocator
{
BAN_NON_COPYABLE(FixedWidthAllocator);
public:
FixedWidthAllocator(Process*, uint32_t);
FixedWidthAllocator(FixedWidthAllocator&&);
~FixedWidthAllocator();
vaddr_t allocate();
void deallocate(vaddr_t);
uint32_t allocation_size() const { return m_allocation_size; }
private:
struct node
{
node* prev { nullptr };
node* next { nullptr };
};
vaddr_t address_of(const node*) const;
void allocate_page_for_node_if_needed(const node*);
private:
static constexpr uint32_t m_min_allocation_size = 16;
Process* m_process;
const uint32_t m_allocation_size;
vaddr_t m_nodes_page { 0 };
vaddr_t m_allocated_pages { 0 };
node* m_free_list { nullptr };
node* m_used_list { nullptr };
uint32_t m_allocated { 0 };
};
}

View File

@@ -4,6 +4,7 @@
#include <BAN/StringView.h>
#include <BAN/Vector.h>
#include <kernel/FS/Inode.h>
#include <kernel/Memory/FixedWidthAllocator.h>
#include <kernel/Memory/Heap.h>
#include <kernel/Memory/MMU.h>
#include <kernel/SpinLock.h>
@@ -55,6 +56,8 @@ namespace Kernel
BAN::ErrorOr<BAN::String> working_directory() const;
BAN::ErrorOr<void> set_working_directory(BAN::StringView);
BAN::ErrorOr<void*> allocate(size_t);
void termid(char*) const;
static Process& current() { return Thread::current().process(); }
@@ -90,6 +93,8 @@ namespace Kernel
BAN::String m_working_directory;
BAN::Vector<Thread*> m_threads;
BAN::Vector<FixedWidthAllocator> m_fixed_width_allocators;
MMU* m_mmu { nullptr };
TTY* m_tty { nullptr };
};

View File

@@ -7,6 +7,7 @@
#define SYS_CLOSE 5
#define SYS_SEEK 6
#define SYS_OPEN 7
#define SYS_ALLOC 8
#include <stdint.h>