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"
21 #include "field-wrapper.h"
23 #include "stream-class.h"
26 #include "lib/func-status.h"
29 struct bt_stream
*bt_packet_borrow_stream(struct bt_packet
*packet
)
31 BT_ASSERT_PRE_DEV_PACKET_NON_NULL(packet
);
32 return packet
->stream
;
36 const struct bt_stream
*bt_packet_borrow_stream_const(
37 const struct bt_packet
*packet
)
39 return bt_packet_borrow_stream((void *) packet
);
43 struct bt_field
*bt_packet_borrow_context_field(struct bt_packet
*packet
)
45 BT_ASSERT_PRE_DEV_PACKET_NON_NULL(packet
);
46 return packet
->context_field
? packet
->context_field
->field
: NULL
;
50 const struct bt_field
*bt_packet_borrow_context_field_const(
51 const struct bt_packet
*packet
)
53 return bt_packet_borrow_context_field((void *) packet
);
56 void _bt_packet_set_is_frozen(const struct bt_packet
*packet
, bool is_frozen
)
62 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
63 "is-frozen=%d", packet
, is_frozen
);
65 if (packet
->context_field
) {
66 BT_LOGD_STR("Setting packet's context field's frozen state.");
67 bt_field_set_is_frozen(packet
->context_field
->field
,
71 ((struct bt_packet
*) packet
)->frozen
= is_frozen
;
75 void reset_packet(struct bt_packet
*packet
)
78 BT_LIB_LOGD("Resetting packet: %!+a", packet
);
79 bt_packet_set_is_frozen(packet
, false);
81 if (packet
->context_field
) {
82 bt_field_set_is_frozen(packet
->context_field
->field
, false);
83 bt_field_reset(packet
->context_field
->field
);
88 void recycle_context_field(struct bt_field_wrapper
*context_field
,
89 struct bt_stream_class
*stream_class
)
91 BT_ASSERT(context_field
);
92 BT_LIB_LOGD("Recycling packet context field: "
93 "addr=%p, %![sc-]+S, %![field-]+f", context_field
,
94 stream_class
, context_field
->field
);
95 bt_object_pool_recycle_object(&stream_class
->packet_context_field_pool
,
99 void bt_packet_recycle(struct bt_packet
*packet
)
101 struct bt_stream
*stream
;
104 BT_LIB_LOGD("Recycling packet: %!+a", packet
);
107 * Those are the important ordered steps:
109 * 1. Reset the packet object (put any permanent reference it
110 * has, unfreeze it and its fields in developer mode, etc.),
111 * but do NOT put its stream's reference. This stream
112 * contains the pool to which we're about to recycle this
113 * packet object, so we must guarantee its existence thanks
114 * to this existing reference.
116 * 2. Move the stream reference to our `stream`
117 * variable so that we can set the packet's stream member
118 * to NULL before recycling it. We CANNOT do this after
119 * we put the stream reference because this bt_object_put_ref()
120 * could destroy the stream, also destroying its
121 * packet pool, thus also destroying our packet object (this
122 * would result in an invalid write access).
124 * 3. Recycle the packet object.
126 * 4. Put our stream reference.
128 reset_packet(packet
);
129 stream
= packet
->stream
;
131 packet
->stream
= NULL
;
132 bt_object_pool_recycle_object(&stream
->packet_pool
, packet
);
133 bt_object_put_ref_no_null_check(&stream
->base
);
136 void bt_packet_destroy(struct bt_packet
*packet
)
138 BT_LIB_LOGD("Destroying packet: %!+a", packet
);
140 if (packet
->context_field
) {
141 if (packet
->stream
) {
142 BT_LOGD_STR("Recycling packet's context field.");
143 recycle_context_field(packet
->context_field
,
144 packet
->stream
->class);
146 bt_field_wrapper_destroy(packet
->context_field
);
149 packet
->context_field
= NULL
;
152 BT_LOGD_STR("Putting packet's stream.");
153 BT_OBJECT_PUT_REF_AND_RESET(packet
->stream
);
157 struct bt_packet
*bt_packet_new(struct bt_stream
*stream
)
159 struct bt_packet
*packet
= NULL
;
160 struct bt_trace_class
*trace_class
= NULL
;
163 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream
);
164 packet
= g_new0(struct bt_packet
, 1);
166 BT_LIB_LOGE_APPEND_CAUSE(
167 "Failed to allocate one packet object.");
171 bt_object_init_shared(&packet
->base
,
172 (bt_object_release_func
) bt_packet_recycle
);
173 packet
->stream
= stream
;
174 bt_object_get_ref_no_null_check(stream
);
175 trace_class
= bt_stream_class_borrow_trace_class_inline(stream
->class);
176 BT_ASSERT(trace_class
);
178 if (stream
->class->packet_context_fc
) {
179 BT_LOGD_STR("Creating initial packet context field.");
180 packet
->context_field
= bt_field_wrapper_create(
181 &stream
->class->packet_context_field_pool
,
182 stream
->class->packet_context_fc
);
183 if (!packet
->context_field
) {
184 BT_LIB_LOGE_APPEND_CAUSE(
185 "Cannot create packet context field wrapper.");
190 BT_LIB_LOGD("Created packet object: %!+a", packet
);
194 BT_OBJECT_PUT_REF_AND_RESET(packet
);
201 struct bt_packet
*bt_packet_create(const struct bt_stream
*c_stream
)
203 struct bt_packet
*packet
= NULL
;
204 struct bt_stream
*stream
= (void *) c_stream
;
206 BT_ASSERT_PRE_NO_ERROR();
207 BT_ASSERT_PRE_STREAM_NON_NULL(stream
);
208 BT_ASSERT_PRE("stream-class-supports-packets",
209 stream
->class->supports_packets
,
210 "Stream class does not support packets: %![sc-]+S",
212 packet
= bt_object_pool_create_object(&stream
->packet_pool
);
213 if (G_UNLIKELY(!packet
)) {
214 BT_LIB_LOGE_APPEND_CAUSE(
215 "Cannot allocate one packet from stream's packet pool: "
216 "%![stream-]+s", stream
);
220 if (G_LIKELY(!packet
->stream
)) {
221 packet
->stream
= stream
;
222 bt_object_get_ref_no_null_check_no_parent_check(
223 &packet
->stream
->base
);
227 return (void *) packet
;
231 void bt_packet_get_ref(const struct bt_packet
*packet
)
233 bt_object_get_ref(packet
);
237 void bt_packet_put_ref(const struct bt_packet
*packet
)
239 bt_object_put_ref(packet
);