NCDModuleIndex.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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, NCDStringIndex *string_index)
  92. {
  93. ASSERT(string_index)
  94. // allocate modules array
  95. if (!(o->modules = BAllocArray(NCDMODULEINDEX_MAX_MODULES, sizeof(o->modules[0])))) {
  96. BLog(BLOG_ERROR, "BAllocArray failed");
  97. goto fail0;
  98. }
  99. // set zero modules
  100. o->num_modules = 0;
  101. // init modules hash
  102. if (!NCDModuleIndex__MHash_Init(&o->modules_hash, NCDMODULEINDEX_MODULES_HASH_SIZE)) {
  103. BLog(BLOG_ERROR, "NCDModuleIndex__MHash_Init failed");
  104. goto fail1;
  105. }
  106. // init base types tree
  107. BAVL_Init(&o->base_types_tree, OFFSET_DIFF(struct NCDModuleIndex_base_type, base_type, base_types_tree_node), (BAVL_comparator)string_pointer_comparator, NULL);
  108. // init method index
  109. if (!NCDMethodIndex_Init(&o->method_index, string_index)) {
  110. BLog(BLOG_ERROR, "NCDMethodIndex_Init failed");
  111. goto fail2;
  112. }
  113. DebugObject_Init(&o->d_obj);
  114. return 1;
  115. fail2:
  116. NCDModuleIndex__MHash_Free(&o->modules_hash);
  117. fail1:
  118. BFree(o->modules);
  119. fail0:
  120. return 0;
  121. }
  122. void NCDModuleIndex_Free (NCDModuleIndex *o)
  123. {
  124. DebugObject_Free(&o->d_obj);
  125. // free base types
  126. while (!BAVL_IsEmpty(&o->base_types_tree)) {
  127. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(BAVL_GetFirst(&o->base_types_tree), struct NCDModuleIndex_base_type, base_types_tree_node);
  128. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  129. free(bt);
  130. }
  131. // free method index
  132. NCDMethodIndex_Free(&o->method_index);
  133. // free modules hash
  134. NCDModuleIndex__MHash_Free(&o->modules_hash);
  135. // free modules array
  136. BFree(o->modules);
  137. }
  138. int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group)
  139. {
  140. DebugObject_Access(&o->d_obj);
  141. ASSERT(group)
  142. for (const struct NCDModule *nm = group->modules; nm->type; nm++) {
  143. if (find_module(o, nm->type)) {
  144. BLog(BLOG_ERROR, "module type '%s' already exists", nm->type);
  145. goto loop_fail0;
  146. }
  147. if (strlen(nm->type) > NCDMODULEINDEX_MAX_TYPE_LEN) {
  148. BLog(BLOG_ERROR, "module type '%s' is too long (dump NCDMODULEINDEX_MAX_TYPE_LEN)", nm->type);
  149. goto loop_fail0;
  150. }
  151. if (o->num_modules == NCDMODULEINDEX_MAX_MODULES) {
  152. BLog(BLOG_ERROR, "too many modules (bump NCDMODULEINDEX_MAX_MODULES)");
  153. goto loop_fail0;
  154. }
  155. struct NCDModuleIndex_module *m = &o->modules[o->num_modules];
  156. strcpy(m->type, nm->type);
  157. m->module = nm;
  158. NCDModuleIndex__MHashRef ref = {m, o->num_modules};
  159. int res = NCDModuleIndex__MHash_Insert(&o->modules_hash, o->modules, ref, NULL);
  160. ASSERT_EXECUTE(res)
  161. const char *base_type = (nm->base_type ? nm->base_type : nm->type);
  162. struct NCDModuleIndex_base_type *bt = find_base_type(o, base_type);
  163. if (bt) {
  164. if (bt->group != group) {
  165. BLog(BLOG_ERROR, "module base type '%s' already exists in another module group", base_type);
  166. goto loop_fail1;
  167. }
  168. } else {
  169. if (!(bt = malloc(sizeof(*bt)))) {
  170. BLog(BLOG_ERROR, "malloc failed");
  171. goto loop_fail1;
  172. }
  173. bt->base_type = base_type;
  174. bt->group = group;
  175. ASSERT_EXECUTE(BAVL_Insert(&o->base_types_tree, &bt->base_types_tree_node, NULL))
  176. }
  177. o->num_modules++;
  178. if (!add_method(m->type, nm, &o->method_index)) {
  179. BLog(BLOG_ERROR, "failed to add method to method index");
  180. return 0;
  181. }
  182. continue;
  183. loop_fail1:
  184. NCDModuleIndex__MHash_Remove(&o->modules_hash, o->modules, ref);
  185. loop_fail0:
  186. return 0;
  187. }
  188. return 1;
  189. }
  190. const struct NCDModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const char *type)
  191. {
  192. DebugObject_Access(&o->d_obj);
  193. ASSERT(type)
  194. struct NCDModuleIndex_module *m = find_module(o, type);
  195. if (!m) {
  196. return NULL;
  197. }
  198. return m->module;
  199. }
  200. int NCDModuleIndex_GetMethodNameId (NCDModuleIndex *o, const char *method_name)
  201. {
  202. DebugObject_Access(&o->d_obj);
  203. ASSERT(method_name)
  204. return NCDMethodIndex_GetMethodNameId(&o->method_index, method_name);
  205. }
  206. const struct NCDModule * NCDModuleIndex_GetMethodModule (NCDModuleIndex *o, NCD_string_id_t obj_type, int method_name_id)
  207. {
  208. DebugObject_Access(&o->d_obj);
  209. return NCDMethodIndex_GetMethodModule(&o->method_index, obj_type, method_name_id);
  210. }