source: mainline/uspace/srv/sysman/edge.c@ 18377301

Last change on this file since 18377301 was 18377301, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

sysman: Fix typo

  • Property mode set to 100644
File size: 3.6 KB
Line 
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 "edge.h"
35
36static void edge_init(unit_edge_t *e)
37{
38 memset(e, 0, sizeof(*e));
39 link_initialize(&e->edges_in);
40 link_initialize(&e->edges_out);
41}
42
43static unit_edge_t *edge_extract_internal(unit_t *input, unit_t *output)
44{
45 list_foreach(input->edges_out, edges_out, unit_edge_t, e) {
46 if (e->output == output) {
47 return e;
48 }
49 }
50
51 return NULL;
52}
53
54unit_edge_t *edge_create(void)
55{
56 unit_edge_t *e = malloc(sizeof(unit_edge_t));
57 if (e) {
58 edge_init(e);
59 }
60 return e;
61}
62
63void edge_destroy(unit_edge_t **e_ptr)
64{
65 unit_edge_t *e = *e_ptr;
66 if (e == NULL) {
67 return;
68 }
69
70 list_remove(&e->edges_in);
71 list_remove(&e->edges_out);
72
73 free(e->output_name);
74 free(e);
75
76 *e_ptr = NULL;
77}
78
79int edge_sprout_out(unit_t *input, const char *output_name)
80{
81 int rc;
82 unit_edge_t *e = edge_create();
83
84 if (e == NULL) {
85 rc = ENOMEM;
86 goto finish;
87 }
88
89 //TODO check multi-edges
90 e->output_name = str_dup(output_name);
91 if (e->output_name == NULL) {
92 rc = ENOMEM;
93 goto finish;
94 }
95
96 list_append(&e->edges_out, &input->edges_out);
97 e->input = input;
98
99 rc = EOK;
100
101finish:
102 if (rc != EOK) {
103 edge_destroy(&e);
104 }
105 return rc;
106}
107
108void edge_resolve_output(unit_edge_t *e, unit_t *output)
109{
110 assert(e->output == NULL);
111 assert(e->output_name != NULL);
112
113 e->output = output;
114 list_append(&e->edges_in, &output->edges_in);
115
116 free(e->output_name);
117 e->output_name = NULL;
118}
119
120
121/**
122 * @return EOK on success
123 * @return ENOMEM
124 * @return EEXISTS
125 */
126int edge_connect(unit_t *input, unit_t *output)
127{
128 if (edge_extract_internal(input, output)) {
129 return EEXISTS;
130 }
131
132 unit_edge_t *e = edge_create();
133 if (e == NULL) {
134 return ENOMEM;
135 }
136
137 list_append(&e->edges_in, &output->edges_in);
138 list_append(&e->edges_out, &input->edges_out);
139
140 e->input = input;
141 e->output = output;
142 return EOK;
143}
144
145/** Remove edge from dependency graph
146 *
147 * Given edge is removed from graph and unallocated.
148 */
149void edge_remove(unit_edge_t **e_ptr)
150{
151 /*
152 * So far it's just passing, however, edge_destroy is considered
153 * low-level and edge_remove could later e.g. support transactions.
154 */
155 edge_destroy(e_ptr);
156}
Note: See TracBrowser for help on using the repository browser.