+static
+struct ctf_file_event *create_event_definitions(struct ctf_trace *td,
+ struct ctf_stream *stream,
+ struct ctf_event *event)
+{
+ struct ctf_file_event *file_event = g_new0(struct ctf_file_event, 1);
+
+ if (event->context_decl) {
+ struct definition *definition =
+ event->context_decl->p.definition_new(&event->context_decl->p,
+ stream->parent_def_scope, 0, 0, "event.context");
+ if (!definition) {
+ goto error;
+ }
+ file_event->event_context = container_of(definition,
+ struct definition_struct, p);
+ stream->parent_def_scope = file_event->event_context->p.scope;
+ }
+ if (event->fields_decl) {
+ struct definition *definition =
+ event->fields_decl->p.definition_new(&event->fields_decl->p,
+ stream->parent_def_scope, 0, 0, "event.fields");
+ if (!definition) {
+ goto error;
+ }
+ file_event->event_fields = container_of(definition,
+ struct definition_struct, p);
+ stream->parent_def_scope = file_event->event_fields->p.scope;
+ }
+ return file_event;
+
+error:
+ if (file_event->event_fields)
+ definition_unref(&file_event->event_fields->p);
+ if (file_event->event_context)
+ definition_unref(&file_event->event_context->p);
+ return NULL;
+}
+
+static
+int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
+{
+ struct ctf_stream_class *stream_class;
+ int ret;
+ int i;
+
+ if (stream->stream_definitions_created)
+ return 0;
+
+ stream_class = stream->stream_class;
+
+ if (stream_class->packet_context_decl) {
+ struct definition *definition =
+ stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
+ stream->parent_def_scope, 0, 0, "stream.packet.context");
+ if (!definition) {
+ ret = -EINVAL;
+ goto error;
+ }
+ stream->stream_packet_context = container_of(definition,
+ struct definition_struct, p);
+ stream->parent_def_scope = stream->stream_packet_context->p.scope;
+ }
+ if (stream_class->event_header_decl) {
+ struct definition *definition =
+ stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
+ stream->parent_def_scope, 0, 0, "stream.event.header");
+ if (!definition) {
+ ret = -EINVAL;
+ goto error;
+ }
+ stream->stream_event_header =
+ container_of(definition, struct definition_struct, p);
+ stream->parent_def_scope = stream->stream_event_header->p.scope;
+ }
+ if (stream_class->event_context_decl) {
+ struct definition *definition =
+ stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
+ stream->parent_def_scope, 0, 0, "stream.event.context");
+ if (!definition) {
+ ret = -EINVAL;
+ goto error;
+ }
+ stream->stream_event_context =
+ container_of(definition, struct definition_struct, p);
+ stream->parent_def_scope = stream->stream_event_context->p.scope;
+ }
+ stream->events_by_id = g_ptr_array_new();
+ g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
+ for (i = 0; i < stream->events_by_id->len; i++) {
+ struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
+ struct ctf_file_event *file_event;
+
+ if (!event)
+ continue;
+ file_event = create_event_definitions(td, stream, event);
+ if (!file_event)
+ goto error_event;
+ g_ptr_array_index(stream->events_by_id, i) = file_event;
+ }
+ return 0;
+
+error_event:
+ for (i = 0; i < stream->events_by_id->len; i++) {
+ struct ctf_file_event *file_event = g_ptr_array_index(stream->events_by_id, i);
+ if (file_event)
+ g_free(file_event);
+ }
+ g_ptr_array_free(stream->events_by_id, TRUE);
+error:
+ if (stream->stream_event_context)
+ definition_unref(&stream->stream_event_context->p);
+ if (stream->stream_event_header)
+ definition_unref(&stream->stream_event_header->p);
+ if (stream->stream_packet_context)
+ definition_unref(&stream->stream_packet_context->p);
+ return ret;
+}
+