Annotation of coherent/b/lib/libc/gen/malloc/trymallo.c, revision 1.1

1.1     ! root        1: /* Test program for malloc errors are
        !             2:  * Error 1 area wrong after initialization
        !             3:  * Error 2 area wrong after realloc shrink
        !             4:  * Error 3 area wrong after realloc expand
        !             5:  * First number after error is what byte was expected
        !             6:  * Second number is what byte was found
        !             7:  * Third number is where
        !             8:  * Fourth number is size of area being looked at
        !             9:  */
        !            10: 
        !            11: #define XSIZ 113
        !            12: #include <stdio.h>
        !            13: 
        !            14: extern char *malloc();
        !            15: extern char *realloc();
        !            16: extern void  free();
        !            17: extern unsigned rand();
        !            18: 
        !            19: static unsigned mct, mfail, rct, rfail, ect, count;
        !            20: 
        !            21: static struct mloc {
        !            22:        char *area;
        !            23:        unsigned siz;
        !            24: } locs[XSIZ];
        !            25: 
        !            26: unsigned
        !            27: vsiz()
        !            28: {
        !            29:        unsigned i;
        !            30: 
        !            31:        i = rand() >> 4;
        !            32:        return(i ? i : 10);
        !            33: }
        !            34:                
        !            35: mkval(siz, loc)
        !            36: unsigned siz;
        !            37: char *loc;
        !            38: {
        !            39:        char fil=siz;
        !            40: 
        !            41:        for(;siz>0; siz--)
        !            42:                *loc++=fil;
        !            43: }
        !            44: 
        !            45: ckval(point, fill, siz, loc)
        !            46: unsigned point, fill, siz;
        !            47: char *loc;
        !            48: {
        !            49:        char fil=fill, chx, *xloc=loc;
        !            50:        unsigned  xsiz=siz;
        !            51: 
        !            52:        for(;siz>0; siz--) {
        !            53:                if((chx=*loc++) != fil) {
        !            54:                   printf("Error: %d %x %x %d %d\n", 
        !            55:                           point, fil, chx, xsiz-siz, xsiz);
        !            56:                   ect++;
        !            57:                   return(1);
        !            58:                }
        !            59:        }
        !            60:        return(0);
        !            61: }
        !            62: 
        !            63: churn(loc)
        !            64: struct mloc *loc;
        !            65: {
        !            66:        unsigned nsiz;
        !            67:        char *savp;
        !            68: 
        !            69:        if(loc->area == NULL) { /* build an area */
        !            70:                mct++;
        !            71:                if(NULL != (loc->area=malloc(loc->siz=vsiz())))
        !            72:                        mkval(loc->siz, loc->area);
        !            73:                else
        !            74:                        mfail++;
        !            75:                return;
        !            76:        }
        !            77:        ckval(1, loc->siz, loc->siz, loc->area);
        !            78:        if(rand() & (1<<10)) {  /* free or realloc */
        !            79:                free(loc->area);
        !            80:                loc->area=NULL;
        !            81:                count--;        /* frees don't count */
        !            82:        } else {
        !            83:                rct++;
        !            84:                if(NULL != (savp=realloc(loc->area, nsiz=vsiz()))) {
        !            85:                        loc->area=savp;
        !            86:                        ckval(2+(loc->siz<nsiz), /* 2=contract 3=expand */
        !            87:                                 loc->siz,       /* filler byte */
        !            88:                                 loc->siz<nsiz ? loc->siz : nsiz, /* size */
        !            89:                                 loc->area);     /* area */
        !            90:                        mkval(loc->siz=nsiz, loc->area);
        !            91:                }
        !            92:                else
        !            93:                        rfail++;
        !            94:        }
        !            95: }
        !            96: 
        !            97: main(argc, argv) int argc; char *argv[];
        !            98: {
        !            99:        if (argc > 1)
        !           100:                srand(atoi(argv[1]));
        !           101: 
        !           102:        for(count=0; count < XSIZ; count++)
        !           103:                locs[count].area = NULL;
        !           104: 
        !           105:        for(count=0; count<500; count++) {
        !           106:                churn(&locs[rand() % XSIZ]);
        !           107:                if(!memok())
        !           108:                        printf("memok %d\n", count);
        !           109:        }
        !           110: 
        !           111:        printf("%4d mallocs     %4d failed\n", mct, mfail);
        !           112:        printf("%4d reallocs    %4d failed\n", rct, rfail);
        !           113:        printf("%d errors reported\n", ect);
        !           114: }

unix.superglobalmegacorp.com

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