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-pre.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"
28 struct bt_stream
*bt_packet_borrow_stream(struct bt_packet
*packet
)
30 BT_ASSERT_PRE_DEV_NON_NULL(packet
, "Packet");
31 return packet
->stream
;
34 const struct bt_stream
*bt_packet_borrow_stream_const(
35 const struct bt_packet
*packet
)
37 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_NON_NULL(packet
, "Packet");
43 return packet
->context_field
? packet
->context_field
->field
: NULL
;
46 const struct bt_field
*bt_packet_borrow_context_field_const(
47 const struct bt_packet
*packet
)
49 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
,
97 void bt_packet_recycle(struct bt_packet
*packet
)
99 struct bt_stream
*stream
;
102 BT_LIB_LOGD("Recycling packet: %!+a", packet
);
105 * Those are the important ordered steps:
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.
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
117 * we put the stream reference because this bt_object_put_ref()
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).
122 * 3. Recycle the packet object.
124 * 4. Put our stream reference.
126 reset_packet(packet
);
127 stream
= packet
->stream
;
129 packet
->stream
= NULL
;
130 bt_object_pool_recycle_object(&stream
->packet_pool
, packet
);
131 bt_object_put_ref_no_null_check(&stream
->base
);
135 void bt_packet_destroy(struct bt_packet
*packet
)
137 BT_LIB_LOGD("Destroying packet: %!+a", packet
);
139 if (packet
->context_field
) {
140 if (packet
->stream
) {
141 BT_LOGD_STR("Recycling packet's context field.");
142 recycle_context_field(packet
->context_field
,
143 packet
->stream
->class);
145 bt_field_wrapper_destroy(packet
->context_field
);
148 packet
->context_field
= NULL
;
151 BT_LOGD_STR("Putting packet's stream.");
152 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
);
200 struct bt_packet
*bt_packet_create(const struct bt_stream
*c_stream
)
202 struct bt_packet
*packet
= NULL
;
203 struct bt_stream
*stream
= (void *) c_stream
;
205 BT_ASSERT_PRE_NO_ERROR();
206 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
207 BT_ASSERT_PRE(stream
->class->supports_packets
,
208 "Stream class does not support packets: %![sc-]+S",
210 packet
= bt_object_pool_create_object(&stream
->packet_pool
);
211 if (G_UNLIKELY(!packet
)) {
212 BT_LIB_LOGE_APPEND_CAUSE(
213 "Cannot allocate one packet from stream's packet pool: "
214 "%![stream-]+s", stream
);
218 if (G_LIKELY(!packet
->stream
)) {
219 packet
->stream
= stream
;
220 bt_object_get_ref_no_null_check_no_parent_check(
221 &packet
->stream
->base
);
225 return (void *) packet
;
228 void bt_packet_get_ref(const struct bt_packet
*packet
)
230 bt_object_get_ref(packet
);
233 void bt_packet_put_ref(const struct bt_packet
*packet
)
235 bt_object_put_ref(packet
);