|
|
1.1 ! root 1: # include "mfile1.h" ! 2: ! 3: lookup(name, s) /* look up name: must agree with s w.r.t. STAG, SMOS and SHIDDEN */ ! 4: register char *name; register s; ! 5: { ! 6: register struct symtab *sp, *spstart; ! 7: ! 8: # ifndef NODBG ! 9: extern int ddebug; ! 10: if (ddebug > 2) { ! 11: printf("lookup(%s, %d), stwart=%d, instruct=%d\n", ! 12: name, s, stwart, instruct); ! 13: } ! 14: # endif ! 15: ! 16: /* compute initial hash index */ ! 17: sp = spstart = stab + ((long)name & 077777) % SYMTSZ; ! 18: ! 19: do { /* look for name */ ! 20: if (sp->stype == TNULL) { /* empty slot */ ! 21: sp->sflags = s; /* set STAG, SMOS if needed */ ! 22: sp->sname = name; ! 23: sp->stype = UNDEF; /* turn off all others */ ! 24: sp->sclass = SNULL; ! 25: return (sp-stab); ! 26: } ! 27: if (sp->sname == name && (sp->sflags & (STAG|SMOS|SHIDDEN)) == s) ! 28: return (sp-stab); ! 29: } while ((++sp >= stab+SYMTSZ ? sp = stab : sp) != spstart); ! 30: cerror("symbol table full"); ! 31: } ! 32: ! 33: #define HASHSIZE 1013 ! 34: #define MAXHUSE ((3*HASHSIZE)/4) ! 35: #define STRTABSIZE 4096 ! 36: ! 37: struct ht { ! 38: struct ht *next; ! 39: int nused; ! 40: char *ptable[HASHSIZE]; ! 41: } hashroot; ! 42: ! 43: char * ! 44: hash(str) /* Lookup str in hash tables; if not found, make new entry. */ ! 45: char *str; ! 46: { ! 47: char *calloc(), *savestr(); ! 48: register char *cp, **hstrp, **htptable; ! 49: register int h, i; ! 50: register struct ht *htp; ! 51: int sh; ! 52: ! 53: for (cp=str, h=0; *cp; ) ! 54: h = (h << 1) + *cp++; ! 55: sh = (h & 077777) % HASHSIZE; ! 56: ! 57: /* Look through each table for name. Use quadratic re-hash. */ ! 58: for (htp = &hashroot; htp; htp = htp->next) { ! 59: htptable = htp->ptable; ! 60: for (h=sh, i=1; i<HASHSIZE; h += i, i += 2) { ! 61: if (h >= HASHSIZE) ! 62: h -= HASHSIZE; ! 63: hstrp = &htptable[h]; ! 64: if (*hstrp) { ! 65: if (strcmp(*hstrp, str)) ! 66: continue; ! 67: return *hstrp; ! 68: } else { ! 69: if (htp->nused > MAXHUSE) ! 70: break; ! 71: htp->nused++; ! 72: return (*hstrp = savestr(str)); ! 73: } ! 74: } ! 75: if (htp->next == 0) ! 76: htp->next = (struct ht *)calloc(1,sizeof(struct ht)); ! 77: } ! 78: cerror("cannot allocate hash table"); ! 79: } ! 80: ! 81: char * ! 82: savestr(str) /* Place string into permanent storage. */ ! 83: register char *str; ! 84: { ! 85: char *memcpy(), *malloc(); ! 86: static char *curstp; static int nchleft; ! 87: register int len; ! 88: ! 89: if ((len = strlen(str)+1) > nchleft && ! 90: (curstp = malloc(nchleft = len+STRTABSIZE)) == 0) ! 91: cerror("cannot allocate string table"); ! 92: str = memcpy(curstp, str, len); ! 93: curstp += len; nchleft -= len; ! 94: return str; ! 95: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.