symtab.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implementation of the symbol table type.
  4. *
  5. * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/string.h>
  9. #include <linux/errno.h>
  10. #include "symtab.h"
  11. static unsigned int symhash(const void *key)
  12. {
  13. /*
  14. * djb2a
  15. * Public domain from cdb v0.75
  16. */
  17. unsigned int hash = 5381;
  18. unsigned char c;
  19. while ((c = *(const unsigned char *)key++))
  20. hash = ((hash << 5) + hash) ^ c;
  21. return hash;
  22. }
  23. static int symcmp(const void *key1, const void *key2)
  24. {
  25. const char *keyp1, *keyp2;
  26. keyp1 = key1;
  27. keyp2 = key2;
  28. return strcmp(keyp1, keyp2);
  29. }
  30. static const struct hashtab_key_params symtab_key_params = {
  31. .hash = symhash,
  32. .cmp = symcmp,
  33. };
  34. int symtab_init(struct symtab *s, u32 size)
  35. {
  36. s->nprim = 0;
  37. return hashtab_init(&s->table, size);
  38. }
  39. int symtab_insert(struct symtab *s, char *name, void *datum)
  40. {
  41. return hashtab_insert(&s->table, name, datum, symtab_key_params);
  42. }
  43. void *symtab_search(struct symtab *s, const char *name)
  44. {
  45. return hashtab_search(&s->table, name, symtab_key_params);
  46. }