152628bc3Ssam #ifndef lint 2*fdb874ddSbostic static char sccsid[] = "@(#)blok.c 4.3 05/30/94"; 352628bc3Ssam #endif 40d870856Smckusick 50d870856Smckusick # 60d870856Smckusick /* 70d870856Smckusick * UNIX shell 80d870856Smckusick * 90d870856Smckusick * S. R. Bourne 100d870856Smckusick * Bell Telephone Laboratories 110d870856Smckusick * 120d870856Smckusick */ 130d870856Smckusick 140d870856Smckusick #include "defs.h" 150d870856Smckusick 160d870856Smckusick 170d870856Smckusick /* 180d870856Smckusick * storage allocator 190d870856Smckusick * (circular first fit strategy) 200d870856Smckusick */ 210d870856Smckusick 220d870856Smckusick #define BUSY 01 230d870856Smckusick #define busy(x) (Rcheat((x)->word)&BUSY) 240d870856Smckusick 250d870856Smckusick POS brkincr=BRKINCR; 260d870856Smckusick BLKPTR blokp; /*current search pointer*/ 270d870856Smckusick BLKPTR bloktop=BLK(end); /*top of arena (last blok)*/ 280d870856Smckusick 290d870856Smckusick 300d870856Smckusick alloc(nbytes)310d870856SmckusickADDRESS alloc(nbytes) 320d870856Smckusick POS nbytes; 330d870856Smckusick { 340d870856Smckusick REG POS rbytes = round(nbytes+BYTESPERWORD,BYTESPERWORD); 350d870856Smckusick 360d870856Smckusick LOOP INT c=0; 370d870856Smckusick REG BLKPTR p = blokp; 380d870856Smckusick REG BLKPTR q; 390d870856Smckusick REP IF !busy(p) 400d870856Smckusick THEN WHILE !busy(q = p->word) DO p->word = q->word OD 410d870856Smckusick IF ADR(q)-ADR(p) >= rbytes 420d870856Smckusick THEN blokp = BLK(ADR(p)+rbytes); 430d870856Smckusick IF q > blokp 440d870856Smckusick THEN blokp->word = p->word; 450d870856Smckusick FI 460d870856Smckusick p->word=BLK(Rcheat(blokp)|BUSY); 470d870856Smckusick return(ADR(p+1)); 480d870856Smckusick FI 490d870856Smckusick FI 500d870856Smckusick q = p; p = BLK(Rcheat(p->word)&~BUSY); 510d870856Smckusick PER p>q ORF (c++)==0 DONE 520d870856Smckusick addblok(rbytes); 530d870856Smckusick POOL 540d870856Smckusick } 550d870856Smckusick addblok(reqd)560d870856SmckusickVOID addblok(reqd) 570d870856Smckusick POS reqd; 580d870856Smckusick { 590d870856Smckusick IF stakbas!=staktop 600d870856Smckusick THEN REG STKPTR rndstak; 610d870856Smckusick REG BLKPTR blokstak; 620d870856Smckusick 630d870856Smckusick pushstak(0); 640d870856Smckusick rndstak=round(staktop,BYTESPERWORD); 650d870856Smckusick blokstak=BLK(stakbas)-1; 660d870856Smckusick blokstak->word=stakbsy; stakbsy=blokstak; 670d870856Smckusick bloktop->word=BLK(Rcheat(rndstak)|BUSY); 680d870856Smckusick bloktop=BLK(rndstak); 690d870856Smckusick FI 700d870856Smckusick reqd += brkincr; reqd &= ~(brkincr-1); 710d870856Smckusick blokp=bloktop; 720d870856Smckusick bloktop=bloktop->word=BLK(Rcheat(bloktop)+reqd); 730d870856Smckusick bloktop->word=BLK(ADR(end)+1); 740d870856Smckusick BEGIN 750d870856Smckusick REG STKPTR stakadr=STK(bloktop+2); 760d870856Smckusick staktop=movstr(stakbot,stakadr); 770d870856Smckusick stakbas=stakbot=stakadr; 780d870856Smckusick END 790d870856Smckusick } 800d870856Smckusick free(ap)810d870856SmckusickVOID free(ap) 820d870856Smckusick BLKPTR ap; 830d870856Smckusick { 840d870856Smckusick REG BLKPTR p; 850d870856Smckusick 860d870856Smckusick IF (p=ap) ANDF p<bloktop 870d870856Smckusick THEN Lcheat((--p)->word) &= ~BUSY; 880d870856Smckusick FI 890d870856Smckusick } 900d870856Smckusick 910d870856Smckusick #ifdef DEBUG chkbptr(ptr)920d870856Smckusickchkbptr(ptr) 930d870856Smckusick BLKPTR ptr; 940d870856Smckusick { 950d870856Smckusick INT exf=0; 960d870856Smckusick REG BLKPTR p = end; 970d870856Smckusick REG BLKPTR q; 980d870856Smckusick INT us=0, un=0; 990d870856Smckusick 1000d870856Smckusick LOOP 1010d870856Smckusick q = Rcheat(p->word)&~BUSY; 1020d870856Smckusick IF p==ptr THEN exf++ FI 1030d870856Smckusick IF q<end ORF q>bloktop THEN abort(3) FI 1040d870856Smckusick IF p==bloktop THEN break FI 1050d870856Smckusick IF busy(p) 1060d870856Smckusick THEN us += q-p; 1070d870856Smckusick ELSE un += q-p; 1080d870856Smckusick FI 1090d870856Smckusick IF p>=q THEN abort(4) FI 1100d870856Smckusick p=q; 1110d870856Smckusick POOL 1120d870856Smckusick IF exf==0 THEN abort(1) FI 1130d870856Smckusick prn(un); prc(SP); prn(us); prc(NL); 1140d870856Smckusick } 1150d870856Smckusick #endif 116*fdb874ddSbostic 117*fdb874ddSbostic void * realloc(cp,nbytes)118*fdb874ddSbosticrealloc(cp, nbytes) 119*fdb874ddSbostic void *cp; 120*fdb874ddSbostic unsigned int nbytes; 121*fdb874ddSbostic { 122*fdb874ddSbostic void *new; 123*fdb874ddSbostic 124*fdb874ddSbostic new = malloc(nbytes); 125*fdb874ddSbostic if (cp == 0) 126*fdb874ddSbostic return (new); 127*fdb874ddSbostic bcopy(cp, new, nbytes); 128*fdb874ddSbostic free(cp); 129*fdb874ddSbostic return (new); 130*fdb874ddSbostic } 131