2 * SPDX-License-Identifier: MIT
4 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
7 #define BT_LOG_TAG "LIB/PACKET"
8 #include "lib/logging.h"
10 #include "lib/assert-cond.h"
11 #include <babeltrace2/trace-ir/packet.h>
12 #include <babeltrace2/trace-ir/trace.h>
13 #include <babeltrace2/trace-ir/stream-class.h>
14 #include <babeltrace2/trace-ir/stream.h>
15 #include "lib/object.h"
16 #include "common/assert.h"
20 #include "field-wrapper.h"
22 #include "stream-class.h"
26 struct bt_stream
*bt_packet_borrow_stream(struct bt_packet
*packet
)
28 BT_ASSERT_PRE_DEV_PACKET_NON_NULL(packet
);
29 return packet
->stream
;
33 const struct bt_stream
*bt_packet_borrow_stream_const(
34 const struct bt_packet
*packet
)
36 return bt_packet_borrow_stream((void *) packet
);
40 struct bt_field
*bt_packet_borrow_context_field(struct bt_packet
*packet
)
42 BT_ASSERT_PRE_DEV_PACKET_NON_NULL(packet
);
43 return packet
->context_field
? packet
->context_field
->field
: NULL
;
47 const struct bt_field
*bt_packet_borrow_context_field_const(
48 const struct bt_packet
*packet
)
50 return bt_packet_borrow_context_field((void *) packet
);
53 void _bt_packet_set_is_frozen(const struct bt_packet
*packet
, bool is_frozen
)
59 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
60 "is-frozen=%d", packet
, is_frozen
);
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
,
68 ((struct bt_packet
*) packet
)->frozen
= is_frozen
;
72 void reset_packet(struct bt_packet
*packet
)
75 BT_LIB_LOGD("Resetting packet: %!+a", packet
);
76 bt_packet_set_is_frozen(packet
, false);
78 if (packet
->context_field
) {
79 bt_field_set_is_frozen(packet
->context_field
->field
, false);
80 bt_field_reset(packet
->context_field
->field
);
85 void recycle_context_field(struct bt_field_wrapper
*context_field
,
86 struct bt_stream_class
*stream_class
)
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
,
96 void bt_packet_recycle(struct bt_packet
*packet
)
98 struct bt_stream
*stream
;
101 BT_LIB_LOGD("Recycling packet: %!+a", packet
);
104 * Those are the important ordered steps:
106 * 1. Reset the packet object (put any permanent reference it
107 * has, unfreeze it and its fields in developer mode, etc.),
108 * but do NOT put its stream's reference. This stream
109 * contains the pool to which we're about to recycle this
110 * packet object, so we must guarantee its existence thanks
111 * to this existing reference.
113 * 2. Move the stream reference to our `stream`
114 * variable so that we can set the packet's stream member
115 * to NULL before recycling it. We CANNOT do this after
116 * we put the stream reference because this bt_object_put_ref()
117 * could destroy the stream, also destroying its
118 * packet pool, thus also destroying our packet object (this
119 * would result in an invalid write access).
121 * 3. Recycle the packet object.
123 * 4. Put our stream reference.
125 reset_packet(packet
);
126 stream
= packet
->stream
;
128 packet
->stream
= NULL
;
129 bt_object_pool_recycle_object(&stream
->packet_pool
, packet
);
130 bt_object_put_ref_no_null_check(&stream
->base
);
133 void bt_packet_destroy(struct bt_packet
*packet
)
135 BT_LIB_LOGD("Destroying packet: %!+a", packet
);
137 if (packet
->context_field
) {
138 if (packet
->stream
) {
139 BT_LOGD_STR("Recycling packet's context field.");
140 recycle_context_field(packet
->context_field
,
141 packet
->stream
->class);
143 bt_field_wrapper_destroy(packet
->context_field
);
146 packet
->context_field
= NULL
;
149 BT_LOGD_STR("Putting packet's stream.");
150 BT_OBJECT_PUT_REF_AND_RESET(packet
->stream
);
154 struct bt_packet
*bt_packet_new(struct bt_stream
*stream
)
156 struct bt_packet
*packet
= NULL
;
157 struct bt_trace_class
*trace_class
= NULL
;
160 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream
);
161 packet
= g_new0(struct bt_packet
, 1);
163 BT_LIB_LOGE_APPEND_CAUSE(
164 "Failed to allocate one packet object.");
168 bt_object_init_shared(&packet
->base
,
169 (bt_object_release_func
) bt_packet_recycle
);
170 packet
->stream
= stream
;
171 bt_object_get_ref_no_null_check(stream
);
172 trace_class
= bt_stream_class_borrow_trace_class_inline(stream
->class);
173 BT_ASSERT(trace_class
);
175 if (stream
->class->packet_context_fc
) {
176 BT_LOGD_STR("Creating initial packet context field.");
177 packet
->context_field
= bt_field_wrapper_create(
178 &stream
->class->packet_context_field_pool
,
179 stream
->class->packet_context_fc
);
180 if (!packet
->context_field
) {
181 BT_LIB_LOGE_APPEND_CAUSE(
182 "Cannot create packet context field wrapper.");
187 BT_LIB_LOGD("Created packet object: %!+a", packet
);
191 BT_OBJECT_PUT_REF_AND_RESET(packet
);
198 struct bt_packet
*bt_packet_create(const struct bt_stream
*c_stream
)
200 struct bt_packet
*packet
= NULL
;
201 struct bt_stream
*stream
= (void *) c_stream
;
203 BT_ASSERT_PRE_NO_ERROR();
204 BT_ASSERT_PRE_STREAM_NON_NULL(stream
);
205 BT_ASSERT_PRE("stream-class-supports-packets",
206 stream
->class->supports_packets
,
207 "Stream class does not support packets: %![sc-]+S",
209 packet
= bt_object_pool_create_object(&stream
->packet_pool
);
210 if (G_UNLIKELY(!packet
)) {
211 BT_LIB_LOGE_APPEND_CAUSE(
212 "Cannot allocate one packet from stream's packet pool: "
213 "%![stream-]+s", stream
);
217 if (G_LIKELY(!packet
->stream
)) {
218 packet
->stream
= stream
;
219 bt_object_get_ref_no_null_check_no_parent_check(
220 &packet
->stream
->base
);
224 return (void *) packet
;
228 void bt_packet_get_ref(const struct bt_packet
*packet
)
230 bt_object_get_ref(packet
);
234 void bt_packet_put_ref(const struct bt_packet
*packet
)
236 bt_object_put_ref(packet
);