multidepend.c 9.9 KB

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