1: #
2: /*
3: * UNIX shell
4: *
5: * S. R. Bourne
6: * Bell Telephone Laboratories
7: *
8: */
9:
10: #include "defs.h"
11:
12:
13: /*
14: * storage allocator
15: * (circular first fit strategy)
16: */
17:
18: #define BUSY 01
19: #define busy(x) (Rcheat((x)->word)&BUSY)
20:
21: POS brkincr=BRKINCR;
22: BLKPTR blokp; /*current search pointer*/
23: BLKPTR bloktop=BLK(end); /*top of arena (last blok)*/
24:
25:
26:
27: ADDRESS alloc(nbytes)
28: POS nbytes;
29: {
30: REG POS rbytes = round(nbytes+BYTESPERWORD,BYTESPERWORD);
31:
32: LOOP INT c=0;
33: REG BLKPTR p = blokp;
34: REG BLKPTR q;
35: REP IF !busy(p)
36: THEN WHILE !busy(q = p->word) DO p->word = q->word OD
37: IF ADR(q)-ADR(p) >= rbytes
38: THEN blokp = BLK(ADR(p)+rbytes);
39: IF q > blokp
40: THEN blokp->word = p->word;
41: FI
42: p->word=BLK(Rcheat(blokp)|BUSY);
43: return(ADR(p+1));
44: FI
45: FI
46: q = p; p = BLK(Rcheat(p->word)&~BUSY);
47: PER p>q ORF (c++)==0 DONE
48: addblok(rbytes);
49: POOL
50: }
51:
52: VOID addblok(reqd)
53: POS reqd;
54: {
55: IF stakbas!=staktop
56: THEN REG STKPTR rndstak;
57: REG BLKPTR blokstak;
58:
59: pushstak(0);
60: rndstak=round(staktop,BYTESPERWORD);
61: blokstak=BLK(stakbas)-1;
62: blokstak->word=stakbsy; stakbsy=blokstak;
63: bloktop->word=BLK(Rcheat(rndstak)|BUSY);
64: bloktop=BLK(rndstak);
65: FI
66: reqd += brkincr; reqd &= ~(brkincr-1);
67: blokp=bloktop;
68: bloktop=bloktop->word=BLK(Rcheat(bloktop)+reqd);
69: bloktop->word=BLK(ADR(end)+1);
70: BEGIN
71: REG STKPTR stakadr=STK(bloktop+2);
72: staktop=movstr(stakbot,stakadr);
73: stakbas=stakbot=stakadr;
74: END
75: }
76:
77: VOID free(ap)
78: BLKPTR ap;
79: {
80: REG BLKPTR p;
81:
82: IF (p=ap) ANDF p<bloktop
83: THEN Lcheat((--p)->word) &= ~BUSY;
84: FI
85: }
86:
87: #ifdef DEBUG
88: chkbptr(ptr)
89: BLKPTR ptr;
90: {
91: INT exf=0;
92: REG BLKPTR p = end;
93: REG BLKPTR q;
94: INT us=0, un=0;
95:
96: LOOP
97: q = Rcheat(p->word)&~BUSY;
98: IF p==ptr THEN exf++ FI
99: IF q<end ORF q>bloktop THEN abort(3) FI
100: IF p==bloktop THEN break FI
101: IF busy(p)
102: THEN us += q-p;
103: ELSE un += q-p;
104: FI
105: IF p>=q THEN abort(4) FI
106: p=q;
107: POOL
108: IF exf==0 THEN abort(1) FI
109: prn(un); prc(SP); prn(us); prc(NL);
110: }
111: #endif
Defined functions
alloc
defined in line
27;
never used
free
defined in line
77; used 7 times
Defined macros
BUSY
defined in line
18; used 6 times
busy
defined in line
19; used 3 times