str_hash.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2006 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * History:
  20. * --------
  21. * 2006-02-02 created by andrei
  22. * 2006-11-24 added numeric string optimized hash function (andrei)
  23. * 2006-12-13 split into hashes.h (more generic) and str_hash.h (andrei)
  24. */
  25. #ifndef _str_hashs_h
  26. #define _str_hashs_h
  27. #include "str.h"
  28. #include "hashes.h"
  29. #include "mem/mem.h"
  30. #include "clist.h"
  31. #include <string.h>
  32. /* generic, simple str keyed hash */
  33. struct str_hash_entry{
  34. struct str_hash_entry* next;
  35. struct str_hash_entry* prev;
  36. str key;
  37. unsigned int flags;
  38. union{
  39. void* p;
  40. char* s;
  41. int n;
  42. char data[sizeof(void*)];
  43. }u;
  44. };
  45. struct str_hash_head{
  46. struct str_hash_entry* next;
  47. struct str_hash_entry* prev;
  48. };
  49. struct str_hash_table{
  50. struct str_hash_head* table;
  51. int size;
  52. };
  53. /* returns 0 on success, <0 on failure */
  54. inline static int str_hash_alloc(struct str_hash_table* ht, int size)
  55. {
  56. ht->table=(struct str_hash_head*)pkg_malloc(sizeof(struct str_hash_head)*size);
  57. if (ht->table==0)
  58. return -1;
  59. ht->size=size;
  60. return 0;
  61. }
  62. inline static void str_hash_init(struct str_hash_table* ht)
  63. {
  64. int r;
  65. for (r=0; r<ht->size; r++) clist_init(&(ht->table[r]), next, prev);
  66. }
  67. inline static void str_hash_add(struct str_hash_table* ht,
  68. struct str_hash_entry* e)
  69. {
  70. int h;
  71. h=get_hash1_raw(e->key.s, e->key.len) % ht->size;
  72. clist_insert(&ht->table[h], e, next, prev);
  73. }
  74. inline static struct str_hash_entry* str_hash_get(struct str_hash_table* ht,
  75. const char* key, int len)
  76. {
  77. int h;
  78. struct str_hash_entry* e;
  79. h=get_hash1_raw(key, len) % ht->size;
  80. clist_foreach(&ht->table[h], e, next){
  81. if ((e->key.len==len) && (memcmp(e->key.s, key, len)==0))
  82. return e;
  83. }
  84. return 0;
  85. }
  86. #define str_hash_del(e) clist_rm(e, next, prev)
  87. #endif