From 149b197a652f36ee77bfa8c902b9f83dbfc304fb Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 9 Nov 2018 17:07:39 +0100 Subject: [PATCH] mem: implement ref to physical page --- core/mem.c | 19 +++++++++++++++++-- core/mem.h | 5 +++-- tests/test.c | 8 ++++---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/core/mem.c b/core/mem.c index 19bea4a..8e1bdc7 100644 --- a/core/mem.c +++ b/core/mem.c @@ -43,7 +43,7 @@ struct mem_desc *addr2memDesc(unsigned long addr) return page_desc + idx; } -unsigned long allocPage(void) +unsigned long allocPhyPage(void) { if (list_is_empty(free_page)) { return (unsigned long)NULL; @@ -54,7 +54,7 @@ unsigned long allocPage(void) return mem->phy_addr; } -int unrefPage(unsigned long addr) +int unrefPhyPage(unsigned long addr) { struct mem_desc *mem = addr2memDesc(addr); if (!mem) { @@ -68,3 +68,18 @@ int unrefPage(unsigned long addr) 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; +} diff --git a/core/mem.h b/core/mem.h index 084c214..1249c0d 100644 --- a/core/mem.h +++ b/core/mem.h @@ -17,5 +17,6 @@ struct mem_desc{ int memSetup(unsigned long upper_mem); -unsigned long allocPage(void); -int unrefPage(unsigned long addr); +unsigned long allocPhyPage(void); +int unrefPhyPage(unsigned long addr); +int refPhyPage(unsigned long addr); diff --git a/tests/test.c b/tests/test.c index c1271bf..a92b09e 100644 --- a/tests/test.c +++ b/tests/test.c @@ -13,7 +13,7 @@ static void testPhymem() int allocCount = 0; int freeCount = 0; - while ((page = (struct mem_desc *)allocPage()) != NULL) { + while ((page = (struct mem_desc *)allocPhyPage()) != NULL) { page->phy_addr = allocCount; allocCount++; list_add_tail(allocated_page_list, page); @@ -24,17 +24,17 @@ static void testPhymem() if (page->phy_addr != (ulong)freeCount) { 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); } 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"); } else { - unrefPage((ulong)page); + unrefPhyPage((ulong)page); } }