|
|
1.1 ! root 1: #include <libc.h> ! 2: #include "worm.h" ! 3: #include "sym.h" ! 4: ! 5: #define NHASH 20011 /* prime please */ ! 6: #define HASHMUL 79L /* this is a good value */ ! 7: static Symtab *hash[NHASH]; ! 8: int sym_mem_fail; ! 9: ! 10: syminit() ! 11: { ! 12: register Symtab **s, *ss; ! 13: ! 14: for(s = hash; s < &hash[NHASH]; s++){ ! 15: for(ss = *s; ss; ss = ss->next) ! 16: free((char *)ss); ! 17: *s = 0; ! 18: } ! 19: } ! 20: ! 21: void * ! 22: symlook(sym, space, install) ! 23: char *sym; ! 24: void *install; ! 25: { ! 26: register long h; ! 27: register char *p; ! 28: register Symtab *s; ! 29: ! 30: for(p = sym, h = space; *p; h += *p++) ! 31: h *= HASHMUL; ! 32: if(h < 0) ! 33: h = ~h; ! 34: h %= NHASH; ! 35: for(s = hash[h]; s; s = s->next) ! 36: if((s->space == space) && (strcmp(s->name, sym) == 0)){ ! 37: if(install) ! 38: s->value = install; ! 39: return(s->value); ! 40: } ! 41: if(install){ ! 42: s = (Symtab *)malloc((unsigned)sizeof(Symtab)); ! 43: if(s == 0){ ! 44: sym_mem_fail++; ! 45: return(install); ! 46: } ! 47: s->space = space; ! 48: s->name = sym; ! 49: s->value = install; ! 50: s->next = hash[h]; ! 51: hash[h] = s; ! 52: } ! 53: return(install); ! 54: } ! 55: ! 56: symdel(sym, space) ! 57: char *sym; ! 58: { ! 59: register long h; ! 60: register char *p; ! 61: register Symtab *s, *ls; ! 62: ! 63: for(p = sym, h = space; *p; h += *p++) ! 64: h *= HASHMUL; ! 65: if(h < 0) ! 66: h = ~h; ! 67: h %= NHASH; ! 68: for(s = hash[h], ls = 0; s; ls = s, s = s->next) ! 69: if((s->space == space) && (strcmp(s->name, sym) == 0)){ ! 70: if(ls) ! 71: ls->next = s->next; ! 72: else ! 73: hash[h] = s->next; ! 74: free((char *)s); ! 75: } ! 76: } ! 77: ! 78: symtraverse(space, fn) ! 79: void (*fn)(); ! 80: { ! 81: register Symtab **s, *ss, *next; ! 82: ! 83: for(s = hash; s < &hash[NHASH]; s++) ! 84: for(ss = *s; ss; ss = next){ ! 85: next = ss->next; ! 86: if(ss->space == space) ! 87: (*fn)(ss->value); ! 88: } ! 89: } ! 90: ! 91: symstat() ! 92: { ! 93: register Symtab **s, *ss; ! 94: int n[NHASH]; ! 95: register i, j; ! 96: int tot; ! 97: double d; ! 98: ! 99: for(i = 0; i < NHASH; i++) ! 100: n[i] = 0; ! 101: for(s = hash; s < &hash[NHASH]; s++){ ! 102: for(j = 0, ss = *s; ss; ss = ss->next) ! 103: j++; ! 104: n[j]++; ! 105: } ! 106: Fprint(1, "N=%ld mul=%ld\n", NHASH, HASHMUL); ! 107: for(i = 0, d = 0, tot = 0; i < NHASH; i++){ ! 108: if(n[i]) Fprint(1, "%d of length %d\n", n[i], i); ! 109: d += n[i]*i; ! 110: if(i) tot += n[i]; ! 111: } ! 112: Fprint(1, "ave len = %g\n", d/tot); ! 113: } ! 114: ! 115: symdump(sym, space) ! 116: char *sym; ! 117: { ! 118: register long h; ! 119: register char *p; ! 120: register Symtab *s; ! 121: ! 122: for(p = sym, h = space; *p; h += *p++) ! 123: h *= HASHMUL; ! 124: if(h < 0) ! 125: h = ~h; ! 126: h %= NHASH; ! 127: print("symdump(%s):\n", sym); ! 128: for(s = hash[h]; s; s = s->next) ! 129: print("\t%s: space=%d value=%ld\n", s->name, s->space, s->value); ! 130: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.