+ /* Stream class already registered to the trace */
+ ret = -1;
+ goto end;
+ }
+ }
+
+ if (stream_class->clock) {
+ struct bt_ctf_clock_class *stream_clock_class =
+ stream_class->clock->clock_class;
+
+ if (trace->is_created_by_writer) {
+ /*
+ * Make sure this clock was also added to the
+ * trace (potentially through its CTF writer
+ * owner).
+ */
+ size_t i;
+
+ for (i = 0; i < trace->clocks->len; i++) {
+ if (trace->clocks->pdata[i] ==
+ stream_clock_class) {
+ /* Found! */
+ break;
+ }
+ }
+
+ if (i == trace->clocks->len) {
+ /* Not found */
+ ret = -1;
+ goto end;
+ }
+ } else {
+ /*
+ * This trace was NOT created by a CTF writer,
+ * thus do not allow the stream class to add to
+ * have a clock at all. Those are two
+ * independent APIs (non-writer and writer
+ * APIs), and isolating them simplifies things.
+ */
+ ret = -1;
+ goto end;
+ }
+ }
+
+ /*
+ * We're about to freeze both the trace and the stream class.
+ * Also, each event class contained in this stream class are
+ * already frozen.
+ *
+ * This trace, this stream class, and all its event classes
+ * should be valid at this point.
+ *
+ * Validate trace and stream class first, then each event
+ * class of this stream class can be validated individually.
+ */
+ packet_header_type =
+ bt_ctf_trace_get_packet_header_type(trace);
+ packet_context_type =
+ bt_ctf_stream_class_get_packet_context_type(stream_class);
+ event_header_type =
+ bt_ctf_stream_class_get_event_header_type(stream_class);
+ stream_event_ctx_type =
+ bt_ctf_stream_class_get_event_context_type(stream_class);
+ ret = bt_ctf_validate_class_types(trace->environment,
+ packet_header_type, packet_context_type, event_header_type,
+ stream_event_ctx_type, NULL, NULL, trace->valid,
+ stream_class->valid, 1, &trace_sc_validation_output,
+ trace_sc_validation_flags);
+ BT_PUT(packet_header_type);
+ BT_PUT(packet_context_type);
+ BT_PUT(event_header_type);
+ BT_PUT(stream_event_ctx_type);
+
+ if (ret) {
+ /*
+ * This means something went wrong during the validation
+ * process, not that the objects are invalid.
+ */
+ goto end;
+ }
+
+ if ((trace_sc_validation_output.valid_flags &
+ trace_sc_validation_flags) !=
+ trace_sc_validation_flags) {
+ /* Invalid trace/stream class */
+ ret = -1;
+ goto end;
+ }
+
+ 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 */