lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / lib / graph / sink.c
1 /*
2 * sink.c
3 *
4 * Babeltrace Sink 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-SINK"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/compiler-internal.h>
33 #include <babeltrace/values.h>
34 #include <babeltrace/graph/component-sink-internal.h>
35 #include <babeltrace/graph/component-internal.h>
36 #include <babeltrace/graph/notification.h>
37 #include <babeltrace/graph/graph.h>
38 #include <babeltrace/assert-internal.h>
39 #include <babeltrace/assert-internal.h>
40
41 BT_HIDDEN
42 void bt_component_sink_destroy(struct bt_component *component)
43 {
44 }
45
46 BT_HIDDEN
47 struct bt_component *bt_component_sink_create(
48 struct bt_component_class *class)
49 {
50 struct bt_component_sink *sink = NULL;
51
52 sink = g_new0(struct bt_component_sink, 1);
53 if (!sink) {
54 BT_LOGE_STR("Failed to allocate one sink component.");
55 goto end;
56 }
57
58 end:
59 return sink ? &sink->parent : NULL;
60 }
61
62 BT_HIDDEN
63 enum bt_component_status bt_component_sink_consume(
64 struct bt_component *component)
65 {
66 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
67 struct bt_component_class_sink *sink_class = NULL;
68
69 BT_ASSERT(component);
70 BT_ASSERT(bt_component_get_class_type(component) == BT_COMPONENT_CLASS_TYPE_SINK);
71 sink_class = container_of(component->class, struct bt_component_class_sink, parent);
72 BT_ASSERT(sink_class->methods.consume);
73 BT_LOGD("Calling user's consume method: "
74 "comp-addr=%p, comp-name=\"%s\"",
75 component, bt_component_get_name(component));
76 ret = sink_class->methods.consume(bt_private_component_from_component(component));
77 BT_LOGD("User method returned: status=%s",
78 bt_component_status_string(ret));
79 if (ret < 0) {
80 BT_LOGW_STR("Consume method failed.");
81 }
82
83 return ret;
84 }
85
86 int64_t bt_component_sink_get_input_port_count(struct bt_component *component)
87 {
88 int64_t ret;
89
90 if (!component) {
91 BT_LOGW_STR("Invalid parameter: component is NULL.");
92 ret = (int64_t) -1;
93 goto end;
94 }
95
96 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
97 BT_LOGW("Invalid parameter: component's class is not a sink component class: "
98 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
99 component, bt_component_get_name(component),
100 bt_component_class_type_string(component->class->type));
101 ret = (int64_t) -1;
102 goto end;
103 }
104
105 /* bt_component_get_input_port_count() logs details/errors */
106 ret = bt_component_get_input_port_count(component);
107
108 end:
109 return ret;
110 }
111
112 struct bt_port *bt_component_sink_get_input_port_by_name(
113 struct bt_component *component, const char *name)
114 {
115 struct bt_port *port = NULL;
116
117 if (!component) {
118 BT_LOGW_STR("Invalid parameter: component is NULL.");
119 goto end;
120 }
121
122 if (!name) {
123 BT_LOGW_STR("Invalid parameter: name is NULL.");
124 goto end;
125 }
126
127 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
128 BT_LOGW("Invalid parameter: component's class is not a sink component class: "
129 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
130 component, bt_component_get_name(component),
131 bt_component_class_type_string(component->class->type));
132 goto end;
133 }
134
135 /* bt_component_get_input_port_by_name() logs details/errors */
136 port = bt_component_get_input_port_by_name(component, name);
137
138 end:
139 return port;
140 }
141
142 struct bt_port *bt_component_sink_get_input_port_by_index(
143 struct bt_component *component, uint64_t index)
144 {
145 struct bt_port *port = NULL;
146
147 if (!component) {
148 BT_LOGW_STR("Invalid parameter: component is NULL.");
149 goto end;
150 }
151
152 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
153 BT_LOGW("Invalid parameter: component's class is not a sink component class: "
154 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
155 component, bt_component_get_name(component),
156 bt_component_class_type_string(component->class->type));
157 goto end;
158 }
159
160 /* bt_component_get_input_port_by_index() logs details/errors */
161 port = bt_component_get_input_port_by_index(component, index);
162
163 end:
164 return port;
165 }
166
167 struct bt_private_port *
168 bt_private_component_sink_get_input_private_port_by_index(
169 struct bt_private_component *private_component, uint64_t index)
170 {
171 /* bt_component_sink_get_input_port_by_index() logs details/errors */
172 return bt_private_port_from_port(
173 bt_component_sink_get_input_port_by_index(
174 bt_component_borrow_from_private(private_component), index));
175 }
176
177 struct bt_private_port *
178 bt_private_component_sink_get_input_private_port_by_name(
179 struct bt_private_component *private_component,
180 const char *name)
181 {
182 /* bt_component_sink_get_input_port_by_name() logs details/errors */
183 return bt_private_port_from_port(
184 bt_component_sink_get_input_port_by_name(
185 bt_component_borrow_from_private(private_component), name));
186 }
187
188 enum bt_component_status bt_private_component_sink_add_input_private_port(
189 struct bt_private_component *private_component,
190 const char *name, void *user_data,
191 struct bt_private_port **user_priv_port)
192 {
193 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
194 struct bt_port *port = NULL;
195 struct bt_component *component =
196 bt_component_borrow_from_private(private_component);
197 struct bt_graph *graph;
198
199 if (!component) {
200 BT_LOGW_STR("Invalid parameter: component is NULL.");
201 status = BT_COMPONENT_STATUS_INVALID;
202 goto end;
203 }
204
205 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
206 BT_LOGW("Invalid parameter: component's class is not a sink component class: "
207 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
208 component, bt_component_get_name(component),
209 bt_component_class_type_string(component->class->type));
210 status = BT_COMPONENT_STATUS_INVALID;
211 goto end;
212 }
213
214 graph = bt_component_borrow_graph(component);
215
216 if (graph && bt_graph_is_canceled(graph)) {
217 BT_LOGW("Cannot add input port to sink component: graph is canceled: "
218 "comp-addr=%p, comp-name=\"%s\", graph-addr=%p",
219 component, bt_component_get_name(component),
220 bt_component_borrow_graph(component));
221 status = BT_COMPONENT_STATUS_GRAPH_IS_CANCELED;
222 goto end;
223 }
224
225 /* bt_component_add_input_port() logs details/errors */
226 port = bt_component_add_input_port(component, name, user_data);
227 if (!port) {
228 status = BT_COMPONENT_STATUS_NOMEM;
229 goto end;
230 }
231
232 if (user_priv_port) {
233 /* Move reference to user */
234 *user_priv_port = bt_private_port_from_port(port);
235 port = NULL;
236 }
237
238 end:
239 bt_put(port);
240 return status;
241 }
This page took 0.036508 seconds and 4 git commands to generate.