在情况1和2中,释放函数在分配函数中做什么?
case 1: if(mem == 0)
// does this condition mean physical memory has not space?
case 2: if(mappages(pgdir, (char*)a, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0)
// does this condtion mean pagetable entry has not allocate in physical memory?我附加了释放函数和分配函数。
参考:https://github.com/fernandabonetti/xv6/blob/master/vm.c
int
allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
{
char *mem;
uint a;
if(newsz >= KERNBASE)
return 0;
if(newsz < oldsz)
return oldsz;
a = PGROUNDUP(oldsz);
for(; a < newsz; a += PGSIZE){
mem = kalloc();
if(mem == 0){
cprintf("allocuvm out of memory\n");
deallocuvm(pgdir, newsz, oldsz);
return 0;
}
memset(mem, 0, PGSIZE);
if(mappages(pgdir, (char*)a, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){
cprintf("allocuvm out of memory (2)\n");
deallocuvm(pgdir, newsz, oldsz);
kfree(mem);
return 0;
}
}
return newsz;
}
int
deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
{
pte_t *pte;
uint a, pa;
if(newsz >= oldsz)
return oldsz;
a = PGROUNDUP(newsz);
for(; a < oldsz; a += PGSIZE){
pte = walkpgdir(pgdir, (char*)a, 0);
if(!pte)
a = PGADDR(PDX(a) + 1, 0, 0) - PGSIZE;
else if((*pte & PTE_P) != 0){
pa = PTE_ADDR(*pte);
if(pa == 0)
panic("kfree");
char *v = P2V(pa);
kfree(v);
*pte = 0;
}
}
return newsz;
}发布于 2019-05-24 14:55:03
allocuvm是Allocate Virtual Memory的简称。此函数负责增加用户在特定页面目录中的虚拟内存。确实有两种情况下此函数可能会失败:
案例1: kalloc函数失败。kalloc是内核分配的缩写。此函数负责返回RAM中当前未使用的新页面的地址。如果返回0,则表示当前没有可用的未使用的页面。
案例2: mappages函数失败。此函数负责通过将新分配的页面与页面目录中可用的下一个虚拟地址进行映射,使新分配的页面可由使用给定页面目录的进程访问。如果此函数失败,则意味着它无法执行此操作,可能是因为页面目录已满。
在这两种情况下,allocuvm都没有设法将用户的内存增加到请求的大小,因此,它会撤消所有的分配,直到故障点,因此虚拟内存将保持不变,并自行返回一个错误。
https://stackoverflow.com/questions/56258056
复制相似问题