lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / lib / ctf-ir / event.c
CommitLineData
273b65be
JG
1/*
2 * event.c
3 *
d2dc44b6 4 * Babeltrace CTF IR - Event
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
7b010242
PP
29#define BT_LOG_TAG "EVENT"
30#include <babeltrace/lib-logging-internal.h>
31
3dca2276 32#include <babeltrace/assert-pre-internal.h>
2e33ac5a
PP
33#include <babeltrace/ctf-ir/fields-internal.h>
34#include <babeltrace/ctf-ir/field-types-internal.h>
ac0c6bdd 35#include <babeltrace/ctf-ir/clock-class.h>
c057dea0
PP
36#include <babeltrace/ctf-ir/clock-value.h>
37#include <babeltrace/ctf-ir/clock-value-internal.h>
7b010242 38#include <babeltrace/ctf-ir/clock-class-internal.h>
adc315b8 39#include <babeltrace/ctf-ir/event-internal.h>
272df73e
PP
40#include <babeltrace/ctf-ir/event-class.h>
41#include <babeltrace/ctf-ir/event-class-internal.h>
2f100782 42#include <babeltrace/ctf-ir/stream-class.h>
c35a1669 43#include <babeltrace/ctf-ir/stream-class-internal.h>
41ac640a 44#include <babeltrace/ctf-ir/stream-internal.h>
4ce9f9d0
PP
45#include <babeltrace/ctf-ir/packet.h>
46#include <babeltrace/ctf-ir/packet-internal.h>
3dca2276 47#include <babeltrace/ctf-ir/trace.h>
bc37ae52 48#include <babeltrace/ctf-ir/trace-internal.h>
09840de5 49#include <babeltrace/ctf-ir/validation-internal.h>
5c3b707d 50#include <babeltrace/ctf-ir/packet-internal.h>
654c1444 51#include <babeltrace/ctf-ir/utils.h>
83509119 52#include <babeltrace/ref.h>
44e0a4f5 53#include <babeltrace/ctf-ir/attributes-internal.h>
3d9990ac 54#include <babeltrace/compiler-internal.h>
f6ccaed9 55#include <babeltrace/assert-internal.h>
7b010242 56#include <inttypes.h>
273b65be 57
3dca2276
PP
58static
59int bt_event_common_validate_types_for_create(
60 struct bt_event_class_common *event_class,
61 struct bt_validation_output *validation_output,
62 bt_validation_flag_copy_field_type_func copy_field_type_func)
273b65be 63{
09840de5 64 int ret;
50842bdc
PP
65 enum bt_validation_flag validation_flags =
66 BT_VALIDATION_FLAG_STREAM |
67 BT_VALIDATION_FLAG_EVENT;
3dca2276
PP
68 struct bt_trace_common *trace = NULL;
69 struct bt_stream_class_common *stream_class = NULL;
70 struct bt_field_type_common *packet_header_type = NULL;
71 struct bt_field_type_common *packet_context_type = NULL;
72 struct bt_field_type_common *event_header_type = NULL;
73 struct bt_field_type_common *stream_event_ctx_type = NULL;
74 struct bt_field_type_common *event_context_type = NULL;
75 struct bt_field_type_common *event_payload_type = NULL;
09840de5 76 int trace_valid = 0;
3dca2276 77 struct bt_value *environment = NULL;
2a3ced3c 78
3dca2276
PP
79 stream_class = bt_event_class_common_borrow_stream_class(event_class);
80 BT_ASSERT(stream_class);
81 trace = bt_stream_class_common_borrow_trace(stream_class);
09840de5 82 if (trace) {
3dca2276 83 BT_LOGD_STR("Event class is part of a trace.");
094ff7c0
PP
84 packet_header_type =
85 bt_trace_common_borrow_packet_header_field_type(trace);
09840de5 86 trace_valid = trace->valid;
f6ccaed9 87 BT_ASSERT(trace_valid);
09840de5
PP
88 environment = trace->environment;
89 }
90
094ff7c0
PP
91 packet_context_type =
92 bt_stream_class_common_borrow_packet_context_field_type(
93 stream_class);
94 event_header_type =
95 bt_stream_class_common_borrow_event_header_field_type(
96 stream_class);
97 stream_event_ctx_type =
98 bt_stream_class_common_borrow_event_context_field_type(
99 stream_class);
100 event_context_type =
101 bt_event_class_common_borrow_context_field_type(event_class);
102 event_payload_type =
103 bt_event_class_common_borrow_payload_field_type(event_class);
50842bdc 104 ret = bt_validate_class_types(environment, packet_header_type,
09840de5
PP
105 packet_context_type, event_header_type, stream_event_ctx_type,
106 event_context_type, event_payload_type, trace_valid,
107 stream_class->valid, event_class->valid,
3dca2276 108 validation_output, validation_flags, copy_field_type_func);
09840de5
PP
109 if (ret) {
110 /*
111 * This means something went wrong during the validation
112 * process, not that the objects are invalid.
113 */
e0bcd1b0 114 BT_LOGE("Failed to validate event and parents: ret=%d", ret);
09840de5
PP
115 goto error;
116 }
117
3dca2276 118 if ((validation_output->valid_flags & validation_flags) !=
09840de5
PP
119 validation_flags) {
120 /* Invalid trace/stream class/event class */
3ee09f3a 121 BT_LOGW("Invalid trace, stream class, or event class: "
3dca2276 122 "valid-flags=0x%x", validation_output->valid_flags);
09840de5
PP
123 goto error;
124 }
125
3dca2276 126 goto end;
0c1fafed 127
3dca2276
PP
128error:
129 bt_validation_output_put_types(validation_output);
130 ret = -1;
b8248cc0 131
3dca2276 132end:
3dca2276
PP
133 return ret;
134}
09840de5 135
3dca2276
PP
136static
137int bt_event_common_create_fields(
312c056a 138 struct bt_stream_class_common *stream_class,
3dca2276
PP
139 struct bt_validation_output *validation_output,
140 void *(*create_field_func)(void *),
312c056a
PP
141 void (*release_field_func)(void *),
142 void *(*create_header_field_func)(void *, void *),
143 void (*release_header_field_func)(void *),
144 struct bt_field_wrapper **header_field,
3dca2276
PP
145 struct bt_field_common **stream_event_context_field,
146 struct bt_field_common **context_field,
147 struct bt_field_common **payload_field)
148{
149 int ret = 0;
be514b0c 150
3dca2276 151 if (validation_output->event_header_type) {
be514b0c 152 BT_LOGD("Creating initial event header field: ft-addr=%p",
3dca2276
PP
153 validation_output->event_header_type);
154 *header_field =
312c056a
PP
155 create_header_field_func(stream_class,
156 validation_output->event_header_type);
3dca2276 157 if (!*header_field) {
be514b0c
PP
158 BT_LOGE_STR("Cannot create initial event header field object.");
159 goto error;
160 }
662e778c 161 }
09840de5 162
3dca2276 163 if (validation_output->stream_event_ctx_type) {
be514b0c 164 BT_LOGD("Creating initial stream event context field: ft-addr=%p",
3dca2276
PP
165 validation_output->stream_event_ctx_type);
166 *stream_event_context_field = create_field_func(
167 validation_output->stream_event_ctx_type);
168 if (!*stream_event_context_field) {
7b010242 169 BT_LOGE_STR("Cannot create initial stream event context field object.");
5fd2e9fd
PP
170 goto error;
171 }
172 }
173
3dca2276 174 if (validation_output->event_context_type) {
be514b0c 175 BT_LOGD("Creating initial event context field: ft-addr=%p",
3dca2276
PP
176 validation_output->event_context_type);
177 *context_field = create_field_func(
178 validation_output->event_context_type);
179 if (!*context_field) {
7b010242 180 BT_LOGE_STR("Cannot create initial event context field object.");
83509119 181 goto error;
662e778c 182 }
f655a84d 183 }
09840de5 184
3dca2276 185 if (validation_output->event_payload_type) {
be514b0c 186 BT_LOGD("Creating initial event payload field: ft-addr=%p",
3dca2276
PP
187 validation_output->event_payload_type);
188 *payload_field = create_field_func(
189 validation_output->event_payload_type);
190 if (!*payload_field) {
7b010242 191 BT_LOGE_STR("Cannot create initial event payload field object.");
09840de5
PP
192 goto error;
193 }
662e778c
JG
194 }
195
3dca2276
PP
196 goto end;
197
198error:
312c056a
PP
199 if (*header_field) {
200 release_header_field_func(*header_field);
201 }
202
203 if (*stream_event_context_field) {
204 release_field_func(*stream_event_context_field);
205 }
206
207 if (*context_field) {
208 release_field_func(*context_field);
209 }
210
211 if (*payload_field) {
212 release_field_func(*payload_field);
213 }
214
3dca2276
PP
215 ret = -1;
216
217end:
218 return ret;
219}
220
221BT_HIDDEN
222int _bt_event_common_validate(struct bt_event_common *event)
223{
224 int ret = 0;
225 struct bt_stream_class_common *stream_class;
226
227 BT_ASSERT(event);
228 if (event->header_field) {
312c056a
PP
229 ret = bt_field_common_validate_recursive(
230 event->header_field->field);
3dca2276
PP
231 if (ret) {
232 BT_ASSERT_PRE_MSG("Invalid event's header field: "
233 "%![event-]+_e, %![field-]+_f",
312c056a 234 event, event->header_field->field);
3dca2276
PP
235 goto end;
236 }
237 }
238
239 stream_class = bt_event_class_common_borrow_stream_class(event->class);
240
241 /*
242 * We should not have been able to create the event without associating
243 * the event class to a stream class.
244 */
245 BT_ASSERT(stream_class);
246
247 if (stream_class->event_context_field_type) {
248 ret = bt_field_common_validate_recursive(
249 event->stream_event_context_field);
250 if (ret) {
251 BT_ASSERT_PRE_MSG("Invalid event's stream event context field: "
252 "%![event-]+_e, %![field-]+_f",
253 event, event->stream_event_context_field);
254 goto end;
255 }
256 }
257
258 if (event->class->context_field_type) {
259 ret = bt_field_common_validate_recursive(event->context_field);
260 if (ret) {
261 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
262 "%![event-]+_e, %![field-]+_f",
263 event, event->context_field);
264 goto end;
265 }
266 }
267
268 ret = bt_field_common_validate_recursive(event->payload_field);
269 if (ret) {
270 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
271 "%![event-]+_e, %![field-]+_f",
272 event, event->payload_field);
273 goto end;
274 }
275
276end:
277 return ret;
278}
279
280BT_HIDDEN
281void _bt_event_common_freeze(struct bt_event_common *event)
282{
283 BT_ASSERT(event);
284
285 if (event->frozen) {
286 return;
287 }
288
289 BT_LOGD("Freezing event: addr=%p, "
290 "event-class-name=\"%s\", event-class-id=%" PRId64,
291 event, bt_event_class_common_get_name(event->class),
292 bt_event_class_common_get_id(event->class));
312c056a
PP
293
294 if (event->header_field) {
295 BT_LOGD_STR("Freezing event's header field.");
296 bt_field_common_set_is_frozen_recursive(
297 event->header_field->field, true);
298 }
299
300 if (event->stream_event_context_field) {
301 BT_LOGD_STR("Freezing event's stream event context field.");
302 bt_field_common_set_is_frozen_recursive(
303 event->stream_event_context_field, true);
304 }
305
306 if (event->context_field) {
307 BT_LOGD_STR("Freezing event's context field.");
308 bt_field_common_set_is_frozen_recursive(event->context_field, true);
309 }
310
311 if (event->payload_field) {
312 BT_LOGD_STR("Freezing event's payload field.");
313 bt_field_common_set_is_frozen_recursive(event->payload_field, true);
314 }
315
3dca2276
PP
316 event->frozen = 1;
317}
318
319BT_HIDDEN
320int bt_event_common_initialize(struct bt_event_common *event,
321 struct bt_event_class_common *event_class,
322 struct bt_clock_class *init_expected_clock_class,
323 bt_object_release_func release_func,
324 bt_validation_flag_copy_field_type_func field_type_copy_func,
325 bool must_be_in_trace,
326 int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class,
327 struct bt_field_type_common *packet_context_field_type,
328 struct bt_field_type_common *event_header_field_type),
312c056a
PP
329 void *(*create_field_func)(void *),
330 void (*release_field_func)(void *),
331 void *(*create_header_field_func)(void *, void *),
332 void (*release_header_field_func)(void *))
3dca2276
PP
333{
334 int ret;
335 struct bt_trace_common *trace = NULL;
336 struct bt_stream_class_common *stream_class = NULL;
312c056a 337 struct bt_field_wrapper *event_header = NULL;
3dca2276
PP
338 struct bt_field_common *stream_event_context = NULL;
339 struct bt_field_common *event_context = NULL;
340 struct bt_field_common *event_payload = NULL;
341 struct bt_validation_output validation_output = { 0 };
342 struct bt_clock_class *expected_clock_class =
343 init_expected_clock_class ? bt_get(init_expected_clock_class) :
344 NULL;
345
346 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
347 BT_LOGD("Initializing common event object: event-class-addr=%p, "
348 "event-class-name=\"%s\", event-class-id=%" PRId64,
349 event_class, bt_event_class_common_get_name(event_class),
350 bt_event_class_common_get_id(event_class));
351
352 stream_class = bt_event_class_common_borrow_stream_class(event_class);
353 BT_ASSERT_PRE(stream_class,
354 "Event class is not part of a stream class: %!+_E", event_class);
355
356 /* The event class was frozen when added to its stream class */
357 BT_ASSERT(event_class->frozen);
358 trace = bt_stream_class_common_borrow_trace(stream_class);
359
360 if (must_be_in_trace) {
361 BT_ASSERT_PRE(trace,
362 "Event class's stream class is not part of a trace: "
363 "%![ec-]+_E, %![ec-]+_S", event_class, stream_class);
364 }
365
366 /*
367 * This must be called before anything that can fail because on
368 * failure, the caller releases the reference to `event` to
369 * destroy it.
370 */
371 bt_object_init(event, release_func);
372
373 if (!stream_class->frozen) {
374 /*
375 * Because this function freezes the stream class,
376 * validate that this stream class contains at most a
377 * single clock class so that we set its expected clock
378 * class for future checks.
379 */
380 ret = bt_stream_class_common_validate_single_clock_class(
381 stream_class, &expected_clock_class);
382 if (ret) {
383 BT_LOGW("Event class's stream class or one of its event "
384 "classes contains a field type which is not "
385 "recursively mapped to the expected "
386 "clock class: "
387 "stream-class-addr=%p, "
388 "stream-class-id=%" PRId64 ", "
389 "stream-class-name=\"%s\", "
390 "expected-clock-class-addr=%p, "
391 "expected-clock-class-name=\"%s\"",
392 stream_class,
393 bt_stream_class_common_get_id(stream_class),
394 bt_stream_class_common_get_name(stream_class),
395 expected_clock_class,
396 expected_clock_class ?
397 bt_clock_class_get_name(expected_clock_class) :
398 NULL);
399 goto error;
400 }
401 }
402
403 /* Validate the trace, the stream class, and the event class */
404 ret = bt_event_common_validate_types_for_create(
405 event_class, &validation_output, field_type_copy_func);
406 if (ret) {
407 /* bt_event_common_validate_types_for_create() logs errors */
408 goto error;
409 }
410
411 if (map_clock_classes_func) {
412 /*
413 * Safe to automatically map selected fields to the
414 * stream's clock's class here because the stream class
415 * is about to be frozen.
416 */
417 if (map_clock_classes_func(stream_class,
418 validation_output.packet_context_type,
419 validation_output.event_header_type)) {
420 BT_LOGW_STR("Cannot automatically map selected stream class's "
421 "field types to stream class's clock's class.");
422 goto error;
423 }
424 }
425
426 /*
427 * event does not share a common ancestor with the event class; it has
428 * to guarantee its existence by holding a reference. This reference
429 * shall be released once the event is associated to a stream since,
430 * from that point, the event and its class will share the same
431 * lifetime.
432 */
433 event->class = bt_get(event_class);
434
312c056a
PP
435 ret = bt_event_common_create_fields(stream_class,
436 &validation_output,
437 create_field_func, release_field_func,
438 create_header_field_func, release_header_field_func,
439 &event_header, &stream_event_context, &event_context,
440 &event_payload);
3dca2276
PP
441 if (ret) {
442 /* bt_event_common_create_fields() logs errors */
443 goto error;
444 }
445
09840de5
PP
446 /*
447 * At this point all the fields are created, potentially from
448 * validated copies of field types, so that the field types and
449 * fields can be replaced in the trace, stream class,
450 * event class, and created event.
451 */
3dca2276
PP
452 bt_validation_replace_types(trace, stream_class, event_class,
453 &validation_output,
454 BT_VALIDATION_FLAG_STREAM | BT_VALIDATION_FLAG_EVENT);
312c056a
PP
455 event->header_field = event_header;
456 event_header = NULL;
457 event->stream_event_context_field = stream_event_context;
458 stream_event_context = NULL;
459 event->context_field = event_context;
460 event_context = NULL;
461 event->payload_field = event_payload;
462 event_payload = NULL;
09840de5
PP
463
464 /*
50842bdc 465 * Put what was not moved in bt_validation_replace_types().
09840de5 466 */
50842bdc 467 bt_validation_output_put_types(&validation_output);
09840de5 468
662e778c
JG
469 /*
470 * Freeze the stream class since the event header must not be changed
471 * anymore.
472 */
3dca2276 473 bt_stream_class_common_freeze(stream_class);
09840de5 474
2a3ced3c
PP
475 /*
476 * It is safe to set the stream class's unique clock class
477 * now because the stream class is frozen.
478 */
479 if (expected_clock_class) {
480 BT_MOVE(stream_class->clock_class, expected_clock_class);
481 }
482
09840de5
PP
483 /*
484 * Mark stream class, and event class as valid since
485 * they're all frozen now.
486 */
487 stream_class->valid = 1;
488 event_class->valid = 1;
489
490 /* Put stuff we borrowed from the event class */
3dca2276 491 BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", "
7b010242 492 "event-class-id=%" PRId64,
3dca2276
PP
493 event, bt_event_class_common_get_name(event->class),
494 bt_event_class_common_get_id(event->class));
495 goto end;
09840de5 496
83509119 497error:
50842bdc 498 bt_validation_output_put_types(&validation_output);
2a3ced3c 499 bt_put(expected_clock_class);
312c056a
PP
500
501 if (event_header) {
502 release_header_field_func(event_header);
503 }
504
505 if (stream_event_context) {
506 release_field_func(stream_event_context);
507 }
508
509 if (event_context) {
510 release_field_func(event_context);
511 }
512
513 if (event_payload) {
514 release_field_func(event_payload);
515 }
516
3dca2276
PP
517 ret = -1;
518
519end:
520 return ret;
521}
522
312c056a
PP
523static
524void bt_event_header_field_recycle(struct bt_field_wrapper *field_wrapper,
525 struct bt_stream_class *stream_class)
526{
527 BT_ASSERT(field_wrapper);
528 BT_LIB_LOGD("Recycling event header field: "
529 "addr=%p, %![sc-]+S, %![field-]+f", field_wrapper,
530 stream_class, field_wrapper->field);
531 bt_object_pool_recycle_object(
532 &stream_class->event_header_field_pool,
533 field_wrapper);
534}
535
536static
537struct bt_field_wrapper *create_event_header_field(
538 struct bt_stream_class *stream_class,
539 struct bt_field_type_common *ft)
540{
541 struct bt_field_wrapper *field_wrapper = NULL;
542
543 field_wrapper = bt_field_wrapper_create(
544 &stream_class->event_header_field_pool, (void *) ft);
545 if (!field_wrapper) {
546 goto error;
547 }
548
549 goto end;
550
551error:
552 if (field_wrapper) {
553 bt_event_header_field_recycle(field_wrapper, stream_class);
554 field_wrapper = NULL;
555 }
556
557end:
558 return field_wrapper;
559}
560
561BT_HIDDEN
562struct bt_event *bt_event_new(struct bt_event_class *event_class)
3dca2276
PP
563{
564 int ret;
565 struct bt_event *event = NULL;
312c056a 566 struct bt_stream_class *stream_class;
3dca2276
PP
567
568 event = g_new0(struct bt_event, 1);
569 if (!event) {
570 BT_LOGE_STR("Failed to allocate one event.");
571 goto error;
572 }
573
574 ret = bt_event_common_initialize(BT_TO_COMMON(event),
312c056a 575 BT_TO_COMMON(event_class), NULL, NULL,
3dca2276 576 (bt_validation_flag_copy_field_type_func) bt_field_type_copy,
312c056a
PP
577 true, NULL,
578 (void *) bt_field_create_recursive,
579 (void *) bt_field_destroy_recursive,
580 (void *) create_event_header_field,
581 (void *) bt_event_header_field_recycle);
3dca2276
PP
582 if (ret) {
583 /* bt_event_common_initialize() logs errors */
584 goto error;
585 }
586
312c056a 587 bt_object_set_is_shared((void *) event, false);
3dca2276 588 event->clock_values = g_hash_table_new_full(g_direct_hash,
312c056a
PP
589 g_direct_equal, NULL,
590 (GDestroyNotify) bt_clock_value_recycle);
591 BT_ASSERT(event->clock_values);
592 stream_class = bt_event_class_borrow_stream_class(event_class);
593 BT_ASSERT(stream_class);
594
595 if (stream_class->common.clock_class) {
596 struct bt_clock_value *clock_value;
597
598 clock_value = bt_clock_value_create(
599 stream_class->common.clock_class);
600 if (!clock_value) {
601 BT_LIB_LOGE("Cannot create clock value from clock class: "
602 "%![cc-]+K", stream_class->common.clock_class);
603 goto error;
604 }
605
606 g_hash_table_insert(event->clock_values,
607 stream_class->common.clock_class, clock_value);
608 }
609
3dca2276
PP
610 goto end;
611
612error:
312c056a
PP
613 if (event) {
614 bt_event_destroy(event);
615 event = NULL;
616 }
617
618end:
619 return event;
620}
621
622BT_ASSERT_PRE_FUNC
623static inline
624void _bt_event_reset_dev_mode(struct bt_event *event)
625{
626 GHashTableIter iter;
627 gpointer key, value;
628
629 BT_ASSERT(event);
630
631 if (event->common.header_field) {
632 bt_field_common_set_is_frozen_recursive(
633 event->common.header_field->field, false);
634 bt_field_common_reset_recursive(
635 event->common.header_field->field);
636 }
637
638 if (event->common.stream_event_context_field) {
639 bt_field_common_set_is_frozen_recursive(
640 event->common.stream_event_context_field, false);
641 bt_field_common_reset_recursive(
642 event->common.stream_event_context_field);
643 }
644
645 if (event->common.context_field) {
646 bt_field_common_set_is_frozen_recursive(
647 event->common.context_field, false);
648 bt_field_common_reset_recursive(event->common.context_field);
649 }
650
651 if (event->common.payload_field) {
652 bt_field_common_set_is_frozen_recursive(
653 event->common.payload_field, false);
654 bt_field_common_reset_recursive(event->common.payload_field);
655 }
656
657 g_hash_table_iter_init(&iter, event->clock_values);
658 while (g_hash_table_iter_next(&iter, &key, &value)) {
659 struct bt_clock_value *clock_value = value;
660
661 BT_ASSERT(clock_value);
662 bt_clock_value_reset(clock_value);
663 bt_clock_value_set_is_frozen(clock_value, false);
664 }
665}
666
667#ifdef BT_DEV_MODE
668# define bt_event_reset_dev_mode _bt_event_reset_dev_mode
669#else
670# define bt_event_reset_dev_mode(_x)
671#endif
672
673static inline
674void bt_event_reset(struct bt_event *event)
675{
676 BT_ASSERT(event);
677 event->common.frozen = false;
678 bt_event_reset_dev_mode(event);
679 BT_PUT(event->packet);
680}
681
682BT_HIDDEN
683struct bt_event *bt_event_create(struct bt_event_class *event_class,
684 struct bt_packet *packet)
685{
686 int ret;
687 struct bt_event *event = NULL;
688
689 BT_ASSERT(event_class);
690 event = bt_object_pool_create_object(&event_class->event_pool);
691 if (!event) {
692 BT_LIB_LOGE("Cannot allocate one event from event class's event pool: "
693 "%![event-class-]+E", event_class);
694 goto error;
695 }
696
697 if (!event->common.class) {
698 event->common.class = bt_get(event_class);
699 }
700
701 BT_ASSERT(packet);
702 ret = bt_event_set_packet(event, packet);
703 if (ret) {
704 BT_LIB_LOGE("Cannot set event's packet: "
705 "%![event-]+e, %![packet-]+a", event, packet);
706 goto error;
707 }
708
709 goto end;
710
711error:
712 if (event) {
713 bt_event_recycle(event);
714 event = NULL;
715 }
3dca2276
PP
716
717end:
83509119 718 return event;
273b65be
JG
719}
720
094ff7c0 721struct bt_event_class *bt_event_borrow_class(struct bt_event *event)
2f100782 722{
f6ccaed9 723 BT_ASSERT_PRE_NON_NULL(event, "Event");
094ff7c0
PP
724 return BT_FROM_COMMON(
725 bt_event_common_borrow_class(BT_TO_COMMON(event)));
2f100782
JG
726}
727
f6ccaed9 728struct bt_stream *bt_event_borrow_stream(struct bt_event *event)
f6ccaed9
PP
729{
730 BT_ASSERT_PRE_NON_NULL(event, "Event");
094ff7c0 731 return event->packet ? event->packet->stream : NULL;
f6ccaed9
PP
732}
733
094ff7c0 734struct bt_field *bt_event_borrow_payload(struct bt_event *event)
e5e6eb3a 735{
094ff7c0
PP
736 return BT_FROM_COMMON(
737 bt_event_common_borrow_payload(BT_TO_COMMON(event)));
e5e6eb3a
JG
738}
739
094ff7c0 740struct bt_field *bt_event_borrow_header(struct bt_event *event)
662e778c 741{
094ff7c0
PP
742 return BT_FROM_COMMON(
743 bt_event_common_borrow_header(BT_TO_COMMON(event)));
662e778c
JG
744}
745
094ff7c0 746struct bt_field *bt_event_borrow_context(struct bt_event *event)
f655a84d 747{
094ff7c0
PP
748 return BT_FROM_COMMON(
749 bt_event_common_borrow_context(BT_TO_COMMON(event)));
f655a84d
JG
750}
751
094ff7c0 752struct bt_field *bt_event_borrow_stream_event_context(
50842bdc 753 struct bt_event *event)
5fd2e9fd 754{
094ff7c0 755 return BT_FROM_COMMON(bt_event_common_borrow_stream_event_context(
3dca2276 756 BT_TO_COMMON(event)));
5fd2e9fd
PP
757}
758
312c056a
PP
759static
760void release_event_header_field(struct bt_field_wrapper *field_wrapper,
761 struct bt_event_common *event_common)
273b65be 762{
312c056a
PP
763 struct bt_event *event = BT_FROM_COMMON(event_common);
764 struct bt_event_class *event_class = bt_event_borrow_class(event);
765
766 if (!event_class) {
767 bt_field_wrapper_destroy(field_wrapper);
768 } else {
769 struct bt_stream_class *stream_class =
770 bt_event_class_borrow_stream_class(event_class);
771
772 BT_ASSERT(stream_class);
773 bt_event_header_field_recycle(field_wrapper, stream_class);
774 }
273b65be
JG
775}
776
312c056a
PP
777BT_HIDDEN
778void bt_event_destroy(struct bt_event *event)
273b65be 779{
312c056a
PP
780 BT_ASSERT(event);
781 bt_event_common_finalize((void *) event,
782 (void *) bt_field_destroy_recursive,
783 (void *) release_event_header_field);
41ac640a 784 g_hash_table_destroy(event->clock_values);
03039f21 785 BT_LOGD_STR("Putting event's packet.");
5c3b707d 786 bt_put(event->packet);
273b65be
JG
787 g_free(event);
788}
789
094ff7c0 790struct bt_clock_value *bt_event_borrow_clock_value(
50842bdc 791 struct bt_event *event, struct bt_clock_class *clock_class)
78586d8a 792{
50842bdc 793 struct bt_clock_value *clock_value = NULL;
78586d8a 794
f6ccaed9
PP
795 BT_ASSERT_PRE_NON_NULL(event, "Event");
796 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
ac0c6bdd 797 clock_value = g_hash_table_lookup(event->clock_values, clock_class);
78586d8a 798 if (!clock_value) {
7b010242
PP
799 BT_LOGV("No clock value associated to the given clock class: "
800 "event-addr=%p, event-class-name=\"%s\", "
801 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
802 "clock-class-name=\"%s\"", event,
3dca2276
PP
803 bt_event_class_common_get_name(event->common.class),
804 bt_event_class_common_get_id(event->common.class),
50842bdc 805 clock_class, bt_clock_class_get_name(clock_class));
78586d8a
JG
806 goto end;
807 }
808
1556a1af
JG
809end:
810 return clock_value;
811}
812
094ff7c0 813struct bt_packet *bt_event_borrow_packet(struct bt_event *event)
5c0f40f4 814{
50842bdc 815 struct bt_packet *packet = NULL;
5c0f40f4 816
f6ccaed9 817 BT_ASSERT_PRE_NON_NULL(event, "Event");
7b010242
PP
818 if (!event->packet) {
819 BT_LOGV("Event has no current packet: addr=%p, "
820 "event-class-name=\"%s\", event-class-id=%" PRId64,
3dca2276
PP
821 event, bt_event_class_common_get_name(event->common.class),
822 bt_event_class_common_get_id(event->common.class));
5c0f40f4
JG
823 goto end;
824 }
825
094ff7c0 826 packet = event->packet;
f6ccaed9 827
5c0f40f4
JG
828end:
829 return packet;
830}
831
312c056a
PP
832BT_HIDDEN
833int bt_event_set_packet(struct bt_event *event, struct bt_packet *packet)
5c3b707d 834{
f6ccaed9
PP
835 BT_ASSERT_PRE_NON_NULL(event, "Event");
836 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
3dca2276 837 BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_TO_COMMON(event), "Event");
5c3b707d
PP
838
839 /*
840 * Make sure the new packet was created by this event's
841 * stream, if it is set.
842 */
f6ccaed9
PP
843 if (bt_event_borrow_stream(event)) {
844 BT_ASSERT_PRE(packet->stream == bt_event_borrow_stream(event),
845 "Packet's stream and event's stream differ: "
846 "%![event-]+e, %![packet-]+a",
847 event, packet);
5c3b707d 848 } else {
3dca2276
PP
849 BT_ASSERT_PRE(bt_event_class_borrow_stream_class(
850 BT_FROM_COMMON(event->common.class)) ==
851 BT_FROM_COMMON(packet->stream->common.stream_class),
f6ccaed9
PP
852 "Packet's stream class and event's stream class differ: "
853 "%![event-]+e, %![packet-]+a",
854 event, packet);
5c3b707d
PP
855 }
856
cacf3147
PP
857 bt_get(packet);
858 BT_MOVE(event->packet, packet);
7b010242
PP
859 BT_LOGV("Set event's packet: event-addr=%p, "
860 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
861 "packet-addr=%p",
3dca2276
PP
862 event, bt_event_class_common_get_name(event->common.class),
863 bt_event_class_common_get_id(event->common.class), packet);
f6ccaed9 864 return 0;
5c3b707d 865}
4ce9f9d0
PP
866
867BT_HIDDEN
f6ccaed9 868void _bt_event_freeze(struct bt_event *event)
4ce9f9d0 869{
3dca2276
PP
870 _bt_event_common_freeze(BT_TO_COMMON(event));
871 BT_LOGD_STR("Freezing event's packet.");
50842bdc 872 bt_packet_freeze(event->packet);
4ce9f9d0 873}
312c056a
PP
874
875BT_HIDDEN
876void bt_event_recycle(struct bt_event *event)
877{
878 struct bt_event_class *event_class;
879
880 BT_ASSERT(event);
881 BT_LIB_LOGD("Recycling event: %!+e", event);
882
883 /*
884 * Those are the important ordered steps:
885 *
886 * 1. Reset the event object (put any permanent reference it
887 * has, unfreeze it and its fields in developer mode, etc.),
888 * but do NOT put its class's reference. This event class
889 * contains the pool to which we're about to recycle this
890 * event object, so we must guarantee its existence thanks
891 * to this existing reference.
892 *
893 * 2. Move the event class reference to our `event_class`
894 * variable so that we can set the event's class member
895 * to NULL before recycling it. We CANNOT do this after
896 * we put the event class reference because this bt_put()
897 * could destroy the event class, also destroying its
898 * event pool, thus also destroying our event object (this
899 * would result in an invalid write access).
900 *
901 * 3. Recycle the event object.
902 *
903 * 4. Put our event class reference.
904 */
905 bt_event_reset(event);
906 event_class = BT_FROM_COMMON(event->common.class);
907 BT_ASSERT(event_class);
908 event->common.class = NULL;
909 bt_object_pool_recycle_object(&event_class->event_pool, event);
910 bt_put(event_class);
911}
912
913int bt_event_move_header(struct bt_event *event,
914 struct bt_event_header_field *header_field)
915{
916 struct bt_stream_class *stream_class;
917 struct bt_field_wrapper *field_wrapper = (void *) header_field;
918
919 BT_ASSERT_PRE_NON_NULL(event, "Event");
920 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field");
921 BT_ASSERT_PRE_HOT(BT_TO_COMMON(event), "Event", ": +%!+e", event);
922 stream_class = bt_event_class_borrow_stream_class(
923 bt_event_borrow_class(event));
924 BT_ASSERT_PRE(stream_class->common.event_header_field_type,
925 "Stream class has no event header field type: %!+S",
926 stream_class);
927
928 /* Recycle current header field: always exists */
929 BT_ASSERT(event->common.header_field);
930 bt_event_header_field_recycle(event->common.header_field,
931 stream_class);
932
933 /* Move new field */
934 event->common.header_field = (void *) field_wrapper;
935 return 0;
936}
This page took 0.092001 seconds and 4 git commands to generate.