Annotation of researchv10no/cmd/lcc/etc/io.c, revision 1.1

1.1     ! root        1: #ifdef BPRINT
        !             2: #define dclproto(func,args) func args;
        !             3: #else
        !             4: #include "c.h"
        !             5: #define FILE int
        !             6: #define EOF -1
        !             7: #endif
        !             8: 
        !             9: /*
        !            10:  * read prof.out; format:
        !            11:  *    #files
        !            12:  *    name
        !            13:  *    ... (#files-1 times)
        !            14:  *    #functions
        !            15:  *    name file# x y count
        !            16:  *    ... (#functions-1 times)
        !            17:  *    #points
        !            18:  *    file# x y count
        !            19:  *    ... (#points-1 times)
        !            20:  */
        !            21: 
        !            22: struct file {
        !            23:        struct file *link;
        !            24:        char *name;
        !            25:        int size, count;
        !            26:        struct count {
        !            27:                int x, y;
        !            28:                int count;
        !            29:        } *counts;
        !            30:        struct func {
        !            31:                struct func *link;
        !            32:                char *name;
        !            33:                struct count count;
        !            34:        } *funcs;
        !            35: } *filelist;
        !            36: 
        !            37: dclproto(static void afunction,(char *, char *, int, int, int))
        !            38: dclproto(static void apoint,(int, char *, int, int, int))
        !            39: dclproto(static struct file *findfile,(char *))
        !            40: dclproto(static int gather,(FILE *))
        !            41: 
        !            42: #ifdef BPRINT
        !            43: /* alloc - allocate n bytes or die */
        !            44: static char *alloc(int n) {
        !            45:        char *new = (char *)malloc(n);
        !            46: 
        !            47:        assert(new);
        !            48:        return new;
        !            49: }
        !            50: 
        !            51: /* string - save a copy of str, if necessary */
        !            52: char *string(char *str) {
        !            53:        static struct string { char *str; struct string *link; } *list;
        !            54:        struct string *p;
        !            55: 
        !            56:        for (p = list; p; p = p->link)
        !            57:                if (strcmp(p->str, str) == 0)
        !            58:                        return p->str;
        !            59:        p = (struct string *)alloc(sizeof *p);
        !            60:        p->str = strcpy((char *)alloc(strlen(str) + 1), str);
        !            61:        p->link = list;
        !            62:        list = p;
        !            63:        return p->str;
        !            64: }
        !            65: #else
        !            66: dclproto(extern void qsort,(struct count *, int, int, int (*)(const void *, const void *)))
        !            67: dclproto(static int fscanf,(FILE *, char *, Generic, Generic, Generic, Generic, Generic))
        !            68: dclproto(extern int sscanf,(char *, char *, Generic, Generic, Generic, Generic, Generic))
        !            69: 
        !            70: /* fscanf - read a line of input according to fmt */
        !            71: static int fscanf(fp, fmt, a1, a2, a3, a4, a5) FILE *fp; char *fmt; Generic a1, a2, a3, a4, a5; {
        !            72:        &fp;
        !            73:        while (*cp) {
        !            74:                if (limit - cp < MAXTOKEN)
        !            75:                        fillbuf();
        !            76:                sscanf(cp, fmt, a1, a2, a3, a4, a5);
        !            77:                while (*cp && *cp != '\n')
        !            78:                        cp++;
        !            79:                if (*cp == '\n') {
        !            80:                        cp++;
        !            81:                        nextline();
        !            82:                }
        !            83:                return 0;
        !            84:        }
        !            85:        return EOF;
        !            86: }
        !            87: #endif
        !            88: 
        !            89: /* afunction - add function name and its data to file's function list */
        !            90: static void afunction(name, file, x, y, count) char *name, *file; {
        !            91:        struct file *p = findfile(file);
        !            92:        struct func **q;
        !            93: 
        !            94:        assert(p);
        !            95:        for (q = &p->funcs; *q && name != (*q)->name; q = &(*q)->link)
        !            96:                ;
        !            97:        if (*q == 0) {
        !            98:                *q = (struct func *)alloc(sizeof **q);
        !            99:                (*q)->name = name;
        !           100:                (*q)->count.x = x;
        !           101:                (*q)->count.y = y;
        !           102:                (*q)->count.count = 0;
        !           103:                (*q)->link = 0;
        !           104:        }
        !           105:        (*q)->count.count += count;
        !           106: }
        !           107: 
        !           108: /* apoint - append execution point i to file's data */ 
        !           109: static void apoint(i, file, x, y, count) char *file; {
        !           110:        struct file *p = findfile(file);
        !           111: 
        !           112:        assert(p);
        !           113:        if (i >= p->size) {
        !           114:                int j;
        !           115:                if (p->size == 0) {
        !           116:                        p->counts = (struct count *)alloc(200*sizeof(struct count));
        !           117:                        p->size = 200;
        !           118:                } else {
        !           119: #ifdef BPRINT
        !           120:                        p->counts = (struct count *)realloc(p->counts, 2*p->size*sizeof(struct count));
        !           121: #else
        !           122:                        struct count *new = (struct count *)alloc(2*p->size*sizeof(struct count));
        !           123:                        for (j = 0; j < p->count; j++)
        !           124:                                new[j] = p->counts[j];
        !           125:                        p->counts = new;
        !           126: #endif
        !           127:                        p->size = 2*p->size;
        !           128:                }
        !           129:                for (j = p->count; j < p->size; j++) {
        !           130:                        static struct count z;
        !           131:                        p->counts[j] = z;
        !           132:                }
        !           133:        }
        !           134:        p->counts[i].x = x;
        !           135:        p->counts[i].y = y;
        !           136:        p->counts[i].count += count;
        !           137:        if (i >= p->count)
        !           138:                p->count = i + 1;
        !           139: }
        !           140: 
        !           141: /* compare - return <0, 0, >0 if a<b, a==b, a>b, resp. */
        !           142: static int compare(a, b) struct count *a, *b; {
        !           143:        if (a->y == b->y)
        !           144:                return a->x - b->x;
        !           145:        return a->y - b->y;
        !           146: }
        !           147: 
        !           148: /* findcount - return count associated with (file,x,y) or -1 */
        !           149: int findcount(file, x, y) char *file; {
        !           150:        static struct file *cursor;
        !           151: 
        !           152:        if (cursor == 0 || cursor->name != file)
        !           153:                cursor = findfile(file);
        !           154:        if (cursor) {
        !           155:                int l, u;
        !           156:                struct count *c = cursor->counts;
        !           157:                for (l = 0, u = cursor->count - 1; l <= u; ) {
        !           158:                        int k = (l + u)/2;
        !           159:                        if (c[k].y > y || c[k].y == y && c[k].x > x)
        !           160:                                u = k - 1;
        !           161:                        else if (c[k].y < y || c[k].y == y && c[k].x < x)
        !           162:                                l = k + 1;
        !           163:                        else
        !           164:                                return c[k].count;
        !           165:                }
        !           166:        }
        !           167:        return -1;
        !           168: }
        !           169: 
        !           170: /* findfile - return file name's file list entry, or 0 */
        !           171: static struct file *findfile(name) char *name; {
        !           172:        struct file *p;
        !           173: 
        !           174:        for (p = filelist; p; p = p->link)
        !           175:                if (p->name == name)
        !           176:                        return p;
        !           177:        return 0;
        !           178: }
        !           179: 
        !           180: /* findfunc - return count associated with function name in file or -1 */
        !           181: int findfunc(name, file) char *name, *file; {
        !           182:        static struct file *cursor;
        !           183: 
        !           184:        if (cursor == 0 || cursor->name != file)
        !           185:                cursor = findfile(file);
        !           186:        if (cursor) {
        !           187:                struct func *p;
        !           188:                for (p = cursor->funcs; p; p = p->link)
        !           189:                        if (p->name == name)
        !           190:                                return p->count.count;
        !           191:        }
        !           192:        return -1;
        !           193: }
        !           194: 
        !           195: /* gather - read prof.out data from fd */
        !           196: static int gather(FILE *fp) {
        !           197:        int i, nfiles, nfuncs, npoints;
        !           198:        char *files[64], buf[120];
        !           199: 
        !           200:        if (fscanf(fp, "%d\n", &nfiles, 0, 0, 0, 0) == EOF)
        !           201:                return 0;
        !           202:        assert(nfiles < sizeof files/sizeof files[0]);
        !           203:        for (i = 0; i < nfiles; i++) {
        !           204:                fscanf(fp, "%s\n", buf, 0, 0, 0, 0);
        !           205:                files[i] = string(buf);
        !           206:                if (!findfile(files[i])) {
        !           207:                        struct file *new = (struct file *)alloc(sizeof *new);
        !           208:                        new->name = files[i];
        !           209:                        new->size = new->count = 0;
        !           210:                        new->counts = 0;
        !           211:                        new->funcs = 0;
        !           212:                        new->link = filelist;
        !           213:                        filelist = new;
        !           214:                }
        !           215:        }
        !           216:        fscanf(fp, "%d\n", &nfuncs, 0, 0, 0, 0);
        !           217:        for (i = 0; i < nfuncs; i++) {
        !           218:                int f, x, y, count;
        !           219:                fscanf(fp, "%s %d %d %d %d\n", buf, &f, &x, &y, &count);
        !           220:                afunction(string(buf), files[f-1], x, y, count);
        !           221:        }
        !           222:        fscanf(fp, "%d\n", &npoints, 0, 0, 0, 0);
        !           223:        for (i = 0; i < npoints; i++) {
        !           224:                int f, x, y, count;
        !           225:                fscanf(fp, "%d %d %d %d\n", &f, &x, &y, &count, 0);
        !           226:                if (f)
        !           227:                        apoint(i, files[f-1], x, y, count);
        !           228:        }
        !           229:        return 1;
        !           230: }
        !           231: 
        !           232: /* process - read prof.out data from file */
        !           233: int process(file) char *file; {
        !           234:        struct file *p;
        !           235:        FILE *fp;
        !           236: 
        !           237: #ifdef BPRINT
        !           238:        if ((fp = fopen(file, "r"))) {
        !           239:                while (gather(fp))
        !           240:                        ;
        !           241:                fclose(fp);
        !           242: #else
        !           243:        int fd;
        !           244:        if ((fd = open(file, 0)) >= 0) {
        !           245:                inputInit(fd);
        !           246:                while (gather(fp))
        !           247:                        ;
        !           248:                close(fd);
        !           249: #endif
        !           250:                for (p = filelist; p; p = p->link)
        !           251:                        qsort(p->counts, p->count, sizeof *p->counts,
        !           252: #ifdef PROTO
        !           253:                                (int (*)(const void *, const void *))
        !           254: #endif
        !           255:                                compare);
        !           256:                return 1;
        !           257:        }
        !           258:        return 0;
        !           259: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.