Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / lib / graph / message / packet.c
CommitLineData
b09a5592
PP
1/*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
b03487ab 24#define BT_LOG_TAG "LIB/MSG-PACKET"
1633ef46 25#include "lib/logging.h"
b09a5592 26
969c1d8a
FD
27#include <stdbool.h>
28
0341b021
PP
29#include "lib/assert-pre.h"
30#include "lib/assert-post.h"
57952005 31#include "compat/compiler.h"
71c5da58 32#include <babeltrace2/trace-ir/packet.h>
57952005 33#include "lib/trace-ir/packet.h"
71c5da58
MJ
34#include <babeltrace2/trace-ir/stream-class.h>
35#include <babeltrace2/trace-ir/stream.h>
57952005
MJ
36#include "lib/trace-ir/stream.h"
37#include "lib/trace-ir/stream-class.h"
38#include "lib/graph/graph.h"
71c5da58
MJ
39#include <babeltrace2/graph/message-packet-beginning-const.h>
40#include <babeltrace2/graph/message-packet-end-const.h>
41#include <babeltrace2/graph/message-packet-beginning.h>
42#include <babeltrace2/graph/message-packet-end.h>
57952005 43#include "common/assert.h"
57952005 44#include "lib/object.h"
b09a5592
PP
45#include <inttypes.h>
46
57952005
MJ
47#include "packet.h"
48
a1f053a9
PP
49static inline
50struct bt_message *new_packet_message(struct bt_graph *graph,
51 enum bt_message_type type, bt_object_release_func recycle_func)
b09a5592 52{
a1f053a9 53 struct bt_message_packet *message;
b09a5592 54
a1f053a9 55 message = g_new0(struct bt_message_packet, 1);
b09a5592 56 if (!message) {
a8f90e5d
PP
57 BT_LIB_LOGE_APPEND_CAUSE(
58 "Failed to allocate one packet message.");
b09a5592
PP
59 goto error;
60 }
61
a1f053a9 62 bt_message_init(&message->parent, type, recycle_func, graph);
b09a5592
PP
63 goto end;
64
65error:
66 BT_OBJECT_PUT_REF_AND_RESET(message);
67
68end:
69 return (void *) message;
70}
71
a1f053a9
PP
72BT_HIDDEN
73struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph)
b09a5592 74{
a1f053a9
PP
75 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_BEGINNING,
76 (bt_object_release_func) bt_message_packet_beginning_recycle);
77}
78
79BT_HIDDEN
80struct bt_message *bt_message_packet_end_new(struct bt_graph *graph)
81{
82 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_END,
83 (bt_object_release_func) bt_message_packet_end_recycle);
84}
85
86static inline
96a0a69d 87struct bt_message *create_packet_message(
a1f053a9 88 struct bt_self_component_port_input_message_iterator *msg_iter,
96a0a69d 89 struct bt_packet *packet, struct bt_object_pool *pool,
fb0977e1 90 bool with_cs, uint64_t raw_value)
a1f053a9
PP
91{
92 struct bt_message_packet *message = NULL;
b09a5592
PP
93 struct bt_stream *stream;
94 struct bt_stream_class *stream_class;
e934e1b6 95 bool need_cs;
b09a5592 96
a1f053a9 97 BT_ASSERT(msg_iter);
b09a5592
PP
98 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
99 stream = bt_packet_borrow_stream(packet);
100 BT_ASSERT(stream);
101 stream_class = bt_stream_borrow_class(stream);
102 BT_ASSERT(stream_class);
34e13c22 103
37a93d41
PP
104 /*
105 * It's not possible to create a packet from a stream of which
106 * the class indicates that packets are not supported.
107 */
108 BT_ASSERT(stream_class->supports_packets);
109
fb0977e1 110 if (pool == &msg_iter->graph->packet_begin_msg_pool) {
e934e1b6 111 need_cs = stream_class->packets_have_beginning_default_clock_snapshot;
34e13c22 112 } else {
e934e1b6 113 need_cs = stream_class->packets_have_end_default_clock_snapshot;
34e13c22
PP
114 }
115
116 /*
117 * `packet_has_default_clock_snapshot` implies that the stream
118 * class has a default clock class (precondition).
119 */
e934e1b6 120 BT_ASSERT_PRE(need_cs ? with_cs : true,
3f511a69 121 "Unexpected stream class configuration when creating "
e934e1b6
PP
122 "a packet beginning or end message: "
123 "a default clock snapshot is needed, but none was provided: "
124 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
125 "cs-val=%" PRIu64,
126 stream, stream_class, with_cs, raw_value);
127 BT_ASSERT_PRE(!need_cs ? !with_cs : true,
128 "Unexpected stream class configuration when creating "
129 "a packet beginning or end message: "
130 "no default clock snapshot is needed, but one was provided: "
96a0a69d
PP
131 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
132 "cs-val=%" PRIu64,
133 stream, stream_class, with_cs, raw_value);
a1f053a9 134 BT_LIB_LOGD("Creating packet message object: "
b09a5592
PP
135 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
136 packet, stream, stream_class);
a1f053a9 137 message = (void *) bt_message_create_from_pool(pool, msg_iter->graph);
b09a5592
PP
138 if (!message) {
139 /* bt_message_create_from_pool() logs errors */
140 goto end;
141 }
142
96a0a69d
PP
143 if (with_cs) {
144 BT_ASSERT(stream_class->default_clock_class);
145 message->default_cs = bt_clock_snapshot_create(
146 stream_class->default_clock_class);
147 if (!message->default_cs) {
864aa43f 148 bt_object_put_ref_no_null_check(message);
96a0a69d
PP
149 message = NULL;
150 goto end;
151 }
152
153 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
154 }
155
b09a5592
PP
156 BT_ASSERT(!message->packet);
157 message->packet = packet;
864aa43f 158 bt_object_get_ref_no_null_check_no_parent_check(
b09a5592
PP
159 &message->packet->base);
160 bt_packet_set_is_frozen(packet, true);
a1f053a9 161 BT_LIB_LOGD("Created packet message object: "
b09a5592
PP
162 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
163 message, packet, stream, stream_class);
164 goto end;
165
166end:
167 return (void *) message;
168}
169
a1f053a9
PP
170struct bt_message *bt_message_packet_beginning_create(
171 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 172 const struct bt_packet *packet)
b09a5592 173{
a1f053a9
PP
174 struct bt_self_component_port_input_message_iterator *msg_iter =
175 (void *) self_msg_iter;
b09a5592 176
a1f053a9 177 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
96a0a69d 178 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 179 &msg_iter->graph->packet_begin_msg_pool, false, 0);
96a0a69d
PP
180}
181
182struct bt_message *bt_message_packet_beginning_create_with_default_clock_snapshot(
183 struct bt_self_message_iterator *self_msg_iter,
184 const struct bt_packet *packet, uint64_t raw_value)
185{
186 struct bt_self_component_port_input_message_iterator *msg_iter =
187 (void *) self_msg_iter;
188
189 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
190 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 191 &msg_iter->graph->packet_begin_msg_pool, true, raw_value);
b09a5592
PP
192}
193
a1f053a9
PP
194struct bt_message *bt_message_packet_end_create(
195 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 196 const struct bt_packet *packet)
b09a5592 197{
a1f053a9
PP
198 struct bt_self_component_port_input_message_iterator *msg_iter =
199 (void *) self_msg_iter;
b09a5592 200
a1f053a9 201 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
96a0a69d 202 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 203 &msg_iter->graph->packet_end_msg_pool, false, 0);
96a0a69d
PP
204}
205
206struct bt_message *bt_message_packet_end_create_with_default_clock_snapshot(
207 struct bt_self_message_iterator *self_msg_iter,
208 const struct bt_packet *packet, uint64_t raw_value)
209{
210 struct bt_self_component_port_input_message_iterator *msg_iter =
211 (void *) self_msg_iter;
212
213 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
214 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 215 &msg_iter->graph->packet_end_msg_pool, true, raw_value);
b09a5592
PP
216}
217
a1f053a9
PP
218BT_HIDDEN
219void bt_message_packet_destroy(struct bt_message *msg)
b09a5592 220{
a1f053a9 221 struct bt_message_packet *packet_msg = (void *) msg;
b09a5592 222
a1f053a9
PP
223 BT_LIB_LOGD("Destroying packet message: %!+n", msg);
224 BT_LIB_LOGD("Putting packet: %!+a", packet_msg->packet);
225 BT_OBJECT_PUT_REF_AND_RESET(packet_msg->packet);
96a0a69d
PP
226
227 if (packet_msg->default_cs) {
228 bt_clock_snapshot_recycle(packet_msg->default_cs);
229 packet_msg->default_cs = NULL;
230 }
231
a1f053a9 232 g_free(msg);
b09a5592
PP
233}
234
a1f053a9
PP
235static inline
236void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
b09a5592 237{
a1f053a9
PP
238 struct bt_message_packet *packet_msg = (void *) msg;
239
240 BT_LIB_LOGD("Recycling packet message: %!+n", msg);
241 bt_message_reset(msg);
864aa43f 242 bt_object_put_ref_no_null_check(&packet_msg->packet->base);
96a0a69d
PP
243
244 if (packet_msg->default_cs) {
245 bt_clock_snapshot_recycle(packet_msg->default_cs);
246 packet_msg->default_cs = NULL;
247 }
248
a1f053a9
PP
249 packet_msg->packet = NULL;
250 msg->graph = NULL;
251 bt_object_pool_recycle_object(pool, msg);
b09a5592
PP
252}
253
254BT_HIDDEN
a1f053a9 255void bt_message_packet_beginning_recycle(struct bt_message *msg)
b09a5592 256{
a1f053a9 257 BT_ASSERT(msg);
b09a5592 258
85e7137b 259 if (G_UNLIKELY(!msg->graph)) {
a1f053a9
PP
260 bt_message_packet_destroy(msg);
261 return;
b09a5592
PP
262 }
263
a1f053a9 264 recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool);
b09a5592
PP
265}
266
a1f053a9
PP
267BT_HIDDEN
268void bt_message_packet_end_recycle(struct bt_message *msg)
b09a5592 269{
a1f053a9 270 BT_ASSERT(msg);
b09a5592 271
85e7137b 272 if (G_UNLIKELY(!msg->graph)) {
a1f053a9
PP
273 bt_message_packet_destroy(msg);
274 return;
b09a5592
PP
275 }
276
a1f053a9 277 recycle_packet_message(msg, &msg->graph->packet_end_msg_pool);
b09a5592
PP
278}
279
a1f053a9
PP
280struct bt_packet *bt_message_packet_beginning_borrow_packet(
281 struct bt_message *message)
b09a5592 282{
a1f053a9 283 struct bt_message_packet *packet_msg = (void *) message;
b09a5592 284
fa6cfec3
PP
285 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
286 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
a1f053a9
PP
287 BT_MESSAGE_TYPE_PACKET_BEGINNING);
288 return packet_msg->packet;
b09a5592
PP
289}
290
a1f053a9
PP
291const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
292 const struct bt_message *message)
b09a5592 293{
a1f053a9
PP
294 return bt_message_packet_beginning_borrow_packet(
295 (void *) message);
b09a5592
PP
296}
297
298struct bt_packet *bt_message_packet_end_borrow_packet(
299 struct bt_message *message)
300{
a1f053a9 301 struct bt_message_packet *packet_msg = (void *) message;
b09a5592 302
fa6cfec3
PP
303 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
304 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
b09a5592 305 BT_MESSAGE_TYPE_PACKET_END);
a1f053a9 306 return packet_msg->packet;
b09a5592
PP
307}
308
309const struct bt_packet *bt_message_packet_end_borrow_packet_const(
310 const struct bt_message *message)
311{
312 return bt_message_packet_end_borrow_packet(
313 (void *) message);
314}
96a0a69d
PP
315
316static inline
11ddb3ef 317const struct bt_clock_snapshot *
96a0a69d 318borrow_packet_message_default_clock_snapshot_const(
11ddb3ef 319 const struct bt_message *message)
96a0a69d
PP
320{
321 struct bt_message_packet *packet_msg = (void *) message;
322
ec4a3354 323 BT_ASSERT_DBG(message);
fa6cfec3
PP
324 BT_ASSERT_PRE_DEV(
325 packet_msg->packet->stream->class->default_clock_class,
96a0a69d
PP
326 "Message's stream's class has no default clock class: "
327 "%![msg-]+n, %![sc-]+S",
328 message, packet_msg->packet->stream->class);
11ddb3ef 329 return packet_msg->default_cs;
96a0a69d
PP
330}
331
11ddb3ef 332const struct bt_clock_snapshot *
96a0a69d 333bt_message_packet_beginning_borrow_default_clock_snapshot_const(
11ddb3ef 334 const struct bt_message *msg)
96a0a69d 335{
fa6cfec3
PP
336 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
337 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
11ddb3ef 338 return borrow_packet_message_default_clock_snapshot_const(msg);
96a0a69d
PP
339}
340
11ddb3ef 341const struct bt_clock_snapshot *
96a0a69d 342bt_message_packet_end_borrow_default_clock_snapshot_const(
11ddb3ef 343 const struct bt_message *msg)
96a0a69d 344{
fa6cfec3
PP
345 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
346 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
11ddb3ef 347 return borrow_packet_message_default_clock_snapshot_const(msg);
96a0a69d 348}
2d2affbb
PP
349
350static inline
351const struct bt_clock_class *
352borrow_packet_message_stream_class_default_clock_class(
353 const struct bt_message *msg)
354{
355 struct bt_message_packet *packet_msg = (void *) msg;
356
ec4a3354 357 BT_ASSERT_DBG(msg);
2d2affbb
PP
358 return packet_msg->packet->stream->class->default_clock_class;
359}
360
361const struct bt_clock_class *
362bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
363 const struct bt_message *msg)
364{
fa6cfec3
PP
365 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
366 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
2d2affbb
PP
367 return borrow_packet_message_stream_class_default_clock_class(msg);
368}
369
370const struct bt_clock_class *
371bt_message_packet_end_borrow_stream_class_default_clock_class_const(
372 const struct bt_message *msg)
373{
fa6cfec3
PP
374 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
375 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
2d2affbb
PP
376 return borrow_packet_message_stream_class_default_clock_class(msg);
377}
This page took 0.068083 seconds and 4 git commands to generate.