ust.c 960 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "logfsos.h"
  2. #include "logfs.h"
  3. #include "local.h"
  4. enum {
  5. USTMOD = 127
  6. };
  7. typedef struct UstNode {
  8. char s[1];
  9. } UstNode;
  10. struct Ust {
  11. UstNode *head[USTMOD];
  12. };
  13. int
  14. logfshashstring(void *s, int n)
  15. {
  16. int h = 0;
  17. char *p;
  18. for(p = s; *p; p++) {
  19. ulong g;
  20. h = (h << 4) + *p;
  21. g = h & 0xf0000000;
  22. if(g != 0)
  23. h ^= ((g >> 24) & 0xff) | g;
  24. }
  25. return (h & ~(1 << 31)) % n;
  26. }
  27. static int
  28. compare(void *entry, void *key)
  29. {
  30. return strcmp((char*)entry, (char*)key) == 0;
  31. }
  32. static int
  33. allocsize(void *key)
  34. {
  35. return strlen(key) + 1;
  36. }
  37. char *
  38. logfsustnew(Ust **ustp)
  39. {
  40. return logfsmapnew(USTMOD, logfshashstring, compare, allocsize, nil, ustp);
  41. }
  42. char *
  43. logfsustadd(Ust *ust, char *s)
  44. {
  45. char *errmsg;
  46. char *ep;
  47. ep = logfsmapfindentry(ust, s);
  48. if(ep != nil) {
  49. // print("ust: found %s\n", s);
  50. return ep;
  51. }
  52. errmsg = logfsmapnewentry(ust, s, &ep);
  53. if(errmsg != nil)
  54. return errmsg;
  55. // print("ust: new %s\n", s);
  56. return strcpy(ep, s);
  57. }