NCDModuleIndex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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/bsize.h>
  34. #include <misc/hashfun.h>
  35. #include <misc/compare.h>
  36. #include <misc/substring.h>
  37. #include <base/BLog.h>
  38. #include "NCDModuleIndex.h"
  39. #include <generated/blog_channel_NCDModuleIndex.h>
  40. #include "NCDModuleIndex_mhash.h"
  41. #include <structure/CHash_impl.h>
  42. static int string_pointer_comparator (void *user, void *v1, void *v2)
  43. {
  44. const char **s1 = v1;
  45. const char **s2 = v2;
  46. int cmp = strcmp(*s1, *s2);
  47. return B_COMPARE(cmp, 0);
  48. }
  49. static struct NCDModuleIndex_module * find_module (NCDModuleIndex *o, const char *type)
  50. {
  51. NCDModuleIndex__MHashRef ref = NCDModuleIndex__MHash_Lookup(&o->modules_hash, 0, type);
  52. ASSERT(!ref.link || !strcmp(ref.link->imodule.module.type, type))
  53. return ref.link;
  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 (const char *type, const struct NCDInterpModule *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. int method_id = NCDMethodIndex_AddMethod(method_index, type, pos, type + pos + search_len, module);
  86. if (method_id < 0) {
  87. BLog(BLOG_ERROR, "NCDMethodIndex_AddMethod failed");
  88. return 0;
  89. }
  90. *out_method_id = method_id;
  91. return 1;
  92. }
  93. int NCDModuleIndex_Init (NCDModuleIndex *o, NCDStringIndex *string_index)
  94. {
  95. ASSERT(string_index)
  96. // init modules hash
  97. if (!NCDModuleIndex__MHash_Init(&o->modules_hash, NCDMODULEINDEX_MODULES_HASH_SIZE)) {
  98. BLog(BLOG_ERROR, "NCDModuleIndex__MHash_Init failed");
  99. goto fail0;
  100. }
  101. #ifndef NDEBUG
  102. // init base types tree
  103. BAVL_Init(&o->base_types_tree, OFFSET_DIFF(struct NCDModuleIndex_base_type, base_type, base_types_tree_node), string_pointer_comparator, NULL);
  104. #endif
  105. // init groups list
  106. LinkedList0_Init(&o->groups_list);
  107. // init method index
  108. if (!NCDMethodIndex_Init(&o->method_index, string_index)) {
  109. BLog(BLOG_ERROR, "NCDMethodIndex_Init failed");
  110. goto fail1;
  111. }
  112. DebugObject_Init(&o->d_obj);
  113. return 1;
  114. fail1:
  115. NCDModuleIndex__MHash_Free(&o->modules_hash);
  116. fail0:
  117. return 0;
  118. }
  119. void NCDModuleIndex_Free (NCDModuleIndex *o)
  120. {
  121. DebugObject_Free(&o->d_obj);
  122. // free groups
  123. LinkedList0Node *ln;
  124. while (ln = LinkedList0_GetFirst(&o->groups_list)) {
  125. struct NCDModuleIndex_group *ig = UPPER_OBJECT(ln, struct NCDModuleIndex_group, groups_list_node);
  126. if (ig->igroup.group.func_globalfree) {
  127. ig->igroup.group.func_globalfree(&ig->igroup);
  128. }
  129. BFree(ig->igroup.strings);
  130. LinkedList0_Remove(&o->groups_list, &ig->groups_list_node);
  131. BFree(ig);
  132. }
  133. #ifndef NDEBUG
  134. // free base types
  135. BAVLNode *tn;
  136. while (tn = BAVL_GetFirst(&o->base_types_tree)) {
  137. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(tn, struct NCDModuleIndex_base_type, base_types_tree_node);
  138. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  139. BFree(bt);
  140. }
  141. #endif
  142. // free method index
  143. NCDMethodIndex_Free(&o->method_index);
  144. // free modules hash
  145. NCDModuleIndex__MHash_Free(&o->modules_hash);
  146. }
  147. int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group, const struct NCDModuleInst_iparams *iparams, NCDStringIndex *string_index)
  148. {
  149. DebugObject_Access(&o->d_obj);
  150. ASSERT(group)
  151. ASSERT(iparams)
  152. ASSERT(string_index)
  153. // count modules in the group
  154. size_t num_modules = 0;
  155. while (group->modules[num_modules].type) {
  156. num_modules++;
  157. }
  158. // compute allocation size
  159. bsize_t size = bsize_add(bsize_fromsize(sizeof(struct NCDModuleIndex_group)), bsize_mul(bsize_fromsize(num_modules), bsize_fromsize(sizeof(struct NCDModuleIndex_module))));
  160. // allocate group
  161. struct NCDModuleIndex_group *ig = BAllocSize(size);
  162. if (!ig) {
  163. BLog(BLOG_ERROR, "BAllocSize failed");
  164. goto fail0;
  165. }
  166. // insert to groups list
  167. LinkedList0_Prepend(&o->groups_list, &ig->groups_list_node);
  168. // copy NCDModuleGroup
  169. ig->igroup.group = *group;
  170. if (!group->strings) {
  171. // not resolving strings
  172. ig->igroup.strings = NULL;
  173. } else {
  174. // compute number of strings
  175. size_t num_strings = 0;
  176. while (group->strings[num_strings]) {
  177. num_strings++;
  178. }
  179. // allocate array for string IDs
  180. ig->igroup.strings = BAllocArray(num_strings, sizeof(ig->igroup.strings[0]));
  181. if (!ig->igroup.strings) {
  182. BLog(BLOG_ERROR, "BAllocArray failed");
  183. goto fail1;
  184. }
  185. // map strings to IDs
  186. for (size_t i = 0; i < num_strings; i++) {
  187. ig->igroup.strings[i] = NCDStringIndex_Get(string_index, group->strings[i]);
  188. if (ig->igroup.strings[i] < 0) {
  189. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  190. goto fail2;
  191. }
  192. }
  193. }
  194. // call group init function
  195. if (group->func_globalinit) {
  196. if (!group->func_globalinit(&ig->igroup, iparams)) {
  197. BLog(BLOG_ERROR, "func_globalinit failed");
  198. goto fail2;
  199. }
  200. }
  201. size_t num_inited_modules = 0;
  202. // initialize modules
  203. for (size_t i = 0; i < num_modules; i++) {
  204. const struct NCDModule *nm = &group->modules[i];
  205. struct NCDModuleIndex_module *m = &ig->modules[i];
  206. // make sure a module with this name doesn't exist already
  207. if (find_module(o, nm->type)) {
  208. BLog(BLOG_ERROR, "module type '%s' already exists", nm->type);
  209. goto loop_fail0;
  210. }
  211. // copy NCDModule structure
  212. m->imodule.module = *nm;
  213. // determine base type
  214. const char *base_type = (nm->base_type ? nm->base_type : nm->type);
  215. ASSERT(base_type)
  216. // map base type to ID
  217. m->imodule.base_type_id = NCDStringIndex_Get(string_index, base_type);
  218. if (m->imodule.base_type_id < 0) {
  219. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  220. goto loop_fail0;
  221. }
  222. // set group pointer
  223. m->imodule.group = &ig->igroup;
  224. // register method
  225. if (!add_method(nm->type, &m->imodule, &o->method_index, &m->method_id)) {
  226. goto loop_fail0;
  227. }
  228. #ifndef NDEBUG
  229. // ensure that this base_type does not appear in any other groups
  230. struct NCDModuleIndex_base_type *bt = find_base_type(o, base_type);
  231. if (bt) {
  232. if (bt->group != ig) {
  233. BLog(BLOG_ERROR, "module base type '%s' already exists in another module group", base_type);
  234. goto loop_fail1;
  235. }
  236. } else {
  237. if (!(bt = BAlloc(sizeof(*bt)))) {
  238. BLog(BLOG_ERROR, "BAlloc failed");
  239. goto loop_fail1;
  240. }
  241. bt->base_type = base_type;
  242. bt->group = ig;
  243. ASSERT_EXECUTE(BAVL_Insert(&o->base_types_tree, &bt->base_types_tree_node, NULL))
  244. }
  245. #endif
  246. // insert to modules hash
  247. NCDModuleIndex__MHashRef ref = {m, m};
  248. int res = NCDModuleIndex__MHash_Insert(&o->modules_hash, 0, ref, NULL);
  249. ASSERT_EXECUTE(res)
  250. num_inited_modules++;
  251. continue;
  252. #ifndef NDEBUG
  253. loop_fail1:
  254. if (m->method_id >= 0) {
  255. NCDMethodIndex_RemoveMethod(&o->method_index, m->method_id);
  256. }
  257. #endif
  258. loop_fail0:
  259. goto fail3;
  260. }
  261. return 1;
  262. fail3:
  263. while (num_inited_modules-- > 0) {
  264. struct NCDModuleIndex_module *m = &ig->modules[num_inited_modules];
  265. NCDModuleIndex__MHashRef ref = {m, m};
  266. NCDModuleIndex__MHash_Remove(&o->modules_hash, 0, ref);
  267. #ifndef NDEBUG
  268. const struct NCDModule *nm = &group->modules[num_inited_modules];
  269. const char *base_type = (nm->base_type ? nm->base_type : nm->type);
  270. struct NCDModuleIndex_base_type *bt = find_base_type(o, base_type);
  271. if (bt) {
  272. ASSERT(bt->group == ig)
  273. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  274. BFree(bt);
  275. }
  276. #endif
  277. if (m->method_id >= 0) {
  278. NCDMethodIndex_RemoveMethod(&o->method_index, m->method_id);
  279. }
  280. }
  281. if (group->func_globalfree) {
  282. group->func_globalfree(&ig->igroup);
  283. }
  284. fail2:
  285. BFree(ig->igroup.strings);
  286. fail1:
  287. LinkedList0_Remove(&o->groups_list, &ig->groups_list_node);
  288. BFree(ig);
  289. fail0:
  290. return 0;
  291. }
  292. const struct NCDInterpModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const char *type)
  293. {
  294. DebugObject_Access(&o->d_obj);
  295. ASSERT(type)
  296. struct NCDModuleIndex_module *m = find_module(o, type);
  297. if (!m) {
  298. return NULL;
  299. }
  300. return &m->imodule;
  301. }
  302. int NCDModuleIndex_GetMethodNameId (NCDModuleIndex *o, const char *method_name)
  303. {
  304. DebugObject_Access(&o->d_obj);
  305. ASSERT(method_name)
  306. return NCDMethodIndex_GetMethodNameId(&o->method_index, method_name);
  307. }
  308. const struct NCDInterpModule * NCDModuleIndex_GetMethodModule (NCDModuleIndex *o, NCD_string_id_t obj_type, int method_name_id)
  309. {
  310. DebugObject_Access(&o->d_obj);
  311. return NCDMethodIndex_GetMethodModule(&o->method_index, obj_type, method_name_id);
  312. }