+ return ret;
+}
+
+/*
+ * babeltrace_iter_seek: seek iterator to given position.
+ */
+int babeltrace_iter_seek(struct babeltrace_iter *iter,
+ const struct trace_collection_pos *begin_pos)
+{
+ int i, stream_id;
+ int ret = 0;
+ struct trace_collection *tc = iter->tc;
+
+ for (i = 0; i < tc->array->len; i++) {
+ struct ctf_trace *tin;
+ struct trace_descriptor *td_read;
+
+ td_read = g_ptr_array_index(tc->array, i);
+ tin = container_of(td_read, struct ctf_trace, parent);
+
+ /* Populate heap with each stream */
+ for (stream_id = 0; stream_id < tin->streams->len;
+ stream_id++) {
+ struct ctf_stream_class *stream;
+ int filenr;
+
+ stream = g_ptr_array_index(tin->streams, stream_id);
+ for (filenr = 0; filenr < stream->streams->len;
+ filenr++) {
+ struct ctf_file_stream *file_stream;
+
+ file_stream = g_ptr_array_index(stream->streams,
+ filenr);
+ ret = babeltrace_filestream_seek(file_stream, begin_pos,
+ stream_id);
+ if (ret < 0)
+ goto end;
+ }
+ }
+ }
+end:
+ return ret;
+}
+
+struct babeltrace_iter *babeltrace_iter_create(struct trace_collection *tc,
+ struct trace_collection_pos *begin_pos,
+ struct trace_collection_pos *end_pos)
+{
+ int i, stream_id;
+ int ret = 0;
+ struct babeltrace_iter *iter;
+
+ iter = malloc(sizeof(struct babeltrace_iter));
+ if (!iter)
+ goto error_malloc;
+ iter->stream_heap = g_new(struct ptr_heap, 1);
+ iter->tc = tc;
+ iter->end_pos = end_pos;
+ iter->callbacks = g_array_new(0, 1, sizeof(struct bt_stream_callbacks));
+ iter->recalculate_dep_graph = 0;
+ iter->main_callbacks.callback = NULL;
+ iter->dep_gc = g_ptr_array_new();
+
+ ret = heap_init(iter->stream_heap, 0, stream_compare);
+ if (ret < 0)
+ goto error_heap_init;
+
+ for (i = 0; i < tc->array->len; i++) {
+ struct ctf_trace *tin;
+ struct trace_descriptor *td_read;
+
+ td_read = g_ptr_array_index(tc->array, i);
+ tin = container_of(td_read, struct ctf_trace, parent);
+
+ /* Populate heap with each stream */
+ for (stream_id = 0; stream_id < tin->streams->len;
+ stream_id++) {
+ struct ctf_stream_class *stream;
+ int filenr;
+
+ stream = g_ptr_array_index(tin->streams, stream_id);
+ if (!stream)
+ continue;
+ for (filenr = 0; filenr < stream->streams->len;
+ filenr++) {
+ struct ctf_file_stream *file_stream;
+
+ file_stream = g_ptr_array_index(stream->streams,
+ filenr);
+
+ if (begin_pos) {
+ ret = babeltrace_filestream_seek(file_stream, begin_pos,
+ stream_id);
+ if (ret == EOF) {
+ ret = 0;
+ continue;
+ } else if (ret) {
+ goto error;
+ }
+ }
+ /* Add to heap */
+ ret = heap_insert(iter->stream_heap, file_stream);
+ if (ret)
+ goto error;
+ }
+ }
+ }
+
+ return iter;
+
+error:
+ heap_free(iter->stream_heap);
+error_heap_init:
+ g_free(iter->stream_heap);
+ free(iter);
+error_malloc:
+ return NULL;
+}
+
+void babeltrace_iter_destroy(struct babeltrace_iter *iter)
+{
+ struct bt_stream_callbacks *bt_stream_cb;
+ struct bt_callback_chain *bt_chain;
+ int i, j;
+
+ heap_free(iter->stream_heap);
+ g_free(iter->stream_heap);
+
+ /* free all events callbacks */
+ if (iter->main_callbacks.callback)
+ g_array_free(iter->main_callbacks.callback, TRUE);
+
+ /* free per-event callbacks */
+ for (i = 0; i < iter->callbacks->len; i++) {
+ bt_stream_cb = &g_array_index(iter->callbacks,
+ struct bt_stream_callbacks, i);
+ if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks)
+ continue;
+ for (j = 0; j < bt_stream_cb->per_id_callbacks->len; j++) {
+ bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
+ struct bt_callback_chain, j);
+ if (bt_chain->callback) {
+ g_array_free(bt_chain->callback, TRUE);
+ }
+ }
+ g_array_free(bt_stream_cb->per_id_callbacks, TRUE);
+ }
+
+ free(iter);
+}
+
+int babeltrace_iter_next(struct babeltrace_iter *iter)
+{
+ struct ctf_file_stream *file_stream, *removed;
+ int ret;
+
+ file_stream = heap_maximum(iter->stream_heap);
+ if (!file_stream) {
+ /* end of file for all streams */
+ ret = 0;
+ goto end;
+ }
+
+ ret = stream_read_event(file_stream);
+ if (ret == EOF) {
+ removed = heap_remove(iter->stream_heap);
+ assert(removed == file_stream);
+ ret = 0;
+ goto end;
+ } else if (ret) {
+ goto end;
+ }
+ /* Reinsert the file stream into the heap, and rebalance. */
+ removed = heap_replace_max(iter->stream_heap, file_stream);
+ assert(removed == file_stream);
+
+end:
+ return ret;
+}
+
+static
+struct ctf_stream_event *extract_ctf_stream_event(struct ctf_stream *stream)
+{
+ struct ctf_stream_class *stream_class = stream->stream_class;
+ struct ctf_event *event_class;
+ struct ctf_stream_event *event;
+ uint64_t id = stream->event_id;
+