dynamic_depend.c 14 KB

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