Skip to content
This repository was archived by the owner on Dec 25, 2025. It is now read-only.

Commit f1fc590

Browse files
committed
Switch to RB-Tree for VMA
1 parent efaaf5c commit f1fc590

File tree

6 files changed

+628
-343
lines changed

6 files changed

+628
-343
lines changed

kernel/include/memory/vma.hpp

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,83 @@
22

33
#include <cstdint>
44
#include <cstddef>
5+
#include "libs/spinlock.hpp"
6+
#include "memory/pagemap.hpp"
57

68
namespace kernel::memory {
7-
struct VmFreeRegion {
8-
uintptr_t start; ///< Start of the free virtual range (inclusive).
9-
size_t length; ///< Length of the free range in bytes.
10-
VmFreeRegion* next; ///< Next region in the sorted free list.
9+
struct VmRegion {
10+
uintptr_t start;
11+
size_t size;
12+
13+
uint8_t flags;
14+
CacheType cache;
15+
16+
VmRegion* parent = nullptr;
17+
VmRegion* left = nullptr;
18+
VmRegion* right = nullptr;
19+
bool is_red = true;
20+
21+
uintptr_t end() const {
22+
return this->start + this->size;
23+
}
1124
};
1225

13-
class VirtualAllocator {
26+
class VmRegionAllocator {
1427
public:
15-
void init(uintptr_t start, size_t length);
28+
VmRegion* allocate();
29+
void deallocate(VmRegion* node);
30+
31+
private:
32+
void refill();
1633

17-
uintptr_t alloc_region(size_t size, size_t align);
18-
void free_region(uintptr_t start, size_t size);
34+
struct FreeNode {
35+
FreeNode* next;
36+
};
37+
38+
FreeNode* free_head = nullptr;
39+
SpinLock lock;
40+
};
41+
42+
class VirtualMemoryAllocator {
43+
public:
44+
void init(uintptr_t start_addr);
45+
46+
void* allocate(size_t size, uint8_t flags = Read | Write,
47+
CacheType cache = CacheType::WriteBack);
48+
49+
void* reserve(size_t size, size_t alignment, uint8_t flags);
50+
void free(void* ptr, bool free_phys);
1951

2052
private:
21-
VmFreeRegion* new_node();
22-
void return_node(VmFreeRegion* node);
23-
void expand_pool();
53+
void map(uintptr_t virt_addr, size_t size, uint8_t flags, CacheType cache);
54+
void unmap(uintptr_t virt_addr, size_t size, bool free_phys);
55+
56+
VmRegion* find_node(uintptr_t start);
57+
uintptr_t find_hole(size_t size, size_t alignment);
58+
59+
void insert_region(uintptr_t start, size_t size, uint8_t flags, CacheType cache);
60+
void insert_region_locked(uintptr_t start, size_t size, uint8_t flags, CacheType cache);
61+
62+
void delete_node_locked(VmRegion* z);
63+
void rotate_left(VmRegion* x);
64+
void rotate_right(VmRegion* x);
65+
void insert_fixup(VmRegion* z);
66+
void delete_fixup(VmRegion* x);
67+
68+
struct alignas(CACHE_LINE_SIZE) CpuCache {
69+
static constexpr int CAPACITY = 256;
70+
uintptr_t va_holes[CAPACITY];
71+
int count = 0;
72+
IrqLock lock;
73+
};
74+
75+
uintptr_t heap_base;
76+
size_t cpu_count;
77+
78+
CpuCache* caches = nullptr;
2479

25-
VmFreeRegion* region_head = nullptr; ///< Head of the sorted free-region list.
26-
VmFreeRegion* free_nodes_head = nullptr; ///< Head of the free-node pool.
80+
SpinLock lock;
81+
VmRegion* root;
82+
VmRegionAllocator metadata_allocator;
2783
};
2884
} // namespace kernel::memory

kernel/include/memory/vmm.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ class VirtualManager {
2424
private:
2525
static void map_pagemap();
2626
static void map_kernel();
27+
28+
static size_t page_size_to_bytes(PageSize size, size_t count);
2729
};
2830
} // namespace kernel::memory

kernel/include/task/process.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct Process : public IntrusiveListNode<ProcessTag> {
6969
IntrusiveList<Process, ProcessTag> children;
7070
IntrusiveList<Thread, ProcessTag> threads;
7171

72-
memory::VirtualAllocator user_vmm;
72+
// memory::VirtualAllocator user_vmm;
7373
int exit_code;
7474

7575
static Process* kernel_proc;
@@ -78,8 +78,8 @@ struct Process : public IntrusiveListNode<ProcessTag> {
7878
Process(); // User
7979
~Process();
8080

81-
void* mmap(size_t count, memory::PageSize size, uint8_t flags);
82-
void munmap(void* addr, size_t count, memory::PageSize size);
81+
// void* mmap(size_t count, memory::PageSize size, uint8_t flags);
82+
// void munmap(void* addr, size_t count, memory::PageSize size);
8383

8484
static void init();
8585

0 commit comments

Comments
 (0)