dynamic_depend.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. const char *name_str = NCDVal_StringData(name_arg);
  267. size_t name_len = NCDVal_StringLength(name_arg);
  268. // find name, create new if needed
  269. struct name *n = find_name(g, name_str, name_len);
  270. if (!n && !(n = name_init(i, g, name_str, name_len))) {
  271. goto fail0;
  272. }
  273. // set name
  274. o->n = n;
  275. // check for order value conflict
  276. if (BAVL_LookupExact(&n->provides_tree, &o->order_value)) {
  277. ModuleLog(i, BLOG_ERROR, "order value already exists");
  278. goto fail0;
  279. }
  280. // add to name's provides tree
  281. ASSERT_EXECUTE(BAVL_Insert(&n->provides_tree, &o->provides_tree_node, NULL))
  282. // set not dying
  283. o->dying = 0;
  284. // signal up
  285. NCDModuleInst_Backend_Up(i);
  286. // should this be the current provide?
  287. if (o == name_get_first_provide(n)) {
  288. if (!n->cur_p) {
  289. name_new_current(n);
  290. }
  291. else if (!n->cur_resetting) {
  292. name_start_resetting(n);
  293. }
  294. }
  295. return;
  296. fail0:
  297. NCDModuleInst_Backend_DeadError(i);
  298. }
  299. static void provide_free (struct provide *o)
  300. {
  301. struct name *n = o->n;
  302. ASSERT(o->dying)
  303. ASSERT(o != n->cur_p)
  304. // remove from name's provides tree
  305. BAVL_Remove(&n->provides_tree, &o->provides_tree_node);
  306. NCDModuleInst_Backend_Dead(o->i);
  307. }
  308. static void provide_func_die (void *vo)
  309. {
  310. struct provide *o = vo;
  311. struct name *n = o->n;
  312. ASSERT(!o->dying)
  313. // set dying
  314. o->dying = 1;
  315. // if this is not the current provide, die right away
  316. if (o != n->cur_p) {
  317. // free provide
  318. provide_free(o);
  319. // free name if unused
  320. name_free_if_unused(n);
  321. return;
  322. }
  323. ASSERT(!n->cur_resetting)
  324. // start resetting
  325. name_start_resetting(n);
  326. }
  327. static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  328. {
  329. struct global *g = ModuleGlobal(i);
  330. struct depend *o = vo;
  331. o->i = i;
  332. // read arguments
  333. NCDValRef name_arg;
  334. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  335. ModuleLog(i, BLOG_ERROR, "wrong arity");
  336. goto fail0;
  337. }
  338. if (!NCDVal_IsString(name_arg)) {
  339. ModuleLog(i, BLOG_ERROR, "wrong type");
  340. goto fail0;
  341. }
  342. const char *name_str = NCDVal_StringData(name_arg);
  343. size_t name_len = NCDVal_StringLength(name_arg);
  344. // find name, create new if needed
  345. struct name *n = find_name(g, name_str, name_len);
  346. if (!n && !(n = name_init(i, g, name_str, name_len))) {
  347. goto fail0;
  348. }
  349. // set name
  350. o->n = n;
  351. if (n->cur_p && !n->cur_resetting) {
  352. // set bound
  353. o->is_bound = 1;
  354. // add to bound depends list
  355. LinkedList0_Prepend(&n->cur_bound_depends_list, &o->depends_list_node);
  356. // signal up
  357. NCDModuleInst_Backend_Up(i);
  358. } else {
  359. // set not bound
  360. o->is_bound = 0;
  361. // add to waiting depends list
  362. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  363. }
  364. return;
  365. fail0:
  366. NCDModuleInst_Backend_DeadError(i);
  367. }
  368. static void depend_func_die (void *vo)
  369. {
  370. struct depend *o = vo;
  371. struct name *n = o->n;
  372. if (o->is_bound) {
  373. ASSERT(n->cur_p)
  374. // remove from bound depends list
  375. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  376. // continue resetting
  377. if (n->cur_resetting) {
  378. name_continue_resetting(n);
  379. }
  380. } else {
  381. // remove from waiting depends list
  382. LinkedList0_Remove(&n->waiting_depends_list, &o->depends_list_node);
  383. // free name if unused
  384. name_free_if_unused(n);
  385. }
  386. NCDModuleInst_Backend_Dead(o->i);
  387. }
  388. static void depend_func_clean (void *vo)
  389. {
  390. struct depend *o = vo;
  391. struct name *n = o->n;
  392. ASSERT(!o->is_bound || n->cur_p)
  393. if (!(o->is_bound && n->cur_resetting)) {
  394. return;
  395. }
  396. // remove from bound depends list
  397. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  398. // set not bound
  399. o->is_bound = 0;
  400. // add to waiting depends list
  401. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  402. // continue resetting
  403. name_continue_resetting(n);
  404. }
  405. static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  406. {
  407. struct depend *o = vo;
  408. struct name *n = o->n;
  409. ASSERT(!o->is_bound || n->cur_p)
  410. if (!o->is_bound) {
  411. return 0;
  412. }
  413. return NCDModuleInst_Backend_GetObj(n->cur_p->i, objname, out_object);
  414. }
  415. static struct NCDModule modules[] = {
  416. {
  417. .type = "dynamic_provide",
  418. .func_new2 = provide_func_new,
  419. .func_die = provide_func_die,
  420. .alloc_size = sizeof(struct provide)
  421. }, {
  422. .type = "dynamic_depend",
  423. .func_new2 = depend_func_new,
  424. .func_die = depend_func_die,
  425. .func_clean = depend_func_clean,
  426. .func_getobj = depend_func_getobj,
  427. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  428. .alloc_size = sizeof(struct depend)
  429. }, {
  430. .type = NULL
  431. }
  432. };
  433. const struct NCDModuleGroup ncdmodule_dynamic_depend = {
  434. .func_globalinit = func_globalinit,
  435. .func_globalfree = func_globalfree,
  436. .modules = modules
  437. };