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