NCDModuleIndex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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, int *out_method_id)
  66. {
  67. ASSERT(type)
  68. ASSERT(module)
  69. ASSERT(method_index)
  70. ASSERT(out_method_id)
  71. const char search[] = "::";
  72. size_t search_len = sizeof(search) - 1;
  73. size_t table[sizeof(search) - 1];
  74. build_substring_backtrack_table_reverse(search, search_len, table);
  75. size_t pos;
  76. if (!find_substring_reverse(type, strlen(type), search, search_len, table, &pos)) {
  77. *out_method_id = -1;
  78. return 1;
  79. }
  80. ASSERT(pos >= 0)
  81. ASSERT(pos <= strlen(type) - search_len)
  82. ASSERT(!memcmp(type + pos, search, search_len))
  83. char save = type[pos];
  84. type[pos] = '\0';
  85. int method_id = NCDMethodIndex_AddMethod(method_index, type, type + pos + search_len, module);
  86. type[pos] = save;
  87. if (method_id < 0) {
  88. BLog(BLOG_ERROR, "NCDMethodIndex_AddMethod failed");
  89. return 0;
  90. }
  91. *out_method_id = method_id;
  92. return 1;
  93. }
  94. int NCDModuleIndex_Init (NCDModuleIndex *o, NCDStringIndex *string_index)
  95. {
  96. ASSERT(string_index)
  97. // allocate modules array
  98. if (!(o->modules = BAllocArray(NCDMODULEINDEX_MAX_MODULES, sizeof(o->modules[0])))) {
  99. BLog(BLOG_ERROR, "BAllocArray failed");
  100. goto fail0;
  101. }
  102. // set zero modules
  103. o->num_modules = 0;
  104. // init modules hash
  105. if (!NCDModuleIndex__MHash_Init(&o->modules_hash, NCDMODULEINDEX_MODULES_HASH_SIZE)) {
  106. BLog(BLOG_ERROR, "NCDModuleIndex__MHash_Init failed");
  107. goto fail1;
  108. }
  109. // init base types tree
  110. BAVL_Init(&o->base_types_tree, OFFSET_DIFF(struct NCDModuleIndex_base_type, base_type, base_types_tree_node), (BAVL_comparator)string_pointer_comparator, NULL);
  111. // init groups list
  112. LinkedList0_Init(&o->groups_list);
  113. // init method index
  114. if (!NCDMethodIndex_Init(&o->method_index, string_index)) {
  115. BLog(BLOG_ERROR, "NCDMethodIndex_Init failed");
  116. goto fail2;
  117. }
  118. DebugObject_Init(&o->d_obj);
  119. return 1;
  120. fail2:
  121. NCDModuleIndex__MHash_Free(&o->modules_hash);
  122. fail1:
  123. BFree(o->modules);
  124. fail0:
  125. return 0;
  126. }
  127. void NCDModuleIndex_Free (NCDModuleIndex *o)
  128. {
  129. DebugObject_Free(&o->d_obj);
  130. // free groups
  131. LinkedList0Node *ln;
  132. while (ln = LinkedList0_GetFirst(&o->groups_list)) {
  133. struct NCDModuleIndex_group *ig = UPPER_OBJECT(ln, struct NCDModuleIndex_group, groups_list_node);
  134. if (ig->group->func_globalfree) {
  135. ig->group->func_globalfree();
  136. }
  137. LinkedList0_Remove(&o->groups_list, &ig->groups_list_node);
  138. BFree(ig);
  139. }
  140. // free base types
  141. while (!BAVL_IsEmpty(&o->base_types_tree)) {
  142. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(BAVL_GetFirst(&o->base_types_tree), struct NCDModuleIndex_base_type, base_types_tree_node);
  143. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  144. free(bt);
  145. }
  146. // free method index
  147. NCDMethodIndex_Free(&o->method_index);
  148. // free modules hash
  149. NCDModuleIndex__MHash_Free(&o->modules_hash);
  150. // free modules array
  151. BFree(o->modules);
  152. }
  153. int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group, const struct NCDModuleInst_iparams *iparams, NCDStringIndex *string_index)
  154. {
  155. DebugObject_Access(&o->d_obj);
  156. ASSERT(group)
  157. ASSERT(iparams)
  158. ASSERT(string_index)
  159. struct NCDModuleIndex_group *ig = BAlloc(sizeof(*ig));
  160. if (!ig) {
  161. BLog(BLOG_ERROR, "BAlloc failed");
  162. goto fail0;
  163. }
  164. ig->group = group;
  165. LinkedList0_Prepend(&o->groups_list, &ig->groups_list_node);
  166. if (group->strings) {
  167. if (!NCDStringIndex_GetRequests(string_index, group->strings)) {
  168. BLog(BLOG_ERROR, "NCDStringIndex_GetRequests failed");
  169. goto fail1;
  170. }
  171. }
  172. if (group->func_globalinit) {
  173. if (!group->func_globalinit(iparams)) {
  174. BLog(BLOG_ERROR, "func_globalinit failed");
  175. goto fail1;
  176. }
  177. }
  178. int num_inited_modules = 0;
  179. for (struct NCDModule *nm = group->modules; nm->type; nm++) {
  180. if (find_module(o, nm->type)) {
  181. BLog(BLOG_ERROR, "module type '%s' already exists", nm->type);
  182. goto loop_fail0;
  183. }
  184. if (strlen(nm->type) > NCDMODULEINDEX_MAX_TYPE_LEN) {
  185. BLog(BLOG_ERROR, "module type '%s' is too long (dump NCDMODULEINDEX_MAX_TYPE_LEN)", nm->type);
  186. goto loop_fail0;
  187. }
  188. if (o->num_modules == NCDMODULEINDEX_MAX_MODULES) {
  189. BLog(BLOG_ERROR, "too many modules (bump NCDMODULEINDEX_MAX_MODULES)");
  190. goto loop_fail0;
  191. }
  192. const char *base_type = (nm->base_type ? nm->base_type : nm->type);
  193. ASSERT(base_type)
  194. nm->base_type_id = NCDStringIndex_Get(string_index, base_type);
  195. if (nm->base_type_id < 0) {
  196. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  197. goto loop_fail0;
  198. }
  199. struct NCDModuleIndex_module *m = &o->modules[o->num_modules];
  200. strcpy(m->type, nm->type);
  201. m->module = nm;
  202. if (!add_method(m->type, nm, &o->method_index, &m->method_id)) {
  203. BLog(BLOG_ERROR, "failed to add method to method index");
  204. goto loop_fail0;
  205. }
  206. struct NCDModuleIndex_base_type *bt = find_base_type(o, base_type);
  207. if (bt) {
  208. if (bt->group != group) {
  209. BLog(BLOG_ERROR, "module base type '%s' already exists in another module group", base_type);
  210. goto loop_fail1;
  211. }
  212. } else {
  213. if (!(bt = malloc(sizeof(*bt)))) {
  214. BLog(BLOG_ERROR, "malloc failed");
  215. goto loop_fail1;
  216. }
  217. bt->base_type = base_type;
  218. bt->group = group;
  219. ASSERT_EXECUTE(BAVL_Insert(&o->base_types_tree, &bt->base_types_tree_node, NULL))
  220. }
  221. NCDModuleIndex__MHashRef ref = {m, o->num_modules};
  222. int res = NCDModuleIndex__MHash_Insert(&o->modules_hash, o->modules, ref, NULL);
  223. ASSERT_EXECUTE(res)
  224. o->num_modules++;
  225. num_inited_modules++;
  226. continue;
  227. loop_fail1:
  228. if (m->method_id >= 0) {
  229. NCDMethodIndex_RemoveMethod(&o->method_index, m->method_id);
  230. }
  231. loop_fail0:
  232. goto fail2;
  233. }
  234. return 1;
  235. fail2:
  236. while (num_inited_modules-- > 0) {
  237. o->num_modules--;
  238. struct NCDModule *nm = group->modules + num_inited_modules;
  239. struct NCDModuleIndex_module *m = &o->modules[o->num_modules];
  240. NCDModuleIndex__MHashRef ref = {m, o->num_modules};
  241. NCDModuleIndex__MHash_Remove(&o->modules_hash, o->modules, ref);
  242. struct NCDModuleIndex_base_type *bt = find_base_type(o, (nm->base_type ? nm->base_type : nm->type));
  243. if (bt) {
  244. ASSERT(bt->group == group)
  245. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  246. free(bt);
  247. }
  248. if (m->method_id >= 0) {
  249. NCDMethodIndex_RemoveMethod(&o->method_index, m->method_id);
  250. }
  251. }
  252. if (group->func_globalfree) {
  253. group->func_globalfree();
  254. }
  255. fail1:
  256. LinkedList0_Remove(&o->groups_list, &ig->groups_list_node);
  257. BFree(ig);
  258. fail0:
  259. return 0;
  260. }
  261. const struct NCDModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const char *type)
  262. {
  263. DebugObject_Access(&o->d_obj);
  264. ASSERT(type)
  265. struct NCDModuleIndex_module *m = find_module(o, type);
  266. if (!m) {
  267. return NULL;
  268. }
  269. return m->module;
  270. }
  271. int NCDModuleIndex_GetMethodNameId (NCDModuleIndex *o, const char *method_name)
  272. {
  273. DebugObject_Access(&o->d_obj);
  274. ASSERT(method_name)
  275. return NCDMethodIndex_GetMethodNameId(&o->method_index, method_name);
  276. }
  277. const struct NCDModule * NCDModuleIndex_GetMethodModule (NCDModuleIndex *o, NCD_string_id_t obj_type, int method_name_id)
  278. {
  279. DebugObject_Access(&o->d_obj);
  280. return NCDMethodIndex_GetMethodModule(&o->method_index, obj_type, method_name_id);
  281. }