Annotation of researchv9/cmd/sh/blok.c, revision 1.1.1.1

1.1       root        1: /*     @(#)blok.c      1.4     */
                      2: /*
                      3:  *     UNIX shell
                      4:  *
                      5:  *     Bell Telephone Laboratories
                      6:  *
                      7:  */
                      8: 
                      9: #include       "defs.h"
                     10: 
                     11: 
                     12: /*
                     13:  *     storage allocator
                     14:  *     (circular first fit strategy)
                     15:  */
                     16: 
                     17: #ifdef CRAY
                     18: #ifdef CRAY2
                     19: #define        BUSY    020000000000
                     20: #else
                     21: #define        BUSY    040000000
                     22: #endif
                     23: #else
                     24: #define BUSY 01
                     25: #endif
                     26: 
                     27: #define        mkbusy(x)       (Rcheat(x) | BUSY)
                     28: #define        mknbusy(x)      (Rcheat(x) & ~BUSY)
                     29: #define busy(x)                (Rcheat((x)->word) & BUSY)
                     30: 
                     31: unsigned       brkincr = BRKINCR;
                     32: struct blk *blokp;                     /*current search pointer*/
                     33: struct blk *bloktop;           /* top of arena (last blok) */
                     34: struct blk *blokbot;           /* first blok */
                     35: 
                     36: char           *brkbegin;
                     37: char           *setbrk();
                     38: 
                     39: char *
                     40: shalloc(nbytes)
                     41:        unsigned nbytes;
                     42: {
                     43:        register unsigned rbytes = round(nbytes+BYTESPERWORD, BYTESPERWORD);
                     44: 
                     45:        for (;;)
                     46:        {
                     47:                int     c = 0;
                     48:                register struct blk *p = blokp;
                     49:                register struct blk *q;
                     50: 
                     51:                do
                     52:                {
                     53:                        if (!busy(p))
                     54:                        {
                     55:                                while (!busy(q = p->word)) {
                     56:                                        p->word = q->word;
                     57:                                }
                     58:                                if ((char *)q - (char *)p >= rbytes)
                     59:                                {
                     60:                                        blokp = (struct blk *)((char *)p + rbytes);
                     61:                                        if (q > blokp)
                     62:                                                blokp->word = p->word;
                     63:                                        p->word = (struct blk *)mkbusy(blokp);
                     64:                                        return((char *)(p + 1));
                     65:                                }
                     66:                        }
                     67:                        q = p;
                     68:                        p = (struct blk *)mknbusy(p->word);
                     69:                } while (p > q || (c++) == 0);
                     70:                addblok(rbytes);
                     71:        }
                     72: }
                     73: 
                     74: addblok(reqd)
                     75:        unsigned reqd;
                     76: {
                     77:        if (stakbot == 0)
                     78:        {
                     79:                brkbegin = setbrk(3 * BRKINCR);
                     80:                bloktop = blokbot = (struct blk *)brkbegin;
                     81:        }
                     82: 
                     83:        if (stakbas != staktop)
                     84:        {
                     85:                register char *rndstak;
                     86:                register struct blk *blokstak;
                     87: 
                     88:                pushstak(0);
                     89: #ifdef CRAY
                     90:                rndstak = (char *)sround(staktop, BYTESPERWORD);
                     91: #else
                     92:                rndstak = (char *)round(staktop, BYTESPERWORD);
                     93: #endif
                     94:                blokstak = (struct blk *)(stakbas) - 1;
                     95:                blokstak->word = stakbsy;
                     96:                stakbsy = blokstak;
                     97:                bloktop->word = (struct blk *)mkbusy(rndstak);
                     98:                bloktop = (struct blk *)(rndstak);
                     99:        }
                    100:        reqd += brkincr;
                    101:        reqd &= ~(brkincr - 1);
                    102:        blokp = bloktop;
                    103:        bloktop = bloktop->word = bloktop + (reqd/BYTESPERWORD);
                    104:        while ((char *)bloktop >= brkend) {
                    105:                setbrk(round((char *)bloktop - brkend + 1, BRKINCR));
                    106:        }
                    107:        bloktop->word = (struct blk *)mkbusy(brkbegin);
                    108:        {
                    109:                register char *stakadr = (char *)(bloktop + 2);
                    110: 
                    111:                if (stakbot != staktop)
                    112:                        staktop = movstr(stakbot, stakadr);
                    113:                else
                    114:                        staktop = stakadr;
                    115: 
                    116:                stakbas = stakbot = stakadr;
                    117:        }
                    118: }
                    119: 
                    120: shfree(ap)
                    121:        struct blk *ap;
                    122: {
                    123:        register struct blk *p;
                    124: 
                    125:        if ((p = ap) && p < bloktop && p >= blokbot)
                    126:        {
                    127: #ifdef DEBUG
                    128:                chkbptr(p);
                    129: #endif
                    130:                --p;
                    131:                p->word = (struct blk *)mknbusy(p->word);
                    132:                p->word = (struct blk *)(Rcheat(p->word) & ~BUSY);
                    133:        }
                    134: 
                    135: 
                    136: }
                    137: 
                    138: 
                    139: #ifdef DEBUG
                    140: 
                    141: chkbptr(ptr)
                    142:        struct blk *ptr;
                    143: {
                    144:        int     exf = 0;
                    145:        register struct blk *p = (struct blk *)brkbegin;
                    146:        register struct blk *q;
                    147:        int     us = 0, un = 0;
                    148: 
                    149:        for (;;)
                    150:        {
                    151:                q = (struct blk *)(Rcheat(p->word) & ~BUSY);
                    152: 
                    153:                if (p+1 == ptr)
                    154:                        exf++;
                    155: 
                    156:                if (q < (struct blk *)brkbegin || q > bloktop)
                    157:                        abort(3);
                    158: 
                    159:                if (p == bloktop)
                    160:                        break;
                    161: 
                    162:                if (busy(p))
                    163:                        us += q - p;
                    164:                else
                    165:                        un += q - p;
                    166: 
                    167:                if (p >= q)
                    168:                        abort(4);
                    169: 
                    170:                p = q;
                    171:        }
                    172:        if (exf == 0) {
                    173:                abort(1);
                    174:        }
                    175: }
                    176: 
                    177: #endif
                    178: 
                    179: chkmem()
                    180: {
                    181:        register struct blk *p = (struct blk *)brkbegin;
                    182:        register struct blk *q;
                    183:        int     us = 0, un = 0;
                    184: 
                    185:        for (;;)
                    186:        {
                    187:                q = (struct blk *)(Rcheat(p->word) & ~BUSY);
                    188: 
                    189:                if (q < (struct blk *)brkbegin || q > bloktop)
                    190:                        abort(3);
                    191: 
                    192:                if (p == bloktop)
                    193:                        break;
                    194: 
                    195:                if (busy(p))
                    196:                        us += q - p;
                    197:                else
                    198:                        un += q - p;
                    199: 
                    200:                if (p >= q)
                    201:                        abort(4);
                    202: 
                    203:                p = q;
                    204:        }
                    205: 
                    206:        prs("un/used/avail ");
                    207:        prn(un);
                    208:        blank();
                    209:        prn(us);
                    210:        blank();
                    211:        prn((char *)bloktop - brkbegin - (un + us));
                    212:        newline();
                    213: 
                    214: }

unix.superglobalmegacorp.com

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