dynamic_depend.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /**
  2. * @file dynamic_depend.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. * @section DESCRIPTION
  30. *
  31. * Dynamic dependencies module.
  32. *
  33. * Synopsis: dynamic_provide(string name, order_value)
  34. * Synopsis: dynamic_depend(string name)
  35. */
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <misc/offset.h>
  39. #include <misc/debug.h>
  40. #include <misc/compare.h>
  41. #include <misc/strdup.h>
  42. #include <misc/balloc.h>
  43. #include <structure/LinkedList0.h>
  44. #include <structure/BAVL.h>
  45. #include <ncd/module_common.h>
  46. #include <generated/blog_channel_ncd_dynamic_depend.h>
  47. struct provide;
  48. struct name_string {
  49. char *data;
  50. size_t len;
  51. };
  52. struct name {
  53. struct global *g;
  54. struct name_string name;
  55. BAVLNode names_tree_node;
  56. BAVL provides_tree;
  57. LinkedList0 waiting_depends_list;
  58. struct provide *cur_p;
  59. LinkedList0 cur_bound_depends_list;
  60. int cur_resetting;
  61. };
  62. struct provide {
  63. NCDModuleInst *i;
  64. struct name *n;
  65. NCDValRef order_value;
  66. BAVLNode provides_tree_node;
  67. int dying;
  68. };
  69. struct depend {
  70. NCDModuleInst *i;
  71. struct name *n;
  72. int is_bound;
  73. LinkedList0Node depends_list_node;
  74. };
  75. struct global {
  76. BAVL names_tree;
  77. };
  78. static void provide_free (struct provide *o);
  79. static void depend_free (struct depend *o);
  80. static int name_string_comparator (void *user, void *vv1, void *vv2)
  81. {
  82. struct name_string *v1 = vv1;
  83. struct name_string *v2 = vv2;
  84. size_t min_len = (v1->len < v2->len ? v1->len : v2->len);
  85. int cmp = memcmp(v1->data, v2->data, min_len);
  86. if (cmp) {
  87. return B_COMPARE(cmp, 0);
  88. }
  89. return B_COMPARE(v1->len, v2->len);
  90. }
  91. static int val_comparator (void *user, NCDValRef *v1, NCDValRef *v2)
  92. {
  93. return NCDVal_Compare(*v1, *v2);
  94. }
  95. static struct name * find_name (struct global *g, const char *name, size_t name_len)
  96. {
  97. struct name_string ns = {(char *)name, name_len};
  98. BAVLNode *tn = BAVL_LookupExact(&g->names_tree, &ns);
  99. if (!tn) {
  100. return NULL;
  101. }
  102. struct name *n = UPPER_OBJECT(tn, struct name, names_tree_node);
  103. ASSERT(n->name.len == name_len)
  104. ASSERT(!memcmp(n->name.data, name, name_len))
  105. return n;
  106. }
  107. static struct name * name_init (NCDModuleInst *i, struct global *g, const char *name, size_t name_len)
  108. {
  109. ASSERT(!find_name(g, name, name_len))
  110. // allocate structure
  111. struct name *o = malloc(sizeof(*o));
  112. if (!o) {
  113. ModuleLog(i, BLOG_ERROR, "malloc failed");
  114. goto fail0;
  115. }
  116. // set global state
  117. o->g = g;
  118. // copy name
  119. if (!(o->name.data = b_strdup_bin(name, name_len))) {
  120. ModuleLog(i, BLOG_ERROR, "strdup failed");
  121. goto fail1;
  122. }
  123. o->name.len = name_len;
  124. // insert to names tree
  125. ASSERT_EXECUTE(BAVL_Insert(&g->names_tree, &o->names_tree_node, NULL))
  126. // init provides tree
  127. BAVL_Init(&o->provides_tree, OFFSET_DIFF(struct provide, order_value, provides_tree_node), (BAVL_comparator)val_comparator, NULL);
  128. // init waiting depends list
  129. LinkedList0_Init(&o->waiting_depends_list);
  130. // set no current provide
  131. o->cur_p = NULL;
  132. return o;
  133. fail1:
  134. free(o);
  135. fail0:
  136. return NULL;
  137. }
  138. static void name_free (struct name *o)
  139. {
  140. ASSERT(BAVL_IsEmpty(&o->provides_tree))
  141. ASSERT(LinkedList0_IsEmpty(&o->waiting_depends_list))
  142. ASSERT(!o->cur_p)
  143. // remove from names tree
  144. BAVL_Remove(&o->g->names_tree, &o->names_tree_node);
  145. // free name
  146. free(o->name.data);
  147. // free structure
  148. free(o);
  149. }
  150. static struct provide * name_get_first_provide (struct name *o)
  151. {
  152. BAVLNode *tn = BAVL_GetFirst(&o->provides_tree);
  153. if (!tn) {
  154. return NULL;
  155. }
  156. struct provide *p = UPPER_OBJECT(tn, struct provide, provides_tree_node);
  157. ASSERT(p->n == o)
  158. return p;
  159. }
  160. static void name_new_current (struct name *o)
  161. {
  162. ASSERT(!o->cur_p)
  163. ASSERT(!BAVL_IsEmpty(&o->provides_tree))
  164. // set current provide
  165. o->cur_p = name_get_first_provide(o);
  166. // init bound depends list
  167. LinkedList0_Init(&o->cur_bound_depends_list);
  168. // set not resetting
  169. o->cur_resetting = 0;
  170. // bind waiting depends
  171. while (!LinkedList0_IsEmpty(&o->waiting_depends_list)) {
  172. struct depend *d = UPPER_OBJECT(LinkedList0_GetFirst(&o->waiting_depends_list), struct depend, depends_list_node);
  173. ASSERT(d->n == o)
  174. ASSERT(!d->is_bound)
  175. // remove from waiting depends list
  176. LinkedList0_Remove(&o->waiting_depends_list, &d->depends_list_node);
  177. // set bound
  178. d->is_bound = 1;
  179. // add to bound depends list
  180. LinkedList0_Prepend(&o->cur_bound_depends_list, &d->depends_list_node);
  181. // signal up
  182. NCDModuleInst_Backend_Up(d->i);
  183. }
  184. }
  185. static void name_free_if_unused (struct name *o)
  186. {
  187. if (BAVL_IsEmpty(&o->provides_tree) && LinkedList0_IsEmpty(&o->waiting_depends_list)) {
  188. name_free(o);
  189. }
  190. }
  191. static void name_continue_resetting (struct name *o)
  192. {
  193. ASSERT(o->cur_p)
  194. ASSERT(o->cur_resetting)
  195. // still have bound depends?
  196. if (!LinkedList0_IsEmpty(&o->cur_bound_depends_list)) {
  197. return;
  198. }
  199. struct provide *old_p = o->cur_p;
  200. // set no current provide
  201. o->cur_p = NULL;
  202. // free old current provide if it's dying
  203. if (old_p->dying) {
  204. provide_free(old_p);
  205. }
  206. if (!BAVL_IsEmpty(&o->provides_tree)) {
  207. // get new current provide
  208. name_new_current(o);
  209. } else {
  210. // free name if unused
  211. name_free_if_unused(o);
  212. }
  213. }
  214. static void name_start_resetting (struct name *o)
  215. {
  216. ASSERT(o->cur_p)
  217. ASSERT(!o->cur_resetting)
  218. // set resetting
  219. o->cur_resetting = 1;
  220. // signal bound depends down
  221. for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->cur_bound_depends_list); ln; ln = LinkedList0Node_Next(ln)) {
  222. struct depend *d = UPPER_OBJECT(ln, struct depend, depends_list_node);
  223. ASSERT(d->n == o)
  224. ASSERT(d->is_bound)
  225. NCDModuleInst_Backend_Down(d->i);
  226. }
  227. // if there were no bound depends, continue right away
  228. name_continue_resetting(o);
  229. }
  230. static int func_globalinit (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params)
  231. {
  232. // allocate global state structure
  233. struct global *g = BAlloc(sizeof(*g));
  234. if (!g) {
  235. BLog(BLOG_ERROR, "BAlloc failed");
  236. return 0;
  237. }
  238. // set group state pointer
  239. group->group_state = g;
  240. // init names tree
  241. BAVL_Init(&g->names_tree, OFFSET_DIFF(struct name, name, names_tree_node), name_string_comparator, NULL);
  242. return 1;
  243. }
  244. static void func_globalfree (struct NCDInterpModuleGroup *group)
  245. {
  246. struct global *g = group->group_state;
  247. ASSERT(BAVL_IsEmpty(&g->names_tree))
  248. // free global state structure
  249. BFree(g);
  250. }
  251. static void provide_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  252. {
  253. struct global *g = ModuleGlobal(i);
  254. struct provide *o = vo;
  255. o->i = i;
  256. // read arguments
  257. NCDValRef name_arg;
  258. if (!NCDVal_ListRead(params->args, 2, &name_arg, &o->order_value)) {
  259. ModuleLog(i, BLOG_ERROR, "wrong arity");
  260. goto fail0;
  261. }
  262. if (!NCDVal_IsString(name_arg)) {
  263. ModuleLog(i, BLOG_ERROR, "wrong type");
  264. goto fail0;
  265. }
  266. MemRef name = NCDVal_StringMemRef(name_arg);
  267. // find name, create new if needed
  268. struct name *n = find_name(g, name.ptr, name.len);
  269. if (!n && !(n = name_init(i, g, name.ptr, name.len))) {
  270. goto fail0;
  271. }
  272. // set name
  273. o->n = n;
  274. // check for order value conflict
  275. if (BAVL_LookupExact(&n->provides_tree, &o->order_value)) {
  276. ModuleLog(i, BLOG_ERROR, "order value already exists");
  277. goto fail0;
  278. }
  279. // add to name's provides tree
  280. ASSERT_EXECUTE(BAVL_Insert(&n->provides_tree, &o->provides_tree_node, NULL))
  281. // set not dying
  282. o->dying = 0;
  283. // signal up
  284. NCDModuleInst_Backend_Up(i);
  285. // should this be the current provide?
  286. if (o == name_get_first_provide(n)) {
  287. if (!n->cur_p) {
  288. name_new_current(n);
  289. }
  290. else if (!n->cur_resetting) {
  291. name_start_resetting(n);
  292. }
  293. }
  294. return;
  295. fail0:
  296. NCDModuleInst_Backend_DeadError(i);
  297. }
  298. static void provide_free (struct provide *o)
  299. {
  300. struct name *n = o->n;
  301. ASSERT(o->dying)
  302. ASSERT(o != n->cur_p)
  303. // remove from name's provides tree
  304. BAVL_Remove(&n->provides_tree, &o->provides_tree_node);
  305. NCDModuleInst_Backend_Dead(o->i);
  306. }
  307. static void provide_func_die (void *vo)
  308. {
  309. struct provide *o = vo;
  310. struct name *n = o->n;
  311. ASSERT(!o->dying)
  312. // set dying
  313. o->dying = 1;
  314. // if this is not the current provide, die right away
  315. if (o != n->cur_p) {
  316. // free provide
  317. provide_free(o);
  318. // free name if unused
  319. name_free_if_unused(n);
  320. return;
  321. }
  322. ASSERT(!n->cur_resetting)
  323. // start resetting
  324. name_start_resetting(n);
  325. }
  326. static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  327. {
  328. struct global *g = ModuleGlobal(i);
  329. struct depend *o = vo;
  330. o->i = i;
  331. // read arguments
  332. NCDValRef name_arg;
  333. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  334. ModuleLog(i, BLOG_ERROR, "wrong arity");
  335. goto fail0;
  336. }
  337. if (!NCDVal_IsString(name_arg)) {
  338. ModuleLog(i, BLOG_ERROR, "wrong type");
  339. goto fail0;
  340. }
  341. MemRef name = NCDVal_StringMemRef(name_arg);
  342. // find name, create new if needed
  343. struct name *n = find_name(g, name.ptr, name.len);
  344. if (!n && !(n = name_init(i, g, name.ptr, name.len))) {
  345. goto fail0;
  346. }
  347. // set name
  348. o->n = n;
  349. if (n->cur_p && !n->cur_resetting) {
  350. // set bound
  351. o->is_bound = 1;
  352. // add to bound depends list
  353. LinkedList0_Prepend(&n->cur_bound_depends_list, &o->depends_list_node);
  354. // signal up
  355. NCDModuleInst_Backend_Up(i);
  356. } else {
  357. // set not bound
  358. o->is_bound = 0;
  359. // add to waiting depends list
  360. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  361. }
  362. return;
  363. fail0:
  364. NCDModuleInst_Backend_DeadError(i);
  365. }
  366. static void depend_func_die (void *vo)
  367. {
  368. struct depend *o = vo;
  369. struct name *n = o->n;
  370. if (o->is_bound) {
  371. ASSERT(n->cur_p)
  372. // remove from bound depends list
  373. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  374. // continue resetting
  375. if (n->cur_resetting) {
  376. name_continue_resetting(n);
  377. }
  378. } else {
  379. // remove from waiting depends list
  380. LinkedList0_Remove(&n->waiting_depends_list, &o->depends_list_node);
  381. // free name if unused
  382. name_free_if_unused(n);
  383. }
  384. NCDModuleInst_Backend_Dead(o->i);
  385. }
  386. static void depend_func_clean (void *vo)
  387. {
  388. struct depend *o = vo;
  389. struct name *n = o->n;
  390. ASSERT(!o->is_bound || n->cur_p)
  391. if (!(o->is_bound && n->cur_resetting)) {
  392. return;
  393. }
  394. // remove from bound depends list
  395. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  396. // set not bound
  397. o->is_bound = 0;
  398. // add to waiting depends list
  399. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  400. // continue resetting
  401. name_continue_resetting(n);
  402. }
  403. static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  404. {
  405. struct depend *o = vo;
  406. struct name *n = o->n;
  407. ASSERT(!o->is_bound || n->cur_p)
  408. if (!o->is_bound) {
  409. return 0;
  410. }
  411. return NCDModuleInst_Backend_GetObj(n->cur_p->i, objname, out_object);
  412. }
  413. static struct NCDModule modules[] = {
  414. {
  415. .type = "dynamic_provide",
  416. .func_new2 = provide_func_new,
  417. .func_die = provide_func_die,
  418. .alloc_size = sizeof(struct provide)
  419. }, {
  420. .type = "dynamic_depend",
  421. .func_new2 = depend_func_new,
  422. .func_die = depend_func_die,
  423. .func_clean = depend_func_clean,
  424. .func_getobj = depend_func_getobj,
  425. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  426. .alloc_size = sizeof(struct depend)
  427. }, {
  428. .type = NULL
  429. }
  430. };
  431. const struct NCDModuleGroup ncdmodule_dynamic_depend = {
  432. .func_globalinit = func_globalinit,
  433. .func_globalfree = func_globalfree,
  434. .modules = modules
  435. };