Consider the following code:

1  int growproc(int n) {
2      if(n > 0) sz = allocuvm(curproc->pgdir, sz, sz + n);
3      else if(n < 0) sz = deallocuvm(curproc->pgdir, sz, sz + n);
4      curproc->sz = sz; switchuvm(curproc);
5  }
6  int allocuvm(pde_t pgdir, uint oldsz, uint newsz) {
7      if(newsz >= KERNBASE) return 0;
8      for(uint a = PGROUNDUP(oldsz); a < newsz; a += PGSIZE) {
9          char* mem = kalloc(); memset(mem, 0, PGSIZE);
10          mappages(pgdir, (char*)a, PGSIZE, V2P(mem), PTE_W|PTE_U); } }