22
33#include < cstdint>
44#include < cstddef>
5+ #include " libs/spinlock.hpp"
6+ #include " memory/pagemap.hpp"
57
68namespace 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
0 commit comments