Skip to content

Commit

Permalink
mm: fix/improve malloc for on-demand paging
Browse files Browse the repository at this point in the history
  • Loading branch information
HidenoriMatsubayashi committed May 5, 2023
1 parent 212d9da commit 2515fbf
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/mm/user_heap/umm_malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,52 @@
#include "common/logger.h"
#include "mm/pgtable.h"

// FIXME
#define PAGING_PAGES (static_cast<uint64_t>(1024) * 1024 * 1024 * 2 / PAGE_SIZE)

namespace evisor {

namespace {
// FIXME: use kUserSize
constexpr uint32_t kPagingPages =
static_cast<uint64_t>(1024) * 1024 * 1024 * 2 / PAGE_SIZE;
constexpr uint32_t kMemMapSize = kPagingPages / 8;

bool userHeapMemMap[PAGING_PAGES] = {false};

uint8_t userMemoryRegionMap_[kPagingPages] = {0};
uint32_t nextFreeSpaceIndex = 0;
} // namespace

void* umm_malloc(size_t size) {
if (size < 1) {
if (size < PAGE_SIZE) {
LOG_ERROR("Requested size (%d) is less than page size(%d)", size,
PAGE_SIZE);
return nullptr;
}

// FIXME
size = size;
if (size % PAGE_SIZE != 0) {
LOG_ERROR("Requested size (%d) is not aligned to page size(%d)", size,
PAGE_SIZE);
return nullptr;
}

for (uint32_t i = 0; i < PAGING_PAGES; i++) {
if (!userHeapMemMap[i]) {
userHeapMemMap[i] = true;
for (uint32_t i = nextFreeSpaceIndex; i < kPagingPages; i++) {
const uint32_t j = i / 8;
const uint8_t bit = 1 << (i % 8);
if ((userMemoryRegionMap_[j] & bit) == 0) {
userMemoryRegionMap_[j] |= bit;
nextFreeSpaceIndex = i + 1;
auto* page = reinterpret_cast<uint64_t*>(kUserStart + i * PAGE_SIZE);
return page;
}
}
PANIC("No free pages!");

PANIC("No free pages in user memory region!");
return nullptr;
}

void umm_free(void* va) {
userHeapMemMap[(reinterpret_cast<uint64_t>(va) - kUserStart) / PAGE_SIZE] =
false;
const uint32_t page =
(reinterpret_cast<uint64_t>(va) - kUserStart) / PAGE_SIZE;
const uint32_t i = page / 8;
const uint8_t bit = 1 << (page % 8);
userMemoryRegionMap_[i] &= ~bit;
}

} // namespace evisor

0 comments on commit 2515fbf

Please sign in to comment.