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

unix.superglobalmegacorp.com

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