blok.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 243 行
C
243 行
#ifndef lintstatic CHTYPE *sccsid = "@(#)blok.c 4.1 7/17/90";#endif lint/************************************************************************ * * * Copyright (c) 1986 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//* * * Modification History: * * 001 - Gary Gaudet for Andy Gadsby 09-mar-88 * i18n version of csh * * * *//* * UNIX shell * * Bell Telephone Laboratories * */#include "defs.h"/* * storage allocator * (circular first fit strategy) */#define BUSY 01#define busy(x) (Rcheat((x)->word) & BUSY)unsigned brkincr = BRKINCR;struct blk *blokp; /*current search pointer*/struct blk *bloktop; /* top of arena (last blok) */CHTYPE *brkbegin;CHTYPE *setbrk();CHTYPE *alloc(nbytes) unsigned nbytes;{ register unsigned rbytes = round(nbytes + BYTESPERWORD, BYTESPERWORD); for (;;) { int c = 0; register struct blk *p = blokp; register struct blk *q; do { if (!busy(p)) { while (!busy(q = p->word)) p->word = q->word; if ((char *)q - (char *)p >= rbytes) { blokp = (struct blk *)((char *)p + rbytes); if (q > blokp) blokp->word = p->word; p->word = (struct blk *)(Rcheat(blokp) | BUSY); return((CHTYPE *)(p + 1)); } } q = p; p = (struct blk *)(Rcheat(p->word) & ~BUSY); } while (p > q || (c++) == 0); addblok(rbytes); }}addblok(reqd) unsigned reqd;{ if (stakbot == 0) { brkbegin = setbrk(3 * BRKINCR); bloktop = (struct blk *)brkbegin; } if (stakbas != staktop) { register CHTYPE *rndstak; register struct blk *blokstak; pushstak(0); rndstak = (CHTYPE *)round(staktop, BYTESPERWORD); blokstak = (struct blk *)(stakbas) - 1; blokstak->word = stakbsy; stakbsy = blokstak; bloktop->word = (struct blk *)(Rcheat(rndstak) | BUSY); bloktop = (struct blk *)(rndstak); } reqd += brkincr; reqd &= ~(brkincr - 1); blokp = bloktop; bloktop = bloktop->word = (struct blk *)(Rcheat(bloktop) + reqd); if ((CHTYPE *) bloktop >= brkend) { /* GAG - error check from MIPS */ if (setbrk(brkincr) == (CHTYPE *)-1) { error(nospace); } } /* * the following expression sets the busy bit */ bloktop->word = (struct blk *)((char *)brkbegin + 1); { register CHTYPE *stakadr = (CHTYPE *)(bloktop + 2); if (stakbot != staktop) staktop = movstr(stakbot, stakadr); else staktop = stakadr; stakbas = stakbot = stakadr; }}free(ap) struct blk *ap;{ register struct blk *p; if ((p = ap) && p < bloktop) {#ifdef DEBUG chkbptr(p);#endif --p; p->word = (struct blk *)(Rcheat(p->word) & ~BUSY); }}#ifdef DEBUGchkbptr(ptr) struct blk *ptr;{ int exf = 0; register struct blk *p = (struct blk *)brkbegin; register struct blk *q; int us = 0, un = 0; for (;;) { q = (struct blk *)(Rcheat(p->word) & ~BUSY); if (p+1 == ptr) exf++; if (q < (struct blk *)brkbegin || q > bloktop) abort(3); if (p == bloktop) break; if (busy(p)) us += q - p; else un += q - p; if (p >= q) abort(4); p = q; } if (exf == 0) abort(1);}chkmem(){ register struct blk *p = (struct blk *)brkbegin; register struct blk *q; int us = 0, un = 0; for (;;) { q = (struct blk *)(Rcheat(p->word) & ~BUSY); if (q < (struct blk *)brkbegin || q > bloktop) abort(3); if (p == bloktop) break; if (busy(p)) us += q - p; else un += q - p; if (p >= q) abort(4); p = q; } prs("un/used/avail "); prn(un); blank(); prn(us); blank(); prn((CHTYPE *)bloktop - brkbegin - (un + us)); newline();}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?