2023-05-18 09:46:41 -07:00
|
|
|
#include "memory/kernel_heap.h"
|
|
|
|
|
|
|
|
#include "debug/debug.h"
|
|
|
|
#include "memory/paging_util.h"
|
|
|
|
|
2023-05-18 11:29:44 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
static KernelHeap* gKernelHeap = nullptr;
|
|
|
|
|
|
|
|
KernelHeap& GetKernelHeap() {
|
|
|
|
if (!gKernelHeap) {
|
|
|
|
panic("Kernel Heap not initialized.");
|
|
|
|
}
|
|
|
|
return *gKernelHeap;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2023-05-18 09:46:41 -07:00
|
|
|
KernelHeap::KernelHeap(uint64_t lower_bound, uint64_t upper_bound)
|
2023-05-18 11:29:44 -07:00
|
|
|
: next_addr_(lower_bound), upper_bound_(upper_bound) {
|
|
|
|
gKernelHeap = this;
|
|
|
|
}
|
2023-05-18 09:46:41 -07:00
|
|
|
|
|
|
|
void* KernelHeap::Allocate(uint64_t size) {
|
|
|
|
if (next_addr_ + size >= upper_bound_) {
|
|
|
|
panic("Kernel Heap Overrun");
|
|
|
|
}
|
|
|
|
EnsureResident(next_addr_, size);
|
|
|
|
uint64_t address = next_addr_;
|
|
|
|
next_addr_ += size;
|
|
|
|
return reinterpret_cast<void*>(address);
|
|
|
|
}
|
2023-05-18 11:29:44 -07:00
|
|
|
|
|
|
|
void* operator new(uint64_t size) { return GetKernelHeap().Allocate(size); }
|
2023-05-18 13:24:02 -07:00
|
|
|
void* operator new[](uint64_t size) { return GetKernelHeap().Allocate(size); }
|
2023-05-18 11:29:44 -07:00
|
|
|
|
|
|
|
void operator delete(void*, uint64_t) {}
|