multidepend.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/LinkedList1.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. LinkedList1Node provides_node;
  58. LinkedList1 depends;
  59. int dying;
  60. };
  61. struct depend {
  62. NCDModuleInst *i;
  63. NCDValRef names;
  64. LinkedList1Node depends_node;
  65. struct provide *provide;
  66. LinkedList1Node provide_node;
  67. int provide_collapsing;
  68. };
  69. static LinkedList1 provides;
  70. static LinkedList1 depends;
  71. static struct provide * find_provide (const char *name)
  72. {
  73. for (LinkedList1Node *n = LinkedList1_GetFirst(&provides); n; n = LinkedList1Node_Next(n)) {
  74. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  75. if (!strcmp(p->name, name)) {
  76. return p;
  77. }
  78. }
  79. return NULL;
  80. }
  81. static struct provide * depend_find_best_provide (struct depend *o)
  82. {
  83. size_t count = NCDVal_ListCount(o->names);
  84. for (size_t j = 0; j < count; j++) {
  85. NCDValRef e = NCDVal_ListGet(o->names, j);
  86. struct provide *p = find_provide(NCDVal_StringValue(e));
  87. if (p && !p->dying) {
  88. return p;
  89. }
  90. }
  91. return NULL;
  92. }
  93. static void depend_update (struct depend *o)
  94. {
  95. // if we're collapsing, do nothing
  96. if (o->provide && o->provide_collapsing) {
  97. return;
  98. }
  99. // find best provide
  100. struct provide *bp = depend_find_best_provide(o);
  101. ASSERT(!bp || !bp->dying)
  102. // has anything changed?
  103. if (bp == o->provide) {
  104. return;
  105. }
  106. if (o->provide) {
  107. // set collapsing
  108. o->provide_collapsing = 1;
  109. // signal down
  110. NCDModuleInst_Backend_Down(o->i);
  111. } else {
  112. // insert to provide's list
  113. LinkedList1_Append(&bp->depends, &o->provide_node);
  114. // set not collapsing
  115. o->provide_collapsing = 0;
  116. // set provide
  117. o->provide = bp;
  118. // signal up
  119. NCDModuleInst_Backend_Up(o->i);
  120. }
  121. }
  122. static int func_globalinit (struct NCDModuleInitParams params)
  123. {
  124. // init provides list
  125. LinkedList1_Init(&provides);
  126. // init depends list
  127. LinkedList1_Init(&depends);
  128. return 1;
  129. }
  130. static void provide_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  131. {
  132. struct provide *o = vo;
  133. o->i = i;
  134. // read arguments
  135. NCDValRef name_arg;
  136. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  137. ModuleLog(i, BLOG_ERROR, "wrong arity");
  138. goto fail0;
  139. }
  140. if (!NCDVal_IsStringNoNulls(name_arg)) {
  141. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  142. goto fail0;
  143. }
  144. o->name = NCDVal_StringValue(name_arg);
  145. // check for existing provide with this name
  146. if (find_provide(o->name)) {
  147. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  148. goto fail0;
  149. }
  150. // insert to provides list
  151. LinkedList1_Append(&provides, &o->provides_node);
  152. // init depends list
  153. LinkedList1_Init(&o->depends);
  154. // set not dying
  155. o->dying = 0;
  156. // signal up.
  157. // This comes above the loop which follows, so that effects on related depend statements are
  158. // computed before this process advances, avoiding problems like failed variable resolutions.
  159. NCDModuleInst_Backend_Up(o->i);
  160. // update depends
  161. for (LinkedList1Node *n = LinkedList1_GetFirst(&depends); n; n = LinkedList1Node_Next(n)) {
  162. struct depend *d = UPPER_OBJECT(n, struct depend, depends_node);
  163. depend_update(d);
  164. }
  165. return;
  166. fail0:
  167. NCDModuleInst_Backend_SetError(i);
  168. NCDModuleInst_Backend_Dead(i);
  169. }
  170. static void provide_free (struct provide *o)
  171. {
  172. ASSERT(LinkedList1_IsEmpty(&o->depends))
  173. // remove from provides list
  174. LinkedList1_Remove(&provides, &o->provides_node);
  175. NCDModuleInst_Backend_Dead(o->i);
  176. }
  177. static void provide_func_die (void *vo)
  178. {
  179. struct provide *o = vo;
  180. ASSERT(!o->dying)
  181. // if we have no depends, die immediately
  182. if (LinkedList1_IsEmpty(&o->depends)) {
  183. provide_free(o);
  184. return;
  185. }
  186. // set dying
  187. o->dying = 1;
  188. // start collapsing our depends
  189. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->depends); n; n = LinkedList1Node_Next(n)) {
  190. struct depend *d = UPPER_OBJECT(n, struct depend, provide_node);
  191. ASSERT(d->provide == o)
  192. // update depend to make sure it is collapsing
  193. depend_update(d);
  194. }
  195. }
  196. static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  197. {
  198. struct depend *o = vo;
  199. o->i = i;
  200. // read arguments
  201. NCDValRef names_arg;
  202. if (!NCDVal_ListRead(params->args, 1, &names_arg)) {
  203. ModuleLog(i, BLOG_ERROR, "wrong arity");
  204. goto fail0;
  205. }
  206. if (!NCDVal_IsList(names_arg)) {
  207. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  208. goto fail0;
  209. }
  210. o->names = names_arg;
  211. // check names list
  212. size_t count = NCDVal_ListCount(o->names);
  213. for (size_t j = 0; j < count; j++) {
  214. NCDValRef e = NCDVal_ListGet(o->names, j);
  215. if (!NCDVal_IsStringNoNulls(e)) {
  216. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  217. goto fail0;
  218. }
  219. }
  220. // insert to depends list
  221. LinkedList1_Append(&depends, &o->depends_node);
  222. // set no provide
  223. o->provide = NULL;
  224. // update
  225. depend_update(o);
  226. return;
  227. fail0:
  228. NCDModuleInst_Backend_SetError(i);
  229. NCDModuleInst_Backend_Dead(i);
  230. }
  231. static void depend_free (struct depend *o)
  232. {
  233. if (o->provide) {
  234. // remove from provide's list
  235. LinkedList1_Remove(&o->provide->depends, &o->provide_node);
  236. // if provide is dying and is empty, let it die
  237. if (o->provide->dying && LinkedList1_IsEmpty(&o->provide->depends)) {
  238. provide_free(o->provide);
  239. }
  240. }
  241. // remove from depends list
  242. LinkedList1_Remove(&depends, &o->depends_node);
  243. NCDModuleInst_Backend_Dead(o->i);
  244. }
  245. static void depend_func_die (void *vo)
  246. {
  247. struct depend *o = vo;
  248. depend_free(o);
  249. }
  250. static void depend_func_clean (void *vo)
  251. {
  252. struct depend *o = vo;
  253. if (!(o->provide && o->provide_collapsing)) {
  254. return;
  255. }
  256. // remove from provide's list
  257. LinkedList1_Remove(&o->provide->depends, &o->provide_node);
  258. // if provide is dying and is empty, let it die
  259. if (o->provide->dying && LinkedList1_IsEmpty(&o->provide->depends)) {
  260. provide_free(o->provide);
  261. }
  262. // set no provide
  263. o->provide = NULL;
  264. // update
  265. depend_update(o);
  266. }
  267. static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  268. {
  269. struct depend *o = vo;
  270. if (!o->provide) {
  271. return 0;
  272. }
  273. return NCDModuleInst_Backend_GetObj(o->provide->i, objname, out_object);
  274. }
  275. static const struct NCDModule modules[] = {
  276. {
  277. .type = "multiprovide",
  278. .func_new2 = provide_func_new,
  279. .func_die = provide_func_die,
  280. .alloc_size = sizeof(struct provide)
  281. }, {
  282. .type = "multidepend",
  283. .func_new2 = depend_func_new,
  284. .func_die = depend_func_die,
  285. .func_clean = depend_func_clean,
  286. .func_getobj = depend_func_getobj,
  287. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  288. .alloc_size = sizeof(struct depend)
  289. }, {
  290. .type = NULL
  291. }
  292. };
  293. const struct NCDModuleGroup ncdmodule_multidepend = {
  294. .func_globalinit = func_globalinit,
  295. .modules = modules
  296. };