lib: strictly type function return status enumerations
[babeltrace.git] / src / lib / trace-ir / packet.c
CommitLineData
f79cf0f0 1/*
f2b0325d 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
b03487ab 23#define BT_LOG_TAG "LIB/PACKET"
1633ef46 24#include "lib/logging.h"
8ea705f0 25
57952005 26#include "lib/assert-pre.h"
71c5da58
MJ
27#include <babeltrace2/trace-ir/packet-const.h>
28#include <babeltrace2/trace-ir/packet.h>
71c5da58 29#include <babeltrace2/trace-ir/trace.h>
71c5da58
MJ
30#include <babeltrace2/trace-ir/stream-class.h>
31#include <babeltrace2/trace-ir/stream.h>
57952005
MJ
32#include "lib/object.h"
33#include "common/assert.h"
be514b0c 34#include <inttypes.h>
f79cf0f0 35
57952005
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"
fb25b9e3 42#include "lib/func-status.h"
57952005 43
7b33a0e0
PP
44#define BT_ASSERT_PRE_PACKET_HOT(_packet) \
45 BT_ASSERT_PRE_HOT((_packet), "Packet", ": %!+a", (_packet))
46
5fe68922 47struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
f79cf0f0 48{
8b45963b 49 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
5fe68922 50 return packet->stream;
f79cf0f0
PP
51}
52
78cf9df6
PP
53const struct bt_stream *bt_packet_borrow_stream_const(
54 const struct bt_packet *packet)
9e550e5f 55{
78cf9df6 56 return bt_packet_borrow_stream((void *) packet);
9e550e5f
PP
57}
58
7b33a0e0 59struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet)
f79cf0f0 60{
a6918753 61 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0 62 return packet->context_field ? packet->context_field->field : NULL;
a6918753 63}
c5c82c6e 64
78cf9df6
PP
65const struct bt_field *bt_packet_borrow_context_field_const(
66 const struct bt_packet *packet)
9e550e5f 67{
78cf9df6 68 return bt_packet_borrow_context_field((void *) packet);
9e550e5f
PP
69}
70
a6918753 71BT_HIDDEN
78cf9df6 72void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen)
a6918753 73{
435ec8af 74 if (!packet) {
a6918753 75 return;
c5c82c6e
PP
76 }
77
7b33a0e0
PP
78 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
79 "is-frozen=%d", packet, is_frozen);
a6918753 80
7b33a0e0
PP
81 if (packet->context_field) {
82 BT_LOGD_STR("Setting packet's context field's frozen state.");
83 bt_field_set_is_frozen(packet->context_field->field,
c5a24b0a 84 is_frozen);
a6918753
PP
85 }
86
78cf9df6 87 ((struct bt_packet *) packet)->frozen = is_frozen;
8b45963b
PP
88}
89
a6918753 90static inline
7b33a0e0 91void reset_packet(struct bt_packet *packet)
8b45963b 92{
a6918753 93 BT_ASSERT(packet);
7b33a0e0 94 BT_LIB_LOGD("Resetting packet: %!+a", packet);
c5a24b0a 95 bt_packet_set_is_frozen(packet, false);
a6918753 96
7b33a0e0
PP
97 if (packet->context_field) {
98 bt_field_set_is_frozen(packet->context_field->field, false);
99 bt_field_reset(packet->context_field->field);
a6918753 100 }
f79cf0f0
PP
101}
102
a6918753 103static
7b33a0e0 104void recycle_context_field(struct bt_field_wrapper *context_field,
a6918753 105 struct bt_stream_class *stream_class)
f79cf0f0 106{
a6918753
PP
107 BT_ASSERT(context_field);
108 BT_LIB_LOGD("Recycling packet context field: "
109 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
110 stream_class, context_field->field);
111 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
112 context_field);
f79cf0f0
PP
113}
114
115BT_HIDDEN
a6918753 116void bt_packet_recycle(struct bt_packet *packet)
f79cf0f0 117{
a6918753 118 struct bt_stream *stream;
f79cf0f0 119
a6918753
PP
120 BT_ASSERT(packet);
121 BT_LIB_LOGD("Recycling packet: %!+a", packet);
122
123 /*
124 * Those are the important ordered steps:
125 *
126 * 1. Reset the packet object (put any permanent reference it
127 * has, unfreeze it and its fields in developer mode, etc.),
128 * but do NOT put its stream's reference. This stream
129 * contains the pool to which we're about to recycle this
130 * packet object, so we must guarantee its existence thanks
131 * to this existing reference.
132 *
133 * 2. Move the stream reference to our `stream`
134 * variable so that we can set the packet's stream member
135 * to NULL before recycling it. We CANNOT do this after
8138bfe1 136 * we put the stream reference because this bt_object_put_ref()
a6918753
PP
137 * could destroy the stream, also destroying its
138 * packet pool, thus also destroying our packet object (this
139 * would result in an invalid write access).
140 *
141 * 3. Recycle the packet object.
142 *
143 * 4. Put our stream reference.
144 */
7b33a0e0 145 reset_packet(packet);
a6918753
PP
146 stream = packet->stream;
147 BT_ASSERT(stream);
148 packet->stream = NULL;
149 bt_object_pool_recycle_object(&stream->packet_pool, packet);
18acc6f8 150 bt_object_put_no_null_check(&stream->base);
f79cf0f0
PP
151}
152
a6918753
PP
153BT_HIDDEN
154void bt_packet_destroy(struct bt_packet *packet)
f79cf0f0 155{
7b33a0e0 156 BT_LIB_LOGD("Destroying packet: %!+a", packet);
a6918753 157
7b33a0e0 158 if (packet->context_field) {
a6918753
PP
159 if (packet->stream) {
160 BT_LOGD_STR("Recycling packet's context field.");
7b33a0e0
PP
161 recycle_context_field(packet->context_field,
162 packet->stream->class);
a6918753 163 } else {
7b33a0e0 164 bt_field_wrapper_destroy(packet->context_field);
a6918753 165 }
1248f5ea
PP
166
167 packet->context_field = NULL;
a6918753
PP
168 }
169
d409daba 170 BT_LOGD_STR("Putting packet's stream.");
1248f5ea 171 BT_OBJECT_PUT_REF_AND_RESET(packet->stream);
f79cf0f0
PP
172 g_free(packet);
173}
174
a6918753
PP
175BT_HIDDEN
176struct bt_packet *bt_packet_new(struct bt_stream *stream)
f79cf0f0 177{
839d52a5 178 struct bt_packet *packet = NULL;
10b7a2e4 179 struct bt_trace_class *trace_class = NULL;
f79cf0f0 180
a6918753 181 BT_ASSERT(stream);
7b33a0e0 182 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream);
839d52a5 183 packet = g_new0(struct bt_packet, 1);
f79cf0f0 184 if (!packet) {
8ea705f0 185 BT_LOGE_STR("Failed to allocate one packet object.");
7b33a0e0 186 goto error;
f79cf0f0
PP
187 }
188
1d7bf349
PP
189 bt_object_init_shared(&packet->base,
190 (bt_object_release_func) bt_packet_recycle);
4b70020d
PP
191 packet->stream = stream;
192 bt_object_get_no_null_check(stream);
10b7a2e4
PP
193 trace_class = bt_stream_class_borrow_trace_class_inline(stream->class);
194 BT_ASSERT(trace_class);
be514b0c 195
939190b3 196 if (stream->class->packet_context_fc) {
7b33a0e0
PP
197 BT_LOGD_STR("Creating initial packet context field.");
198 packet->context_field = bt_field_wrapper_create(
199 &stream->class->packet_context_field_pool,
939190b3 200 stream->class->packet_context_fc);
7b33a0e0
PP
201 if (!packet->context_field) {
202 BT_LOGE_STR("Cannot create packet context field wrapper.");
203 goto error;
be514b0c 204 }
f79cf0f0
PP
205 }
206
7b33a0e0
PP
207 BT_LIB_LOGD("Created packet object: %!+a", packet);
208 goto end;
e5815ba2 209
7b33a0e0 210error:
8138bfe1 211 BT_OBJECT_PUT_REF_AND_RESET(packet);
e5815ba2
PP
212
213end:
7b33a0e0 214 return packet;
e5815ba2
PP
215}
216
6a6975d2 217struct bt_packet *bt_packet_create(const struct bt_stream *c_stream)
a6918753
PP
218{
219 struct bt_packet *packet = NULL;
6a6975d2 220 struct bt_stream *stream = (void *) c_stream;
a6918753
PP
221
222 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
223 packet = bt_object_pool_create_object(&stream->packet_pool);
85e7137b 224 if (G_UNLIKELY(!packet)) {
a6918753
PP
225 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
226 "%![stream-]+s", stream);
c5a24b0a 227 goto end;
a6918753
PP
228 }
229
85e7137b 230 if (G_LIKELY(!packet->stream)) {
c5a24b0a
PP
231 packet->stream = stream;
232 bt_object_get_no_null_check_no_parent_check(
18acc6f8 233 &packet->stream->base);
a6918753
PP
234 }
235
a6918753 236end:
9e550e5f 237 return (void *) packet;
a6918753
PP
238}
239
fb25b9e3
PP
240enum bt_packet_move_context_field_status bt_packet_move_context_field(
241 struct bt_packet *packet,
78cf9df6 242 struct bt_packet_context_field *context_field)
a6918753
PP
243{
244 struct bt_stream_class *stream_class;
245 struct bt_field_wrapper *field_wrapper = (void *) context_field;
246
7b33a0e0 247 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
a6918753 248 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Context field");
8fc063a2 249 BT_ASSERT_PRE_HOT(packet, "Packet", ": %!+a", packet);
7b33a0e0 250 stream_class = packet->stream->class;
939190b3 251 BT_ASSERT_PRE(stream_class->packet_context_fc,
66fd07a5 252 "Stream class has no packet context field class: %!+S",
a6918753 253 stream_class);
939190b3
PP
254 BT_ASSERT_PRE(field_wrapper->field->class ==
255 stream_class->packet_context_fc,
0476f6f7 256 "Unexpected packet context field's class: "
939190b3
PP
257 "%![fc-]+F, %![expected-fc-]+F", field_wrapper->field->class,
258 stream_class->packet_context_fc);
a6918753
PP
259
260 /* Recycle current context field: always exists */
7b33a0e0
PP
261 BT_ASSERT(packet->context_field);
262 recycle_context_field(packet->context_field, stream_class);
a6918753
PP
263
264 /* Move new field */
7b33a0e0 265 packet->context_field = field_wrapper;
fb25b9e3 266 return BT_FUNC_STATUS_OK;
a6918753 267}
8fc063a2 268
8c6884d9
PP
269void bt_packet_get_ref(const struct bt_packet *packet)
270{
271 bt_object_get_ref(packet);
272}
273
274void bt_packet_put_ref(const struct bt_packet *packet)
275{
276 bt_object_put_ref(packet);
277}
This page took 0.09325 seconds and 4 git commands to generate.