|
|
1.1 root 1: /* @(#)blok.c 1.4 */
2: /*
3: * UNIX shell
4: *
5: * Bell Telephone Laboratories
6: *
7: */
8:
9: #include "defs.h"
10:
11:
12: /*
13: * storage allocator
14: * (circular first fit strategy)
15: */
16:
17: #define BUSY 01
18: #define busy(x) (Rcheat((x)->word) & BUSY)
19:
20: unsigned brkincr = BRKINCR;
21: struct blk *blokp; /*current search pointer*/
22: struct blk *bloktop; /* top of arena (last blok) */
23:
24: char *brkbegin;
25: char *setbrk();
26:
27: char *
28: alloc(nbytes)
29: unsigned nbytes;
30: {
31: register unsigned rbytes = round(nbytes+BYTESPERWORD, BYTESPERWORD);
32:
33: for (;;)
34: {
35: int c = 0;
36: register struct blk *p = blokp;
37: register struct blk *q;
38:
39: do
40: {
41: if (!busy(p))
42: {
43: while (!busy(q = p->word))
44: p->word = q->word;
45: if ((char *)q - (char *)p >= rbytes)
46: {
47: blokp = (struct blk *)((char *)p + rbytes);
48: if (q > blokp)
49: blokp->word = p->word;
50: p->word = (struct blk *)(Rcheat(blokp) | BUSY);
51: return((char *)(p + 1));
52: }
53: }
54: q = p;
55: p = (struct blk *)(Rcheat(p->word) & ~BUSY);
56: } while (p > q || (c++) == 0);
57: addblok(rbytes);
58: }
59: }
60:
61: addblok(reqd)
62: unsigned reqd;
63: {
64: if (stakbot == 0)
65: {
66: brkbegin = setbrk(3 * BRKINCR);
67: bloktop = (struct blk *)brkbegin;
68: }
69:
70: if (stakbas != staktop)
71: {
72: register char *rndstak;
73: register struct blk *blokstak;
74:
75: pushstak(0);
76: rndstak = (char *)round(staktop, BYTESPERWORD);
77: blokstak = (struct blk *)(stakbas) - 1;
78: blokstak->word = stakbsy;
79: stakbsy = blokstak;
80: bloktop->word = (struct blk *)(Rcheat(rndstak) | BUSY);
81: bloktop = (struct blk *)(rndstak);
82: }
83: reqd += brkincr;
84: reqd &= ~(brkincr - 1);
85: blokp = bloktop;
86: bloktop = bloktop->word = (struct blk *)(Rcheat(bloktop) + reqd);
87: bloktop->word = (struct blk *)(brkbegin + 1);
88: {
89: register char *stakadr = (char *)(bloktop + 2);
90:
91: if (stakbot != staktop)
92: staktop = movstr(stakbot, stakadr);
93: else
94: staktop = stakadr;
95:
96: stakbas = stakbot = stakadr;
97: }
98: }
99:
100: free(ap)
101: struct blk *ap;
102: {
103: register struct blk *p;
104:
105: if ((p = ap) && p < bloktop)
106: {
107: #ifdef DEBUG
108: chkbptr(p);
109: #endif
110: --p;
111: p->word = (struct blk *)(Rcheat(p->word) & ~BUSY);
112: }
113:
114:
115: }
116:
117:
118: #ifdef DEBUG
119:
120: chkbptr(ptr)
121: struct blk *ptr;
122: {
123: int exf = 0;
124: register struct blk *p = (struct blk *)brkbegin;
125: register struct blk *q;
126: int us = 0, un = 0;
127:
128: for (;;)
129: {
130: q = (struct blk *)(Rcheat(p->word) & ~BUSY);
131:
132: if (p+1 == ptr)
133: exf++;
134:
135: if (q < (struct blk *)brkbegin || q > bloktop)
136: abort(3);
137:
138: if (p == bloktop)
139: break;
140:
141: if (busy(p))
142: us += q - p;
143: else
144: un += q - p;
145:
146: if (p >= q)
147: abort(4);
148:
149: p = q;
150: }
151: if (exf == 0)
152: abort(1);
153: }
154:
155:
156: chkmem()
157: {
158: register struct blk *p = (struct blk *)brkbegin;
159: register struct blk *q;
160: int us = 0, un = 0;
161:
162: for (;;)
163: {
164: q = (struct blk *)(Rcheat(p->word) & ~BUSY);
165:
166: if (q < (struct blk *)brkbegin || q > bloktop)
167: abort(3);
168:
169: if (p == bloktop)
170: break;
171:
172: if (busy(p))
173: us += q - p;
174: else
175: un += q - p;
176:
177: if (p >= q)
178: abort(4);
179:
180: p = q;
181: }
182:
183: prs("un/used/avail ");
184: prn(un);
185: blank();
186: prn(us);
187: blank();
188: prn((char *)bloktop - brkbegin - (un + us));
189: newline();
190:
191: }
192:
193: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.