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