multidepend.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * @file multidepend.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. * Multiple-option dependencies module.
  32. *
  33. * Synopsis: multiprovide(string name)
  34. * Arguments:
  35. * name - provider identifier
  36. *
  37. * Synopsis: multidepend(list(string) names)
  38. * Arguments:
  39. * names - list of provider identifiers. The dependency is satisfied by any
  40. * provide statement with a provider identifier contained in this list.
  41. * The order of provider identifiers in the list specifies priority
  42. * (higher priority first).
  43. * Variables: Provides variables available from the corresponding provide,
  44. * ("modname.varname" or "modname").
  45. */
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <misc/offset.h>
  49. #include <misc/debug.h>
  50. #include <structure/LinkedList2.h>
  51. #include <ncd/NCDModule.h>
  52. #include <generated/blog_channel_ncd_multidepend.h>
  53. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  54. struct provide {
  55. NCDModuleInst *i;
  56. const char *name;
  57. LinkedList2Node provides_node;
  58. LinkedList2 depends;
  59. int dying;
  60. };
  61. struct depend {
  62. NCDModuleInst *i;
  63. NCDValRef names;
  64. LinkedList2Node depends_node;
  65. struct provide *provide;
  66. LinkedList2Node provide_node;
  67. int provide_collapsing;
  68. };
  69. static LinkedList2 provides;
  70. static LinkedList2 depends;
  71. static struct provide * find_provide (const char *name)
  72. {
  73. LinkedList2Iterator it;
  74. LinkedList2Iterator_InitForward(&it, &provides);
  75. LinkedList2Node *n;
  76. while (n = LinkedList2Iterator_Next(&it)) {
  77. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  78. if (!strcmp(p->name, name)) {
  79. LinkedList2Iterator_Free(&it);
  80. return p;
  81. }
  82. }
  83. return NULL;
  84. }
  85. static struct provide * depend_find_best_provide (struct depend *o)
  86. {
  87. size_t count = NCDVal_ListCount(o->names);
  88. for (size_t j = 0; j < count; j++) {
  89. NCDValRef e = NCDVal_ListGet(o->names, j);
  90. struct provide *p = find_provide(NCDVal_StringValue(e));
  91. if (p && !p->dying) {
  92. return p;
  93. }
  94. }
  95. return NULL;
  96. }
  97. static void depend_update (struct depend *o)
  98. {
  99. // if we're collapsing, do nothing
  100. if (o->provide && o->provide_collapsing) {
  101. return;
  102. }
  103. // find best provide
  104. struct provide *bp = depend_find_best_provide(o);
  105. ASSERT(!bp || !bp->dying)
  106. // has anything changed?
  107. if (bp == o->provide) {
  108. return;
  109. }
  110. if (o->provide) {
  111. // set collapsing
  112. o->provide_collapsing = 1;
  113. // signal down
  114. NCDModuleInst_Backend_Down(o->i);
  115. } else {
  116. // insert to provide's list
  117. LinkedList2_Append(&bp->depends, &o->provide_node);
  118. // set not collapsing
  119. o->provide_collapsing = 0;
  120. // set provide
  121. o->provide = bp;
  122. // signal up
  123. NCDModuleInst_Backend_Up(o->i);
  124. }
  125. }
  126. static int func_globalinit (struct NCDModuleInitParams params)
  127. {
  128. // init provides list
  129. LinkedList2_Init(&provides);
  130. // init depends list
  131. LinkedList2_Init(&depends);
  132. return 1;
  133. }
  134. static void provide_func_new (NCDModuleInst *i)
  135. {
  136. // allocate instance
  137. struct provide *o = malloc(sizeof(*o));
  138. if (!o) {
  139. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  140. goto fail0;
  141. }
  142. o->i = i;
  143. NCDModuleInst_Backend_SetUser(i, o);
  144. // read arguments
  145. NCDValRef name_arg;
  146. if (!NCDVal_ListRead(o->i->args, 1, &name_arg)) {
  147. ModuleLog(i, BLOG_ERROR, "wrong arity");
  148. goto fail1;
  149. }
  150. if (!NCDVal_IsStringNoNulls(name_arg)) {
  151. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  152. goto fail1;
  153. }
  154. o->name = NCDVal_StringValue(name_arg);
  155. // check for existing provide with this name
  156. if (find_provide(o->name)) {
  157. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  158. goto fail1;
  159. }
  160. // insert to provides list
  161. LinkedList2_Append(&provides, &o->provides_node);
  162. // init depends list
  163. LinkedList2_Init(&o->depends);
  164. // set not dying
  165. o->dying = 0;
  166. // signal up.
  167. // This comes above the loop which follows, so that effects on related depend statements are
  168. // computed before this process advances, avoiding problems like failed variable resolutions.
  169. NCDModuleInst_Backend_Up(o->i);
  170. // update depends
  171. LinkedList2Iterator it;
  172. LinkedList2Iterator_InitForward(&it, &depends);
  173. LinkedList2Node *n;
  174. while (n = LinkedList2Iterator_Next(&it)) {
  175. struct depend *d = UPPER_OBJECT(n, struct depend, depends_node);
  176. depend_update(d);
  177. }
  178. return;
  179. fail1:
  180. free(o);
  181. fail0:
  182. NCDModuleInst_Backend_SetError(i);
  183. NCDModuleInst_Backend_Dead(i);
  184. }
  185. static void provide_free (struct provide *o)
  186. {
  187. ASSERT(LinkedList2_IsEmpty(&o->depends))
  188. NCDModuleInst *i = o->i;
  189. // remove from provides list
  190. LinkedList2_Remove(&provides, &o->provides_node);
  191. // free instance
  192. free(o);
  193. NCDModuleInst_Backend_Dead(i);
  194. }
  195. static void provide_func_die (void *vo)
  196. {
  197. struct provide *o = vo;
  198. ASSERT(!o->dying)
  199. // if we have no depends, die immediately
  200. if (LinkedList2_IsEmpty(&o->depends)) {
  201. provide_free(o);
  202. return;
  203. }
  204. // set dying
  205. o->dying = 1;
  206. // start collapsing our depends
  207. LinkedList2Iterator it;
  208. LinkedList2Iterator_InitForward(&it, &o->depends);
  209. LinkedList2Node *n;
  210. while (n = LinkedList2Iterator_Next(&it)) {
  211. struct depend *d = UPPER_OBJECT(n, struct depend, provide_node);
  212. ASSERT(d->provide == o)
  213. // update depend to make sure it is collapsing
  214. depend_update(d);
  215. }
  216. }
  217. static void depend_func_new (NCDModuleInst *i)
  218. {
  219. // allocate instance
  220. struct depend *o = malloc(sizeof(*o));
  221. if (!o) {
  222. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  223. goto fail0;
  224. }
  225. o->i = i;
  226. NCDModuleInst_Backend_SetUser(i, o);
  227. // read arguments
  228. NCDValRef names_arg;
  229. if (!NCDVal_ListRead(o->i->args, 1, &names_arg)) {
  230. ModuleLog(i, BLOG_ERROR, "wrong arity");
  231. goto fail1;
  232. }
  233. if (!NCDVal_IsList(names_arg)) {
  234. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  235. goto fail1;
  236. }
  237. o->names = names_arg;
  238. // check names list
  239. size_t count = NCDVal_ListCount(o->names);
  240. for (size_t j = 0; j < count; j++) {
  241. NCDValRef e = NCDVal_ListGet(o->names, j);
  242. if (!NCDVal_IsStringNoNulls(e)) {
  243. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  244. goto fail1;
  245. }
  246. }
  247. // insert to depends list
  248. LinkedList2_Append(&depends, &o->depends_node);
  249. // set no provide
  250. o->provide = NULL;
  251. // update
  252. depend_update(o);
  253. return;
  254. fail1:
  255. free(o);
  256. fail0:
  257. NCDModuleInst_Backend_SetError(i);
  258. NCDModuleInst_Backend_Dead(i);
  259. }
  260. static void depend_free (struct depend *o)
  261. {
  262. NCDModuleInst *i = o->i;
  263. if (o->provide) {
  264. // remove from provide's list
  265. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  266. // if provide is dying and is empty, let it die
  267. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  268. provide_free(o->provide);
  269. }
  270. }
  271. // remove from depends list
  272. LinkedList2_Remove(&depends, &o->depends_node);
  273. // free instance
  274. free(o);
  275. NCDModuleInst_Backend_Dead(i);
  276. }
  277. static void depend_func_die (void *vo)
  278. {
  279. struct depend *o = vo;
  280. depend_free(o);
  281. }
  282. static void depend_func_clean (void *vo)
  283. {
  284. struct depend *o = vo;
  285. if (!(o->provide && o->provide_collapsing)) {
  286. return;
  287. }
  288. // remove from provide's list
  289. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  290. // if provide is dying and is empty, let it die
  291. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  292. provide_free(o->provide);
  293. }
  294. // set no provide
  295. o->provide = NULL;
  296. // update
  297. depend_update(o);
  298. }
  299. static int depend_func_getobj (void *vo, const char *objname, NCDObject *out_object)
  300. {
  301. struct depend *o = vo;
  302. if (!o->provide) {
  303. return 0;
  304. }
  305. return NCDModuleInst_Backend_GetObj(o->provide->i, objname, out_object);
  306. }
  307. static const struct NCDModule modules[] = {
  308. {
  309. .type = "multiprovide",
  310. .func_new = provide_func_new,
  311. .func_die = provide_func_die
  312. }, {
  313. .type = "multidepend",
  314. .func_new = depend_func_new,
  315. .func_die = depend_func_die,
  316. .func_clean = depend_func_clean,
  317. .func_getobj = depend_func_getobj,
  318. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
  319. }, {
  320. .type = NULL
  321. }
  322. };
  323. const struct NCDModuleGroup ncdmodule_multidepend = {
  324. .func_globalinit = func_globalinit,
  325. .modules = modules
  326. };