-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c197e6a
commit afdcb0e
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Implementation | ||
|
||
1. Allocate and Deallocate Physical Pages | ||
**Functionality**: | ||
|
||
- `allocatePhysicalPage()` uses `mmap` to allocate a 4KB physical page. | ||
- Destructor `~MemoryManager()` uses `munmap` to deallocate physical pages. | ||
|
||
``` | ||
void* allocatePhysicalPage() { | ||
void* page = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
if (page == MAP_FAILED) { | ||
std::cerr << "Error while mmapping page " << page << ": " << std::strerror(errno) << std::endl; | ||
return nullptr; | ||
} | ||
physicalPages_.push_back(page); | ||
return page; | ||
} | ||
MemoryManager::~MemoryManager() { | ||
for (void* page : physicalPages_) { | ||
if (munmap(page, PAGE_SIZE) != 0) { | ||
std::cerr << "Error unmapping page at " << page << ": " << std::strerror(errno) << std::endl; | ||
} | ||
} | ||
} | ||
``` |