File: disc.c

package info (click to toggle)
9term 1.6.6-5
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 2,340 kB
  • ctags: 2,035
  • sloc: ansic: 17,308; makefile: 220; sh: 178
file content (335 lines) | stat: -rw-r--r-- 6,859 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* Copyright (c) 1992 AT&T - All rights reserved. */
#include "sam.h"

#define	BLOCKFILL	(BLOCKSIZE/2)

static Discdesc	desc[NBUFFILES];

void		bkalloc(Disc*, int);
void		bkfree(Disc*, int);
void		bkwrite(Disc*, Rune*, int, int, int);
void		bkread(Disc*, Rune*, int, int, int);


Discdesc *
Dstart(void)
{
	int i, fd;
	Discdesc *dd;

	for(i=0, dd=desc; dd->fd; i++, dd++)
		if(i == NBUFFILES-1)
			panic("too many buffer files");
	fd = newtmp(i);
	if(fd < 0)
		panic("can't create buffer file");
	dd->fd = fd;
	return dd;
}

Disc *
Dopen(Discdesc *dd)
{
	Disc *d;

	d = emalloc(sizeof(Disc));
	d->desc = dd;
	return d;
}

void
Dclose(Disc *d)
{
	int i;

	for(i=d->block.nused; --i>=0; )	/* backwards because bkfree() stacks */
		bkfree(d, i);
	free(d->block.listptr);
	free(d);
}

int
Dread(Disc *d, Rune *addr, int n, Posn p1)
{
	int i, nb, nr;
	Posn p = 0, p2 = p1+n;

	for(i=0; i<d->block.nused; i++){
		if((p+=d->block.blkptr[i].nrunes) > p1){
			p -= d->block.blkptr[i].nrunes;
			goto out;
		}
	}
	if(p == p1)
		return 0;	/* eof */
	return -1;		/* past eof */

    out:
	n = 0;
	if(p != p1){	/* trailing partial block */
		nb = d->block.blkptr[i].nrunes;
		if(p2 > p+nb)
			nr = nb-(p1-p);
		else
			nr = p2-p1;
		bkread(d, addr, nr, i, p1-p);
		/* advance to next block */
		p += nb;
		addr += nr;
		n += nr;
		i++;
	}
	/* whole blocks */
	while(p<p2 && (nb = d->block.blkptr[i].nrunes)<=p2-p){
		if(i >= d->block.nused)
			return n;	/* eof */
		bkread(d, addr, nb, i, 0);
		p += nb;
		addr += nb;
		n += nb;
		i++;
	}
	if(p < p2){	/* any initial partial block left? */
		nr = p2-p;
		nb = d->block.blkptr[i].nrunes;
		if(nr>nb)
			nr = nb;		/* eof */
		/* just read in the part that survives */
		bkread(d, addr, nr, i, 0);
		n += nr;
	}
	return n;
}

void
Dinsert(Disc *d, Rune *addr, int n, Posn p0) /* if addr null, just make space */
{
	int i, nb, ni;
	Posn p = 0;
	Rune hold[BLOCKSIZE];
	int nhold;

	for(i=0; i<d->block.nused; i++){
		if((p+=d->block.blkptr[i].nrunes) >= p0){
			p -= d->block.blkptr[i].nrunes;
			goto out;
		}
	}
	if(p != p0)
		panic("Dinsert");	/* beyond eof */

    out:
	d->nrunes += n;
	nhold = 0;
	if(i<d->block.nused && (nb=d->block.blkptr[i].nrunes)>p0-p){
		nhold = nb-(p0-p);
		bkread(d, hold, nhold, i, p0-p);
		d->block.blkptr[i].nrunes -= nhold;	/* no write necessary */
	}
	/* insertion point is now at end of block i (which may not exist) */
	while(n > 0){
		if(i < d->block.nused
		&& (nb=d->block.blkptr[i].nrunes) < BLOCKFILL){
			/* fill this block */
			if(nb+n > BLOCKSIZE)
				ni = BLOCKFILL-nb;
			else
				ni = n;
			if(addr)
				bkwrite(d, addr, ni, i, nb);
			nb += ni;
		}else{	/* make new block */
			if(i < d->block.nused)
				i++;	/* put after this block, if it exists */
			bkalloc(d, i);
			if(n > BLOCKSIZE)
				ni = BLOCKFILL;
			else
				ni = n;
			if(addr)
				bkwrite(d, addr, ni, i, 0);
			nb = ni;
		}
		d->block.blkptr[i].nrunes = nb;
		if(addr)
			addr += ni;
		n -= ni;
	}
	if(nhold){
		if(i < d->block.nused
		&& (nb=d->block.blkptr[i].nrunes)+nhold < BLOCKSIZE){
			/* fill this block */
			bkwrite(d, hold, nhold, i, nb);
			nb += nhold;
		}else{	/* make new block */
			if(i < d->block.nused)
				i++;	/* put after this block, if it exists */
			bkalloc(d, i);
			bkwrite(d, hold, nhold, i, 0);
			nb = nhold;
		}
		d->block.blkptr[i].nrunes = nb;
	}
}

void
Ddelete(Disc *d, Posn p1, Posn p2)
{
	int i, nb, nd;
	Posn p = 0;
	Rune buf[BLOCKSIZE];

	for(i = 0; i<d->block.nused; i++){
		if((p+=d->block.blkptr[i].nrunes) > p1){
			p -= d->block.blkptr[i].nrunes;
			goto out;
		}
	}
	if(p1!=d->nrunes || p2!=p1)
		panic("Ddelete");
	return;	/* beyond eof */

    out:
	d->nrunes -= p2-p1;
	if(p != p1){	/* throw away partial block */
		nb = d->block.blkptr[i].nrunes;
		bkread(d, buf, nb, i, 0);
		if(p2 >= p+nb)
			nd = nb-(p1-p);
		else{
			nd = p2-p1;
			memmove(buf+(p1-p), buf+(p1-p)+nd, RUNESIZE*(nb-((p1-p)+nd)));
		}
		nb -= nd;
		bkwrite(d, buf, nb, i, 0);
		d->block.blkptr[i].nrunes = nb;
		p2 -= nd;
		/* advance to next block */
		p += nb;
		i++;
	}
	/* throw away whole blocks */
	while(p<p2 && (nb = d->block.blkptr[i].nrunes)<=p2-p){
		if(i >= d->block.nused)
			panic("Ddelete 2");
		bkfree(d, i);
		p2 -= nb;
	}
	if(p >= p2)	/* any initial partial block left to delete? */
		return;	/* no */
	nd = p2-p;
	nb = d->block.blkptr[i].nrunes;
	/* just read in the part that survives */
	bkread(d, buf, nb-=nd, i, nd);
	/* a little block merging */
	if(nb<BLOCKSIZE/2 && i>0 && (nd = d->block.blkptr[i-1].nrunes)<BLOCKSIZE/2){
		memmove(buf+nd, buf, RUNESIZE*nb);
		bkread(d, buf, nd, --i, 0);
		bkfree(d, i);
		nb += nd;
	}
	bkwrite(d, buf, nb, i, 0);
	d->block.blkptr[i].nrunes = nb;
}

void
Dreplace(Disc *d, Posn p1, Posn p2, Rune *addr, int n)
{
	int i, nb, nr;
	Posn p = 0;
	Rune buf[BLOCKSIZE];

	if(p2-p1 > n)
		Ddelete(d, p1+n, p2);
	else if(p2-p1 < n)
		Dinsert(d, 0, n-(p2-p1), p2);
	if(n == 0)
		return;
	p2 = p1+n;
	/* they're now conformal; replace in place */
	for(i=0; i<d->block.nused; i++){
		if((p+=d->block.blkptr[i].nrunes) > p1){
			p -= d->block.blkptr[i].nrunes;
			goto out;
		}
	}
	panic("Dreplace");

    out:
	if(p != p1){	/* trailing partial block */
		nb = d->block.blkptr[i].nrunes;
		bkread(d, buf, nb, i, 0);
		if(p2 > p+nb)
			nr = nb-(p1-p);
		else
			nr = p2-p1;
		memmove(buf+p1-p, addr, RUNESIZE*nr);
		bkwrite(d, buf, nb, i, 0);
		/* advance to next block */
		p += nb;
		addr += nr;
		i++;
	}
	/* whole blocks */
	while(p<p2 && (nb = d->block.blkptr[i].nrunes)<=p2-p){
		if(i >= d->block.nused)
			panic("Dreplace 2");
		bkwrite(d, addr, nb, i, 0);
		p += nb;
		addr += nb;
		i++;
	}
	if(p < p2){	/* any initial partial block left? */
		nr = p2-p;
		nb = d->block.blkptr[i].nrunes;
		/* just read in the part that survives */
		bkread(d, buf+nr, nb-nr, i, nr);
		memmove(buf, addr, RUNESIZE*nr);
		bkwrite(d, buf, nb, i, 0);
	}
}

void
bkread(Disc *d, Rune *loc, int n, int bk, int off)
{
	Seek(d->desc->fd, RUNESIZE*(BLOCKSIZE*d->block.blkptr[bk].bnum+off), 0);
	Read(d->desc->fd, loc, n*RUNESIZE);
}

void
bkwrite(Disc *d, Rune *loc, int n, int bk, int off)
{
	Seek(d->desc->fd, RUNESIZE*(BLOCKSIZE*d->block.blkptr[bk].bnum+off), 0);
	Write(d->desc->fd, loc, n*RUNESIZE);
		/*
		 * sleazy hack to avoid silly SGI kernel bug
		 *	fill partial block with garbage
		 */
	if (off+n >= d->block.blkptr[bk].nrunes && off+n < BLOCKSIZE)
		Write(d->desc->fd, genbuf, (BLOCKSIZE-(off+n))*RUNESIZE);
}

void
bkalloc(Disc *d, int n)
{
	Discdesc *dd = d->desc;
	ulong bnum;

	if(dd->free.nused)
		bnum = dd->free.longptr[--dd->free.nused];
	else
		bnum = dd->nbk++;
	if(bnum >= 1<<(8*(sizeof(((Block*)0)->bnum))))
		error(Etmpovfl);
	inslist(&d->block, n, 0L);
	d->block.blkptr[n].bnum = bnum;
}

void
bkfree(Disc *d, int n)
{
	Discdesc *dd = d->desc;

	inslist(&dd->free, dd->free.nused, d->block.blkptr[n].bnum);
	dellist(&d->block, n);
}