Fix: lib: do not check the frozen state in bt_X_set_is_frozen()
[babeltrace.git] / lib / ctf-ir / packet.c
1 /*
2 * packet.c
3 *
4 * Babeltrace CTF IR - Stream packet
5 *
6 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #define BT_LOG_TAG "PACKET"
28 #include <babeltrace/lib-logging-internal.h>
29
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/ctf-ir/fields-internal.h>
32 #include <babeltrace/ctf-ir/packet.h>
33 #include <babeltrace/ctf-ir/packet-internal.h>
34 #include <babeltrace/ctf-ir/field-wrapper-internal.h>
35 #include <babeltrace/ctf-ir/trace.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/stream-class.h>
38 #include <babeltrace/ctf-ir/stream.h>
39 #include <babeltrace/ctf-ir/stream-internal.h>
40 #include <babeltrace/ctf-ir/trace-internal.h>
41 #include <babeltrace/object-internal.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/assert-internal.h>
44 #include <inttypes.h>
45
46 struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
47 {
48 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
49 return packet->stream;
50 }
51
52 struct bt_field *bt_packet_borrow_header(struct bt_packet *packet)
53 {
54 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
55 return packet->header ? (void *) packet->header->field : NULL;
56 }
57
58 struct bt_field *bt_packet_borrow_context(struct bt_packet *packet)
59 {
60 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
61 return packet->context ? (void *) packet->context->field : NULL;
62 }
63
64 BT_HIDDEN
65 void _bt_packet_set_is_frozen(struct bt_packet *packet, bool is_frozen)
66 {
67 if (!packet) {
68 return;
69 }
70
71 BT_LOGD("Freezing packet: addr=%p", packet);
72
73 if (packet->header) {
74 BT_LOGD_STR("Freezing packet's header field.");
75 bt_field_set_is_frozen_recursive((void *) packet->header->field,
76 is_frozen);
77 }
78
79 if (packet->context) {
80 BT_LOGD_STR("Freezing packet's context field.");
81 bt_field_set_is_frozen_recursive((void *) packet->context->field,
82 is_frozen);
83 }
84
85 packet->frozen = is_frozen;
86 }
87
88 static inline
89 void bt_packet_reset(struct bt_packet *packet)
90 {
91 BT_ASSERT(packet);
92 bt_packet_set_is_frozen(packet, false);
93
94 if (packet->header) {
95 bt_field_set_is_frozen_recursive(
96 (void *) packet->header->field, false);
97 bt_field_reset_recursive((void *) packet->header->field);
98 }
99
100 if (packet->context) {
101 bt_field_set_is_frozen_recursive(
102 (void *) packet->context->field, false);
103 bt_field_reset_recursive((void *) packet->context->field);
104 }
105
106 bt_clock_value_set_reset(&packet->begin_cv_set);
107 bt_clock_value_set_reset(&packet->end_cv_set);
108 }
109
110 static
111 void bt_packet_header_field_recycle(struct bt_field_wrapper *header_field,
112 struct bt_trace *trace)
113 {
114 BT_ASSERT(header_field);
115 BT_LIB_LOGD("Recycling packet header field: "
116 "addr=%p, %![trace-]+t, %![field-]+f", header_field,
117 trace, header_field->field);
118 bt_object_pool_recycle_object(&trace->packet_header_field_pool,
119 header_field);
120 }
121
122 static
123 void bt_packet_context_field_recycle(struct bt_field_wrapper *context_field,
124 struct bt_stream_class *stream_class)
125 {
126 BT_ASSERT(context_field);
127 BT_LIB_LOGD("Recycling packet context field: "
128 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
129 stream_class, context_field->field);
130 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
131 context_field);
132 }
133
134 BT_HIDDEN
135 void bt_packet_recycle(struct bt_packet *packet)
136 {
137 struct bt_stream *stream;
138
139 BT_ASSERT(packet);
140 BT_LIB_LOGD("Recycling packet: %!+a", packet);
141
142 /*
143 * Those are the important ordered steps:
144 *
145 * 1. Reset the packet object (put any permanent reference it
146 * has, unfreeze it and its fields in developer mode, etc.),
147 * but do NOT put its stream's reference. This stream
148 * contains the pool to which we're about to recycle this
149 * packet object, so we must guarantee its existence thanks
150 * to this existing reference.
151 *
152 * 2. Move the stream reference to our `stream`
153 * variable so that we can set the packet's stream member
154 * to NULL before recycling it. We CANNOT do this after
155 * we put the stream reference because this bt_put()
156 * could destroy the stream, also destroying its
157 * packet pool, thus also destroying our packet object (this
158 * would result in an invalid write access).
159 *
160 * 3. Recycle the packet object.
161 *
162 * 4. Put our stream reference.
163 */
164 bt_packet_reset(packet);
165 stream = packet->stream;
166 BT_ASSERT(stream);
167 packet->stream = NULL;
168 bt_object_pool_recycle_object(&stream->packet_pool, packet);
169 bt_object_put_no_null_check(&stream->common.base);
170 }
171
172 BT_HIDDEN
173 void bt_packet_destroy(struct bt_packet *packet)
174 {
175 BT_LOGD("Destroying packet: addr=%p", packet);
176 BT_LOGD_STR("Destroying packet's header field.");
177
178 if (packet->header) {
179 if (packet->stream) {
180 BT_LOGD_STR("Recycling packet's header field.");
181 bt_packet_header_field_recycle(packet->header,
182 bt_stream_class_borrow_trace(
183 bt_stream_borrow_class(packet->stream)));
184 } else {
185 bt_field_wrapper_destroy(packet->header);
186 }
187 }
188
189 if (packet->context) {
190 if (packet->stream) {
191 BT_LOGD_STR("Recycling packet's context field.");
192 bt_packet_context_field_recycle(packet->context,
193 bt_stream_borrow_class(packet->stream));
194 } else {
195 bt_field_wrapper_destroy(packet->context);
196 }
197 }
198
199 bt_clock_value_set_finalize(&packet->begin_cv_set);
200 bt_clock_value_set_finalize(&packet->end_cv_set);
201 BT_LOGD_STR("Putting packet's stream.");
202 bt_put(packet->stream);
203 g_free(packet);
204 }
205
206 BT_HIDDEN
207 struct bt_packet *bt_packet_new(struct bt_stream *stream)
208 {
209 struct bt_packet *packet = NULL;
210 struct bt_stream_class *stream_class = NULL;
211 struct bt_trace *trace = NULL;
212
213 BT_ASSERT(stream);
214 BT_LOGD("Creating packet object: stream-addr=%p, "
215 "stream-name=\"%s\", stream-class-addr=%p, "
216 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
217 stream, bt_stream_get_name(stream),
218 stream->common.stream_class,
219 bt_stream_class_common_get_name(stream->common.stream_class),
220 bt_stream_class_common_get_id(stream->common.stream_class));
221 stream_class = bt_stream_borrow_class(stream);
222 BT_ASSERT(stream_class);
223 trace = bt_stream_class_borrow_trace(stream_class);
224 BT_ASSERT(trace);
225 packet = g_new0(struct bt_packet, 1);
226 if (!packet) {
227 BT_LOGE_STR("Failed to allocate one packet object.");
228 goto end;
229 }
230
231 bt_object_init_shared(&packet->base,
232 (bt_object_release_func) bt_packet_recycle);
233 packet->stream = bt_get(stream);
234
235 if (trace->common.packet_header_field_type) {
236 BT_LOGD("Creating initial packet header field: ft-addr=%p",
237 trace->common.packet_header_field_type);
238 packet->header = bt_field_wrapper_create(
239 &trace->packet_header_field_pool,
240 (void *) trace->common.packet_header_field_type);
241 if (!packet->header) {
242 BT_LOGE("Cannot create packet header field wrapper.");
243 BT_PUT(packet);
244 goto end;
245 }
246 }
247
248 if (stream->common.stream_class->packet_context_field_type) {
249 BT_LOGD("Creating initial packet context field: ft-addr=%p",
250 stream->common.stream_class->packet_context_field_type);
251 packet->context = bt_field_wrapper_create(
252 &stream_class->packet_context_field_pool,
253 (void *) stream->common.stream_class->packet_context_field_type);
254 if (!packet->context) {
255 BT_LOGE("Cannot create packet context field wrapper.");
256 BT_PUT(packet);
257 goto end;
258 }
259 }
260
261 if (bt_clock_value_set_initialize(&packet->begin_cv_set)) {
262 BT_PUT(packet);
263 goto end;
264 }
265
266 if (bt_clock_value_set_initialize(&packet->end_cv_set)) {
267 BT_PUT(packet);
268 goto end;
269 }
270
271 BT_LOGD("Created packet object: addr=%p", packet);
272
273 end:
274 return packet;
275 }
276
277 struct bt_packet *bt_packet_create(struct bt_stream *stream)
278 {
279 struct bt_packet *packet = NULL;
280
281 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
282 packet = bt_object_pool_create_object(&stream->packet_pool);
283 if (unlikely(!packet)) {
284 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
285 "%![stream-]+s", stream);
286 goto end;
287 }
288
289 if (unlikely(!packet->stream)) {
290 packet->stream = stream;
291 bt_object_get_no_null_check_no_parent_check(
292 &packet->stream->common.base);
293 }
294
295 goto end;
296
297 end:
298 return packet;
299 }
300
301 int bt_packet_move_header(struct bt_packet *packet,
302 struct bt_packet_header_field *header_field)
303 {
304 struct bt_trace *trace;
305 struct bt_field_wrapper *field_wrapper = (void *) header_field;
306
307 BT_ASSERT_PRE_NON_NULL(packet, "Event");
308 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field");
309 BT_ASSERT_PRE_HOT(packet, "Packet", ": %!+a", packet);
310 trace = bt_stream_class_borrow_trace(
311 bt_stream_borrow_class(packet->stream));
312 BT_ASSERT_PRE(trace->common.packet_header_field_type,
313 "Trace has no packet header field type: %!+t",
314 trace);
315
316 /* TODO: compare field types (precondition) */
317
318 /* Recycle current header field: always exists */
319 BT_ASSERT(packet->header);
320 bt_packet_header_field_recycle(packet->header, trace);
321
322 /* Move new field */
323 packet->header = field_wrapper;
324 return 0;
325 }
326
327 int bt_packet_move_context(struct bt_packet *packet,
328 struct bt_packet_context_field *context_field)
329 {
330 struct bt_stream_class *stream_class;
331 struct bt_field_wrapper *field_wrapper = (void *) context_field;
332
333 BT_ASSERT_PRE_NON_NULL(packet, "Event");
334 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Context field");
335 BT_ASSERT_PRE_HOT(packet, "Packet", ": %!+a", packet);
336 stream_class = bt_stream_borrow_class(packet->stream);
337 BT_ASSERT_PRE(stream_class->common.packet_context_field_type,
338 "Stream class has no packet context field type: %!+S",
339 stream_class);
340
341 /* TODO: compare field types (precondition) */
342
343 /* Recycle current context field: always exists */
344 BT_ASSERT(packet->context);
345 bt_packet_context_field_recycle(packet->context, stream_class);
346
347 /* Move new field */
348 packet->context = field_wrapper;
349 return 0;
350 }
351
352 int bt_packet_set_beginning_clock_value(struct bt_packet *packet,
353 struct bt_clock_class *clock_class, uint64_t raw_value,
354 bt_bool is_default)
355 {
356 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
357 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
358 BT_ASSERT_PRE_HOT(packet, "Packet", ": %!+a", packet);
359 BT_ASSERT_PRE(is_default,
360 "You can only set a default clock value as of this version.");
361 return bt_clock_value_set_set_clock_value(&packet->begin_cv_set,
362 clock_class, raw_value, is_default);
363 }
364
365 struct bt_clock_value *bt_packet_borrow_default_begin_clock_value(
366 struct bt_packet *packet)
367 {
368 struct bt_clock_value *clock_value = NULL;
369
370 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
371 clock_value = packet->begin_cv_set.default_cv;
372 if (!clock_value) {
373 BT_LIB_LOGV("No default clock value: %![packet-]+a", packet);
374 }
375
376 return clock_value;
377 }
378
379 int bt_packet_set_end_clock_value(struct bt_packet *packet,
380 struct bt_clock_class *clock_class, uint64_t raw_value,
381 bt_bool is_default)
382 {
383 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
384 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
385 BT_ASSERT_PRE_HOT(packet, "Packet", ": %!+a", packet);
386 BT_ASSERT_PRE(is_default,
387 "You can only set a default clock value as of this version.");
388 return bt_clock_value_set_set_clock_value(&packet->end_cv_set,
389 clock_class, raw_value, is_default);
390 }
391
392 struct bt_clock_value *bt_packet_borrow_default_end_clock_value(
393 struct bt_packet *packet)
394 {
395 struct bt_clock_value *clock_value = NULL;
396
397 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
398 clock_value = packet->end_cv_set.default_cv;
399 if (!clock_value) {
400 BT_LIB_LOGV("No default clock value: %![packet-]+a", packet);
401 }
402
403 return clock_value;
404 }
This page took 0.038323 seconds and 5 git commands to generate.