NCDModuleIndex.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. #include "NCDModuleIndex_func_vec.h"
  43. #include <structure/Vector_impl.h>
  44. #include "NCDModuleIndex_fhash.h"
  45. #include <structure/CHash_impl.h>
  46. static int string_pointer_comparator (void *user, void *v1, void *v2)
  47. {
  48. const char **s1 = v1;
  49. const char **s2 = v2;
  50. int cmp = strcmp(*s1, *s2);
  51. return B_COMPARE(cmp, 0);
  52. }
  53. static struct NCDModuleIndex_module * find_module (NCDModuleIndex *o, const char *type)
  54. {
  55. NCDModuleIndex__MHashRef ref = NCDModuleIndex__MHash_Lookup(&o->modules_hash, 0, type);
  56. ASSERT(!ref.link || !strcmp(ref.link->imodule.module.type, type))
  57. return ref.link;
  58. }
  59. #ifndef NDEBUG
  60. static struct NCDModuleIndex_base_type * find_base_type (NCDModuleIndex *o, const char *base_type)
  61. {
  62. BAVLNode *node = BAVL_LookupExact(&o->base_types_tree, &base_type);
  63. if (!node) {
  64. return NULL;
  65. }
  66. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(node, struct NCDModuleIndex_base_type, base_types_tree_node);
  67. ASSERT(!strcmp(bt->base_type, base_type))
  68. return bt;
  69. }
  70. #endif
  71. static int add_method (const char *type, const struct NCDInterpModule *module, NCDMethodIndex *method_index, int *out_method_id)
  72. {
  73. ASSERT(type)
  74. ASSERT(module)
  75. ASSERT(method_index)
  76. ASSERT(out_method_id)
  77. const char search[] = "::";
  78. size_t search_len = sizeof(search) - 1;
  79. size_t table[sizeof(search) - 1];
  80. build_substring_backtrack_table_reverse(MemRef_Make(search, search_len), table);
  81. size_t pos;
  82. if (!find_substring_reverse(MemRef_MakeCstr(type), MemRef_Make(search, search_len), table, &pos)) {
  83. *out_method_id = -1;
  84. return 1;
  85. }
  86. ASSERT(pos >= 0)
  87. ASSERT(pos <= strlen(type) - search_len)
  88. ASSERT(!memcmp(type + pos, search, search_len))
  89. int method_id = NCDMethodIndex_AddMethod(method_index, type, pos, type + pos + search_len, module);
  90. if (method_id < 0) {
  91. BLog(BLOG_ERROR, "NCDMethodIndex_AddMethod failed");
  92. return 0;
  93. }
  94. *out_method_id = method_id;
  95. return 1;
  96. }
  97. int NCDModuleIndex_Init (NCDModuleIndex *o, NCDStringIndex *string_index)
  98. {
  99. ASSERT(string_index)
  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 fail0;
  104. }
  105. #ifndef NDEBUG
  106. // init base types tree
  107. BAVL_Init(&o->base_types_tree, OFFSET_DIFF(struct NCDModuleIndex_base_type, base_type, base_types_tree_node), string_pointer_comparator, NULL);
  108. #endif
  109. // init groups list
  110. LinkedList0_Init(&o->groups_list);
  111. // init method index
  112. if (!NCDMethodIndex_Init(&o->method_index, string_index)) {
  113. BLog(BLOG_ERROR, "NCDMethodIndex_Init failed");
  114. goto fail1;
  115. }
  116. // init functions vector
  117. if (!NCDModuleIndex__FuncVec_Init(&o->func_vec, NCDMODULEINDEX_FUNCTIONS_VEC_INITIAL_SIZE)) {
  118. BLog(BLOG_ERROR, "NCDModuleIndex__FuncVec_Init failed");
  119. goto fail2;
  120. }
  121. // init functions hash
  122. if (!NCDModuleIndex__FuncHash_Init(&o->func_hash, NCDMODULEINDEX_FUNCTIONS_HASH_SIZE)) {
  123. BLog(BLOG_ERROR, "NCDModuleIndex__FuncHash_Init failed");
  124. goto fail3;
  125. }
  126. DebugObject_Init(&o->d_obj);
  127. return 1;
  128. fail3:
  129. NCDModuleIndex__FuncVec_Free(&o->func_vec);
  130. fail2:
  131. NCDMethodIndex_Free(&o->method_index);
  132. fail1:
  133. NCDModuleIndex__MHash_Free(&o->modules_hash);
  134. fail0:
  135. return 0;
  136. }
  137. void NCDModuleIndex_Free (NCDModuleIndex *o)
  138. {
  139. DebugObject_Free(&o->d_obj);
  140. // free groups
  141. LinkedList0Node *ln;
  142. while (ln = LinkedList0_GetFirst(&o->groups_list)) {
  143. struct NCDModuleIndex_group *ig = UPPER_OBJECT(ln, struct NCDModuleIndex_group, groups_list_node);
  144. if (ig->igroup.group.func_globalfree) {
  145. ig->igroup.group.func_globalfree(&ig->igroup);
  146. }
  147. BFree(ig->igroup.strings);
  148. LinkedList0_Remove(&o->groups_list, &ig->groups_list_node);
  149. BFree(ig);
  150. }
  151. #ifndef NDEBUG
  152. // free base types
  153. BAVLNode *tn;
  154. while (tn = BAVL_GetFirst(&o->base_types_tree)) {
  155. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(tn, struct NCDModuleIndex_base_type, base_types_tree_node);
  156. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  157. BFree(bt);
  158. }
  159. #endif
  160. // free functions hash
  161. NCDModuleIndex__FuncHash_Free(&o->func_hash);
  162. // free functions vector
  163. NCDModuleIndex__FuncVec_Free(&o->func_vec);
  164. // free method index
  165. NCDMethodIndex_Free(&o->method_index);
  166. // free modules hash
  167. NCDModuleIndex__MHash_Free(&o->modules_hash);
  168. }
  169. int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group, const struct NCDModuleInst_iparams *iparams, NCDStringIndex *string_index)
  170. {
  171. DebugObject_Access(&o->d_obj);
  172. ASSERT(group)
  173. ASSERT(iparams)
  174. ASSERT(string_index)
  175. // count modules in the group
  176. size_t num_modules = 0;
  177. if (group->modules) {
  178. while (group->modules[num_modules].type) {
  179. num_modules++;
  180. }
  181. }
  182. // compute allocation size
  183. bsize_t size = bsize_add(bsize_fromsize(sizeof(struct NCDModuleIndex_group)), bsize_mul(bsize_fromsize(num_modules), bsize_fromsize(sizeof(struct NCDModuleIndex_module))));
  184. // allocate group
  185. struct NCDModuleIndex_group *ig = BAllocSize(size);
  186. if (!ig) {
  187. BLog(BLOG_ERROR, "BAllocSize failed");
  188. goto fail0;
  189. }
  190. // insert to groups list
  191. LinkedList0_Prepend(&o->groups_list, &ig->groups_list_node);
  192. // copy NCDModuleGroup
  193. ig->igroup.group = *group;
  194. if (!group->strings) {
  195. // not resolving strings
  196. ig->igroup.strings = NULL;
  197. } else {
  198. // compute number of strings
  199. size_t num_strings = 0;
  200. while (group->strings[num_strings]) {
  201. num_strings++;
  202. }
  203. // allocate array for string IDs
  204. ig->igroup.strings = BAllocArray(num_strings, sizeof(ig->igroup.strings[0]));
  205. if (!ig->igroup.strings) {
  206. BLog(BLOG_ERROR, "BAllocArray failed");
  207. goto fail1;
  208. }
  209. // map strings to IDs
  210. for (size_t i = 0; i < num_strings; i++) {
  211. ig->igroup.strings[i] = NCDStringIndex_Get(string_index, group->strings[i]);
  212. if (ig->igroup.strings[i] < 0) {
  213. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  214. goto fail2;
  215. }
  216. }
  217. }
  218. // call group init function
  219. if (group->func_globalinit) {
  220. if (!group->func_globalinit(&ig->igroup, iparams)) {
  221. BLog(BLOG_ERROR, "func_globalinit failed");
  222. goto fail2;
  223. }
  224. }
  225. size_t num_inited_modules = 0;
  226. // initialize modules
  227. if (group->modules) {
  228. for (size_t i = 0; i < num_modules; i++) {
  229. const struct NCDModule *nm = &group->modules[i];
  230. struct NCDModuleIndex_module *m = &ig->modules[i];
  231. // make sure a module with this name doesn't exist already
  232. if (find_module(o, nm->type)) {
  233. BLog(BLOG_ERROR, "module type '%s' already exists", nm->type);
  234. goto loop_fail0;
  235. }
  236. // copy NCDModule structure
  237. m->imodule.module = *nm;
  238. // determine base type
  239. const char *base_type = (nm->base_type ? nm->base_type : nm->type);
  240. ASSERT(base_type)
  241. // map base type to ID
  242. m->imodule.base_type_id = NCDStringIndex_Get(string_index, base_type);
  243. if (m->imodule.base_type_id < 0) {
  244. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  245. goto loop_fail0;
  246. }
  247. // set group pointer
  248. m->imodule.group = &ig->igroup;
  249. // register method
  250. if (!add_method(nm->type, &m->imodule, &o->method_index, &m->method_id)) {
  251. goto loop_fail0;
  252. }
  253. #ifndef NDEBUG
  254. // ensure that this base_type does not appear in any other groups
  255. struct NCDModuleIndex_base_type *bt = find_base_type(o, base_type);
  256. if (bt) {
  257. if (bt->group != ig) {
  258. BLog(BLOG_ERROR, "module base type '%s' already exists in another module group", base_type);
  259. goto loop_fail1;
  260. }
  261. } else {
  262. if (!(bt = BAlloc(sizeof(*bt)))) {
  263. BLog(BLOG_ERROR, "BAlloc failed");
  264. goto loop_fail1;
  265. }
  266. bt->base_type = base_type;
  267. bt->group = ig;
  268. ASSERT_EXECUTE(BAVL_Insert(&o->base_types_tree, &bt->base_types_tree_node, NULL))
  269. }
  270. #endif
  271. // insert to modules hash
  272. NCDModuleIndex__MHashRef ref = {m, m};
  273. int res = NCDModuleIndex__MHash_Insert(&o->modules_hash, 0, ref, NULL);
  274. ASSERT_EXECUTE(res)
  275. num_inited_modules++;
  276. continue;
  277. #ifndef NDEBUG
  278. loop_fail1:
  279. if (m->method_id >= 0) {
  280. NCDMethodIndex_RemoveMethod(&o->method_index, m->method_id);
  281. }
  282. #endif
  283. loop_fail0:
  284. goto fail3;
  285. }
  286. }
  287. size_t prev_func_count = NCDModuleIndex__FuncVec_Count(&o->func_vec);
  288. if (group->functions) {
  289. for (struct NCDModuleFunction const *mfunc = group->functions; mfunc->func_name; mfunc++) {
  290. NCD_string_id_t func_name_id = NCDStringIndex_Get(string_index, mfunc->func_name);
  291. if (func_name_id < 0) {
  292. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  293. goto fail4;
  294. }
  295. NCDModuleIndex__FuncHashRef lookup_ref = NCDModuleIndex__FuncHash_Lookup(&o->func_hash, o, func_name_id);
  296. if (lookup_ref.link >= 0) {
  297. BLog(BLOG_ERROR, "Function already exists: %s", mfunc->func_name);
  298. goto fail4;
  299. }
  300. size_t func_index;
  301. struct NCDModuleIndex__Func *func = NCDModuleIndex__FuncVec_Push(&o->func_vec, &func_index);
  302. if (!func) {
  303. BLog(BLOG_ERROR, "NCDModuleIndex__FuncVec_Push failed");
  304. goto fail4;
  305. }
  306. func->ifunc.function = *mfunc;
  307. func->ifunc.func_name_id = func_name_id;
  308. func->ifunc.group = &ig->igroup;
  309. NCDModuleIndex__FuncHashRef ref = {func, func_index};
  310. int res = NCDModuleIndex__FuncHash_Insert(&o->func_hash, o, ref, NULL);
  311. ASSERT_EXECUTE(res)
  312. }
  313. }
  314. return 1;
  315. fail4:
  316. while (NCDModuleIndex__FuncVec_Count(&o->func_vec) > prev_func_count) {
  317. size_t func_index;
  318. struct NCDModuleIndex__Func *func = NCDModuleIndex__FuncVec_Pop(&o->func_vec, &func_index);
  319. NCDModuleIndex__FuncHashRef ref = {func, func_index};
  320. NCDModuleIndex__FuncHash_Remove(&o->func_hash, o, ref);
  321. }
  322. fail3:
  323. while (num_inited_modules-- > 0) {
  324. struct NCDModuleIndex_module *m = &ig->modules[num_inited_modules];
  325. NCDModuleIndex__MHashRef ref = {m, m};
  326. NCDModuleIndex__MHash_Remove(&o->modules_hash, 0, ref);
  327. #ifndef NDEBUG
  328. const struct NCDModule *nm = &group->modules[num_inited_modules];
  329. const char *base_type = (nm->base_type ? nm->base_type : nm->type);
  330. struct NCDModuleIndex_base_type *bt = find_base_type(o, base_type);
  331. if (bt) {
  332. ASSERT(bt->group == ig)
  333. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  334. BFree(bt);
  335. }
  336. #endif
  337. if (m->method_id >= 0) {
  338. NCDMethodIndex_RemoveMethod(&o->method_index, m->method_id);
  339. }
  340. }
  341. if (group->func_globalfree) {
  342. group->func_globalfree(&ig->igroup);
  343. }
  344. fail2:
  345. BFree(ig->igroup.strings);
  346. fail1:
  347. LinkedList0_Remove(&o->groups_list, &ig->groups_list_node);
  348. BFree(ig);
  349. fail0:
  350. return 0;
  351. }
  352. const struct NCDInterpModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const char *type)
  353. {
  354. DebugObject_Access(&o->d_obj);
  355. ASSERT(type)
  356. struct NCDModuleIndex_module *m = find_module(o, type);
  357. if (!m) {
  358. return NULL;
  359. }
  360. return &m->imodule;
  361. }
  362. int NCDModuleIndex_GetMethodNameId (NCDModuleIndex *o, const char *method_name)
  363. {
  364. DebugObject_Access(&o->d_obj);
  365. ASSERT(method_name)
  366. return NCDMethodIndex_GetMethodNameId(&o->method_index, method_name);
  367. }
  368. const struct NCDInterpModule * NCDModuleIndex_GetMethodModule (NCDModuleIndex *o, NCD_string_id_t obj_type, int method_name_id)
  369. {
  370. DebugObject_Access(&o->d_obj);
  371. return NCDMethodIndex_GetMethodModule(&o->method_index, obj_type, method_name_id);
  372. }
  373. const struct NCDInterpFunction * NCDModuleIndex_FindFunction (NCDModuleIndex *o, NCD_string_id_t func_name_id)
  374. {
  375. DebugObject_Access(&o->d_obj);
  376. NCDModuleIndex__FuncHashRef lookup_ref = NCDModuleIndex__FuncHash_Lookup(&o->func_hash, o, func_name_id);
  377. if (lookup_ref.link < 0) {
  378. return NULL;
  379. }
  380. return &lookup_ref.ptr->ifunc;
  381. }