Initial version of Allocator complete. Added new, delete, new\[\] and delete\[\]. Preparations for Process and Kernel made

This commit is contained in:
Xnoe 2021-10-25 20:59:49 +01:00
parent e6e156e255
commit b4375d1269
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
3 changed files with 28 additions and 4 deletions

View File

@ -22,8 +22,8 @@ int main() {
Allocator kernel_allocator = Allocator(&kernel_pd, &phys_pm, &virt_pm, 0xd0000000);
void* alloc = kernel_allocator.allocate(4096);
void* alloc2 = kernel_allocator.allocate(4096);
uint8_t* test2 = new(&kernel_allocator)uint8_t;
uint8_t* test = new(&kernel_allocator)uint8_t[1024];
init_idt();
init_term();
@ -32,7 +32,7 @@ int main() {
printf("KERNEL OK!\n");
printf("Alloc: %x\nAlloc2: %x\n", alloc, alloc2);
printf("Test: %x\nTest 2:%x\n", test, test2);
init_keyboard();

View File

@ -176,7 +176,7 @@ Allocator::Allocator(PageDirectory* page_directory, PageMap* phys, PageMap* virt
}
void* Allocator::allocate(uint32_t size) {
uint32_t count = (size + (4096%size)) / 4096;
uint32_t count = (size + (4096 - size % 4096)) / 4096;
uint32_t virt_addr = virt->find_next_available_from(this->virt_alloc_base, count);
this->virt->mark_unavailable(virt_addr, count);
@ -198,3 +198,19 @@ void Allocator::deallocate(uint32_t virt_addr) {
phys->mark_available(phys_addr);
virt->mark_unavailable(virt_addr);
}
void* operator new (uint32_t size, Allocator* allocator) {
return allocator->allocate(size);
}
void operator delete (void* ptr, Allocator* allocator) {
allocator->deallocate((uint32_t)ptr);
}
void* operator new[] (uint32_t size, Allocator* allocator) {
return allocator->allocate(size);
}
void operator delete[] (void* ptr, Allocator* allocator) {
allocator->deallocate((uint32_t)ptr);
}

View File

@ -83,6 +83,10 @@ public:
void* operator new (uint32_t size, Allocator* allocator);
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;
@ -91,4 +95,8 @@ public:
Process(uint32_t PID);
};
class Kernel : private Process {
};
#endif