Visibility hidden by default
[babeltrace.git] / src / lib / trace-ir / packet.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_LOG_TAG "LIB/PACKET"
8 #include "lib/logging.h"
9
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"
17 #include <inttypes.h>
18 #include <stdbool.h>
19
20 #include "field.h"
21 #include "field-wrapper.h"
22 #include "packet.h"
23 #include "stream-class.h"
24 #include "stream.h"
25 #include "trace.h"
26 #include "lib/func-status.h"
27
28 BT_EXPORT
29 struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
30 {
31 BT_ASSERT_PRE_DEV_PACKET_NON_NULL(packet);
32 return packet->stream;
33 }
34
35 BT_EXPORT
36 const struct bt_stream *bt_packet_borrow_stream_const(
37 const struct bt_packet *packet)
38 {
39 return bt_packet_borrow_stream((void *) packet);
40 }
41
42 BT_EXPORT
43 struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet)
44 {
45 BT_ASSERT_PRE_DEV_PACKET_NON_NULL(packet);
46 return packet->context_field ? packet->context_field->field : NULL;
47 }
48
49 BT_EXPORT
50 const struct bt_field *bt_packet_borrow_context_field_const(
51 const struct bt_packet *packet)
52 {
53 return bt_packet_borrow_context_field((void *) packet);
54 }
55
56 void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen)
57 {
58 if (!packet) {
59 return;
60 }
61
62 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
63 "is-frozen=%d", packet, is_frozen);
64
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,
68 is_frozen);
69 }
70
71 ((struct bt_packet *) packet)->frozen = is_frozen;
72 }
73
74 static inline
75 void reset_packet(struct bt_packet *packet)
76 {
77 BT_ASSERT(packet);
78 BT_LIB_LOGD("Resetting packet: %!+a", packet);
79 bt_packet_set_is_frozen(packet, false);
80
81 if (packet->context_field) {
82 bt_field_set_is_frozen(packet->context_field->field, false);
83 bt_field_reset(packet->context_field->field);
84 }
85 }
86
87 static
88 void recycle_context_field(struct bt_field_wrapper *context_field,
89 struct bt_stream_class *stream_class)
90 {
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,
96 context_field);
97 }
98
99 void bt_packet_recycle(struct bt_packet *packet)
100 {
101 struct bt_stream *stream;
102
103 BT_ASSERT(packet);
104 BT_LIB_LOGD("Recycling packet: %!+a", packet);
105
106 /*
107 * Those are the important ordered steps:
108 *
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.
115 *
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).
123 *
124 * 3. Recycle the packet object.
125 *
126 * 4. Put our stream reference.
127 */
128 reset_packet(packet);
129 stream = packet->stream;
130 BT_ASSERT(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);
134 }
135
136 void bt_packet_destroy(struct bt_packet *packet)
137 {
138 BT_LIB_LOGD("Destroying packet: %!+a", packet);
139
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);
145 } else {
146 bt_field_wrapper_destroy(packet->context_field);
147 }
148
149 packet->context_field = NULL;
150 }
151
152 BT_LOGD_STR("Putting packet's stream.");
153 BT_OBJECT_PUT_REF_AND_RESET(packet->stream);
154 g_free(packet);
155 }
156
157 struct bt_packet *bt_packet_new(struct bt_stream *stream)
158 {
159 struct bt_packet *packet = NULL;
160 struct bt_trace_class *trace_class = NULL;
161
162 BT_ASSERT(stream);
163 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream);
164 packet = g_new0(struct bt_packet, 1);
165 if (!packet) {
166 BT_LIB_LOGE_APPEND_CAUSE(
167 "Failed to allocate one packet object.");
168 goto error;
169 }
170
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);
177
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.");
186 goto error;
187 }
188 }
189
190 BT_LIB_LOGD("Created packet object: %!+a", packet);
191 goto end;
192
193 error:
194 BT_OBJECT_PUT_REF_AND_RESET(packet);
195
196 end:
197 return packet;
198 }
199
200 BT_EXPORT
201 struct bt_packet *bt_packet_create(const struct bt_stream *c_stream)
202 {
203 struct bt_packet *packet = NULL;
204 struct bt_stream *stream = (void *) c_stream;
205
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",
211 stream->class);
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);
217 goto end;
218 }
219
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);
224 }
225
226 end:
227 return (void *) packet;
228 }
229
230 BT_EXPORT
231 void bt_packet_get_ref(const struct bt_packet *packet)
232 {
233 bt_object_get_ref(packet);
234 }
235
236 BT_EXPORT
237 void bt_packet_put_ref(const struct bt_packet *packet)
238 {
239 bt_object_put_ref(packet);
240 }
This page took 0.051446 seconds and 4 git commands to generate.