dynamic_depend.c 13 KB

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