NCDModuleIndex.c 11 KB

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