plugins/ctf/fs-src/fs.c: "msgier" -> "notifier"
[babeltrace.git] / lib / graph / iterator.c
CommitLineData
47e5a032 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
47e5a032 3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
47e5a032
JG
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
d6e69534 24#define BT_LOG_TAG "MSG-ITER"
5af447e5
PP
25#include <babeltrace/lib-logging-internal.h>
26
3d9990ac 27#include <babeltrace/compiler-internal.h>
c6bd8523 28#include <babeltrace/trace-ir/field.h>
40f4ba76 29#include <babeltrace/trace-ir/event-const.h>
56e18c4c 30#include <babeltrace/trace-ir/event-internal.h>
40f4ba76 31#include <babeltrace/trace-ir/packet-const.h>
56e18c4c
PP
32#include <babeltrace/trace-ir/packet-internal.h>
33#include <babeltrace/trace-ir/stream-internal.h>
0d72b8c3 34#include <babeltrace/graph/connection-const.h>
bd14d768 35#include <babeltrace/graph/connection-internal.h>
0d72b8c3 36#include <babeltrace/graph/component-const.h>
e5be10ef 37#include <babeltrace/graph/component-internal.h>
b2e0c907
PP
38#include <babeltrace/graph/component-source-internal.h>
39#include <babeltrace/graph/component-class-internal.h>
8ed535b5 40#include <babeltrace/graph/component-class-sink-colander-internal.h>
0d72b8c3 41#include <babeltrace/graph/component-sink-const.h>
d6e69534
PP
42#include <babeltrace/graph/message-const.h>
43#include <babeltrace/graph/message-iterator.h>
44#include <babeltrace/graph/message-iterator-internal.h>
45#include <babeltrace/graph/self-component-port-input-message-iterator.h>
46#include <babeltrace/graph/port-output-message-iterator.h>
47#include <babeltrace/graph/message-internal.h>
48#include <babeltrace/graph/message-event-const.h>
49#include <babeltrace/graph/message-event-internal.h>
50#include <babeltrace/graph/message-packet-const.h>
51#include <babeltrace/graph/message-packet-internal.h>
52#include <babeltrace/graph/message-stream-const.h>
53#include <babeltrace/graph/message-stream-internal.h>
0d72b8c3
PP
54#include <babeltrace/graph/port-const.h>
55#include <babeltrace/graph/graph.h>
56#include <babeltrace/graph/graph-const.h>
8ed535b5 57#include <babeltrace/graph/graph-internal.h>
c55a9f58 58#include <babeltrace/types.h>
f6ccaed9 59#include <babeltrace/assert-internal.h>
f42867e2 60#include <babeltrace/assert-pre-internal.h>
fa054faf 61#include <stdint.h>
2ec84d26 62#include <inttypes.h>
0fbb9a9f 63#include <stdlib.h>
3230ee6b 64
d4393e08
PP
65/*
66 * TODO: Use graph's state (number of active iterators, etc.) and
67 * possibly system specifications to make a better guess than this.
68 */
d6e69534 69#define MSG_BATCH_SIZE 15
d4393e08 70
3230ee6b 71struct stream_state {
40f4ba76
PP
72 const struct bt_stream *stream; /* owned by this */
73 const struct bt_packet *cur_packet; /* owned by this */
d6e69534 74 uint64_t expected_msg_seq_num;
c55a9f58 75 bt_bool is_ended;
3230ee6b
PP
76};
77
26e21a82 78BT_ASSERT_PRE_FUNC
3230ee6b
PP
79static
80void destroy_stream_state(struct stream_state *stream_state)
81{
82 if (!stream_state) {
83 return;
84 }
85
5af447e5
PP
86 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state);
87 BT_LOGV_STR("Putting stream state's current packet.");
d94d92ac 88 BT_OBJECT_PUT_REF_AND_RESET(stream_state->cur_packet);
5af447e5 89 BT_LOGV_STR("Putting stream state's stream.");
d94d92ac 90 BT_OBJECT_PUT_REF_AND_RESET(stream_state->stream);
3230ee6b
PP
91 g_free(stream_state);
92}
93
26e21a82 94BT_ASSERT_PRE_FUNC
3230ee6b 95static
40f4ba76 96struct stream_state *create_stream_state(const struct bt_stream *stream)
3230ee6b
PP
97{
98 struct stream_state *stream_state = g_new0(struct stream_state, 1);
99
100 if (!stream_state) {
5af447e5 101 BT_LOGE_STR("Failed to allocate one stream state.");
3230ee6b
PP
102 goto end;
103 }
104
105 /*
f42867e2 106 * We keep a reference to the stream until we know it's ended.
3230ee6b 107 */
398454ed
PP
108 stream_state->stream = stream;
109 bt_object_get_no_null_check(stream_state->stream);
d94d92ac 110 BT_LIB_LOGV("Created stream state: %![stream-]+s, "
5af447e5 111 "stream-state-addr=%p",
d94d92ac 112 stream, stream_state);
3230ee6b
PP
113
114end:
115 return stream_state;
116}
47e5a032 117
8ed535b5 118static
d6e69534 119void destroy_base_message_iterator(struct bt_object *obj)
8ed535b5 120{
d6e69534 121 struct bt_message_iterator *iterator = (void *) obj;
d4393e08
PP
122
123 BT_ASSERT(iterator);
124
d6e69534
PP
125 if (iterator->msgs) {
126 g_ptr_array_free(iterator->msgs, TRUE);
127 iterator->msgs = NULL;
d4393e08
PP
128 }
129
130 g_free(iterator);
8ed535b5
PP
131}
132
47e5a032 133static
d6e69534 134void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj)
47e5a032 135{
d6e69534 136 struct bt_self_component_port_input_message_iterator *iterator;
8738a040 137
f6ccaed9 138 BT_ASSERT(obj);
d3eb6e8f 139
bd14d768 140 /*
d6e69534 141 * The message iterator's reference count is 0 if we're
bd14d768
PP
142 * here. Increment it to avoid a double-destroy (possibly
143 * infinitely recursive). This could happen for example if the
d6e69534 144 * message iterator's finalization function does
d94d92ac
PP
145 * bt_object_get_ref() (or anything that causes
146 * bt_object_get_ref() to be called) on itself (ref. count goes
147 * from 0 to 1), and then bt_object_put_ref(): the reference
148 * count would go from 1 to 0 again and this function would be
149 * called again.
bd14d768 150 */
3fea54f6 151 obj->ref_count++;
07245ac2 152 iterator = (void *) obj;
d6e69534 153 BT_LIB_LOGD("Destroying self component input port message iterator object: "
d94d92ac 154 "%!+i", iterator);
d6e69534 155 bt_self_component_port_input_message_iterator_finalize(iterator);
d3eb6e8f 156
3230ee6b
PP
157 if (iterator->stream_states) {
158 /*
159 * Remove our destroy listener from each stream which
160 * has a state in this iterator. Otherwise the destroy
161 * listener would be called with an invalid/other
d6e69534 162 * message iterator object.
3230ee6b 163 */
3230ee6b 164 g_hash_table_destroy(iterator->stream_states);
d94d92ac 165 iterator->stream_states = NULL;
3230ee6b
PP
166 }
167
bd14d768
PP
168 if (iterator->connection) {
169 /*
170 * Remove ourself from the originating connection so
171 * that it does not try to finalize a dangling pointer
172 * later.
173 */
174 bt_connection_remove_iterator(iterator->connection, iterator);
d94d92ac 175 iterator->connection = NULL;
bd14d768
PP
176 }
177
d6e69534 178 destroy_base_message_iterator(obj);
47e5a032
JG
179}
180
bd14d768 181BT_HIDDEN
d6e69534
PP
182void bt_self_component_port_input_message_iterator_finalize(
183 struct bt_self_component_port_input_message_iterator *iterator)
bd14d768 184{
d94d92ac
PP
185 typedef void (*method_t)(void *);
186
bd14d768 187 struct bt_component_class *comp_class = NULL;
d94d92ac 188 method_t method = NULL;
bd14d768 189
f6ccaed9 190 BT_ASSERT(iterator);
bd14d768
PP
191
192 switch (iterator->state) {
d6e69534 193 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
088d4023 194 /* Skip user finalization if user initialization failed */
d6e69534 195 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
d94d92ac 196 "%!+i", iterator);
088d4023 197 return;
d6e69534
PP
198 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED:
199 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED_AND_ENDED:
bd14d768 200 /* Already finalized */
d6e69534 201 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
d94d92ac 202 "%!+i", iterator);
bd14d768
PP
203 return;
204 default:
205 break;
206 }
207
d6e69534 208 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
5af447e5 209
d6e69534
PP
210 if (iterator->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED) {
211 BT_LIB_LOGD("Updating message iterator's state: "
212 "new-state=BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED_AND_ENDED");
213 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED_AND_ENDED;
df14f8af 214 } else {
d6e69534
PP
215 BT_LIB_LOGD("Updating message iterator's state: "
216 "new-state=BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED");
217 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED;
df14f8af
MD
218 }
219
f6ccaed9 220 BT_ASSERT(iterator->upstream_component);
bd14d768
PP
221 comp_class = iterator->upstream_component->class;
222
223 /* Call user-defined destroy method */
224 switch (comp_class->type) {
225 case BT_COMPONENT_CLASS_TYPE_SOURCE:
226 {
d94d92ac
PP
227 struct bt_component_class_source *src_comp_cls =
228 (void *) comp_class;
bd14d768 229
d6e69534 230 method = (method_t) src_comp_cls->methods.msg_iter_finalize;
bd14d768
PP
231 break;
232 }
233 case BT_COMPONENT_CLASS_TYPE_FILTER:
234 {
d94d92ac
PP
235 struct bt_component_class_filter *flt_comp_cls =
236 (void *) comp_class;
bd14d768 237
d6e69534 238 method = (method_t) flt_comp_cls->methods.msg_iter_finalize;
bd14d768
PP
239 break;
240 }
241 default:
242 /* Unreachable */
0fbb9a9f 243 abort();
bd14d768
PP
244 }
245
d94d92ac
PP
246 if (method) {
247 BT_LIB_LOGD("Calling user's finalization method: %!+i",
5af447e5 248 iterator);
d94d92ac 249 method(iterator);
bd14d768
PP
250 }
251
bd14d768
PP
252 iterator->upstream_component = NULL;
253 iterator->upstream_port = NULL;
d6e69534 254 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator);
bd14d768
PP
255}
256
257BT_HIDDEN
d6e69534
PP
258void bt_self_component_port_input_message_iterator_set_connection(
259 struct bt_self_component_port_input_message_iterator *iterator,
bd14d768
PP
260 struct bt_connection *connection)
261{
f6ccaed9 262 BT_ASSERT(iterator);
bd14d768 263 iterator->connection = connection;
d6e69534 264 BT_LIB_LOGV("Set message iterator's connection: "
d94d92ac 265 "%![iter-]+i, %![conn-]+x", iterator, connection);
bd14d768
PP
266}
267
90157d89 268static
d6e69534
PP
269int init_message_iterator(struct bt_message_iterator *iterator,
270 enum bt_message_iterator_type type,
90157d89
PP
271 bt_object_release_func destroy)
272{
d4393e08
PP
273 int ret = 0;
274
3fea54f6 275 bt_object_init_shared(&iterator->base, destroy);
90157d89 276 iterator->type = type;
d6e69534
PP
277 iterator->msgs = g_ptr_array_new();
278 if (!iterator->msgs) {
d4393e08
PP
279 BT_LOGE_STR("Failed to allocate a GPtrArray.");
280 ret = -1;
281 goto end;
282 }
283
d6e69534 284 g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE);
d4393e08
PP
285
286end:
287 return ret;
90157d89
PP
288}
289
d94d92ac 290static
d6e69534
PP
291struct bt_self_component_port_input_message_iterator *
292bt_self_component_port_input_message_iterator_create_initial(
3230ee6b 293 struct bt_component *upstream_comp,
d94d92ac 294 struct bt_port *upstream_port)
47e5a032 295{
d4393e08 296 int ret;
d6e69534 297 struct bt_self_component_port_input_message_iterator *iterator = NULL;
47e5a032 298
f6ccaed9
PP
299 BT_ASSERT(upstream_comp);
300 BT_ASSERT(upstream_port);
f6ccaed9 301 BT_ASSERT(bt_port_is_connected(upstream_port));
d6e69534 302 BT_LIB_LOGD("Creating initial message iterator on self component input port: "
d94d92ac
PP
303 "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port);
304 BT_ASSERT(bt_component_get_class_type(upstream_comp) ==
305 BT_COMPONENT_CLASS_TYPE_SOURCE ||
306 bt_component_get_class_type(upstream_comp) ==
307 BT_COMPONENT_CLASS_TYPE_FILTER);
308 iterator = g_new0(
d6e69534 309 struct bt_self_component_port_input_message_iterator, 1);
47e5a032 310 if (!iterator) {
d94d92ac 311 BT_LOGE_STR("Failed to allocate one self component input port "
d6e69534 312 "message iterator.");
73d5c1ad 313 goto end;
47e5a032
JG
314 }
315
d6e69534
PP
316 ret = init_message_iterator((void *) iterator,
317 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT,
318 bt_self_component_port_input_message_iterator_destroy);
d4393e08 319 if (ret) {
d6e69534 320 /* init_message_iterator() logs errors */
d94d92ac 321 BT_OBJECT_PUT_REF_AND_RESET(iterator);
d4393e08
PP
322 goto end;
323 }
3230ee6b
PP
324
325 iterator->stream_states = g_hash_table_new_full(g_direct_hash,
326 g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state);
327 if (!iterator->stream_states) {
5af447e5 328 BT_LOGE_STR("Failed to allocate a GHashTable.");
d94d92ac 329 BT_OBJECT_PUT_REF_AND_RESET(iterator);
73d5c1ad 330 goto end;
3230ee6b
PP
331 }
332
bd14d768
PP
333 iterator->upstream_component = upstream_comp;
334 iterator->upstream_port = upstream_port;
d94d92ac 335 iterator->connection = iterator->upstream_port->connection;
5c563278 336 iterator->graph = bt_component_borrow_graph(upstream_comp);
d6e69534
PP
337 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED;
338 BT_LIB_LOGD("Created initial message iterator on self component input port: "
d94d92ac
PP
339 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
340 upstream_port, upstream_comp, iterator);
3230ee6b 341
47e5a032 342end:
d94d92ac 343 return iterator;
47e5a032
JG
344}
345
d6e69534
PP
346struct bt_self_component_port_input_message_iterator *
347bt_self_component_port_input_message_iterator_create(
d94d92ac 348 struct bt_self_component_port_input *self_port)
ea8d3e58 349{
d6e69534 350 typedef enum bt_self_message_iterator_status (*init_method_t)(
d94d92ac
PP
351 void *, void *, void *);
352
353 init_method_t init_method = NULL;
d6e69534 354 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac
PP
355 NULL;
356 struct bt_port *port = (void *) self_port;
357 struct bt_port *upstream_port;
358 struct bt_component *comp;
359 struct bt_component *upstream_comp;
360 struct bt_component_class *upstream_comp_cls;
361
362 BT_ASSERT_PRE_NON_NULL(port, "Port");
0d72b8c3 363 comp = bt_port_borrow_component_inline(port);
d94d92ac
PP
364 BT_ASSERT_PRE(bt_port_is_connected(port),
365 "Port is not connected: %![port-]+p", port);
366 BT_ASSERT_PRE(comp, "Port is not part of a component: %![port-]+p",
367 port);
368 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp),
369 "Port's component's graph is canceled: "
370 "%![port-]+p, %![comp-]+c", port, comp);
371 BT_ASSERT(port->connection);
372 upstream_port = port->connection->upstream_port;
373 BT_ASSERT(upstream_port);
0d72b8c3 374 upstream_comp = bt_port_borrow_component_inline(upstream_port);
d94d92ac
PP
375 BT_ASSERT(upstream_comp);
376 upstream_comp_cls = upstream_comp->class;
377 BT_ASSERT(upstream_comp->class->type ==
378 BT_COMPONENT_CLASS_TYPE_SOURCE ||
379 upstream_comp->class->type ==
380 BT_COMPONENT_CLASS_TYPE_FILTER);
d6e69534 381 iterator = bt_self_component_port_input_message_iterator_create_initial(
d94d92ac
PP
382 upstream_comp, upstream_port);
383 if (!iterator) {
384 BT_LOGW_STR("Cannot create self component input port "
d6e69534 385 "message iterator.");
d94d92ac
PP
386 goto end;
387 }
890882ef 388
d94d92ac
PP
389 switch (upstream_comp_cls->type) {
390 case BT_COMPONENT_CLASS_TYPE_SOURCE:
391 {
392 struct bt_component_class_source *src_comp_cls =
393 (void *) upstream_comp_cls;
394
395 init_method =
d6e69534 396 (init_method_t) src_comp_cls->methods.msg_iter_init;
d94d92ac
PP
397 break;
398 }
399 case BT_COMPONENT_CLASS_TYPE_FILTER:
400 {
401 struct bt_component_class_filter *flt_comp_cls =
402 (void *) upstream_comp_cls;
403
404 init_method =
d6e69534 405 (init_method_t) flt_comp_cls->methods.msg_iter_init;
d94d92ac
PP
406 break;
407 }
408 default:
409 /* Unreachable */
410 abort();
411 }
412
413 if (init_method) {
414 int iter_status;
415
416 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
417 iter_status = init_method(iterator, upstream_comp,
418 upstream_port);
419 BT_LOGD("User method returned: status=%s",
d6e69534
PP
420 bt_message_iterator_status_string(iter_status));
421 if (iter_status != BT_MESSAGE_ITERATOR_STATUS_OK) {
d94d92ac
PP
422 BT_LOGW_STR("Initialization method failed.");
423 goto end;
424 }
425 }
426
d6e69534 427 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE;
d94d92ac 428 g_ptr_array_add(port->connection->iterators, iterator);
d6e69534 429 BT_LIB_LOGD("Created message iterator on self component input port: "
d94d92ac
PP
430 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
431 upstream_port, upstream_comp, iterator);
432
433end:
434 return iterator;
ea8d3e58
JG
435}
436
d6e69534
PP
437void *bt_self_message_iterator_get_data(
438 const struct bt_self_message_iterator *self_iterator)
ea8d3e58 439{
d6e69534 440 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac 441 (void *) self_iterator;
ea8d3e58 442
d6e69534 443 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
d94d92ac 444 return iterator->user_data;
8738a040 445}
413bc2c4 446
d6e69534
PP
447void bt_self_message_iterator_set_data(
448 struct bt_self_message_iterator *self_iterator, void *data)
5c563278 449{
d6e69534 450 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac 451 (void *) self_iterator;
5c563278 452
d6e69534 453 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
d94d92ac 454 iterator->user_data = data;
d6e69534 455 BT_LIB_LOGV("Set message iterator's user data: "
d94d92ac 456 "%!+i, user-data-addr=%p", iterator, data);
5c563278
PP
457}
458
f42867e2
PP
459BT_ASSERT_PRE_FUNC
460static inline
d6e69534 461void bt_message_borrow_packet_stream(const struct bt_message *msg,
40f4ba76
PP
462 const struct bt_stream **stream,
463 const struct bt_packet **packet)
fa054faf 464{
d6e69534 465 BT_ASSERT(msg);
fa054faf 466
d6e69534
PP
467 switch (msg->type) {
468 case BT_MESSAGE_TYPE_EVENT:
40f4ba76 469 *packet = bt_event_borrow_packet_const(
d6e69534 470 bt_message_event_borrow_event_const(msg));
40f4ba76 471 *stream = bt_packet_borrow_stream_const(*packet);
fa054faf 472 break;
d6e69534
PP
473 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
474 *stream = bt_message_stream_beginning_borrow_stream_const(msg);
fa054faf 475 break;
d6e69534
PP
476 case BT_MESSAGE_TYPE_STREAM_END:
477 *stream = bt_message_stream_end_borrow_stream_const(msg);
fa054faf 478 break;
d6e69534
PP
479 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
480 *packet = bt_message_packet_beginning_borrow_packet_const(msg);
40f4ba76 481 *stream = bt_packet_borrow_stream_const(*packet);
fa054faf 482 break;
d6e69534
PP
483 case BT_MESSAGE_TYPE_PACKET_END:
484 *packet = bt_message_packet_end_borrow_packet_const(msg);
40f4ba76 485 *stream = bt_packet_borrow_stream_const(*packet);
2ec84d26 486 break;
fa054faf 487 default:
f42867e2 488 break;
fa054faf 489 }
fa054faf
PP
490}
491
f42867e2
PP
492BT_ASSERT_PRE_FUNC
493static inline
d6e69534
PP
494bool validate_message(
495 struct bt_self_component_port_input_message_iterator *iterator,
496 const struct bt_message *c_msg)
3230ee6b 497{
f42867e2 498 bool is_valid = true;
3230ee6b 499 struct stream_state *stream_state;
40f4ba76
PP
500 const struct bt_stream *stream = NULL;
501 const struct bt_packet *packet = NULL;
d6e69534 502 struct bt_message *msg = (void *) c_msg;
f42867e2 503
d6e69534
PP
504 BT_ASSERT(msg);
505 bt_message_borrow_packet_stream(c_msg, &stream, &packet);
f42867e2
PP
506
507 if (!stream) {
d6e69534 508 /* we don't care about messages not attached to streams */
f42867e2
PP
509 goto end;
510 }
3230ee6b 511
f42867e2
PP
512 stream_state = g_hash_table_lookup(iterator->stream_states, stream);
513 if (!stream_state) {
3230ee6b 514 /*
d6e69534
PP
515 * No stream state for this stream: this message
516 * MUST be a BT_MESSAGE_TYPE_STREAM_BEGINNING message
f42867e2 517 * and its sequence number must be 0.
3230ee6b 518 */
d6e69534
PP
519 if (c_msg->type != BT_MESSAGE_TYPE_STREAM_BEGINNING) {
520 BT_ASSERT_PRE_MSG("Unexpected message: missing a "
521 "BT_MESSAGE_TYPE_STREAM_BEGINNING "
522 "message prior to this message: "
f42867e2
PP
523 "%![stream-]+s", stream);
524 is_valid = false;
3230ee6b
PP
525 goto end;
526 }
527
d6e69534
PP
528 if (c_msg->seq_num == -1ULL) {
529 msg->seq_num = 0;
3230ee6b
PP
530 }
531
d6e69534
PP
532 if (c_msg->seq_num != 0) {
533 BT_ASSERT_PRE_MSG("Unexpected message sequence "
534 "number for this message iterator: "
535 "this is the first message for this "
f42867e2
PP
536 "stream, expecting sequence number 0: "
537 "seq-num=%" PRIu64 ", %![stream-]+s",
d6e69534 538 c_msg->seq_num, stream);
f42867e2 539 is_valid = false;
3230ee6b 540 goto end;
3230ee6b 541 }
3230ee6b 542
f42867e2
PP
543 stream_state = create_stream_state(stream);
544 if (!stream_state) {
545 abort();
546 }
fa054faf 547
40f4ba76
PP
548 g_hash_table_insert(iterator->stream_states,
549 (void *) stream, stream_state);
d6e69534 550 stream_state->expected_msg_seq_num++;
f42867e2 551 goto end;
fa054faf
PP
552 }
553
f42867e2
PP
554 if (stream_state->is_ended) {
555 /*
d6e69534 556 * There's a new message which has a reference to a
f42867e2 557 * stream which, from this iterator's point of view, is
d6e69534 558 * ended ("end of stream" message was returned).
f42867e2
PP
559 * This is bad: the API guarantees that it can never
560 * happen.
561 */
562 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
563 stream);
564 is_valid = false;
fa054faf
PP
565 goto end;
566 }
567
d6e69534
PP
568 if (c_msg->seq_num == -1ULL) {
569 msg->seq_num = stream_state->expected_msg_seq_num;
3230ee6b
PP
570 }
571
d6e69534
PP
572 if (c_msg->seq_num != -1ULL &&
573 c_msg->seq_num != stream_state->expected_msg_seq_num) {
574 BT_ASSERT_PRE_MSG("Unexpected message sequence number: "
f42867e2
PP
575 "seq-num=%" PRIu64 ", "
576 "expected-seq-num=%" PRIu64 ", %![stream-]+s",
d6e69534 577 c_msg->seq_num, stream_state->expected_msg_seq_num,
f42867e2
PP
578 stream);
579 is_valid = false;
fa054faf
PP
580 goto end;
581 }
582
d6e69534
PP
583 switch (c_msg->type) {
584 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
585 BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_STREAM_BEGINNING "
586 "message at this point: msg-seq-num=%" PRIu64 ", "
587 "%![stream-]+s", c_msg->seq_num, stream);
f42867e2
PP
588 is_valid = false;
589 goto end;
d6e69534 590 case BT_MESSAGE_TYPE_STREAM_END:
f42867e2 591 if (stream_state->cur_packet) {
d6e69534
PP
592 BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_STREAM_END "
593 "message: missing a "
594 "BT_MESSAGE_TYPE_PACKET_END message "
595 "prior to this message: "
596 "msg-seq-num=%" PRIu64 ", "
597 "%![stream-]+s", c_msg->seq_num, stream);
f42867e2
PP
598 is_valid = false;
599 goto end;
600 }
d6e69534 601 stream_state->expected_msg_seq_num++;
f42867e2
PP
602 stream_state->is_ended = true;
603 goto end;
d6e69534 604 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
f42867e2 605 if (stream_state->cur_packet) {
d6e69534
PP
606 BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_PACKET_BEGINNING "
607 "message at this point: missing a "
608 "BT_MESSAGE_TYPE_PACKET_END message "
609 "prior to this message: "
610 "msg-seq-num=%" PRIu64 ", %![stream-]+s, "
611 "%![packet-]+a", c_msg->seq_num, stream,
f42867e2
PP
612 packet);
613 is_valid = false;
614 goto end;
615 }
d6e69534 616 stream_state->expected_msg_seq_num++;
398454ed
PP
617 stream_state->cur_packet = packet;
618 bt_object_get_no_null_check(stream_state->cur_packet);
f42867e2 619 goto end;
d6e69534 620 case BT_MESSAGE_TYPE_PACKET_END:
f42867e2 621 if (!stream_state->cur_packet) {
d6e69534
PP
622 BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_PACKET_END "
623 "message at this point: missing a "
624 "BT_MESSAGE_TYPE_PACKET_BEGINNING message "
625 "prior to this message: "
626 "msg-seq-num=%" PRIu64 ", %![stream-]+s, "
627 "%![packet-]+a", c_msg->seq_num, stream,
f42867e2
PP
628 packet);
629 is_valid = false;
630 goto end;
631 }
d6e69534 632 stream_state->expected_msg_seq_num++;
65300d60 633 BT_OBJECT_PUT_REF_AND_RESET(stream_state->cur_packet);
f42867e2 634 goto end;
d6e69534 635 case BT_MESSAGE_TYPE_EVENT:
f42867e2
PP
636 if (packet != stream_state->cur_packet) {
637 BT_ASSERT_PRE_MSG("Unexpected packet for "
d6e69534
PP
638 "BT_MESSAGE_TYPE_EVENT message: "
639 "msg-seq-num=%" PRIu64 ", %![stream-]+s, "
640 "%![msg-packet-]+a, %![expected-packet-]+a",
641 c_msg->seq_num, stream,
f42867e2
PP
642 stream_state->cur_packet, packet);
643 is_valid = false;
644 goto end;
645 }
d6e69534 646 stream_state->expected_msg_seq_num++;
f42867e2
PP
647 goto end;
648 default:
649 break;
3230ee6b
PP
650 }
651
3230ee6b 652end:
f42867e2 653 return is_valid;
3230ee6b
PP
654}
655
d4393e08
PP
656BT_ASSERT_PRE_FUNC
657static inline
d6e69534
PP
658bool validate_messages(
659 struct bt_self_component_port_input_message_iterator *iterator,
d4393e08
PP
660 uint64_t count)
661{
662 bool ret = true;
d6e69534
PP
663 bt_message_array_const msgs =
664 (void *) iterator->base.msgs->pdata;
d4393e08
PP
665 uint64_t i;
666
667 for (i = 0; i < count; i++) {
d6e69534 668 ret = validate_message(iterator, msgs[i]);
d4393e08
PP
669 if (!ret) {
670 break;
671 }
672 }
673
674 return ret;
675}
676
f42867e2 677BT_ASSERT_PRE_FUNC
d6e69534
PP
678static inline bool self_comp_port_input_msg_iter_can_end(
679 struct bt_self_component_port_input_message_iterator *iterator)
3230ee6b 680{
f42867e2
PP
681 GHashTableIter iter;
682 gpointer stream_key, state_value;
683 bool ret = true;
3230ee6b 684
f42867e2
PP
685 /*
686 * Verify that this iterator received a
d6e69534 687 * BT_MESSAGE_TYPE_STREAM_END message for each stream
f42867e2
PP
688 * which has a state.
689 */
3230ee6b 690
f42867e2 691 g_hash_table_iter_init(&iter, iterator->stream_states);
3230ee6b 692
f42867e2
PP
693 while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) {
694 struct stream_state *stream_state = (void *) state_value;
3230ee6b 695
f42867e2
PP
696 BT_ASSERT(stream_state);
697 BT_ASSERT(stream_key);
fa054faf 698
f42867e2 699 if (!stream_state->is_ended) {
d6e69534 700 BT_ASSERT_PRE_MSG("Ending message iterator, "
f42867e2
PP
701 "but stream is not ended: "
702 "%![stream-]s", stream_key);
703 ret = false;
704 goto end;
705 }
3230ee6b
PP
706 }
707
3230ee6b 708end:
3230ee6b
PP
709 return ret;
710}
711
d6e69534
PP
712enum bt_message_iterator_status
713bt_self_component_port_input_message_iterator_next(
714 struct bt_self_component_port_input_message_iterator *iterator,
715 bt_message_array_const *msgs, uint64_t *user_count)
3230ee6b 716{
d6e69534
PP
717 typedef enum bt_self_message_iterator_status (*method_t)(
718 void *, bt_message_array_const, uint64_t, uint64_t *);
d94d92ac
PP
719
720 method_t method = NULL;
721 struct bt_component_class *comp_cls;
d6e69534 722 int status = BT_MESSAGE_ITERATOR_STATUS_OK;
d94d92ac 723
d6e69534
PP
724 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
725 BT_ASSERT_PRE_NON_NULL(msgs, "Message array (output)");
726 BT_ASSERT_PRE_NON_NULL(user_count, "Message count (output)");
f42867e2 727 BT_ASSERT_PRE(iterator->state ==
d6e69534
PP
728 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE,
729 "Message iterator's \"next\" called, but "
f42867e2
PP
730 "iterator is in the wrong state: %!+i", iterator);
731 BT_ASSERT(iterator->upstream_component);
732 BT_ASSERT(iterator->upstream_component->class);
d94d92ac 733 BT_LIB_LOGD("Getting next self component input port "
d6e69534 734 "message iterator's messages: %!+i", iterator);
d94d92ac 735 comp_cls = iterator->upstream_component->class;
d3eb6e8f 736
3230ee6b 737 /* Pick the appropriate "next" method */
d94d92ac 738 switch (comp_cls->type) {
d3eb6e8f
PP
739 case BT_COMPONENT_CLASS_TYPE_SOURCE:
740 {
d94d92ac
PP
741 struct bt_component_class_source *src_comp_cls =
742 (void *) comp_cls;
d3eb6e8f 743
d6e69534 744 method = (method_t) src_comp_cls->methods.msg_iter_next;
d3eb6e8f
PP
745 break;
746 }
747 case BT_COMPONENT_CLASS_TYPE_FILTER:
748 {
d94d92ac
PP
749 struct bt_component_class_filter *flt_comp_cls =
750 (void *) comp_cls;
d3eb6e8f 751
d6e69534 752 method = (method_t) flt_comp_cls->methods.msg_iter_next;
d3eb6e8f
PP
753 break;
754 }
755 default:
0fbb9a9f 756 abort();
d3eb6e8f
PP
757 }
758
3230ee6b 759 /*
d6e69534 760 * Call the user's "next" method to get the next messages
fa054faf 761 * and status.
3230ee6b 762 */
d94d92ac 763 BT_ASSERT(method);
f42867e2 764 BT_LOGD_STR("Calling user's \"next\" method.");
d94d92ac 765 status = method(iterator,
d6e69534
PP
766 (void *) iterator->base.msgs->pdata,
767 MSG_BATCH_SIZE, user_count);
f42867e2 768 BT_LOGD("User method returned: status=%s",
d6e69534 769 bt_message_iterator_status_string(status));
d4393e08 770 if (status < 0) {
f42867e2 771 BT_LOGW_STR("User method failed.");
f42867e2
PP
772 goto end;
773 }
3230ee6b 774
d6e69534
PP
775 if (iterator->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED ||
776 iterator->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED_AND_ENDED) {
f42867e2
PP
777 /*
778 * The user's "next" method, somehow, cancelled its own
d6e69534 779 * message iterator. This can happen, for example,
f42867e2
PP
780 * when the user's method removes the port on which
781 * there's the connection from which the iterator was
782 * created. In this case, said connection is ended, and
d6e69534 783 * all its message iterators are finalized.
f42867e2 784 *
d6e69534
PP
785 * Only bt_object_put_ref() the returned message if
786 * the status is BT_MESSAGE_ITERATOR_STATUS_OK
d94d92ac 787 * because otherwise this field could be garbage.
f42867e2 788 */
d6e69534 789 if (status == BT_MESSAGE_ITERATOR_STATUS_OK) {
d4393e08 790 uint64_t i;
d6e69534
PP
791 bt_message_array_const msgs =
792 (void *) iterator->base.msgs->pdata;
d4393e08
PP
793
794 for (i = 0; i < *user_count; i++) {
d6e69534 795 bt_object_put_ref(msgs[i]);
d4393e08 796 }
3230ee6b
PP
797 }
798
d6e69534 799 status = BT_MESSAGE_ITERATOR_STATUS_CANCELED;
f42867e2
PP
800 goto end;
801 }
8cf27cc5 802
d4393e08 803 switch (status) {
d6e69534
PP
804 case BT_MESSAGE_ITERATOR_STATUS_OK:
805 BT_ASSERT_PRE(validate_messages(iterator, *user_count),
806 "Messages are invalid at this point: "
807 "%![msg-iter-]+i, count=%" PRIu64,
d4393e08 808 iterator, *user_count);
d6e69534 809 *msgs = (void *) iterator->base.msgs->pdata;
d4393e08 810 break;
d6e69534 811 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
d4393e08 812 goto end;
d6e69534
PP
813 case BT_MESSAGE_ITERATOR_STATUS_END:
814 BT_ASSERT_PRE(self_comp_port_input_msg_iter_can_end(iterator),
815 "Message iterator cannot end at this point: "
f42867e2
PP
816 "%!+i", iterator);
817 BT_ASSERT(iterator->state ==
d6e69534
PP
818 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
819 iterator->state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED;
f42867e2 820 BT_LOGD("Set new status: status=%s",
d6e69534 821 bt_message_iterator_status_string(status));
f42867e2 822 goto end;
f42867e2
PP
823 default:
824 /* Unknown non-error status */
825 abort();
41a2b7ae
PP
826 }
827
828end:
3230ee6b
PP
829 return status;
830}
831
d6e69534
PP
832enum bt_message_iterator_status
833bt_port_output_message_iterator_next(
834 struct bt_port_output_message_iterator *iterator,
835 bt_message_array_const *msgs_to_user,
d4393e08 836 uint64_t *count_to_user)
3230ee6b 837{
d6e69534 838 enum bt_message_iterator_status status;
07245ac2 839 enum bt_graph_status graph_status;
3230ee6b 840
d6e69534
PP
841 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
842 BT_ASSERT_PRE_NON_NULL(msgs_to_user, "Message array (output)");
843 BT_ASSERT_PRE_NON_NULL(count_to_user, "Message count (output)");
844 BT_LIB_LOGD("Getting next output port message iterator's messages: "
07245ac2 845 "%!+i", iterator);
d4393e08 846
d94d92ac
PP
847 graph_status = bt_graph_consume_sink_no_check(iterator->graph,
848 iterator->colander);
07245ac2
PP
849 switch (graph_status) {
850 case BT_GRAPH_STATUS_CANCELED:
07245ac2 851 case BT_GRAPH_STATUS_AGAIN:
07245ac2 852 case BT_GRAPH_STATUS_END:
07245ac2 853 case BT_GRAPH_STATUS_NOMEM:
d94d92ac 854 status = (int) graph_status;
07245ac2
PP
855 break;
856 case BT_GRAPH_STATUS_OK:
d6e69534 857 status = BT_MESSAGE_ITERATOR_STATUS_OK;
d4393e08
PP
858
859 /*
d6e69534 860 * On success, the colander sink moves the messages
d4393e08 861 * to this iterator's array and sets this iterator's
d6e69534 862 * message count: move them to the user.
d4393e08 863 */
d6e69534 864 *msgs_to_user = (void *) iterator->base.msgs->pdata;
d94d92ac 865 *count_to_user = iterator->count;
90157d89 866 break;
90157d89 867 default:
07245ac2 868 /* Other errors */
d6e69534 869 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
90157d89 870 }
3230ee6b 871
3230ee6b 872 return status;
53d45b87
JG
873}
874
d6e69534
PP
875struct bt_component *bt_self_component_port_input_message_iterator_borrow_component(
876 struct bt_self_component_port_input_message_iterator *iterator)
d94d92ac 877{
d6e69534 878 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
d94d92ac
PP
879 return iterator->upstream_component;
880}
881
d6e69534
PP
882struct bt_self_component *bt_self_message_iterator_borrow_component(
883 struct bt_self_message_iterator *self_iterator)
413bc2c4 884{
d6e69534 885 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac 886 (void *) self_iterator;
90157d89 887
d6e69534 888 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
d94d92ac 889 return (void *) iterator->upstream_component;
413bc2c4
JG
890}
891
d6e69534
PP
892struct bt_self_port_output *bt_self_message_iterator_borrow_port(
893 struct bt_self_message_iterator *self_iterator)
91457551 894{
d6e69534 895 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac
PP
896 (void *) self_iterator;
897
d6e69534 898 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
d94d92ac 899 return (void *) iterator->upstream_port;
91457551 900}
8ed535b5
PP
901
902static
d6e69534 903void bt_port_output_message_iterator_destroy(struct bt_object *obj)
8ed535b5 904{
d6e69534 905 struct bt_port_output_message_iterator *iterator = (void *) obj;
8ed535b5 906
d6e69534 907 BT_LIB_LOGD("Destroying output port message iterator object: %!+i",
8ed535b5
PP
908 iterator);
909 BT_LOGD_STR("Putting graph.");
d94d92ac 910 BT_OBJECT_PUT_REF_AND_RESET(iterator->graph);
8ed535b5 911 BT_LOGD_STR("Putting colander sink component.");
d94d92ac 912 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
d6e69534 913 destroy_base_message_iterator(obj);
8ed535b5
PP
914}
915
d6e69534
PP
916struct bt_port_output_message_iterator *
917bt_port_output_message_iterator_create(
0d72b8c3
PP
918 struct bt_graph *graph,
919 const struct bt_port_output *output_port)
8ed535b5 920{
d6e69534 921 struct bt_port_output_message_iterator *iterator = NULL;
d94d92ac 922 struct bt_component_class_sink *colander_comp_cls = NULL;
8ed535b5 923 struct bt_component *output_port_comp = NULL;
d94d92ac 924 struct bt_component_sink *colander_comp;
8ed535b5 925 enum bt_graph_status graph_status;
d94d92ac 926 struct bt_port_input *colander_in_port = NULL;
8ed535b5 927 struct bt_component_class_sink_colander_data colander_data;
d4393e08 928 int ret;
8ed535b5 929
d94d92ac 930 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
f42867e2 931 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
0d72b8c3
PP
932 output_port_comp = bt_port_borrow_component_inline(
933 (const void *) output_port);
f42867e2
PP
934 BT_ASSERT_PRE(output_port_comp,
935 "Output port has no component: %!+p", output_port);
d94d92ac
PP
936 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) ==
937 (void *) graph,
938 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
939 graph, output_port);
8ed535b5 940
d6e69534
PP
941 /* Create message iterator */
942 BT_LIB_LOGD("Creating message iterator on output port: "
d94d92ac 943 "%![port-]+p, %![comp-]+c", output_port, output_port_comp);
d6e69534 944 iterator = g_new0(struct bt_port_output_message_iterator, 1);
8ed535b5 945 if (!iterator) {
d6e69534 946 BT_LOGE_STR("Failed to allocate one output port message iterator.");
8ed535b5
PP
947 goto error;
948 }
949
d6e69534
PP
950 ret = init_message_iterator((void *) iterator,
951 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT,
952 bt_port_output_message_iterator_destroy);
d4393e08 953 if (ret) {
d6e69534 954 /* init_message_iterator() logs errors */
65300d60 955 BT_OBJECT_PUT_REF_AND_RESET(iterator);
d4393e08
PP
956 goto end;
957 }
8ed535b5
PP
958
959 /* Create colander component */
960 colander_comp_cls = bt_component_class_sink_colander_get();
961 if (!colander_comp_cls) {
962 BT_LOGW("Cannot get colander sink component class.");
963 goto error;
964 }
965
398454ed
PP
966 iterator->graph = graph;
967 bt_object_get_no_null_check(iterator->graph);
d6e69534 968 colander_data.msgs = (void *) iterator->base.msgs->pdata;
d4393e08 969 colander_data.count_addr = &iterator->count;
5fd91d88
PP
970
971 /* Hope that nobody uses this very unique name */
d94d92ac 972 graph_status =
0d72b8c3 973 bt_graph_add_sink_component_with_init_method_data(
5fd91d88
PP
974 (void *) graph, colander_comp_cls,
975 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
0d72b8c3 976 NULL, &colander_data, (void *) &iterator->colander);
8ed535b5 977 if (graph_status != BT_GRAPH_STATUS_OK) {
d94d92ac
PP
978 BT_LIB_LOGW("Cannot add colander sink component to graph: "
979 "%1[graph-]+g, status=%s", graph,
8ed535b5
PP
980 bt_graph_status_string(graph_status));
981 goto error;
982 }
983
984 /*
985 * Connect provided output port to the colander component's
986 * input port.
987 */
0d72b8c3
PP
988 colander_in_port =
989 (void *) bt_component_sink_borrow_input_port_by_index_const(
990 (void *) iterator->colander, 0);
f6ccaed9 991 BT_ASSERT(colander_in_port);
0d72b8c3 992 graph_status = bt_graph_connect_ports(graph,
8ed535b5
PP
993 output_port, colander_in_port, NULL);
994 if (graph_status != BT_GRAPH_STATUS_OK) {
d94d92ac
PP
995 BT_LIB_LOGW("Cannot add colander sink component to graph: "
996 "%![graph-]+g, %![comp-]+c, status=%s", graph,
997 iterator->colander,
8ed535b5
PP
998 bt_graph_status_string(graph_status));
999 goto error;
1000 }
1001
1002 /*
1003 * At this point everything went fine. Make the graph
d6e69534 1004 * nonconsumable forever so that only this message iterator
8ed535b5 1005 * can consume (thanks to bt_graph_consume_sink_no_check()).
d6e69534
PP
1006 * This avoids leaking the message created by the colander
1007 * sink and moved to the message iterator's message
07245ac2 1008 * member.
8ed535b5 1009 */
d94d92ac 1010 bt_graph_set_can_consume(iterator->graph, false);
8ed535b5
PP
1011 goto end;
1012
1013error:
1014 if (iterator && iterator->graph && iterator->colander) {
1015 int ret;
1016
1017 /* Remove created colander component from graph if any */
1018 colander_comp = iterator->colander;
65300d60 1019 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
8ed535b5
PP
1020
1021 /*
1022 * At this point the colander component's reference
1023 * count is 0 because iterator->colander was the only
1024 * owner. We also know that it is not connected because
1025 * this is the last operation before this function
1026 * succeeds.
1027 *
1028 * Since we honor the preconditions here,
1029 * bt_graph_remove_unconnected_component() always
1030 * succeeds.
1031 */
1032 ret = bt_graph_remove_unconnected_component(iterator->graph,
d94d92ac 1033 (void *) colander_comp);
f6ccaed9 1034 BT_ASSERT(ret == 0);
8ed535b5
PP
1035 }
1036
65300d60 1037 BT_OBJECT_PUT_REF_AND_RESET(iterator);
8ed535b5
PP
1038
1039end:
65300d60 1040 bt_object_put_ref(colander_comp_cls);
8ed535b5
PP
1041 return (void *) iterator;
1042}
c5b9b441 1043
d6e69534
PP
1044void bt_port_output_message_iterator_get_ref(
1045 const struct bt_port_output_message_iterator *iterator)
c5b9b441
PP
1046{
1047 bt_object_get_ref(iterator);
1048}
1049
d6e69534
PP
1050void bt_port_output_message_iterator_put_ref(
1051 const struct bt_port_output_message_iterator *iterator)
c5b9b441
PP
1052{
1053 bt_object_put_ref(iterator);
1054}
1055
d6e69534
PP
1056void bt_self_component_port_input_message_iterator_get_ref(
1057 const struct bt_self_component_port_input_message_iterator *iterator)
c5b9b441
PP
1058{
1059 bt_object_get_ref(iterator);
1060}
1061
d6e69534
PP
1062void bt_self_component_port_input_message_iterator_put_ref(
1063 const struct bt_self_component_port_input_message_iterator *iterator)
c5b9b441
PP
1064{
1065 bt_object_put_ref(iterator);
1066}
This page took 0.099024 seconds and 4 git commands to generate.