+static
+enum bt_notification_iterator_status populate_heap(struct ctf_fs_iterator *it)
+{
+ size_t i, pending_streams_count = it->pending_streams->len;
+ enum bt_notification_iterator_status ret =
+ BT_NOTIFICATION_ITERATOR_STATUS_OK;
+
+ /* Insert one stream-associated notification for each stream. */
+ for (i = 0; i < pending_streams_count; i++) {
+ struct bt_notification *notification;
+ struct ctf_fs_stream *fs_stream;
+ struct bt_ctf_stream *stream;
+ size_t pending_stream_index = pending_streams_count - 1 - i;
+
+ fs_stream = g_ptr_array_index(it->pending_streams,
+ pending_stream_index);
+
+ do {
+ int heap_ret;
+
+ ret = ctf_fs_iterator_get_next_notification(
+ it, fs_stream, ¬ification);
+ if (ret && ret != BT_NOTIFICATION_ITERATOR_STATUS_END) {
+ printf_debug("Failed to populate heap at stream %zu\n",
+ pending_stream_index);
+ goto end;
+ }
+
+ stream = internal_bt_notification_get_stream(
+ notification);
+ if (stream) {
+ gboolean inserted;
+
+ /*
+ * Associate pending ctf_fs_stream to
+ * bt_ctf_stream. Ownership of stream
+ * is passed to the stream ht.
+ */
+ inserted = g_hash_table_insert(it->stream_ht,
+ stream, fs_stream);
+ if (!inserted) {
+ ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
+ printf_debug("Failed to associate fs stream to ctf stream\n");
+ goto end;
+ }
+ }
+
+ heap_ret = bt_notification_heap_insert(
+ it->pending_notifications,
+ notification);
+ bt_put(notification);
+ if (heap_ret) {
+ ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
+ printf_debug("Failed to insert notification in heap\n");
+ goto end;
+ }
+ } while (!stream && ret != BT_NOTIFICATION_ITERATOR_STATUS_END);
+ /*
+ * Set NULL so the destruction callback registered with the
+ * array is not invoked on the stream (its ownership was
+ * transferred to the streams hashtable).
+ */
+ g_ptr_array_index(it->pending_streams,
+ pending_stream_index) = NULL;
+ g_ptr_array_remove_index(it->pending_streams,
+ pending_stream_index);
+ }
+
+ g_ptr_array_free(it->pending_streams, TRUE);
+ it->pending_streams = NULL;
+end:
+ return ret;
+}
+
+struct bt_notification_iterator_next_return ctf_fs_iterator_next(
+ struct bt_private_notification_iterator *iterator)
+{
+ int heap_ret;
+ struct bt_ctf_stream *stream = NULL;
+ struct ctf_fs_stream *fs_stream;
+ struct bt_notification *next_stream_notification;
+ struct ctf_fs_iterator *ctf_it =
+ bt_private_notification_iterator_get_user_data(
+ iterator);
+ struct bt_notification_iterator_next_return ret = {
+ .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
+ .notification = NULL,
+ };
+
+ ret.notification =
+ bt_notification_heap_pop(ctf_it->pending_notifications);
+ if (!ret.notification && !ctf_it->pending_streams) {
+ ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
+ goto end;
+ }
+
+ if (!ret.notification && ctf_it->pending_streams) {
+ /*
+ * Insert at one notification per stream in the heap and pop
+ * one.
+ */
+ ret.status = populate_heap(ctf_it);
+ if (ret.status) {
+ goto end;
+ }
+
+ ret.notification = bt_notification_heap_pop(
+ ctf_it->pending_notifications);
+ if (!ret.notification) {
+ ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
+ goto end;
+ }
+ }
+
+ /* notification is set from here. */
+
+ stream = internal_bt_notification_get_stream(ret.notification);
+ if (!stream) {
+ /*
+ * The current notification is not associated to a particular
+ * stream, there is no need to insert a new notification from
+ * a stream in the heap.
+ */