| 1 | /*
|
|---|
| 2 | * Copyright (c) 2015 Michal Koutny
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <assert.h>
|
|---|
| 30 | #include <errno.h>
|
|---|
| 31 | #include <stdlib.h>
|
|---|
| 32 | #include <str.h>
|
|---|
| 33 |
|
|---|
| 34 | #include "dep.h"
|
|---|
| 35 |
|
|---|
| 36 | static void dep_dependency_init(unit_dependency_t *dep)
|
|---|
| 37 | {
|
|---|
| 38 | memset(dep, 0, sizeof(*dep));
|
|---|
| 39 | link_initialize(&dep->dependants);
|
|---|
| 40 | link_initialize(&dep->dependencies);
|
|---|
| 41 |
|
|---|
| 42 | dep->state = DEP_EMBRYO;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | unit_dependency_t *dep_dependency_create(void)
|
|---|
| 46 | {
|
|---|
| 47 | unit_dependency_t *dep = malloc(sizeof(unit_dependency_t));
|
|---|
| 48 | if (dep) {
|
|---|
| 49 | dep_dependency_init(dep);
|
|---|
| 50 | }
|
|---|
| 51 | return dep;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | void dep_dependency_destroy(unit_dependency_t **dep_ptr)
|
|---|
| 55 | {
|
|---|
| 56 | unit_dependency_t *dep = *dep_ptr;
|
|---|
| 57 | if (dep == NULL) {
|
|---|
| 58 | return;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | list_remove(&dep->dependencies);
|
|---|
| 62 | list_remove(&dep->dependants);
|
|---|
| 63 |
|
|---|
| 64 | free(dep->dependency_name);
|
|---|
| 65 | free(dep);
|
|---|
| 66 |
|
|---|
| 67 | *dep_ptr = NULL;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | int dep_sprout_dependency(unit_t *dependant, const char *dependency_name)
|
|---|
| 71 | {
|
|---|
| 72 | unit_dependency_t *dep = dep_dependency_create();
|
|---|
| 73 | int rc;
|
|---|
| 74 |
|
|---|
| 75 | if (dep == NULL) {
|
|---|
| 76 | rc = ENOMEM;
|
|---|
| 77 | goto finish;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | dep->dependency_name = str_dup(dependency_name);
|
|---|
| 81 | if (dep->dependency_name == NULL) {
|
|---|
| 82 | rc = ENOMEM;
|
|---|
| 83 | goto finish;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | list_append(&dep->dependencies, &dependant->dependencies);
|
|---|
| 87 | dep->dependant = dependant;
|
|---|
| 88 |
|
|---|
| 89 | rc = EOK;
|
|---|
| 90 |
|
|---|
| 91 | finish:
|
|---|
| 92 | if (rc != EOK) {
|
|---|
| 93 | dep_dependency_destroy(&dep);
|
|---|
| 94 | }
|
|---|
| 95 | return rc;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | void dep_resolve_dependency(unit_dependency_t *dep, unit_t *unit)
|
|---|
| 99 | {
|
|---|
| 100 | assert(dep->dependency == NULL);
|
|---|
| 101 | assert(dep->dependency_name != NULL);
|
|---|
| 102 |
|
|---|
| 103 | // TODO add to other side dependants list
|
|---|
| 104 | dep->dependency = unit;
|
|---|
| 105 | free(dep->dependency_name);
|
|---|
| 106 | dep->dependency_name = NULL;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | * @return EOK on success
|
|---|
| 112 | * @return ENOMEM
|
|---|
| 113 | */
|
|---|
| 114 | int dep_add_dependency(unit_t *dependant, unit_t *dependency)
|
|---|
| 115 | {
|
|---|
| 116 | unit_dependency_t *dep = dep_dependency_create();
|
|---|
| 117 | if (dep == NULL) {
|
|---|
| 118 | return ENOMEM;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | // TODO check existence of the dep
|
|---|
| 122 | // TODO locking
|
|---|
| 123 | // TODO check types and states of connected units
|
|---|
| 124 | list_append(&dep->dependants, &dependency->dependants);
|
|---|
| 125 | list_append(&dep->dependencies, &dependant->dependencies);
|
|---|
| 126 |
|
|---|
| 127 | dep->dependant = dependant;
|
|---|
| 128 | dep->dependency = dependency;
|
|---|
| 129 | return EOK;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /** Remove dependency from dependency graph
|
|---|
| 133 | *
|
|---|
| 134 | * Given dependency is removed from graph and unallocated.
|
|---|
| 135 | */
|
|---|
| 136 | void dep_remove_dependency(unit_dependency_t **dep_ptr)
|
|---|
| 137 | {
|
|---|
| 138 | // TODO here should be some checks, othewise replace this wrapper with
|
|---|
| 139 | // direct destroy
|
|---|
| 140 | dep_dependency_destroy(dep_ptr);
|
|---|
| 141 | }
|
|---|