Annotation of researchv10dc/cmd/icon/src/tran/mem.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Memory initialization and allocation for the translator.
        !             3:  */
        !             4: 
        !             5: #include "itran.h"
        !             6: #include "sym.h"
        !             7: #include "tree.h"
        !             8: #include "../h/memsize.h"
        !             9: 
        !            10: struct lentry **lhash;         /* hash area for local table */
        !            11: struct gentry **ghash;         /* hash area for global table */
        !            12: struct centry **chash;         /* hash area for constant table */
        !            13: struct ientry **ihash;         /* hash area for identifier table */
        !            14: 
        !            15: nodeptr       tree;            /* parse tree space */
        !            16: nodeptr       tend;            /* end of parse tree space */
        !            17: struct lentry *ltable;         /* local table */
        !            18: struct gentry *gtable;         /* global table */
        !            19: struct centry *ctable;         /* constant table */
        !            20: struct ientry *itable;         /* identifier table */
        !            21: 
        !            22: char *strings;                 /* string space */
        !            23: char *send;                    /* end of string space */
        !            24: 
        !            25: nodeptr        tfree;          /* free pointer for parse tree space */
        !            26: struct lentry *lfree;          /* free pointer for local table */
        !            27: struct gentry *gfree;          /* free pointer for global table */
        !            28: struct centry *ctfree;         /* free pointer to constant table */
        !            29: struct ientry *ifree;          /* free pointer for identifier table */
        !            30: char         *strfree;         /* free pointer for string space */
        !            31: 
        !            32: int tsize  = TSize;            /* initial size of parse tree space */
        !            33: int lsize  = LSize;            /* initial size of local table */
        !            34: int gsize  = GSize;            /* initial size of global table */
        !            35: int csize  = CSize;            /* initial size of constant table */
        !            36: int isize  = ISize;            /* initial size of identifier table */
        !            37: int ssize  = SSize;            /* initial size of string space */
        !            38: int lhsize = LhSize;           /* initial size of local hash table */
        !            39: int ghsize = GhSize;           /* initial size of global hash table */
        !            40: int chsize = ChSize;           /* initial size of constant hash table */
        !            41: int ihsize = IhSize;           /* initial size of identifier hash table */
        !            42: int lmask;                     /* mask for local table hash */
        !            43: int gmask;                     /* mask for global table hash */
        !            44: int cmask;                     /* mask for constant table hash */
        !            45: int imask;                     /* mask for identifier table hash */
        !            46: 
        !            47: int memsetd;
        !            48: 
        !            49: /*
        !            50:  * meminit does per-file initialization of various data structures used
        !            51:  *  by the translator.
        !            52:  */
        !            53: meminit()
        !            54:    {
        !            55:    register *p;
        !            56: 
        !            57:    if (!memsetd)
        !            58:       memalloc();              /* allocate data regions for first file */
        !            59:    /*
        !            60:     * Reset the free pointer for each region.
        !            61:     */
        !            62:    lfree = ltable;
        !            63:    gfree = gtable;
        !            64:    ctfree = ctable;
        !            65:    ifree = itable;
        !            66:    strfree = strings;
        !            67:    tfree = tree;
        !            68:    /*
        !            69:     * Zero out the hash tables.
        !            70:     */
        !            71:    for (p = (int *)lhash; p < (int *)&lhash[lhsize]; p++)
        !            72:       *p = NULL;
        !            73:    for (p = (int *)ghash; p < (int *)&ghash[ghsize]; p++)
        !            74:       *p = NULL;
        !            75:    for (p = (int *)chash; p < (int *)&chash[chsize]; p++)
        !            76:       *p = NULL;
        !            77:    for (p = (int *)ihash; p < (int *)&ihash[ihsize]; p++)
        !            78:       *p = NULL;
        !            79: 
        !            80:    /*
        !            81:     * Vestigial structures - these flags are only incremented after
        !            82:     *  a call to syserr.  Idea was apparently to count number of
        !            83:     *  entries in an overflowing table, but wasn't completely
        !            84:     *  implemented.
        !            85:     */
        !            86:    alclflg = 0;
        !            87:    alcgflg = 0;
        !            88:    alccflg = 0;
        !            89:    }
        !            90: 
        !            91: /*
        !            92:  * allocate gets n*size bytes of storage and returns a pointer to it.
        !            93:  */
        !            94: 
        !            95: char *allocate(n, size)
        !            96: int n, size;
        !            97:    {
        !            98: #ifndef MSDOS
        !            99:    return (char *)malloc(n*size);
        !           100: #else MSDOS
        !           101:    return (char *)calloc(n,size);
        !           102: #endif MSDOS
        !           103:    }
        !           104: 
        !           105: /*
        !           106:  * memalloc computes sizes of data regions needed by the translator
        !           107:  * obtains space for them, and initializes pointers to them
        !           108:  */
        !           109: 
        !           110: memalloc()
        !           111: {
        !           112:    register int i;
        !           113:    char *allocate();
        !           114: 
        !           115:    /*
        !           116:     * Round sizes of hash tables for locals, globals, constants, and
        !           117:     *  identifiers to next larger power of two.  The corresponding
        !           118:     *  mask values are set to one less than the hash table size so that
        !           119:     *  an integer value can be &'d with the mask to produce a hash value.
        !           120:     *  (See [lgc]hasher in sym.h.)
        !           121:     */
        !           122:    for (i = 1; i < lhsize; i <<= 1) ;
        !           123:    lhsize = i;
        !           124:    lmask = i - 1;
        !           125:    for (i = 1; i < ghsize; i <<= 1) ;
        !           126:    ghsize = i;
        !           127:    gmask = i - 1;
        !           128:    for (i = 1; i < chsize; i <<= 1) ;
        !           129:    chsize = i;
        !           130:    cmask = i - 1;
        !           131:    for (i = 1; i < ihsize; i <<= 1) ;
        !           132:    ihsize = i;
        !           133:    imask = i - 1;
        !           134: 
        !           135:    /*
        !           136:     * Allocate the various data structures.
        !           137:     */
        !           138:    lhash = (struct lentry **)  allocate(lhsize, sizeof(struct lentry *));
        !           139:    ghash = (struct gentry **)  allocate(ghsize, sizeof(struct gentry *));
        !           140:    chash = (struct centry **)  allocate(chsize, sizeof(struct centry *));
        !           141:    ihash = (struct ientry **)  allocate(ihsize, sizeof(struct ientry *));
        !           142:    ltable = (struct lentry *)  allocate(lsize, sizeof(struct lentry));
        !           143:    gtable = (struct gentry *)  allocate(gsize, sizeof(struct gentry));
        !           144:    ctable = (struct centry *)  allocate(csize, sizeof(struct centry));
        !           145:    itable = (struct ientry *)  allocate(isize, sizeof(struct ientry));
        !           146:    tree = (nodeptr)            allocate(tsize, sizeof(int));
        !           147:    strings =                   allocate(ssize, sizeof(char));
        !           148:    tend = (nodeptr)((int *)tree + tsize);
        !           149:    send = strings + ssize;
        !           150:    /*
        !           151:     * Check to see if there was enough memory.  This assumes that the
        !           152:     *  allocation for strings fails if any of the other allocations
        !           153:     *  failed.  Apparent bug - That assumption is not necessarily valid.
        !           154:     */
        !           155:    if (strings == NULL) {
        !           156:       fprintf(stderr, "Can't get enough memory\n");
        !           157:       exit(ErrorExit);
        !           158:       }
        !           159: 
        !           160: }

unix.superglobalmegacorp.com

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