mem: implement ref to physical page

This commit is contained in:
Mathieu Maret 2018-11-09 17:07:39 +01:00
parent a7b6139f06
commit 149b197a65
3 changed files with 24 additions and 8 deletions

View File

@ -43,7 +43,7 @@ struct mem_desc *addr2memDesc(unsigned long addr)
return page_desc + idx; return page_desc + idx;
} }
unsigned long allocPage(void) unsigned long allocPhyPage(void)
{ {
if (list_is_empty(free_page)) { if (list_is_empty(free_page)) {
return (unsigned long)NULL; return (unsigned long)NULL;
@ -54,7 +54,7 @@ unsigned long allocPage(void)
return mem->phy_addr; return mem->phy_addr;
} }
int unrefPage(unsigned long addr) int unrefPhyPage(unsigned long addr)
{ {
struct mem_desc *mem = addr2memDesc(addr); struct mem_desc *mem = addr2memDesc(addr);
if (!mem) { if (!mem) {
@ -68,3 +68,18 @@ int unrefPage(unsigned long addr)
return 0; return 0;
} }
int refPhyPage(unsigned long addr)
{
struct mem_desc *mem = addr2memDesc(addr);
if (!mem) {
return -1;
}
mem->ref++;
if (mem->ref == 1) {
list_add_tail(used_page, mem);
list_delete(free_page, mem);
}
return 0;
}

View File

@ -17,5 +17,6 @@ struct mem_desc{
int memSetup(unsigned long upper_mem); int memSetup(unsigned long upper_mem);
unsigned long allocPage(void); unsigned long allocPhyPage(void);
int unrefPage(unsigned long addr); int unrefPhyPage(unsigned long addr);
int refPhyPage(unsigned long addr);

View File

@ -13,7 +13,7 @@ static void testPhymem()
int allocCount = 0; int allocCount = 0;
int freeCount = 0; int freeCount = 0;
while ((page = (struct mem_desc *)allocPage()) != NULL) { while ((page = (struct mem_desc *)allocPhyPage()) != NULL) {
page->phy_addr = allocCount; page->phy_addr = allocCount;
allocCount++; allocCount++;
list_add_tail(allocated_page_list, page); list_add_tail(allocated_page_list, page);
@ -24,17 +24,17 @@ static void testPhymem()
if (page->phy_addr != (ulong)freeCount) { if (page->phy_addr != (ulong)freeCount) {
printf("Error page %d modified\n", (ulong)page); printf("Error page %d modified\n", (ulong)page);
} }
if (unrefPage((ulong)page)) { if (unrefPhyPage((ulong)page)) {
printf("Failed to free page %d\n", (ulong)page); printf("Failed to free page %d\n", (ulong)page);
} }
freeCount++; freeCount++;
} }
printf("%d pages freed\n", freeCount); printf("%d pages freed\n", freeCount);
if ((page = (struct mem_desc *)allocPage()) == NULL) { if ((page = (struct mem_desc *)allocPhyPage()) == NULL) {
printf("Error ! Cannot allocate memory\n"); printf("Error ! Cannot allocate memory\n");
} else { } else {
unrefPage((ulong)page); unrefPhyPage((ulong)page);
} }
} }