dtrie.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * $Id: dtrie.c 5237 2008-11-21 10:17:10Z henningw $
  3. *
  4. * Copyright (C) 2008 1&1 Internet AG
  5. *
  6. * This file is part of sip-router, a free SIP server.
  7. *
  8. * sip-router is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * sip-router is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * \file
  24. * \brief Trie datastructure with utility functions
  25. *
  26. * Provides a generic trie datastructure and utility functions to
  27. * initialize and manage individual nodes. Its optimized towards
  28. * the usecase of a matching tree that contains only digits, e.g.
  29. * for LCR or blacklist modules. Nevertheless it also supports the
  30. * matching of characters when configured correctly. For normal
  31. * digit only matching you need to use a branches parameter of
  32. * 10, when you use 128, the complete standard ascii charset is
  33. * available for matching. The trie is setup in shared memory.
  34. * - Module: \ref carrierroute
  35. * - Module: \ref userblacklist
  36. */
  37. #include "dtrie.h"
  38. #include "../../dprint.h"
  39. #include "../../mem/shm_mem.h"
  40. #include "../../mem/mem.h"
  41. struct dtrie_node_t *dtrie_init(const unsigned int branches)
  42. {
  43. struct dtrie_node_t *root;
  44. root = shm_malloc(sizeof(struct dtrie_node_t));
  45. if (root == NULL) {
  46. SHM_MEM_ERROR;
  47. return NULL;
  48. }
  49. LM_DBG("allocate %lu bytes for root at %p\n",
  50. (long unsigned)sizeof(struct dtrie_node_t), root);
  51. memset(root, 0, sizeof(struct dtrie_node_t));
  52. root->child = shm_malloc(sizeof(struct dtrie_node_t *) * branches);
  53. if (root->child == NULL) {
  54. shm_free(root);
  55. SHM_MEM_ERROR;
  56. return NULL;
  57. }
  58. LM_DBG("allocate %lu bytes for %d root children pointer at %p\n",
  59. (long unsigned)sizeof(struct dtrie_node_t *) * branches,
  60. branches, root->child);
  61. memset(root->child, 0, sizeof(struct dtrie_node_t *) * branches);
  62. return root;
  63. }
  64. void dtrie_delete(struct dtrie_node_t *root, struct dtrie_node_t *node,
  65. dt_delete_func_t delete_payload, const unsigned int branches)
  66. {
  67. unsigned int i;
  68. if (node==NULL) return;
  69. for (i=0; i<branches; i++) {
  70. dtrie_delete(root, node->child[i], delete_payload, branches);
  71. node->child[i] = NULL;
  72. }
  73. if (delete_payload) {
  74. delete_payload(node->data);
  75. }
  76. node->data = NULL;
  77. if (node != root) {
  78. LM_DBG("free node at %p\n", node);
  79. shm_free(node->child);
  80. node->child = NULL;
  81. shm_free(node);
  82. }
  83. }
  84. void dtrie_destroy(struct dtrie_node_t **root, dt_delete_func_t delete_payload, const unsigned int branches)
  85. {
  86. if ((root!=NULL) && (*root!=NULL)) {
  87. dtrie_delete(*root, *root, delete_payload, branches);
  88. LM_DBG("free root at %p\n", root);
  89. shm_free((*root)->child);
  90. shm_free(*root);
  91. *root = NULL;
  92. }
  93. }
  94. void dtrie_clear(struct dtrie_node_t *root, dt_delete_func_t delete_payload,
  95. const unsigned int branches)
  96. {
  97. dtrie_delete(root, root, delete_payload, branches);
  98. }
  99. int dtrie_insert(struct dtrie_node_t *root, const char *number, const unsigned int numberlen,
  100. void *data, const unsigned int branches)
  101. {
  102. struct dtrie_node_t *node = root;
  103. unsigned char digit, i=0;
  104. while (i<numberlen) {
  105. if (branches==10) {
  106. digit = number[i] - '0';
  107. if (digit>9) {
  108. LM_ERR("cannot insert non-numerical character\n");
  109. return -1;
  110. }
  111. } else {
  112. digit = number[i];
  113. if (digit>127) {
  114. LM_ERR("cannot insert extended ascii character\n");
  115. return -1;
  116. }
  117. }
  118. if (node->child[digit] == NULL) {
  119. node->child[digit] = shm_malloc(sizeof(struct dtrie_node_t));
  120. if(node->child[digit] == NULL ){
  121. SHM_MEM_ERROR;
  122. return -1;
  123. }
  124. LM_DBG("allocate %lu bytes for node at %p\n", (long unsigned)sizeof(struct dtrie_node_t), node->child[digit]);
  125. memset(node->child[digit], 0, sizeof(struct dtrie_node_t));
  126. node->child[digit]->child = shm_malloc(sizeof(struct dtrie_node_t *) * branches);
  127. if(node->child[digit]->child == NULL){
  128. SHM_MEM_ERROR;
  129. shm_free(node->child[digit]);
  130. return -1;
  131. }
  132. LM_DBG("allocate %lu bytes for %d root children pointer at %p\n",
  133. (long unsigned)sizeof(struct dtrie_node_t *) * branches,
  134. branches, node->child[digit]->child);
  135. memset(node->child[digit]->child, 0, sizeof(struct dtrie_node_t *) * branches);
  136. }
  137. node = node->child[digit];
  138. i++;
  139. }
  140. node->data = data;
  141. return 0;
  142. }
  143. unsigned int dtrie_size(const struct dtrie_node_t *root, const unsigned int branches)
  144. {
  145. unsigned int i, sum = 0;
  146. if (root == NULL) return 0;
  147. for (i=0; i<branches; i++) {
  148. sum += dtrie_size(root->child[i], branches);
  149. }
  150. return sum+1;
  151. }
  152. unsigned int dtrie_loaded_nodes(const struct dtrie_node_t *root, const unsigned int branches)
  153. {
  154. unsigned int i, sum = 0;
  155. if (root == NULL) return 0;
  156. for (i=0; i<branches; i++) {
  157. sum += dtrie_loaded_nodes(root->child[i], branches);
  158. }
  159. if (root->data != NULL) sum++;
  160. return sum;
  161. }
  162. unsigned int dtrie_leaves(const struct dtrie_node_t *root, const unsigned int branches)
  163. {
  164. unsigned int i, sum = 0, leaf = 1;
  165. for (i=0; i<branches; i++) {
  166. if (root->child[i]) {
  167. sum += dtrie_leaves(root->child[i], branches);
  168. leaf = 0;
  169. }
  170. }
  171. return sum+leaf;
  172. }
  173. void **dtrie_longest_match(struct dtrie_node_t *root, const char *number,
  174. const unsigned int numberlen, int *nmatchptr, const unsigned int branches)
  175. {
  176. struct dtrie_node_t *node = root;
  177. unsigned char digit, i = 0;
  178. void **ret = NULL;
  179. if (nmatchptr) *nmatchptr=-1;
  180. if (node->data != NULL) {
  181. if (nmatchptr) *nmatchptr=0;
  182. ret = &node->data;
  183. }
  184. while (i<numberlen) {
  185. if (branches==10) {
  186. digit = number[i] - '0';
  187. if (digit>9) return ret;
  188. } else {
  189. digit = number[i];
  190. if (digit>127) return ret;
  191. }
  192. if (node->child[digit] == NULL) return ret;
  193. node = node->child[digit];
  194. i++;
  195. if (node->data != NULL) {
  196. if (nmatchptr) *nmatchptr=i;
  197. ret = &node->data;
  198. }
  199. }
  200. return ret;
  201. }
  202. void **dtrie_contains(struct dtrie_node_t *root, const char *number,
  203. const unsigned int numberlen, const unsigned int branches)
  204. {
  205. int nmatch;
  206. void **ret;
  207. ret = dtrie_longest_match(root, number, numberlen, &nmatch, branches);
  208. if (nmatch == numberlen) return ret;
  209. return NULL;
  210. }