symtab.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "mk.h"
  10. #define NHASH 4099
  11. #define HASHMUL 79L /* this is a good value */
  12. static Symtab *hash[NHASH];
  13. void
  14. syminit(void)
  15. {
  16. Symtab **s, *ss;
  17. for(s = hash; s < &hash[NHASH]; s++){
  18. for(ss = *s; ss; ss = ss->next)
  19. free((char *)ss);
  20. *s = 0;
  21. }
  22. }
  23. Symtab *
  24. symlook(char *sym, int space, void *install)
  25. {
  26. int32_t h;
  27. char *p;
  28. Symtab *s;
  29. for(p = sym, h = space; *p; h += *p++)
  30. h *= HASHMUL;
  31. if(h < 0)
  32. h = ~h;
  33. h %= NHASH;
  34. for(s = hash[h]; s; s = s->next)
  35. if((s->space == space) && (strcmp(s->name, sym) == 0))
  36. return(s);
  37. if(install == 0)
  38. return(0);
  39. s = (Symtab *)Malloc(sizeof(Symtab));
  40. s->space = space;
  41. s->name = sym;
  42. s->u.ptr = install;
  43. s->next = hash[h];
  44. hash[h] = s;
  45. return(s);
  46. }
  47. void
  48. symdel(char *sym, int space)
  49. {
  50. int32_t h;
  51. char *p;
  52. Symtab *s, *ls;
  53. /* multiple memory leaks */
  54. for(p = sym, h = space; *p; h += *p++)
  55. h *= HASHMUL;
  56. if(h < 0)
  57. h = ~h;
  58. h %= NHASH;
  59. for(s = hash[h], ls = 0; s; ls = s, s = s->next)
  60. if((s->space == space) && (strcmp(s->name, sym) == 0)){
  61. if(ls)
  62. ls->next = s->next;
  63. else
  64. hash[h] = s->next;
  65. free((char *)s);
  66. }
  67. }
  68. void
  69. symtraverse(int space, void (*fn)(Symtab*))
  70. {
  71. Symtab **s, *ss;
  72. for(s = hash; s < &hash[NHASH]; s++)
  73. for(ss = *s; ss; ss = ss->next)
  74. if(ss->space == space)
  75. (*fn)(ss);
  76. }
  77. void
  78. symstat(void)
  79. {
  80. Symtab **s, *ss;
  81. int n;
  82. int l[1000];
  83. memset((char *)l, 0, sizeof(l));
  84. for(s = hash; s < &hash[NHASH]; s++){
  85. for(ss = *s, n = 0; ss; ss = ss->next)
  86. n++;
  87. l[n]++;
  88. }
  89. for(n = 0; n < 1000; n++)
  90. if(l[n]) Bprint(&bout, "%d of length %d\n", l[n], n);
  91. }