mem: implement ref to physical page
This commit is contained in:
parent
a7b6139f06
commit
149b197a65
19
core/mem.c
19
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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user