+ switch (status) {
+ case BT_CTF_NOTIF_ITER_STATUS_EOF:
+ ret = BT_NOTIFICATION_ITERATOR_STATUS_END;
+ break;
+ case BT_CTF_NOTIF_ITER_STATUS_OK:
+ ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
+ break;
+ case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
+ /*
+ * Should not make it this far as this is medium-specific;
+ * there is nothing for the user to do and it should have been
+ * handled upstream.
+ */
+ assert(0);
+ case BT_CTF_NOTIF_ITER_STATUS_INVAL:
+ /* No argument provided by the user, so don't return INVAL. */
+ case BT_CTF_NOTIF_ITER_STATUS_ERROR:
+ default:
+ ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
+ break;
+ }
+ return ret;
+}
+
+/*
+ * Remove me. This is a temporary work-around due to our inhability to use
+ * libbabeltrace-ctf from libbabeltrace-plugin.
+ */
+static
+struct bt_ctf_stream *internal_bt_notification_get_stream(
+ struct bt_notification *notification)
+{
+ struct bt_ctf_stream *stream = NULL;
+
+ assert(notification);
+ switch (bt_notification_get_type(notification)) {
+ case BT_NOTIFICATION_TYPE_EVENT:
+ {
+ struct bt_ctf_event *event;
+
+ event = bt_notification_event_get_event(notification);
+ stream = bt_ctf_event_get_stream(event);
+ bt_put(event);
+ break;
+ }
+ case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
+ {
+ struct bt_ctf_packet *packet;
+
+ packet = bt_notification_packet_begin_get_packet(notification);
+ stream = bt_ctf_packet_get_stream(packet);
+ bt_put(packet);
+ break;
+ }
+ case BT_NOTIFICATION_TYPE_PACKET_END:
+ {
+ struct bt_ctf_packet *packet;
+
+ packet = bt_notification_packet_end_get_packet(notification);
+ stream = bt_ctf_packet_get_stream(packet);
+ bt_put(packet);
+ break;
+ }
+ case BT_NOTIFICATION_TYPE_STREAM_END:
+ stream = bt_notification_stream_end_get_stream(notification);
+ break;
+ default:
+ goto end;
+ }
+end:
+ return stream;
+}
+
+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;