| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- /**
- * @file dynamic_depend.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
- *
- * Dynamic dependencies module.
- *
- * Synopsis: dynamic_provide(string name, order_value)
- * Synopsis: dynamic_depend(string name)
- */
- #include <stdlib.h>
- #include <string.h>
- #include <misc/offset.h>
- #include <misc/debug.h>
- #include <structure/LinkedList0.h>
- #include <structure/BAVL.h>
- #include <ncd/NCDModule.h>
- #include <generated/blog_channel_ncd_dynamic_depend.h>
- #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
- struct provide;
- struct name {
- char *name;
- BAVLNode names_tree_node;
- BAVL provides_tree;
- LinkedList0 waiting_depends_list;
- struct provide *cur_p;
- LinkedList0 cur_bound_depends_list;
- int cur_resetting;
- };
- struct provide {
- NCDModuleInst *i;
- struct name *n;
- NCDValue *order_value;
- BAVLNode provides_tree_node;
- int dying;
- };
- struct depend {
- NCDModuleInst *i;
- struct name *n;
- int is_bound;
- LinkedList0Node depends_list_node;
- };
- static BAVL names_tree;
- static void provide_free (struct provide *o);
- static void depend_free (struct depend *o);
- static int stringptr_comparator (void *user, char **v1, char **v2)
- {
- int cmp = strcmp(*v1, *v2);
- if (cmp < 0) {
- return -1;
- }
- if (cmp > 0) {
- return 1;
- }
- return 0;
- }
- static int valueptr_comparator (void *user, NCDValue **v1, NCDValue **v2)
- {
- return NCDValue_Compare(*v1, *v2);
- }
- static struct name * find_name (const char *name)
- {
- BAVLNode *tn = BAVL_LookupExact(&names_tree, &name);
- if (!tn) {
- return NULL;
- }
-
- struct name *n = UPPER_OBJECT(tn, struct name, names_tree_node);
- ASSERT(!strcmp(n->name, name))
-
- return n;
- }
- static struct name * name_init (NCDModuleInst *i, const char *name)
- {
- ASSERT(!find_name(name))
-
- // allocate structure
- struct name *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "malloc failed");
- goto fail0;
- }
-
- // copy name
- if (!(o->name = strdup(name))) {
- ModuleLog(i, BLOG_ERROR, "strdup failed");
- goto fail1;
- }
-
- // insert to names tree
- ASSERT_EXECUTE(BAVL_Insert(&names_tree, &o->names_tree_node, NULL))
-
- // init provides tree
- BAVL_Init(&o->provides_tree, OFFSET_DIFF(struct provide, order_value, provides_tree_node), (BAVL_comparator)valueptr_comparator, NULL);
-
- // init waiting depends list
- LinkedList0_Init(&o->waiting_depends_list);
-
- // set no current provide
- o->cur_p = NULL;
-
- return o;
-
- fail1:
- free(o);
- fail0:
- return NULL;
- }
- static void name_free (struct name *o)
- {
- ASSERT(BAVL_IsEmpty(&o->provides_tree))
- ASSERT(LinkedList0_IsEmpty(&o->waiting_depends_list))
- ASSERT(!o->cur_p)
-
- // remove from names tree
- BAVL_Remove(&names_tree, &o->names_tree_node);
-
- // free name
- free(o->name);
-
- // free structure
- free(o);
- }
- static struct provide * name_get_first_provide (struct name *o)
- {
- BAVLNode *tn = BAVL_GetFirst(&o->provides_tree);
- if (!tn) {
- return NULL;
- }
-
- struct provide *p = UPPER_OBJECT(tn, struct provide, provides_tree_node);
- ASSERT(p->n == o)
-
- return p;
- }
- static void name_new_current (struct name *o)
- {
- ASSERT(!o->cur_p)
- ASSERT(!BAVL_IsEmpty(&o->provides_tree))
-
- // set current provide
- o->cur_p = name_get_first_provide(o);
-
- // init bound depends list
- LinkedList0_Init(&o->cur_bound_depends_list);
-
- // set not resetting
- o->cur_resetting = 0;
-
- // bind waiting depends
- while (!LinkedList0_IsEmpty(&o->waiting_depends_list)) {
- struct depend *d = UPPER_OBJECT(LinkedList0_GetFirst(&o->waiting_depends_list), struct depend, depends_list_node);
- ASSERT(d->n == o)
- ASSERT(!d->is_bound)
-
- // remove from waiting depends list
- LinkedList0_Remove(&o->waiting_depends_list, &d->depends_list_node);
-
- // set bound
- d->is_bound = 1;
-
- // add to bound depends list
- LinkedList0_Prepend(&o->cur_bound_depends_list, &d->depends_list_node);
-
- // signal up
- NCDModuleInst_Backend_Up(d->i);
- }
- }
- static void name_free_if_unused (struct name *o)
- {
- if (BAVL_IsEmpty(&o->provides_tree) && LinkedList0_IsEmpty(&o->waiting_depends_list)) {
- name_free(o);
- }
- }
- static void name_continue_resetting (struct name *o)
- {
- ASSERT(o->cur_p)
- ASSERT(o->cur_resetting)
-
- // still have bound depends?
- if (!LinkedList0_IsEmpty(&o->cur_bound_depends_list)) {
- return;
- }
-
- struct provide *old_p = o->cur_p;
-
- // set no current provide
- o->cur_p = NULL;
-
- // free old current provide if it's dying
- if (old_p->dying) {
- provide_free(old_p);
- }
-
- if (!BAVL_IsEmpty(&o->provides_tree)) {
- // get new current provide
- name_new_current(o);
- } else {
- // free name if unused
- name_free_if_unused(o);
- }
- }
- static void name_start_resetting (struct name *o)
- {
- ASSERT(o->cur_p)
- ASSERT(!o->cur_resetting)
-
- // set resetting
- o->cur_resetting = 1;
-
- // signal bound depends down
- for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->cur_bound_depends_list); ln; ln = LinkedList0Node_Next(ln)) {
- struct depend *d = UPPER_OBJECT(ln, struct depend, depends_list_node);
- ASSERT(d->n == o)
- ASSERT(d->is_bound)
- NCDModuleInst_Backend_Down(d->i);
- }
-
- // if there were no bound depends, continue right away
- name_continue_resetting(o);
- }
- static int func_globalinit (struct NCDModuleInitParams params)
- {
- // init names tree
- BAVL_Init(&names_tree, OFFSET_DIFF(struct name, name, names_tree_node), (BAVL_comparator)stringptr_comparator, NULL);
-
- return 1;
- }
- static void provide_func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct provide *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // read arguments
- NCDValue *name_arg;
- if (!NCDValue_ListRead(i->args, 2, &name_arg, &o->order_value)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
- if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
- ModuleLog(i, BLOG_ERROR, "wrong type");
- goto fail1;
- }
- char *name_str = NCDValue_StringValue(name_arg);
-
- // find name, create new if needed
- struct name *n = find_name(name_str);
- if (!n && !(n = name_init(i, name_str))) {
- goto fail1;
- }
-
- // set name
- o->n = n;
-
- // check for order value conflict
- if (BAVL_LookupExact(&n->provides_tree, &o->order_value)) {
- ModuleLog(i, BLOG_ERROR, "order value already exists");
- goto fail1;
- }
-
- // add to name's provides tree
- ASSERT_EXECUTE(BAVL_Insert(&n->provides_tree, &o->provides_tree_node, NULL))
-
- // set not dying
- o->dying = 0;
-
- // signal up
- NCDModuleInst_Backend_Up(i);
-
- // should this be the current provide?
- if (o == name_get_first_provide(n)) {
- if (!n->cur_p) {
- name_new_current(n);
- }
- else if (!n->cur_resetting) {
- name_start_resetting(n);
- }
- }
-
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void provide_free (struct provide *o)
- {
- struct name *n = o->n;
- ASSERT(o->dying)
- ASSERT(o != n->cur_p)
-
- NCDModuleInst *i = o->i;
-
- // remove from name's provides tree
- BAVL_Remove(&n->provides_tree, &o->provides_tree_node);
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static void provide_func_die (void *vo)
- {
- struct provide *o = vo;
- struct name *n = o->n;
- ASSERT(!o->dying)
-
- // set dying
- o->dying = 1;
-
- // if this is not the current provide, die right away
- if (o != n->cur_p) {
- // free provide
- provide_free(o);
-
- // free name if unused
- name_free_if_unused(n);
- return;
- }
-
- ASSERT(!n->cur_resetting)
-
- // start resetting
- name_start_resetting(n);
- }
- static void depend_func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct depend *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // read arguments
- NCDValue *name_arg;
- if (!NCDValue_ListRead(i->args, 1, &name_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
- if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
- ModuleLog(i, BLOG_ERROR, "wrong type");
- goto fail1;
- }
- char *name_str = NCDValue_StringValue(name_arg);
-
- // find name, create new if needed
- struct name *n = find_name(name_str);
- if (!n && !(n = name_init(i, name_str))) {
- goto fail1;
- }
-
- // set name
- o->n = n;
-
- if (n->cur_p && !n->cur_resetting) {
- // set bound
- o->is_bound = 1;
-
- // add to bound depends list
- LinkedList0_Prepend(&n->cur_bound_depends_list, &o->depends_list_node);
-
- // signal up
- NCDModuleInst_Backend_Up(i);
- } else {
- // set not bound
- o->is_bound = 0;
-
- // add to waiting depends list
- LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
- }
-
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void depend_func_die (void *vo)
- {
- struct depend *o = vo;
- NCDModuleInst *i = o->i;
- struct name *n = o->n;
-
- if (o->is_bound) {
- ASSERT(n->cur_p)
-
- // remove from bound depends list
- LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
-
- // continue resetting
- if (n->cur_resetting) {
- name_continue_resetting(n);
- }
- } else {
- // remove from waiting depends list
- LinkedList0_Remove(&n->waiting_depends_list, &o->depends_list_node);
-
- // free name if unused
- name_free_if_unused(n);
- }
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static void depend_func_clean (void *vo)
- {
- struct depend *o = vo;
- struct name *n = o->n;
- ASSERT(!o->is_bound || n->cur_p)
-
- if (!(o->is_bound && n->cur_resetting)) {
- return;
- }
-
- // remove from bound depends list
- LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
-
- // set not bound
- o->is_bound = 0;
-
- // add to waiting depends list
- LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
-
- // continue resetting
- name_continue_resetting(n);
- }
- static int depend_func_getvar (void *vo, const char *varname, NCDValue *out)
- {
- struct depend *o = vo;
- struct name *n = o->n;
- ASSERT(!o->is_bound || n->cur_p)
-
- if (!o->is_bound) {
- return 0;
- }
-
- return NCDModuleInst_Backend_GetVar(n->cur_p->i, varname, out);
- }
- static NCDModuleInst * depend_func_getobj (void *vo, const char *objname)
- {
- struct depend *o = vo;
- struct name *n = o->n;
- ASSERT(!o->is_bound || n->cur_p)
-
- if (!o->is_bound) {
- return NULL;
- }
-
- return NCDModuleInst_Backend_GetObj(n->cur_p->i, objname);
- }
- static const struct NCDModule modules[] = {
- {
- .type = "dynamic_provide",
- .func_new = provide_func_new,
- .func_die = provide_func_die
- }, {
- .type = "dynamic_depend",
- .func_new = depend_func_new,
- .func_die = depend_func_die,
- .func_clean = depend_func_clean,
- .func_getvar = depend_func_getvar,
- .func_getobj = depend_func_getobj,
- .can_resolve_when_down = 1
- }, {
- .type = NULL
- }
- };
- const struct NCDModuleGroup ncdmodule_dynamic_depend = {
- .func_globalinit = func_globalinit,
- .modules = modules
- };
|