Annotation of 43BSD/usr.bin/struct/0.alloc.c, revision 1.1

1.1     ! root        1: #ifndef lint
        !             2: static char sccsid[] = "@(#)0.alloc.c  4.1     (Berkeley)      2/11/83";
        !             3: #endif not lint
        !             4: 
        !             5: #include <stdio.h>
        !             6: #include "def.h"
        !             7: int routbeg;
        !             8: 
        !             9: extern int debug;
        !            10: struct coreblk {struct coreblk *nxtblk;
        !            11:                        int blksize;
        !            12:                        int nxtfree;
        !            13:                        int *blk;
        !            14:                        };
        !            15: 
        !            16: long space;
        !            17: challoc(n)
        !            18: int n;
        !            19:        {
        !            20:        int i;
        !            21:        i = malloc(n);
        !            22:        if(i) { space += n; return(i); }
        !            23:        fprintf(stderr,"alloc out of space\n");
        !            24:        fprintf(stderr,"total space alloc'ed = %D\n",space);
        !            25:        fprintf(stderr,"%d more bytes requested\n",n);
        !            26:        exit(1);
        !            27:        }
        !            28: 
        !            29: 
        !            30: chfree(p,n)
        !            31: int *p,n;
        !            32:        {
        !            33:        ASSERT(p,chfree);
        !            34:        space -= n;
        !            35:        free(p);
        !            36:        }
        !            37: 
        !            38: 
        !            39: struct coreblk *tcore, *gcore;
        !            40: int tblksize=12, gblksize=300;
        !            41: 
        !            42: 
        !            43: balloc(n,p,size)               /* allocate n bytes from coreblk *p */
        !            44: int n,size;            /* use specifies where called */
        !            45: struct coreblk **p;
        !            46:        {
        !            47:        int i;
        !            48:        struct coreblk *q;
        !            49:        n = (n+sizeof(i)-1)/sizeof(i);  /* convert bytes to wds to ensure ints always at wd boundaries */
        !            50:        for (q = *p; ; q = q->nxtblk)
        !            51:                {
        !            52:                if (!q)
        !            53:                        {
        !            54:                        q = morespace(n,p,size);
        !            55:                        break;
        !            56:                        }
        !            57:                if (q-> blksize - q->nxtfree >= n)  break;
        !            58:                }
        !            59:        i = q->nxtfree;
        !            60:        q ->nxtfree += n;
        !            61:        return( &(q->blk)[i]);
        !            62:        }
        !            63: 
        !            64: talloc(n)              /* allocate from line-by-line storage area */
        !            65: int n;
        !            66:        {return(balloc(n,&tcore,tblksize)); }
        !            67: 
        !            68: galloc(n)              /* allocate from graph storage area */
        !            69: int n;
        !            70:        {
        !            71:        return(balloc(n,&gcore,gblksize));
        !            72:        }
        !            73: 
        !            74: reuse(p)               /* set nxtfree so coreblk can be reused */
        !            75: struct coreblk *p;
        !            76:        {
        !            77:        for (; p; p=p->nxtblk)  p->nxtfree = 0;  
        !            78:        }
        !            79: 
        !            80: bfree(p)               /* free coreblk p */
        !            81: struct coreblk *p;
        !            82:        {
        !            83:        if (!p) return;
        !            84:        bfree(p->nxtblk);
        !            85:        p->nxtblk = 0;
        !            86:        free(p);
        !            87:        }
        !            88: 
        !            89: 
        !            90: morespace(n,p,size)            /* get at least n more wds for coreblk *p */
        !            91: int n,size;
        !            92: struct coreblk **p;
        !            93:        {struct coreblk *q;
        !            94:        int t,i;
        !            95: 
        !            96:        t = n<size?size:n;
        !            97:        q = malloc(i=t*sizeof(*(q->blk))+sizeof(*q));
        !            98:        if(!q){
        !            99:                error(": alloc out of space","","");
        !           100:                fprintf(stderr,"space = %D\n",space);
        !           101:                fprintf(stderr,"%d more bytes requested\n",n);
        !           102:                exit(1);
        !           103:                }
        !           104:        space += i;
        !           105:        q->nxtblk = *p;
        !           106:        *p = q;
        !           107:        q -> blksize = t;
        !           108:        q-> nxtfree = 0;
        !           109:        q->blk = q + 1;
        !           110:        return(q);
        !           111:        }
        !           112: 
        !           113: 
        !           114: 
        !           115: 
        !           116: freegraf()
        !           117:        {
        !           118:        bfree(gcore);
        !           119:        gcore = 0;
        !           120: 
        !           121: 
        !           122:        }
        !           123: 
        !           124: 
        !           125: 
        !           126: 
        !           127: 
        !           128: 
        !           129: 
        !           130: 
        !           131: 
        !           132: error(mess1, mess2, mess3)
        !           133: char *mess1, *mess2, *mess3;
        !           134:        {
        !           135:        static lastbeg;
        !           136:        if (lastbeg != routbeg)
        !           137:                {
        !           138:                fprintf(stderr,"routine beginning on line %d:\n",routbeg);
        !           139:                lastbeg = routbeg;
        !           140:                }
        !           141:        fprintf(stderr,"error %s %s %s\n",mess1, mess2, mess3);
        !           142:        }
        !           143: 
        !           144: 
        !           145: faterr(mess1, mess2, mess3)
        !           146: char *mess1, *mess2, *mess3;
        !           147:        {
        !           148:        error(mess1, mess2, mess3);
        !           149:        exit(1);
        !           150:        }
        !           151: 
        !           152: 
        !           153: strerr(mess1, mess2, mess3)
        !           154: char *mess1, *mess2, *mess3;
        !           155:        {
        !           156:        error("struct error: ",mess1, mess2);
        !           157:        }

unix.superglobalmegacorp.com

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