1: #include <sys/param.h>
2: #include <sys/systm.h>
3: #include <sys/conf.h>
4: #include <sys/buf.h>
5:
6: #ifdef UCB_SCCSID
7: static char sccs_id[] = "@(#)pk3.c 3.1";
8: #endif
9:
10:
11: #define XPRI 30
12: #define NBLOCKS 10
13:
14: int bwaiting, wcount;
15: struct buf *bufps[NBLOCKS];
16: char *nbase[NBLOCKS]; /* normal allocations */
17: short nmap[NBLOCKS]; /* 1 bit == 32 bytes */
18:
19: char log[] ={0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4};
20: #define FULL -1
21: #define LOCK s = spl6()
22: #define UNLOCK splx(s)
23:
24: /*
25: * getepack: get empty packet
26: * with size specified by bitmask.
27: */
28: char *
29: getepack(bits)
30: {
31: register i;
32: int s, savbits;
33: char **base;
34: short *map;
35:
36: base = nbase; map = nmap;
37: savbits = bits;
38:
39: /*
40: * search for space
41: */
42: LOCK;
43: for(;;) {
44:
45: if (bits == FULL)
46: goto force;
47:
48: for(i=0; i<NBLOCKS; i++) {
49: register m;
50: register unsigned n;
51: register offset;
52:
53: m = map[i];
54: if (m==FULL || base[i]==NULL)
55: continue;
56: if (bits == 1) {
57: n = m;
58: m |= m+1;
59: n = m-n;
60: for (offset=0; n > 16; n >>= 4)
61: offset += 4;
62: offset += log[n];
63: } else {
64: bits = savbits;
65: for(n=17; --n; bits <<= 1)
66: if ((m&bits)==0)
67: goto found;
68: continue;
69: found:
70: offset = 16-n;
71: m |= bits;
72: }
73: map[i] = m;
74: UNLOCK;
75: return(base[i] + 32*offset);
76: }
77: /*
78: * grab another block from the system
79: */
80: force:
81: for(i=0;i<NBLOCKS;i++) {
82: register struct buf *bp;
83:
84: if (bufps[i]!=NULL)
85: continue;
86: bufps[i] = bp = geteblk();
87: bp->b_flags |= B_PACK;
88: bp->b_flags |= B_PBUSY;
89: map[i] = bits;
90: base[i] = bp->b_un.b_addr;
91: UNLOCK;
92: return(bp->b_un.b_addr);
93: }
94: /*
95: * sleep until something is released
96: */
97: bwaiting++;
98: wcount++;
99: sleep((caddr_t)&bwaiting, XPRI);
100: bwaiting--;
101: }
102: }
103:
104: /*
105: * freepack: release space beginning
106: * at address p with length specified
107: * by bits.
108: */
109: freepack(p, bits)
110: char *p;
111: {
112: register i, d, s;
113: char **base;
114: short *map;
115:
116: if (p==NULL)
117: return;
118: LOCK;
119: base = nbase; map = nmap;
120:
121: for(i=0;i<NBLOCKS;i++) {
122: d = p-base[i];
123: if (d>=0 && d<=512)
124: goto found;
125: }
126: goto out;
127: found:
128: d >>= 5;
129: d = (bits<<d);
130: map[i] &= ~d;
131: if (map[i]==0) {
132: register struct buf *bp;
133:
134: bp = bufps[i];
135: bp->b_flags &= ~B_PBUSY;
136: base[i] = NULL;
137: bufps[i] = NULL;
138: #ifdef UCB_BUFOUT
139: abrelse(bp);
140: #else
141: brelse(bp);
142: #endif
143: }
144: if (bwaiting)
145: wakeup((caddr_t)&bwaiting);
146: out:
147: splx(s);
148: }
149:
150:
151:
152: /*
153: * integer to bitmap conversion
154: */
155: dtom(d)
156: register d;
157: {
158: register m;
159:
160: m = 1;
161: while (d>32) {
162: d -= 32;
163: m |= m+1;
164: }
165: return(m);
166: }
167:
168: #define NRECS 160
169: int reclist[NRECS];
170: int recbits[NRECS];
Defined functions
dtom
defined in line
155; used 2 times
Defined variables
bufps
defined in line
15; used 4 times
log
defined in line
19; used 1 times
nbase
defined in line
16; used 2 times
nmap
defined in line
17; used 2 times
Defined macros
FULL
defined in line
20; used 2 times
LOCK
defined in line
21; used 2 times
XPRI
defined in line
11; used 1 times