Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[babeltrace.git] / lib / graph / source.c
1 /*
2 * source.c
3 *
4 * Babeltrace Source Component
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "COMP-SOURCE"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/object.h>
33 #include <babeltrace/compiler-internal.h>
34 #include <babeltrace/graph/private-component.h>
35 #include <babeltrace/graph/component-source-internal.h>
36 #include <babeltrace/graph/component-internal.h>
37 #include <babeltrace/graph/port-internal.h>
38 #include <babeltrace/graph/notification-iterator.h>
39 #include <babeltrace/graph/notification-iterator-internal.h>
40 #include <babeltrace/graph/graph.h>
41
42 BT_HIDDEN
43 void bt_component_source_destroy(struct bt_component *component)
44 {
45 }
46
47 BT_HIDDEN
48 struct bt_component *bt_component_source_create(
49 struct bt_component_class *class)
50 {
51 struct bt_component_source *source = NULL;
52
53 source = g_new0(struct bt_component_source, 1);
54 if (!source) {
55 BT_LOGE_STR("Failed to allocate one source component.");
56 goto end;
57 }
58
59 end:
60 return source ? &source->parent : NULL;
61 }
62
63 int64_t bt_component_source_get_output_port_count(
64 struct bt_component *component)
65 {
66 int64_t ret;
67
68 if (!component) {
69 BT_LOGW_STR("Invalid parameter: component is NULL.");
70 ret = (int64_t) -1;
71 goto end;
72 }
73
74 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SOURCE) {
75 BT_LOGW("Invalid parameter: component's class is not a source component class: "
76 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
77 component, bt_component_get_name(component),
78 bt_component_class_type_string(component->class->type));
79 ret = (int64_t) -1;
80 goto end;
81 }
82
83 /* bt_component_get_output_port_count() logs details/errors */
84 ret = bt_component_get_output_port_count(component);
85
86 end:
87 return ret;
88 }
89
90 struct bt_port *bt_component_source_get_output_port_by_name(
91 struct bt_component *component, const char *name)
92 {
93 struct bt_port *port = NULL;
94
95 if (!component) {
96 BT_LOGW_STR("Invalid parameter: component is NULL.");
97 goto end;
98 }
99
100 if (!name) {
101 BT_LOGW_STR("Invalid parameter: name is NULL.");
102 goto end;
103 }
104
105 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SOURCE) {
106 BT_LOGW("Invalid parameter: component's class is not a source component class: "
107 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
108 component, bt_component_get_name(component),
109 bt_component_class_type_string(component->class->type));
110 goto end;
111 }
112
113 /* bt_component_get_output_port_by_name() logs details/errors */
114 port = bt_component_get_output_port_by_name(component, name);
115
116 end:
117 return port;
118 }
119
120 struct bt_port *bt_component_source_get_output_port_by_index(
121 struct bt_component *component, uint64_t index)
122 {
123 struct bt_port *port = NULL;
124
125 if (!component) {
126 BT_LOGW_STR("Invalid parameter: component is NULL.");
127 goto end;
128 }
129
130 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SOURCE) {
131 BT_LOGW("Invalid parameter: component's class is not a source component class: "
132 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
133 component, bt_component_get_name(component),
134 bt_component_class_type_string(component->class->type));
135 goto end;
136 }
137
138 /* bt_component_get_output_port_by_index() logs details/errors */
139 port = bt_component_get_output_port_by_index(component, index);
140
141 end:
142 return port;
143 }
144
145 struct bt_private_port *
146 bt_private_component_source_get_output_private_port_by_name(
147 struct bt_private_component *private_component,
148 const char *name)
149 {
150 /* bt_component_source_get_output_port_by_name() logs details/errors */
151 return bt_private_port_from_port(
152 bt_component_source_get_output_port_by_name(
153 bt_component_borrow_from_private(private_component), name));
154 }
155
156 struct bt_private_port *
157 bt_private_component_source_get_output_private_port_by_index(
158 struct bt_private_component *private_component, uint64_t index)
159 {
160 /* bt_component_source_get_output_port_by_index() logs details/errors */
161 return bt_private_port_from_port(
162 bt_component_source_get_output_port_by_index(
163 bt_component_borrow_from_private(private_component), index));
164 }
165
166 enum bt_component_status bt_private_component_source_add_output_private_port(
167 struct bt_private_component *private_component,
168 const char *name, void *user_data,
169 struct bt_private_port **user_priv_port)
170 {
171 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
172 struct bt_port *port = NULL;
173 struct bt_component *component =
174 bt_component_borrow_from_private(private_component);
175 struct bt_graph *graph;
176
177 if (!component) {
178 BT_LOGW_STR("Invalid parameter: component is NULL.");
179 status = BT_COMPONENT_STATUS_INVALID;
180 goto end;
181 }
182
183 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SOURCE) {
184 BT_LOGW("Invalid parameter: component's class is not a source component class: "
185 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
186 component, bt_component_get_name(component),
187 bt_component_class_type_string(component->class->type));
188 status = BT_COMPONENT_STATUS_INVALID;
189 goto end;
190 }
191
192 graph = bt_component_borrow_graph(component);
193
194 if (graph && bt_graph_is_canceled(graph)) {
195 BT_LOGW("Cannot add output port to source component: graph is canceled: "
196 "comp-addr=%p, comp-name=\"%s\", graph-addr=%p",
197 component, bt_component_get_name(component),
198 bt_component_borrow_graph(component));
199 status = BT_COMPONENT_STATUS_GRAPH_IS_CANCELED;
200 goto end;
201 }
202
203 /* bt_component_add_output_port() logs details and errors */
204 port = bt_component_add_output_port(component, name, user_data);
205 if (!port) {
206 status = BT_COMPONENT_STATUS_NOMEM;
207 goto end;
208 }
209
210 if (user_priv_port) {
211 /* Move reference to user */
212 *user_priv_port = bt_private_port_from_port(port);
213 port = NULL;
214 }
215
216 end:
217 bt_object_put_ref(port);
218 return status;
219 }
This page took 0.039732 seconds and 4 git commands to generate.