Document libbabeltrace2's C API
[babeltrace.git] / src / lib / trace-ir / packet.c
CommitLineData
f79cf0f0 1/*
e2f7325d 2 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
f79cf0f0
PP
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
350ad6c1 23#define BT_LOG_TAG "LIB/PACKET"
c2d9d9cf 24#include "lib/logging.h"
8ea705f0 25
578e048b 26#include "lib/assert-pre.h"
3fadfbc0 27#include <babeltrace2/trace-ir/packet.h>
3fadfbc0 28#include <babeltrace2/trace-ir/trace.h>
3fadfbc0
MJ
29#include <babeltrace2/trace-ir/stream-class.h>
30#include <babeltrace2/trace-ir/stream.h>
578e048b
MJ
31#include "lib/object.h"
32#include "common/assert.h"
be514b0c 33#include <inttypes.h>
c4f23e30 34#include <stdbool.h>
f79cf0f0 35
578e048b
MJ
36#include "field.h"
37#include "field-wrapper.h"
38#include "packet.h"
39#include "stream-class.h"
40#include "stream.h"
41#include "trace.h"
d24d5663 42#include "lib/func-status.h"
578e048b 43
094ff7c0 44struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
f79cf0f0 45{
bdb288b3 46 BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet");
094ff7c0 47 return packet->stream;
f79cf0f0
PP
48}
49
40f4ba76
PP
50const struct bt_stream *bt_packet_borrow_stream_const(
51 const struct bt_packet *packet)
e5be10ef 52{
40f4ba76 53 return bt_packet_borrow_stream((void *) packet);
e5be10ef
PP
54}
55
44c440bc 56struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet)
f79cf0f0 57{
bdb288b3 58 BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet");
44c440bc 59 return packet->context_field ? packet->context_field->field : NULL;
312c056a 60}
c5c82c6e 61
40f4ba76
PP
62const struct bt_field *bt_packet_borrow_context_field_const(
63 const struct bt_packet *packet)
e5be10ef 64{
40f4ba76 65 return bt_packet_borrow_context_field((void *) packet);
e5be10ef
PP
66}
67
312c056a 68BT_HIDDEN
40f4ba76 69void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen)
312c056a 70{
59e3c4db 71 if (!packet) {
312c056a 72 return;
c5c82c6e
PP
73 }
74
44c440bc
PP
75 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
76 "is-frozen=%d", packet, is_frozen);
312c056a 77
44c440bc
PP
78 if (packet->context_field) {
79 BT_LOGD_STR("Setting packet's context field's frozen state.");
80 bt_field_set_is_frozen(packet->context_field->field,
6c677fb5 81 is_frozen);
312c056a
PP
82 }
83
40f4ba76 84 ((struct bt_packet *) packet)->frozen = is_frozen;
f6ccaed9
PP
85}
86
312c056a 87static inline
44c440bc 88void reset_packet(struct bt_packet *packet)
f6ccaed9 89{
312c056a 90 BT_ASSERT(packet);
44c440bc 91 BT_LIB_LOGD("Resetting packet: %!+a", packet);
6c677fb5 92 bt_packet_set_is_frozen(packet, false);
312c056a 93
44c440bc
PP
94 if (packet->context_field) {
95 bt_field_set_is_frozen(packet->context_field->field, false);
96 bt_field_reset(packet->context_field->field);
312c056a 97 }
f79cf0f0
PP
98}
99
312c056a 100static
44c440bc 101void recycle_context_field(struct bt_field_wrapper *context_field,
312c056a 102 struct bt_stream_class *stream_class)
f79cf0f0 103{
312c056a
PP
104 BT_ASSERT(context_field);
105 BT_LIB_LOGD("Recycling packet context field: "
106 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
107 stream_class, context_field->field);
108 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
109 context_field);
f79cf0f0
PP
110}
111
112BT_HIDDEN
312c056a 113void bt_packet_recycle(struct bt_packet *packet)
f79cf0f0 114{
312c056a 115 struct bt_stream *stream;
f79cf0f0 116
312c056a
PP
117 BT_ASSERT(packet);
118 BT_LIB_LOGD("Recycling packet: %!+a", packet);
119
120 /*
121 * Those are the important ordered steps:
122 *
123 * 1. Reset the packet object (put any permanent reference it
124 * has, unfreeze it and its fields in developer mode, etc.),
125 * but do NOT put its stream's reference. This stream
126 * contains the pool to which we're about to recycle this
127 * packet object, so we must guarantee its existence thanks
128 * to this existing reference.
129 *
130 * 2. Move the stream reference to our `stream`
131 * variable so that we can set the packet's stream member
132 * to NULL before recycling it. We CANNOT do this after
65300d60 133 * we put the stream reference because this bt_object_put_ref()
312c056a
PP
134 * could destroy the stream, also destroying its
135 * packet pool, thus also destroying our packet object (this
136 * would result in an invalid write access).
137 *
138 * 3. Recycle the packet object.
139 *
140 * 4. Put our stream reference.
141 */
44c440bc 142 reset_packet(packet);
312c056a
PP
143 stream = packet->stream;
144 BT_ASSERT(stream);
145 packet->stream = NULL;
146 bt_object_pool_recycle_object(&stream->packet_pool, packet);
6871026b 147 bt_object_put_ref_no_null_check(&stream->base);
f79cf0f0
PP
148}
149
312c056a
PP
150BT_HIDDEN
151void bt_packet_destroy(struct bt_packet *packet)
f79cf0f0 152{
44c440bc 153 BT_LIB_LOGD("Destroying packet: %!+a", packet);
312c056a 154
44c440bc 155 if (packet->context_field) {
312c056a
PP
156 if (packet->stream) {
157 BT_LOGD_STR("Recycling packet's context field.");
44c440bc
PP
158 recycle_context_field(packet->context_field,
159 packet->stream->class);
312c056a 160 } else {
44c440bc 161 bt_field_wrapper_destroy(packet->context_field);
312c056a 162 }
238b7404
PP
163
164 packet->context_field = NULL;
312c056a
PP
165 }
166
d409daba 167 BT_LOGD_STR("Putting packet's stream.");
238b7404 168 BT_OBJECT_PUT_REF_AND_RESET(packet->stream);
f79cf0f0
PP
169 g_free(packet);
170}
171
312c056a
PP
172BT_HIDDEN
173struct bt_packet *bt_packet_new(struct bt_stream *stream)
f79cf0f0 174{
50842bdc 175 struct bt_packet *packet = NULL;
862ca4ed 176 struct bt_trace_class *trace_class = NULL;
f79cf0f0 177
312c056a 178 BT_ASSERT(stream);
44c440bc 179 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream);
50842bdc 180 packet = g_new0(struct bt_packet, 1);
f79cf0f0 181 if (!packet) {
870631a2
PP
182 BT_LIB_LOGE_APPEND_CAUSE(
183 "Failed to allocate one packet object.");
44c440bc 184 goto error;
f79cf0f0
PP
185 }
186
3fea54f6
PP
187 bt_object_init_shared(&packet->base,
188 (bt_object_release_func) bt_packet_recycle);
398454ed 189 packet->stream = stream;
6871026b 190 bt_object_get_ref_no_null_check(stream);
862ca4ed
PP
191 trace_class = bt_stream_class_borrow_trace_class_inline(stream->class);
192 BT_ASSERT(trace_class);
be514b0c 193
5cd6d0e5 194 if (stream->class->packet_context_fc) {
44c440bc
PP
195 BT_LOGD_STR("Creating initial packet context field.");
196 packet->context_field = bt_field_wrapper_create(
197 &stream->class->packet_context_field_pool,
5cd6d0e5 198 stream->class->packet_context_fc);
44c440bc 199 if (!packet->context_field) {
870631a2
PP
200 BT_LIB_LOGE_APPEND_CAUSE(
201 "Cannot create packet context field wrapper.");
44c440bc 202 goto error;
be514b0c 203 }
f79cf0f0
PP
204 }
205
44c440bc
PP
206 BT_LIB_LOGD("Created packet object: %!+a", packet);
207 goto end;
ccf82993 208
44c440bc 209error:
65300d60 210 BT_OBJECT_PUT_REF_AND_RESET(packet);
ccf82993
PP
211
212end:
44c440bc 213 return packet;
ccf82993
PP
214}
215
58085ca4 216struct bt_packet *bt_packet_create(const struct bt_stream *c_stream)
312c056a
PP
217{
218 struct bt_packet *packet = NULL;
58085ca4 219 struct bt_stream *stream = (void *) c_stream;
312c056a 220
17f3083a 221 BT_ASSERT_PRE_NO_ERROR();
312c056a 222 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
26fc5aed
PP
223 BT_ASSERT_PRE(stream->class->supports_packets,
224 "Stream class does not support packets: %![sc-]+S",
225 stream->class);
312c056a 226 packet = bt_object_pool_create_object(&stream->packet_pool);
91d81473 227 if (G_UNLIKELY(!packet)) {
870631a2
PP
228 BT_LIB_LOGE_APPEND_CAUSE(
229 "Cannot allocate one packet from stream's packet pool: "
312c056a 230 "%![stream-]+s", stream);
6c677fb5 231 goto end;
312c056a
PP
232 }
233
91d81473 234 if (G_LIKELY(!packet->stream)) {
6c677fb5 235 packet->stream = stream;
6871026b 236 bt_object_get_ref_no_null_check_no_parent_check(
cb6f1f7d 237 &packet->stream->base);
312c056a
PP
238 }
239
312c056a 240end:
e5be10ef 241 return (void *) packet;
312c056a
PP
242}
243
c5b9b441
PP
244void bt_packet_get_ref(const struct bt_packet *packet)
245{
246 bt_object_get_ref(packet);
247}
248
249void bt_packet_put_ref(const struct bt_packet *packet)
250{
251 bt_object_put_ref(packet);
252}
This page took 0.087492 seconds and 4 git commands to generate.