paging optimization

This commit is contained in:
2026-01-28 21:37:05 +11:00
parent 1326696724
commit 68df6c7ebb
5 changed files with 45 additions and 38 deletions

View File

@@ -52,23 +52,6 @@ void PageFrameAllocator::InitBitmap(size_t bitmapSize, void* bufferAddress){
}
}
void PageFrameAllocator::FreePage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == false) return; // page is already free
// free page in bitmap
if (PageBitmap.Set(index, false)) {
freeMemory += 4096;
usedMemory -= 4096;
}
}
void PageFrameAllocator::FreePages(void* address, uint64_t pageCount) {
for (int t = 0; t < pageCount; t++) {
FreePage((void*)((uint64_t)address + (t * 4096)));
}
}
void PageFrameAllocator::LockPage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == true) return; // page is already locked
@@ -86,23 +69,6 @@ void PageFrameAllocator::LockPages(void* address, uint64_t pageCount) {
}
}
void PageFrameAllocator::UnreservePage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == false) return; // page is already free
// free page in bitmap
if (PageBitmap.Set(index, false)) {
freeMemory += 4096;
reservedMemory -= 4096;
}
}
void PageFrameAllocator::UnreservePages(void* address, uint64_t pageCount) {
for (int t = 0; t < pageCount; t++) {
UnreservePage((void*)((uint64_t)address + (t * 4096)));
}
}
void PageFrameAllocator::ReservePage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == true) return; // page is already locked
@@ -120,17 +86,58 @@ void PageFrameAllocator::ReservePages(void* address, uint64_t pageCount) {
}
}
uint64_t pageBitmapIndex = 0;
void* PageFrameAllocator::RequestPage() {
for (uint64_t index = 0; index < PageBitmap.Size * 8; index++) {
if (PageBitmap[index] == true) continue;
for (; pageBitmapIndex < PageBitmap.Size * 8; pageBitmapIndex++) {
if (PageBitmap[pageBitmapIndex] == true) continue;
LockPage((void*)(index * 4096));
return (void*)(index * 4096);
LockPage((void*)(pageBitmapIndex * 4096));
return (void*)(pageBitmapIndex * 4096);
}
return NULL; // page frame swap to file
}
void PageFrameAllocator::FreePage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == false) return; // page is already free
// free page in bitmap
if (PageBitmap.Set(index, false)) {
freeMemory += 4096;
usedMemory -= 4096;
if (pageBitmapIndex > index) pageBitmapIndex = index;
}
}
void PageFrameAllocator::FreePages(void* address, uint64_t pageCount) {
for (int t = 0; t < pageCount; t++) {
FreePage((void*)((uint64_t)address + (t * 4096)));
}
}
void PageFrameAllocator::UnreservePage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == false) return; // page is already free
// free page in bitmap
if (PageBitmap.Set(index, false)) {
freeMemory += 4096;
reservedMemory -= 4096;
if (pageBitmapIndex > index) pageBitmapIndex = index;
}
}
void PageFrameAllocator::UnreservePages(void* address, uint64_t pageCount) {
for (int t = 0; t < pageCount; t++) {
UnreservePage((void*)((uint64_t)address + (t * 4096)));
}
}
uint64_t PageFrameAllocator::GetFreeRAM() {
return freeMemory;
}