multidepend.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. char *name;
  57. LinkedList2Node provides_node;
  58. LinkedList2 depends;
  59. int dying;
  60. };
  61. struct depend {
  62. NCDModuleInst *i;
  63. NCDValue *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. NCDValue *e = NCDValue_ListFirst(o->names);
  88. while (e) {
  89. struct provide *p = find_provide(NCDValue_StringValue(e));
  90. if (p && !p->dying) {
  91. return p;
  92. }
  93. e = NCDValue_ListNext(o->names, e);
  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. NCDModuleInst_Backend_SetUser(i, o);
  143. // init arguments
  144. o->i = i;
  145. // read arguments
  146. NCDValue *name_arg;
  147. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  148. ModuleLog(i, BLOG_ERROR, "wrong arity");
  149. goto fail1;
  150. }
  151. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  152. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  153. goto fail1;
  154. }
  155. o->name = NCDValue_StringValue(name_arg);
  156. // check for existing provide with this name
  157. if (find_provide(o->name)) {
  158. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  159. goto fail1;
  160. }
  161. // insert to provides list
  162. LinkedList2_Append(&provides, &o->provides_node);
  163. // init depends list
  164. LinkedList2_Init(&o->depends);
  165. // set not dying
  166. o->dying = 0;
  167. // signal up.
  168. // This comes above the loop which follows, so that effects on related depend statements are
  169. // computed before this process advances, avoiding problems like failed variable resolutions.
  170. NCDModuleInst_Backend_Up(o->i);
  171. // update depends
  172. LinkedList2Iterator it;
  173. LinkedList2Iterator_InitForward(&it, &depends);
  174. LinkedList2Node *n;
  175. while (n = LinkedList2Iterator_Next(&it)) {
  176. struct depend *d = UPPER_OBJECT(n, struct depend, depends_node);
  177. depend_update(d);
  178. }
  179. return;
  180. fail1:
  181. free(o);
  182. fail0:
  183. NCDModuleInst_Backend_SetError(i);
  184. NCDModuleInst_Backend_Dead(i);
  185. }
  186. static void provide_free (struct provide *o)
  187. {
  188. ASSERT(LinkedList2_IsEmpty(&o->depends))
  189. NCDModuleInst *i = o->i;
  190. // remove from provides list
  191. LinkedList2_Remove(&provides, &o->provides_node);
  192. // free instance
  193. free(o);
  194. NCDModuleInst_Backend_Dead(i);
  195. }
  196. static void provide_func_die (void *vo)
  197. {
  198. struct provide *o = vo;
  199. ASSERT(!o->dying)
  200. // if we have no depends, die immediately
  201. if (LinkedList2_IsEmpty(&o->depends)) {
  202. provide_free(o);
  203. return;
  204. }
  205. // set dying
  206. o->dying = 1;
  207. // start collapsing our depends
  208. LinkedList2Iterator it;
  209. LinkedList2Iterator_InitForward(&it, &o->depends);
  210. LinkedList2Node *n;
  211. while (n = LinkedList2Iterator_Next(&it)) {
  212. struct depend *d = UPPER_OBJECT(n, struct depend, provide_node);
  213. ASSERT(d->provide == o)
  214. // update depend to make sure it is collapsing
  215. depend_update(d);
  216. }
  217. }
  218. static void depend_func_new (NCDModuleInst *i)
  219. {
  220. // allocate instance
  221. struct depend *o = malloc(sizeof(*o));
  222. if (!o) {
  223. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  224. goto fail0;
  225. }
  226. NCDModuleInst_Backend_SetUser(i, o);
  227. // init arguments
  228. o->i = i;
  229. // read arguments
  230. NCDValue *names_arg;
  231. if (!NCDValue_ListRead(o->i->args, 1, &names_arg)) {
  232. ModuleLog(i, BLOG_ERROR, "wrong arity");
  233. goto fail1;
  234. }
  235. if (NCDValue_Type(names_arg) != NCDVALUE_LIST) {
  236. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  237. goto fail1;
  238. }
  239. o->names = names_arg;
  240. // check names list
  241. NCDValue *e = NCDValue_ListFirst(o->names);
  242. while (e) {
  243. if (NCDValue_Type(e) != NCDVALUE_STRING) {
  244. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  245. goto fail1;
  246. }
  247. e = NCDValue_ListNext(o->names, e);
  248. }
  249. // insert to depends list
  250. LinkedList2_Append(&depends, &o->depends_node);
  251. // set no provide
  252. o->provide = NULL;
  253. // update
  254. depend_update(o);
  255. return;
  256. fail1:
  257. free(o);
  258. fail0:
  259. NCDModuleInst_Backend_SetError(i);
  260. NCDModuleInst_Backend_Dead(i);
  261. }
  262. static void depend_free (struct depend *o)
  263. {
  264. NCDModuleInst *i = o->i;
  265. if (o->provide) {
  266. // remove from provide's list
  267. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  268. // if provide is dying and is empty, let it die
  269. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  270. provide_free(o->provide);
  271. }
  272. }
  273. // remove from depends list
  274. LinkedList2_Remove(&depends, &o->depends_node);
  275. // free instance
  276. free(o);
  277. NCDModuleInst_Backend_Dead(i);
  278. }
  279. static void depend_func_die (void *vo)
  280. {
  281. struct depend *o = vo;
  282. depend_free(o);
  283. }
  284. static void depend_func_clean (void *vo)
  285. {
  286. struct depend *o = vo;
  287. if (!(o->provide && o->provide_collapsing)) {
  288. return;
  289. }
  290. // remove from provide's list
  291. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  292. // if provide is dying and is empty, let it die
  293. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  294. provide_free(o->provide);
  295. }
  296. // set no provide
  297. o->provide = NULL;
  298. // update
  299. depend_update(o);
  300. }
  301. static int depend_func_getobj (void *vo, const char *objname, NCDObject *out_object)
  302. {
  303. struct depend *o = vo;
  304. if (!o->provide) {
  305. return 0;
  306. }
  307. return NCDModuleInst_Backend_GetObj(o->provide->i, objname, out_object);
  308. }
  309. static const struct NCDModule modules[] = {
  310. {
  311. .type = "multiprovide",
  312. .func_new = provide_func_new,
  313. .func_die = provide_func_die
  314. }, {
  315. .type = "multidepend",
  316. .func_new = depend_func_new,
  317. .func_die = depend_func_die,
  318. .func_clean = depend_func_clean,
  319. .func_getobj = depend_func_getobj,
  320. .can_resolve_when_down = 1
  321. }, {
  322. .type = NULL
  323. }
  324. };
  325. const struct NCDModuleGroup ncdmodule_multidepend = {
  326. .func_globalinit = func_globalinit,
  327. .modules = modules
  328. };