-static inline
-int bt_event_initialize(struct bt_event *event,
- struct bt_event_class *event_class,
- bt_validation_flag_copy_field_type_func field_type_copy_func,
- create_field_func create_field_func,
- release_field_func release_field_func,
- create_header_field_func create_header_field_func,
- release_header_field_func release_header_field_func)
-{
- int ret;
- struct bt_trace *trace = NULL;
- struct bt_stream_class *stream_class = NULL;
- struct bt_field_wrapper *event_header = NULL;
- struct bt_field *stream_event_context = NULL;
- struct bt_field *event_context = NULL;
- struct bt_field *event_payload = NULL;
- struct bt_validation_output validation_output = { 0 };
- struct bt_clock_class *expected_clock_class = NULL;
-
- BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
- BT_LOGD("Initializing event object: event-class-addr=%p, "
- "event-class-name=\"%s\", event-class-id=%" PRId64,
- event_class, bt_event_class_get_name(event_class),
- bt_event_class_get_id(event_class));
-
- stream_class = bt_event_class_borrow_stream_class(event_class);
- BT_ASSERT_PRE(stream_class,
- "Event class is not part of a stream class: %!+E", event_class);
-
- /* The event class was frozen when added to its stream class */
- BT_ASSERT(event_class->frozen);
- trace = bt_stream_class_borrow_trace(stream_class);
- BT_ASSERT_PRE(trace,
- "Event class's stream class is not part of a trace: "
- "%![ec-]+E, %![ec-]+S", event_class, stream_class);
-
- /*
- * This must be called before anything that can fail because on
- * failure, the caller releases the reference to `event` to
- * destroy it.
- */
- bt_object_init_unique(&event->base);
-
- if (!stream_class->frozen) {
- /*
- * Because this function freezes the stream class,
- * validate that this stream class contains at most a
- * single clock class so that we set its expected clock
- * class for future checks.
- */
- ret = bt_stream_class_validate_single_clock_class(
- stream_class, &expected_clock_class);
- if (ret) {
- BT_LOGW("Event class's stream class or one of its event "
- "classes contains a field type which is not "
- "recursively mapped to the expected "
- "clock class: "
- "stream-class-addr=%p, "
- "stream-class-id=%" PRId64 ", "
- "stream-class-name=\"%s\", "
- "expected-clock-class-addr=%p, "
- "expected-clock-class-name=\"%s\"",
- stream_class,
- bt_stream_class_get_id(stream_class),
- bt_stream_class_get_name(stream_class),
- expected_clock_class,
- expected_clock_class ?
- bt_clock_class_get_name(expected_clock_class) :
- NULL);
- goto error;
- }
- }
-
- /* Validate the trace, the stream class, and the event class */
- ret = bt_event_validate_types_for_create(
- event_class, &validation_output, field_type_copy_func);
- if (ret) {
- /* bt_event_validate_types_for_create() logs errors */
- goto error;
- }
-
- /*
- * event does not share a common ancestor with the event class; it has
- * to guarantee its existence by holding a reference. This reference
- * shall be released once the event is associated to a stream since,
- * from that point, the event and its class will share the same
- * lifetime.
- *
- * TODO: Is this still true now that this API and CTF writer are
- * two different implementations?
- */
- event->class = bt_get(event_class);
- ret = bt_event_create_fields(stream_class,
- &validation_output,
- create_field_func, release_field_func,
- create_header_field_func, release_header_field_func,
- &event_header, &stream_event_context, &event_context,
- &event_payload);
- if (ret) {
- /* bt_event_create_fields() logs errors */
- goto error;
- }
-
- /*
- * At this point all the fields are created, potentially from
- * validated copies of field types, so that the field types and
- * fields can be replaced in the trace, stream class,
- * event class, and created event.
- */
- bt_validation_replace_types(trace, stream_class, event_class,
- &validation_output,
- BT_VALIDATION_FLAG_STREAM | BT_VALIDATION_FLAG_EVENT);
- event->header_field = event_header;
- event_header = NULL;
- event->stream_event_context_field = stream_event_context;
- stream_event_context = NULL;
- event->context_field = event_context;
- event_context = NULL;
- event->payload_field = event_payload;
- event_payload = NULL;
-
- /*
- * Put what was not moved in bt_validation_replace_types().
- */
- bt_validation_output_put_types(&validation_output);
-
- /*
- * Freeze the stream class since the event header must not be changed
- * anymore.
- */
- bt_stream_class_freeze(stream_class);
-
- /*
- * It is safe to set the stream class's unique clock class
- * now because the stream class is frozen.
- */
- if (expected_clock_class) {
- BT_MOVE(stream_class->clock_class, expected_clock_class);
- }
-
- /*
- * Mark stream class, and event class as valid since
- * they're all frozen now.
- */
- stream_class->valid = 1;
- event_class->valid = 1;
-
- /* Put stuff we borrowed from the event class */
- BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", "
- "event-class-id=%" PRId64,
- event, bt_event_class_get_name(event->class),
- bt_event_class_get_id(event->class));
- goto end;
-
-error:
- bt_validation_output_put_types(&validation_output);
- bt_put(expected_clock_class);
-
- if (event_header) {
- release_header_field_func(event_header, stream_class);
- }
-
- if (stream_event_context) {
- release_field_func(stream_event_context);
- }
-
- if (event_context) {
- release_field_func(event_context);
- }
-
- if (event_payload) {
- release_field_func(event_payload);
- }
-
- ret = -1;
-
-end:
- return ret;
-}
-