|
|
1.1 root 1: #include "cbt.h"
2:
3: typedef struct bhd {
4: struct bhd *next;
5: short fd;
6: long which;
7: char *where;
8: } bhd;
9:
10: enum {Nhash = 601, Max = 1000};
11: bhd *bhds[Nhash], *bhfree;
12: bhd bhpool[Max];
13: int btptr;
14: #define hash(a, b) ((b << 8) | a) % Nhash
15:
16: btbufinit()
17: { int i;
18: char *p;
19: p = (char *) malloc(Max * NDSZ);
20: if(!p)
21: abort();
22: for(i = 1; i < Max; i++) {
23: bhpool[i-1].next = bhpool + i;
24: bhpool[i].where = p + NDSZ * i;
25: }
26: bhpool[0].where = p;
27: bhfree = bhpool + 0;
28: }
29:
30: btbufdone(fd)
31: { int i;
32: bhd *p, *q;
33: for(i = 0; i < Nhash; i++) {
34: p = bhds[i];
35: loop:
36: if(!p)
37: continue;
38: if(p->fd != fd) {
39: p = p->next;
40: goto loop;
41: }
42: lseek(p->fd, p->which*NDSZ, 0);
43: write(p->fd, p->where, NDSZ); /* check these dammit */
44: q = p;
45: p = p->next;
46: q->next = bhfree;
47: goto loop;
48: }
49: }
50:
51: btbufwrt(fd, which, from)
52: char *from;
53: { int n;
54: bhd *p;
55: n = hash(fd, which);
56: for(p = bhds[n]; p; p = p->next)
57: if(p->fd == fd && p->which == which) {
58: btbcpy(p->where, from, NDSZ);
59: return;
60: }
61: if(!bhfree)
62: btbuffree();
63: p = bhfree;
64: bhfree = p->next;
65: p->next = bhds[n];
66: bhds[n] = p;
67: p->fd = fd;
68: p->which = which;
69: btbcpy(p->where, from, NDSZ);
70: }
71:
72: btbuffree()
73: { bhd *p;
74: loop:
75: if(btptr >= Max)
76: btptr = 0;
77: p = bhpool + btptr++;
78: if(!p->next)
79: goto loop;
80: bhfree = p->next;
81: p->next = bhfree->next;
82: bhfree->next = 0;
83: lseek(bhfree->fd, bhfree->which*NDSZ, 0);
84: write(bhfree->fd, bhfree->where, NDSZ); /* check this dammit */
85: }
86:
87: btbufread(fd, which, to)
88: char *to;
89: { int n;
90: bhd *p;
91: static init;
92: if(!init++)
93: btbufinit();
94: n = hash(fd, which);
95: for(p = bhds[n]; p; p = p->next)
96: if(p->fd == fd && p->which == which) {
97: btbcpy(to, p->where, NDSZ);
98: return(0);
99: }
100:
101: if(!bhfree)
102: btbuffree();
103: p = bhfree;
104: bhfree = p->next;
105: p->next = bhds[n];
106: bhds[n] = p;
107: p->fd = fd;
108: p->which = which;
109: if(lseek(fd, which * NDSZ, 0) == -1)
110: return(EOF);
111: if(read(fd, p->where, NDSZ) != NDSZ)
112: return(EOF);
113: btbcpy(to, p->where, NDSZ);
114: return(0);
115: }
116:
117: btbcpy(to, from, sz)
118: register int *to, *from, sz;
119: { register int cnt = sz/sizeof(int);
120: while(cnt-- > 0)
121: *to++ = *from++;
122: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.