| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- /**
- * @file multidepend.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @section DESCRIPTION
- *
- * Multiple-option dependencies module.
- *
- * Synopsis: multiprovide(string name)
- * Arguments:
- * name - provider identifier
- *
- * Synopsis: multidepend(list(string) names)
- * Arguments:
- * names - list of provider identifiers. The dependency is satisfied by any
- * provide statement with a provider identifier contained in this list.
- * The order of provider identifiers in the list specifies priority
- * (higher priority first).
- * Variables: Provides variables available from the corresponding provide,
- * ("modname.varname" or "modname").
- */
- #include <stdlib.h>
- #include <string.h>
- #include <misc/offset.h>
- #include <misc/debug.h>
- #include <structure/LinkedList1.h>
- #include <ncd/NCDModule.h>
- #include <generated/blog_channel_ncd_multidepend.h>
- #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
- struct provide {
- NCDModuleInst *i;
- const char *name;
- LinkedList1Node provides_node;
- LinkedList1 depends;
- int dying;
- };
- struct depend {
- NCDModuleInst *i;
- NCDValRef names;
- LinkedList1Node depends_node;
- struct provide *provide;
- LinkedList1Node provide_node;
- int provide_collapsing;
- };
- static LinkedList1 provides;
- static LinkedList1 depends;
- static struct provide * find_provide (const char *name)
- {
- for (LinkedList1Node *n = LinkedList1_GetFirst(&provides); n; n = LinkedList1Node_Next(n)) {
- struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
- if (!strcmp(p->name, name)) {
- return p;
- }
- }
-
- return NULL;
- }
- static struct provide * depend_find_best_provide (struct depend *o)
- {
- size_t count = NCDVal_ListCount(o->names);
-
- for (size_t j = 0; j < count; j++) {
- NCDValRef e = NCDVal_ListGet(o->names, j);
- struct provide *p = find_provide(NCDVal_StringValue(e));
- if (p && !p->dying) {
- return p;
- }
- }
-
- return NULL;
- }
- static void depend_update (struct depend *o)
- {
- // if we're collapsing, do nothing
- if (o->provide && o->provide_collapsing) {
- return;
- }
-
- // find best provide
- struct provide *bp = depend_find_best_provide(o);
- ASSERT(!bp || !bp->dying)
-
- // has anything changed?
- if (bp == o->provide) {
- return;
- }
-
- if (o->provide) {
- // set collapsing
- o->provide_collapsing = 1;
-
- // signal down
- NCDModuleInst_Backend_Down(o->i);
- } else {
- // insert to provide's list
- LinkedList1_Append(&bp->depends, &o->provide_node);
-
- // set not collapsing
- o->provide_collapsing = 0;
-
- // set provide
- o->provide = bp;
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
- }
- }
- static int func_globalinit (struct NCDModuleInitParams params)
- {
- // init provides list
- LinkedList1_Init(&provides);
-
- // init depends list
- LinkedList1_Init(&depends);
-
- return 1;
- }
- static void provide_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- struct provide *o = vo;
- o->i = i;
-
- // read arguments
- NCDValRef name_arg;
- if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
- if (!NCDVal_IsStringNoNulls(name_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
- o->name = NCDVal_StringValue(name_arg);
-
- // check for existing provide with this name
- if (find_provide(o->name)) {
- ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
- goto fail0;
- }
-
- // insert to provides list
- LinkedList1_Append(&provides, &o->provides_node);
-
- // init depends list
- LinkedList1_Init(&o->depends);
-
- // set not dying
- o->dying = 0;
-
- // signal up.
- // This comes above the loop which follows, so that effects on related depend statements are
- // computed before this process advances, avoiding problems like failed variable resolutions.
- NCDModuleInst_Backend_Up(o->i);
-
- // update depends
- for (LinkedList1Node *n = LinkedList1_GetFirst(&depends); n; n = LinkedList1Node_Next(n)) {
- struct depend *d = UPPER_OBJECT(n, struct depend, depends_node);
- depend_update(d);
- }
-
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void provide_free (struct provide *o)
- {
- ASSERT(LinkedList1_IsEmpty(&o->depends))
-
- // remove from provides list
- LinkedList1_Remove(&provides, &o->provides_node);
-
- NCDModuleInst_Backend_Dead(o->i);
- }
- static void provide_func_die (void *vo)
- {
- struct provide *o = vo;
- ASSERT(!o->dying)
-
- // if we have no depends, die immediately
- if (LinkedList1_IsEmpty(&o->depends)) {
- provide_free(o);
- return;
- }
-
- // set dying
- o->dying = 1;
-
- // start collapsing our depends
- for (LinkedList1Node *n = LinkedList1_GetFirst(&o->depends); n; n = LinkedList1Node_Next(n)) {
- struct depend *d = UPPER_OBJECT(n, struct depend, provide_node);
- ASSERT(d->provide == o)
-
- // update depend to make sure it is collapsing
- depend_update(d);
- }
- }
- static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- struct depend *o = vo;
- o->i = i;
-
- // read arguments
- NCDValRef names_arg;
- if (!NCDVal_ListRead(params->args, 1, &names_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
- if (!NCDVal_IsList(names_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
- o->names = names_arg;
-
- // check names list
- size_t count = NCDVal_ListCount(o->names);
- for (size_t j = 0; j < count; j++) {
- NCDValRef e = NCDVal_ListGet(o->names, j);
- if (!NCDVal_IsStringNoNulls(e)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
- }
-
- // insert to depends list
- LinkedList1_Append(&depends, &o->depends_node);
-
- // set no provide
- o->provide = NULL;
-
- // update
- depend_update(o);
-
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void depend_free (struct depend *o)
- {
- if (o->provide) {
- // remove from provide's list
- LinkedList1_Remove(&o->provide->depends, &o->provide_node);
-
- // if provide is dying and is empty, let it die
- if (o->provide->dying && LinkedList1_IsEmpty(&o->provide->depends)) {
- provide_free(o->provide);
- }
- }
-
- // remove from depends list
- LinkedList1_Remove(&depends, &o->depends_node);
-
- NCDModuleInst_Backend_Dead(o->i);
- }
- static void depend_func_die (void *vo)
- {
- struct depend *o = vo;
-
- depend_free(o);
- }
- static void depend_func_clean (void *vo)
- {
- struct depend *o = vo;
-
- if (!(o->provide && o->provide_collapsing)) {
- return;
- }
-
- // remove from provide's list
- LinkedList1_Remove(&o->provide->depends, &o->provide_node);
-
- // if provide is dying and is empty, let it die
- if (o->provide->dying && LinkedList1_IsEmpty(&o->provide->depends)) {
- provide_free(o->provide);
- }
-
- // set no provide
- o->provide = NULL;
-
- // update
- depend_update(o);
- }
- static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
- {
- struct depend *o = vo;
-
- if (!o->provide) {
- return 0;
- }
-
- return NCDModuleInst_Backend_GetObj(o->provide->i, objname, out_object);
- }
- static struct NCDModule modules[] = {
- {
- .type = "multiprovide",
- .func_new2 = provide_func_new,
- .func_die = provide_func_die,
- .alloc_size = sizeof(struct provide)
- }, {
- .type = "multidepend",
- .func_new2 = depend_func_new,
- .func_die = depend_func_die,
- .func_clean = depend_func_clean,
- .func_getobj = depend_func_getobj,
- .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
- .alloc_size = sizeof(struct depend)
- }, {
- .type = NULL
- }
- };
- const struct NCDModuleGroup ncdmodule_multidepend = {
- .func_globalinit = func_globalinit,
- .modules = modules
- };
|