NCDModuleIndex.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @file NCDModuleIndex.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <misc/offset.h>
  32. #include <misc/balloc.h>
  33. #include <misc/hashfun.h>
  34. #include <misc/compare.h>
  35. #include <misc/substring.h>
  36. #include <base/BLog.h>
  37. #include "NCDModuleIndex.h"
  38. #include <generated/blog_channel_NCDModuleIndex.h>
  39. #include "NCDModuleIndex_mhash.h"
  40. #include <structure/CHash_impl.h>
  41. static int string_pointer_comparator (void *user, const char **s1, const char **s2)
  42. {
  43. int cmp = strcmp(*s1, *s2);
  44. return B_COMPARE(cmp, 0);
  45. }
  46. static struct NCDModuleIndex_module * find_module (NCDModuleIndex *o, const char *type)
  47. {
  48. NCDModuleIndex__MHashRef ref = NCDModuleIndex__MHash_Lookup(&o->modules_hash, o->modules, type);
  49. if (ref.link == NCDModuleIndex__MHashNullLink()) {
  50. return NULL;
  51. }
  52. ASSERT(!strcmp(ref.ptr->type, type))
  53. return ref.ptr;
  54. }
  55. static struct NCDModuleIndex_base_type * find_base_type (NCDModuleIndex *o, const char *base_type)
  56. {
  57. BAVLNode *node = BAVL_LookupExact(&o->base_types_tree, &base_type);
  58. if (!node) {
  59. return NULL;
  60. }
  61. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(node, struct NCDModuleIndex_base_type, base_types_tree_node);
  62. ASSERT(!strcmp(bt->base_type, base_type))
  63. return bt;
  64. }
  65. static int add_method (char *type, const struct NCDModule *module, NCDMethodIndex *method_index)
  66. {
  67. ASSERT(type)
  68. ASSERT(module)
  69. ASSERT(method_index)
  70. const char search[] = "::";
  71. size_t search_len = sizeof(search) - 1;
  72. size_t table[sizeof(search) - 1];
  73. build_substring_backtrack_table_reverse(search, search_len, table);
  74. size_t pos;
  75. if (!find_substring_reverse(type, strlen(type), search, search_len, table, &pos)) {
  76. return 1;
  77. }
  78. ASSERT(pos >= 0)
  79. ASSERT(pos <= strlen(type) - search_len)
  80. ASSERT(!memcmp(type + pos, search, search_len))
  81. char save = type[pos];
  82. type[pos] = '\0';
  83. int res = NCDMethodIndex_AddMethod(method_index, type, type + pos + search_len, module);
  84. type[pos] = save;
  85. if (!res) {
  86. BLog(BLOG_ERROR, "NCDMethodIndex_AddMethod failed");
  87. return 0;
  88. }
  89. return 1;
  90. }
  91. int NCDModuleIndex_Init (NCDModuleIndex *o)
  92. {
  93. // allocate modules array
  94. if (!(o->modules = BAllocArray(NCDMODULEINDEX_MAX_MODULES, sizeof(o->modules[0])))) {
  95. BLog(BLOG_ERROR, "BAllocArray failed");
  96. goto fail0;
  97. }
  98. // set zero modules
  99. o->num_modules = 0;
  100. // init modules hash
  101. if (!NCDModuleIndex__MHash_Init(&o->modules_hash, NCDMODULEINDEX_MODULES_HASH_SIZE)) {
  102. BLog(BLOG_ERROR, "NCDModuleIndex__MHash_Init failed");
  103. goto fail1;
  104. }
  105. // init base types tree
  106. BAVL_Init(&o->base_types_tree, OFFSET_DIFF(struct NCDModuleIndex_base_type, base_type, base_types_tree_node), (BAVL_comparator)string_pointer_comparator, NULL);
  107. DebugObject_Init(&o->d_obj);
  108. return 1;
  109. fail1:
  110. BFree(o->modules);
  111. fail0:
  112. return 0;
  113. }
  114. void NCDModuleIndex_Free (NCDModuleIndex *o)
  115. {
  116. DebugObject_Free(&o->d_obj);
  117. // free base types
  118. while (!BAVL_IsEmpty(&o->base_types_tree)) {
  119. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(BAVL_GetFirst(&o->base_types_tree), struct NCDModuleIndex_base_type, base_types_tree_node);
  120. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  121. free(bt);
  122. }
  123. // free modules hash
  124. NCDModuleIndex__MHash_Free(&o->modules_hash);
  125. // free modules array
  126. BFree(o->modules);
  127. }
  128. int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group, NCDMethodIndex *method_index)
  129. {
  130. DebugObject_Access(&o->d_obj);
  131. ASSERT(group)
  132. ASSERT(method_index)
  133. for (const struct NCDModule *nm = group->modules; nm->type; nm++) {
  134. if (find_module(o, nm->type)) {
  135. BLog(BLOG_ERROR, "module type '%s' already exists", nm->type);
  136. goto loop_fail0;
  137. }
  138. if (strlen(nm->type) > NCDMODULEINDEX_MAX_TYPE_LEN) {
  139. BLog(BLOG_ERROR, "module type '%s' is too long (dump NCDMODULEINDEX_MAX_TYPE_LEN)", nm->type);
  140. goto loop_fail0;
  141. }
  142. if (o->num_modules == NCDMODULEINDEX_MAX_MODULES) {
  143. BLog(BLOG_ERROR, "too many modules (bump NCDMODULEINDEX_MAX_MODULES)");
  144. goto loop_fail0;
  145. }
  146. struct NCDModuleIndex_module *m = &o->modules[o->num_modules];
  147. strcpy(m->type, nm->type);
  148. m->module = nm;
  149. NCDModuleIndex__MHashRef ref = {m, o->num_modules};
  150. int res = NCDModuleIndex__MHash_Insert(&o->modules_hash, o->modules, ref, NULL);
  151. ASSERT(res)
  152. const char *base_type = (nm->base_type ? nm->base_type : nm->type);
  153. struct NCDModuleIndex_base_type *bt = find_base_type(o, base_type);
  154. if (bt) {
  155. if (bt->group != group) {
  156. BLog(BLOG_ERROR, "module base type '%s' already exists in another module group", base_type);
  157. goto loop_fail1;
  158. }
  159. } else {
  160. if (!(bt = malloc(sizeof(*bt)))) {
  161. BLog(BLOG_ERROR, "malloc failed");
  162. goto loop_fail1;
  163. }
  164. bt->base_type = base_type;
  165. bt->group = group;
  166. ASSERT_EXECUTE(BAVL_Insert(&o->base_types_tree, &bt->base_types_tree_node, NULL))
  167. }
  168. o->num_modules++;
  169. if (!add_method(m->type, nm, method_index)) {
  170. BLog(BLOG_ERROR, "failed to add method to method index");
  171. return 0;
  172. }
  173. continue;
  174. loop_fail1:
  175. NCDModuleIndex__MHash_Remove(&o->modules_hash, o->modules, ref);
  176. loop_fail0:
  177. return 0;
  178. }
  179. return 1;
  180. }
  181. const struct NCDModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const char *type)
  182. {
  183. DebugObject_Access(&o->d_obj);
  184. ASSERT(type)
  185. struct NCDModuleIndex_module *m = find_module(o, type);
  186. if (!m) {
  187. return NULL;
  188. }
  189. return m->module;
  190. }