+ if (event_class_count > 0) {
+ ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
+ event_class_count);
+ if (!ec_validation_outputs) {
+ ret = -1;
+ goto end;
+ }
+ }
+
+ /* Validate each event class individually */
+ for (i = 0; i < event_class_count; i++) {
+ struct bt_ctf_event_class *event_class =
+ bt_ctf_stream_class_get_event_class(stream_class, i);
+ struct bt_ctf_field_type *event_context_type = NULL;
+ struct bt_ctf_field_type *event_payload_type = NULL;
+
+ event_context_type =
+ bt_ctf_event_class_get_context_type(event_class);
+ event_payload_type =
+ bt_ctf_event_class_get_payload_type(event_class);
+
+ /*
+ * It is important to use the field types returned by
+ * the previous trace and stream class validation here
+ * because copies could have been made.
+ */
+ ret = bt_ctf_validate_class_types(trace->environment,
+ trace_sc_validation_output.packet_header_type,
+ trace_sc_validation_output.packet_context_type,
+ trace_sc_validation_output.event_header_type,
+ trace_sc_validation_output.stream_event_ctx_type,
+ event_context_type, event_payload_type,
+ 1, 1, event_class->valid, &ec_validation_outputs[i],
+ ec_validation_flags);
+ BT_PUT(event_context_type);
+ BT_PUT(event_payload_type);
+ BT_PUT(event_class);
+
+ if (ret) {
+ goto end;
+ }
+
+ if ((ec_validation_outputs[i].valid_flags &
+ ec_validation_flags) != ec_validation_flags) {
+ /* Invalid event class */
+ ret = -1;
+ goto end;
+ }
+ }
+
+ stream_id = bt_ctf_stream_class_get_id(stream_class);
+ if (stream_id < 0) {
+ stream_id = trace->next_stream_id++;
+
+ /* Try to assign a new stream id */
+ for (i = 0; i < trace->stream_classes->len; i++) {
+ if (stream_id == bt_ctf_stream_class_get_id(
+ trace->stream_classes->pdata[i])) {
+ /* Duplicate stream id found */
+ ret = -1;
+ goto end;
+ }
+ }
+
+ if (bt_ctf_stream_class_set_id_no_check(stream_class,
+ stream_id)) {
+ /* TODO Should retry with a different stream id */
+ ret = -1;
+ goto end;
+ }
+ }
+
+ bt_object_set_parent(stream_class, trace);
+ g_ptr_array_add(trace->stream_classes, stream_class);
+
+ /*
+ * At this point we know that the function will be successful.
+ * Therefore we can replace the trace and stream class field
+ * types with what's in their validation output structure and
+ * mark them as valid. We can also replace the field types of
+ * all the event classes of the stream class and mark them as
+ * valid.
+ */
+ bt_ctf_validation_replace_types(trace, stream_class, NULL,
+ &trace_sc_validation_output, trace_sc_validation_flags);
+ trace->valid = 1;
+ stream_class->valid = 1;
+
+ /*
+ * Put what was not moved in bt_ctf_validation_replace_types().
+ */
+ bt_ctf_validation_output_put_types(&trace_sc_validation_output);
+
+ for (i = 0; i < event_class_count; i++) {
+ struct bt_ctf_event_class *event_class =
+ bt_ctf_stream_class_get_event_class(stream_class, i);
+
+ bt_ctf_validation_replace_types(NULL, NULL, event_class,
+ &ec_validation_outputs[i], ec_validation_flags);
+ event_class->valid = 1;
+ BT_PUT(event_class);
+
+ /*
+ * Put what was not moved in
+ * bt_ctf_validation_replace_types().
+ */
+ bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
+ }
+
+ /*
+ * All field type byte orders set as "native" byte ordering can now be
+ * safely set to trace's own endianness, including the stream class'.
+ */
+ bt_ctf_field_type_set_native_byte_order(trace->packet_header_type,
+ trace->byte_order);
+ bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order);
+
+ /*
+ * Freeze the trace and the stream class.
+ */
+ bt_ctf_stream_class_freeze(stream_class);
+ bt_ctf_trace_freeze(trace);
+
+ /* Notifiy listeners of the trace's schema modification. */
+ bt_ctf_stream_class_visit(stream_class,
+ bt_ctf_trace_object_modification, trace);