Annotation of src/usr.bin/sort/fsort.c, Revision 1.13
1.13 ! millert 1: /* $OpenBSD: fsort.c,v 1.12 2004/09/14 22:57:58 deraadt Exp $ */
1.1 millert 2:
3: /*-
4: * Copyright (c) 1993
5: * The Regents of the University of California. All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Peter McIlroy.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
1.9 millert 18: * 3. Neither the name of the University nor the names of its contributors
1.1 millert 19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
35: #ifndef lint
36: #if 0
37: static char sccsid[] = "@(#)fsort.c 8.1 (Berkeley) 6/6/93";
38: #else
1.13 ! millert 39: static char rcsid[] = "$OpenBSD: fsort.c,v 1.12 2004/09/14 22:57:58 deraadt Exp $";
1.1 millert 40: #endif
41: #endif /* not lint */
42:
43: /*
44: * Read in the next bin. If it fits in one segment sort it;
45: * otherwise refine it by segment deeper by one character,
46: * and try again on smaller bins. Sort the final bin at this level
47: * of recursion to keep the head of fstack at 0.
48: * After PANIC passes, abort to merge sort.
1.4 millert 49: */
1.1 millert 50: #include "sort.h"
51: #include "fsort.h"
52:
53: #include <stdlib.h>
54: #include <string.h>
55:
1.13 ! millert 56: u_char *buffer = NULL, *linebuf = NULL;
! 57: size_t bufsize = BUFSIZE, linebuf_size = MAXLLEN;
1.1 millert 58: struct tempfile fstack[MAXFCT];
1.12 deraadt 59: extern char toutpath[];
1.1 millert 60: #define FSORTMAX 4
61: int PANIC = FSORTMAX;
62:
63: void
1.11 deraadt 64: fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp,
65: struct field *ftbl)
1.1 millert 66: {
1.13 ! millert 67: u_char *bufend, *weights, **keypos, *tmpbuf;
! 68: static u_char **keylist;
1.1 millert 69: int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0;
1.5 millert 70: int c, nelem;
1.8 ericj 71: long sizes[NBINS+1];
1.1 millert 72: union f_handle tfiles, mstart = {MAXFCT-16};
1.8 ericj 73: int (*get)(int, union f_handle, int, RECHEADER *,
1.1 millert 74: u_char *, struct field *);
1.8 ericj 75: RECHEADER *crec;
1.1 millert 76: struct field tfield[2];
77: FILE *prevfp, *tailfp[FSORTMAX+1];
78:
79: memset(tailfp, 0, sizeof(tailfp));
80: prevfp = outfp;
81: memset(tfield, 0, sizeof(tfield));
82: if (ftbl[0].flags & R)
83: tfield[0].weights = Rascii;
84: else
85: tfield[0].weights = ascii;
86: tfield[0].icol.num = 1;
87: weights = ftbl[0].weights;
1.13 ! millert 88: bufend = buffer + bufsize;
! 89: if (keylist == NULL) {
! 90: if ((keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL)
! 91: err(2, NULL);
1.1 millert 92: }
93: if (binno >= 0) {
94: tfiles.top = infiles.top + nfiles;
95: get = getnext;
96: } else {
97: tfiles.top = 0;
98: if (SINGL_FLD)
99: get = makeline;
100: else
101: get = makekey;
102: }
103: for (;;) {
104: memset(sizes, 0, sizeof(sizes));
105: c = ntfiles = 0;
106: if (binno == weights[REC_D] &&
107: !(SINGL_FLD && ftbl[0].flags & F)) { /* pop */
108: rd_append(weights[REC_D],
109: infiles, nfiles, prevfp, buffer, bufend);
110: break;
111: } else if (binno == weights[REC_D]) {
112: depth = 0; /* start over on flat weights */
113: ftbl = tfield;
114: weights = ftbl[0].weights;
115: }
116: while (c != EOF) {
117: keypos = keylist;
118: nelem = 0;
119: crec = (RECHEADER *) buffer;
120: while((c = get(binno, infiles, nfiles, crec, bufend,
121: ftbl)) == 0) {
122: *keypos++ = crec->data + depth;
123: if (++nelem == MAXNUM) {
124: c = BUFFEND;
125: break;
126: }
127: crec =(RECHEADER *) ((char *) crec +
128: SALIGN(crec->length) + sizeof(TRECHEADER));
129: }
1.8 ericj 130: /*
131: * buffer was too small for data, allocate
132: * a bigger buffer.
133: */
134: if (c == BUFFEND && nelem == 0) {
135: bufsize *= 2;
1.13 ! millert 136: tmpbuf = realloc(buffer, bufsize);
! 137: if (!tmpbuf)
1.8 ericj 138: err(2, "failed to realloc buffer");
1.13 ! millert 139: crec = (RECHEADER *)
! 140: (tmpbuf + ((u_char *)crec - buffer));
! 141: buffer = tmpbuf;
1.8 ericj 142: bufend = buffer + bufsize;
143: continue;
144: }
1.1 millert 145: if (c == BUFFEND || ntfiles || mfct) { /* push */
146: if (panic >= PANIC) {
147: fstack[MAXFCT-16+mfct].fp = ftmp();
148: if (radixsort((const u_char **)keylist,
149: nelem, weights, REC_D))
150: err(2, NULL);
151: append(keylist, nelem, depth, fstack[
152: MAXFCT-16+mfct].fp, putrec, ftbl);
153: mfct++;
154: /* reduce number of open files */
155: if (mfct == 16 ||(c == EOF && ntfiles)) {
156: fstack[tfiles.top + ntfiles].fp
157: = ftmp();
158: fmerge(0, mstart, mfct, geteasy,
159: fstack[tfiles.top+ntfiles].fp,
160: putrec, ftbl);
1.4 millert 161: ntfiles++;
1.1 millert 162: mfct = 0;
163: }
164: } else {
165: fstack[tfiles.top + ntfiles].fp= ftmp();
166: onepass(keylist, depth, nelem, sizes,
167: weights, fstack[tfiles.top+ntfiles].fp);
1.4 millert 168: ntfiles++;
1.1 millert 169: }
170: }
171: }
172: get = getnext;
173: if (!ntfiles && !mfct) { /* everything in memory--pop */
174: if (nelem > 1 && radixsort((const u_char **)keylist,
175: nelem, weights, REC_D))
176: err(2, NULL);
177: append(keylist, nelem, depth, outfp, putline, ftbl);
178: break; /* pop */
179: }
180: if (panic >= PANIC) {
181: if (!ntfiles)
182: fmerge(0, mstart, mfct, geteasy,
183: outfp, putline, ftbl);
184: else
185: fmerge(0, tfiles, ntfiles, geteasy,
186: outfp, putline, ftbl);
187: break;
188:
189: }
190: total = maxb = lastb = 0; /* find if one bin dominates */
191: for (i = 0; i < NBINS; i++)
192: if (sizes[i]) {
193: if (sizes[i] > sizes[maxb])
194: maxb = i;
195: lastb = i;
196: total += sizes[i];
197: }
198: if (sizes[maxb] < max((total / 2) , BUFSIZE))
199: maxb = lastb; /* otherwise pop after last bin */
200: fstack[tfiles.top].lastb = lastb;
201: fstack[tfiles.top].maxb = maxb;
202:
203: /* start refining next level. */
204: get(-1, tfiles, ntfiles, crec, bufend, 0); /* rewind */
205: for (i = 0; i < maxb; i++) {
206: if (!sizes[i]) /* bin empty; step ahead file offset */
207: get(i, tfiles, ntfiles, crec, bufend, 0);
208: else
209: fsort(i, depth+1, tfiles, ntfiles, outfp, ftbl);
210: }
211: if (lastb != maxb) {
212: if (prevfp != outfp)
213: tailfp[panic] = prevfp;
214: prevfp = ftmp();
215: for (i = maxb+1; i <= lastb; i++)
216: if (!sizes[i])
217: get(i, tfiles, ntfiles, crec, bufend,0);
218: else
219: fsort(i, depth+1, tfiles, ntfiles,
220: prevfp, ftbl);
221: }
222:
223: /* sort biggest (or last) bin at this level */
224: depth++;
225: panic++;
226: binno = maxb;
227: infiles.top = tfiles.top; /* getnext will free tfiles, */
228: nfiles = ntfiles; /* so overwrite them */
229: }
230: if (prevfp != outfp) {
231: concat(outfp, prevfp);
232: fclose(prevfp);
233: }
234: for (i = panic; i >= 0; --i)
235: if (tailfp[i]) {
236: concat(outfp, tailfp[i]);
237: fclose(tailfp[i]);
238: }
239: }
240:
241: /*
1.4 millert 242: * This is one pass of radix exchange, dumping the bins to disk.
1.1 millert 243: */
244: #define swap(a, b, t) t = a, a = b, b = t
245: void
1.11 deraadt 246: onepass(u_char **a, int depth, long n, long sizes[], u_char *tr, FILE *fp)
1.1 millert 247: {
1.8 ericj 248: size_t tsizes[NBINS+1];
1.1 millert 249: u_char **bin[257], **top[256], ***bp, ***bpmax, ***tp;
1.7 millert 250: static int histo[256];
1.1 millert 251: int *hp;
1.8 ericj 252: int c;
1.1 millert 253: u_char **an, *t, **aj;
1.8 ericj 254: u_char **ak, *r;
1.1 millert 255:
256: memset(tsizes, 0, sizeof(tsizes));
257: depth += sizeof(TRECHEADER);
1.8 ericj 258: an = &a[n];
1.1 millert 259: for (ak = a; ak < an; ak++) {
260: histo[c = tr[**ak]]++;
261: tsizes[c] += ((RECHEADER *) (*ak -= depth))->length;
262: }
263:
264: bin[0] = a;
265: bpmax = bin + 256;
266: tp = top, hp = histo;
267: for (bp = bin; bp < bpmax; bp++) {
268: *tp++ = *(bp+1) = *bp + (c = *hp);
269: *hp++ = 0;
270: if (c <= 1)
271: continue;
272: }
1.4 millert 273: for (aj = a; aj < an; *aj = r, aj = bin[c+1])
1.1 millert 274: for(r = *aj; aj < (ak = --top[c = tr[r[depth]]]) ;)
275: swap(*ak, r, t);
276:
277: for (ak = a, c = 0; c < 256; c++) {
278: an = bin[c+1];
279: n = an - ak;
280: tsizes[c] += n * sizeof(TRECHEADER);
281: /* tell getnext how many elements in this bin, this segment. */
1.8 ericj 282: EWRITE(&tsizes[c], sizeof(size_t), 1, fp);
1.1 millert 283: sizes[c] += tsizes[c];
284: for (; ak < an; ++ak)
285: putrec((RECHEADER *) *ak, fp);
286: }
287: }