[BACK]Return to fsort.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sort

Annotation of src/usr.bin/sort/fsort.c, Revision 1.15

1.14      naddy       1: /*     $OpenBSD: fsort.c,v 1.13 2006/10/18 23:30:43 millert 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.14      naddy      39: static char rcsid[] = "$OpenBSD: fsort.c,v 1.13 2006/10/18 23:30:43 millert 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.15    ! millert    56: u_char *buffer = NULL, *bufend = 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.15    ! millert    67:        u_char *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.15    ! millert    88:        if (keylist == NULL) {
        !            89:                if ((keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL)
        !            90:                        err(2, NULL);
1.14      naddy      91:        }
1.1       millert    92:        if (binno >= 0) {
                     93:                tfiles.top = infiles.top + nfiles;
                     94:                get = getnext;
                     95:        } else {
                     96:                tfiles.top = 0;
                     97:                if (SINGL_FLD)
                     98:                        get = makeline;
                     99:                else
                    100:                        get = makekey;
                    101:        }
                    102:        for (;;) {
                    103:                memset(sizes, 0, sizeof(sizes));
                    104:                c = ntfiles = 0;
                    105:                if (binno == weights[REC_D] &&
                    106:                    !(SINGL_FLD && ftbl[0].flags & F)) {        /* pop */
                    107:                        rd_append(weights[REC_D],
                    108:                            infiles, nfiles, prevfp, buffer, bufend);
                    109:                        break;
                    110:                } else if (binno == weights[REC_D]) {
                    111:                        depth = 0;              /* start over on flat weights */
                    112:                        ftbl = tfield;
                    113:                        weights = ftbl[0].weights;
                    114:                }
                    115:                while (c != EOF) {
                    116:                        keypos = keylist;
                    117:                        nelem = 0;
                    118:                        crec = (RECHEADER *) buffer;
                    119:                        while((c = get(binno, infiles, nfiles, crec, bufend,
                    120:                            ftbl)) == 0) {
                    121:                                *keypos++ = crec->data + depth;
                    122:                                if (++nelem == MAXNUM) {
                    123:                                        c = BUFFEND;
                    124:                                        break;
                    125:                                }
                    126:                                crec =(RECHEADER *)     ((char *) crec +
                    127:                                SALIGN(crec->length) + sizeof(TRECHEADER));
                    128:                        }
1.8       ericj     129:                        /*
                    130:                         * buffer was too small for data, allocate
                    131:                         * a bigger buffer.
                    132:                         */
                    133:                        if (c == BUFFEND && nelem == 0) {
                    134:                                bufsize *= 2;
1.15    ! millert   135:                                tmpbuf = realloc(buffer, bufsize);
        !           136:                                if (!tmpbuf)
1.8       ericj     137:                                        err(2, "failed to realloc buffer");
1.15    ! millert   138:                                crec = (RECHEADER *)
        !           139:                                    (tmpbuf + ((u_char *)crec - buffer));
        !           140:                                buffer = tmpbuf;
        !           141:                                bufend = buffer + bufsize - 1;
1.8       ericj     142:                                continue;
                    143:                        }
1.1       millert   144:                        if (c == BUFFEND || ntfiles || mfct) {  /* push */
                    145:                                if (panic >= PANIC) {
                    146:                                        fstack[MAXFCT-16+mfct].fp = ftmp();
                    147:                                        if (radixsort((const u_char **)keylist,
                    148:                                            nelem, weights, REC_D))
                    149:                                                err(2, NULL);
                    150:                                        append(keylist, nelem, depth, fstack[
                    151:                                         MAXFCT-16+mfct].fp, putrec, ftbl);
                    152:                                        mfct++;
                    153:                                        /* reduce number of open files */
                    154:                                        if (mfct == 16 ||(c == EOF && ntfiles)) {
                    155:                                                fstack[tfiles.top + ntfiles].fp
                    156:                                                    = ftmp();
                    157:                                                fmerge(0, mstart, mfct, geteasy,
                    158:                                                  fstack[tfiles.top+ntfiles].fp,
                    159:                                                  putrec, ftbl);
1.4       millert   160:                                                ntfiles++;
1.1       millert   161:                                                mfct = 0;
                    162:                                        }
                    163:                                } else {
                    164:                                        fstack[tfiles.top + ntfiles].fp= ftmp();
                    165:                                        onepass(keylist, depth, nelem, sizes,
                    166:                                        weights, fstack[tfiles.top+ntfiles].fp);
1.4       millert   167:                                        ntfiles++;
1.1       millert   168:                                }
                    169:                        }
                    170:                }
                    171:                get = getnext;
                    172:                if (!ntfiles && !mfct) {        /* everything in memory--pop */
                    173:                        if (nelem > 1 && radixsort((const u_char **)keylist,
                    174:                            nelem, weights, REC_D))
                    175:                                err(2, NULL);
                    176:                        append(keylist, nelem, depth, outfp, putline, ftbl);
                    177:                        break;                                  /* pop */
                    178:                }
                    179:                if (panic >= PANIC) {
                    180:                        if (!ntfiles)
                    181:                                fmerge(0, mstart, mfct, geteasy,
                    182:                                    outfp, putline, ftbl);
                    183:                        else
                    184:                                fmerge(0, tfiles, ntfiles, geteasy,
                    185:                                    outfp, putline, ftbl);
                    186:                        break;
                    187:                }
                    188:                total = maxb = lastb = 0;       /* find if one bin dominates */
                    189:                for (i = 0; i < NBINS; i++)
                    190:                  if (sizes[i]) {
                    191:                        if (sizes[i] > sizes[maxb])
                    192:                                maxb = i;
                    193:                        lastb = i;
                    194:                        total += sizes[i];
                    195:                }
                    196:                if (sizes[maxb] < max((total / 2) , BUFSIZE))
                    197:                        maxb = lastb;   /* otherwise pop after last bin */
                    198:                fstack[tfiles.top].lastb = lastb;
                    199:                fstack[tfiles.top].maxb = maxb;
                    200:
                    201:                        /* start refining next level. */
                    202:                get(-1, tfiles, ntfiles, crec, bufend, 0);      /* rewind */
                    203:                for (i = 0; i < maxb; i++) {
                    204:                        if (!sizes[i])  /* bin empty; step ahead file offset */
                    205:                                get(i, tfiles, ntfiles, crec, bufend, 0);
                    206:                        else
                    207:                                fsort(i, depth+1, tfiles, ntfiles, outfp, ftbl);
                    208:                }
                    209:                if (lastb != maxb) {
                    210:                        if (prevfp != outfp)
                    211:                                tailfp[panic] = prevfp;
                    212:                        prevfp = ftmp();
                    213:                        for (i = maxb+1; i <= lastb; i++)
                    214:                                if (!sizes[i])
                    215:                                        get(i, tfiles, ntfiles, crec, bufend,0);
                    216:                                else
                    217:                                        fsort(i, depth+1, tfiles, ntfiles,
                    218:                                            prevfp, ftbl);
                    219:                }
                    220:
                    221:                /* sort biggest (or last) bin at this level */
                    222:                depth++;
                    223:                panic++;
                    224:                binno = maxb;
                    225:                infiles.top = tfiles.top;       /* getnext will free tfiles, */
                    226:                nfiles = ntfiles;               /* so overwrite them */
                    227:        }
                    228:        if (prevfp != outfp) {
                    229:                concat(outfp, prevfp);
                    230:                fclose(prevfp);
                    231:        }
                    232:        for (i = panic; i >= 0; --i)
                    233:                if (tailfp[i]) {
                    234:                        concat(outfp, tailfp[i]);
                    235:                        fclose(tailfp[i]);
                    236:                }
                    237: }
                    238:
                    239: /*
1.4       millert   240:  * This is one pass of radix exchange, dumping the bins to disk.
1.1       millert   241:  */
                    242: #define swap(a, b, t) t = a, a = b, b = t
                    243: void
1.11      deraadt   244: onepass(u_char **a, int depth, long n, long sizes[], u_char *tr, FILE *fp)
1.1       millert   245: {
1.8       ericj     246:        size_t tsizes[NBINS+1];
1.1       millert   247:        u_char **bin[257], **top[256], ***bp, ***bpmax, ***tp;
1.7       millert   248:        static int histo[256];
1.1       millert   249:        int *hp;
1.8       ericj     250:        int c;
1.1       millert   251:        u_char **an, *t, **aj;
1.8       ericj     252:        u_char **ak, *r;
1.1       millert   253:
                    254:        memset(tsizes, 0, sizeof(tsizes));
                    255:        depth += sizeof(TRECHEADER);
1.8       ericj     256:        an = &a[n];
1.1       millert   257:        for (ak = a; ak < an; ak++) {
                    258:                histo[c = tr[**ak]]++;
                    259:                tsizes[c] += ((RECHEADER *) (*ak -= depth))->length;
                    260:        }
                    261:
                    262:        bin[0] = a;
                    263:        bpmax = bin + 256;
                    264:        tp = top, hp = histo;
                    265:        for (bp = bin; bp < bpmax; bp++) {
                    266:                *tp++ = *(bp+1) = *bp + (c = *hp);
                    267:                *hp++ = 0;
                    268:                if (c <= 1)
                    269:                        continue;
                    270:        }
1.4       millert   271:        for (aj = a; aj < an; *aj = r, aj = bin[c+1])
1.1       millert   272:                for(r = *aj; aj < (ak = --top[c = tr[r[depth]]]) ;)
                    273:                        swap(*ak, r, t);
                    274:
                    275:        for (ak = a, c = 0; c < 256; c++) {
                    276:                an = bin[c+1];
                    277:                n = an - ak;
                    278:                tsizes[c] += n * sizeof(TRECHEADER);
                    279:                /* tell getnext how many elements in this bin, this segment. */
1.8       ericj     280:                EWRITE(&tsizes[c], sizeof(size_t), 1, fp);
1.1       millert   281:                sizes[c] += tsizes[c];
                    282:                for (; ak < an; ++ak)
                    283:                        putrec((RECHEADER *) *ak, fp);
                    284:        }
                    285: }