code:expand - ikarishinjieva/unixV6-code-analyze-chs GitHub Wiki
- 进程图像大小变化时的相关处理
- 传入一个参数 newsize
- newsize:进程图像的新大小
2250
2251 /*
2252 * Change the size of the data+stack regions of the process.
2253 * If the size is shrinking, it's easy-- just release the
2254 * extra core. If it's growing, and there is core, just
2255 * allocate it and copy the image, taking care to reset
2256 * registers to account for the fact that the system's
2257 * stack has moved.
2259 * swapped out after adjusting the size requirement--
2260 * when it comes in, enough core will be allocated.
2261 * Because of the ssave and SSWAP flags, control will
2262 * resume after the swap in swtch, which executes the return
2263 * from this stack level.
2264 *
2265 * After the expansion, the caller will take care of copying
2266 * the user's stack towards or away from the data area.
2267 */
2268 expand(newsize)
2269 {
2270 int i, n;
2271 register *p, a1, a2;
2272
2274 n = p->p_size;
2275 p->p_size = newsize;
2276 a1 = p->p_addr;
2277 if(n >= newsize) {
2278 mfree(coremap, n-newsize, a1+newsize);
2279 return;
2280 }
2281 savu(u.u_rsav);
- 若进程图像所需空间保持不变或者变小,则直接释放原内存中多余的空间,返回
2282 a2 = malloc(coremap, newsize);
- 保存当前r5,r6至u.u_rsav[2]数组中
2283 if(a2 == NULL) {
- 若进程图像所需空间变大,则在内存中分配一块新的空间
2284 savu(u.u_ssav);
2285 xswap(p, 1, n);
2286 p->p_flag =| SSWAP;
2287 swtch();
2288 /* no return */
2289 }
- 若分配失败,则当前进程无法继续在内存中滞留
- 保存当前r5,r6至u.u_ssav[2]数组中,因为之后会置SSWAP标志,对于带有SSWAP标志的进程,swtch选中其上台时会从u.u_ssav[2]数组中恢复r5,r6
- 将进程图像调出至 盘交换区
- 置SSWAP标志
- 调用swtch进行进程调度
2290 p->p_addr = a2;
2291 for(i=0; i<n; i++)
2292 copyseg(a1+i, a2++);
2293 mfree(coremap, n, a1);
- 若分配成功
- 将原进程图像搬至新分配的内存空间
2295 sureg();
2296 }
- 释放原进程图像在内存中的空间
- 为进程加载地址映照表
2297 /* -------------------------*/
2298
2299