2 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #define BT_LOG_TAG "LIB/PACKET"
24 #include "lib/logging.h"
26 #include "lib/assert-pre.h"
27 #include <babeltrace2/trace-ir/packet-const.h>
28 #include <babeltrace2/trace-ir/packet.h>
29 #include <babeltrace2/trace-ir/trace.h>
30 #include <babeltrace2/trace-ir/stream-class.h>
31 #include <babeltrace2/trace-ir/stream.h>
32 #include "lib/object.h"
33 #include "common/assert.h"
37 #include "field-wrapper.h"
39 #include "stream-class.h"
42 #include "lib/func-status.h"
44 #define BT_ASSERT_PRE_PACKET_HOT(_packet) \
45 BT_ASSERT_PRE_HOT((_packet), "Packet", ": %!+a", (_packet))
47 struct bt_stream
*bt_packet_borrow_stream(struct bt_packet
*packet
)
49 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
50 return packet
->stream
;
53 const struct bt_stream
*bt_packet_borrow_stream_const(
54 const struct bt_packet
*packet
)
56 return bt_packet_borrow_stream((void *) packet
);
59 struct bt_field
*bt_packet_borrow_context_field(struct bt_packet
*packet
)
61 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
62 return packet
->context_field
? packet
->context_field
->field
: NULL
;
65 const struct bt_field
*bt_packet_borrow_context_field_const(
66 const struct bt_packet
*packet
)
68 return bt_packet_borrow_context_field((void *) packet
);
72 void _bt_packet_set_is_frozen(const struct bt_packet
*packet
, bool is_frozen
)
78 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
79 "is-frozen=%d", packet
, is_frozen
);
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
,
87 ((struct bt_packet
*) packet
)->frozen
= is_frozen
;
91 void reset_packet(struct bt_packet
*packet
)
94 BT_LIB_LOGD("Resetting packet: %!+a", packet
);
95 bt_packet_set_is_frozen(packet
, false);
97 if (packet
->context_field
) {
98 bt_field_set_is_frozen(packet
->context_field
->field
, false);
99 bt_field_reset(packet
->context_field
->field
);
104 void recycle_context_field(struct bt_field_wrapper
*context_field
,
105 struct bt_stream_class
*stream_class
)
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
,
116 void bt_packet_recycle(struct bt_packet
*packet
)
118 struct bt_stream
*stream
;
121 BT_LIB_LOGD("Recycling packet: %!+a", packet
);
124 * Those are the important ordered steps:
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.
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
136 * we put the stream reference because this bt_object_put_ref()
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).
141 * 3. Recycle the packet object.
143 * 4. Put our stream reference.
145 reset_packet(packet
);
146 stream
= packet
->stream
;
148 packet
->stream
= NULL
;
149 bt_object_pool_recycle_object(&stream
->packet_pool
, packet
);
150 bt_object_put_no_null_check(&stream
->base
);
154 void bt_packet_destroy(struct bt_packet
*packet
)
156 BT_LIB_LOGD("Destroying packet: %!+a", packet
);
158 if (packet
->context_field
) {
159 if (packet
->stream
) {
160 BT_LOGD_STR("Recycling packet's context field.");
161 recycle_context_field(packet
->context_field
,
162 packet
->stream
->class);
164 bt_field_wrapper_destroy(packet
->context_field
);
167 packet
->context_field
= NULL
;
170 BT_LOGD_STR("Putting packet's stream.");
171 BT_OBJECT_PUT_REF_AND_RESET(packet
->stream
);
176 struct bt_packet
*bt_packet_new(struct bt_stream
*stream
)
178 struct bt_packet
*packet
= NULL
;
179 struct bt_trace_class
*trace_class
= NULL
;
182 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream
);
183 packet
= g_new0(struct bt_packet
, 1);
185 BT_LOGE_STR("Failed to allocate one packet object.");
189 bt_object_init_shared(&packet
->base
,
190 (bt_object_release_func
) bt_packet_recycle
);
191 packet
->stream
= stream
;
192 bt_object_get_no_null_check(stream
);
193 trace_class
= bt_stream_class_borrow_trace_class_inline(stream
->class);
194 BT_ASSERT(trace_class
);
196 if (stream
->class->packet_context_fc
) {
197 BT_LOGD_STR("Creating initial packet context field.");
198 packet
->context_field
= bt_field_wrapper_create(
199 &stream
->class->packet_context_field_pool
,
200 stream
->class->packet_context_fc
);
201 if (!packet
->context_field
) {
202 BT_LOGE_STR("Cannot create packet context field wrapper.");
207 BT_LIB_LOGD("Created packet object: %!+a", packet
);
211 BT_OBJECT_PUT_REF_AND_RESET(packet
);
217 struct bt_packet
*bt_packet_create(const struct bt_stream
*c_stream
)
219 struct bt_packet
*packet
= NULL
;
220 struct bt_stream
*stream
= (void *) c_stream
;
222 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
223 packet
= bt_object_pool_create_object(&stream
->packet_pool
);
224 if (G_UNLIKELY(!packet
)) {
225 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
226 "%![stream-]+s", stream
);
230 if (G_LIKELY(!packet
->stream
)) {
231 packet
->stream
= stream
;
232 bt_object_get_no_null_check_no_parent_check(
233 &packet
->stream
->base
);
237 return (void *) packet
;
240 enum bt_packet_move_context_field_status
bt_packet_move_context_field(
241 struct bt_packet
*packet
,
242 struct bt_packet_context_field
*context_field
)
244 struct bt_stream_class
*stream_class
;
245 struct bt_field_wrapper
*field_wrapper
= (void *) context_field
;
247 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
248 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Context field");
249 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
250 stream_class
= packet
->stream
->class;
251 BT_ASSERT_PRE(stream_class
->packet_context_fc
,
252 "Stream class has no packet context field class: %!+S",
254 BT_ASSERT_PRE(field_wrapper
->field
->class ==
255 stream_class
->packet_context_fc
,
256 "Unexpected packet context field's class: "
257 "%![fc-]+F, %![expected-fc-]+F", field_wrapper
->field
->class,
258 stream_class
->packet_context_fc
);
260 /* Recycle current context field: always exists */
261 BT_ASSERT(packet
->context_field
);
262 recycle_context_field(packet
->context_field
, stream_class
);
265 packet
->context_field
= field_wrapper
;
266 return BT_FUNC_STATUS_OK
;
269 void bt_packet_get_ref(const struct bt_packet
*packet
)
271 bt_object_get_ref(packet
);
274 void bt_packet_put_ref(const struct bt_packet
*packet
)
276 bt_object_put_ref(packet
);