lib: notification iterator: transfer a batch of notifications
[babeltrace.git] / plugins / ctf / common / notif-iter / notif-iter.c
index 7128e184ba3821272e1d689415b9c0c795a06843..fc9bccc2c1eef88d9e7c93feb9df32c5d590d3ef 100644 (file)
@@ -64,8 +64,11 @@ struct stack_entry {
 
 /* Visit stack */
 struct stack {
-       /* Entries (struct stack_entry *) (top is last element) */
-       GPtrArray *entries;
+       /* Entries (struct stack_entry) */
+       GArray *entries;
+
+       /* Number of active entries */
+       size_t size;
 };
 
 /* State */
@@ -132,6 +135,9 @@ struct bt_notif_iter {
        /* Visit stack */
        struct stack *stack;
 
+       /* Current graph to create notifications (weak) */
+       struct bt_graph *graph;
+
        /*
         * Current dynamic scope field pointer.
         *
@@ -327,12 +333,6 @@ static
 enum bt_btr_status btr_timestamp_end_cb(void *value,
                struct bt_field_type *type, void *data);
 
-static
-void stack_entry_free_func(gpointer data)
-{
-       g_free(data);
-}
-
 static
 struct stack *stack_new(struct bt_notif_iter *notit)
 {
@@ -344,9 +344,9 @@ struct stack *stack_new(struct bt_notif_iter *notit)
                goto error;
        }
 
-       stack->entries = g_ptr_array_new_with_free_func(stack_entry_free_func);
+       stack->entries = g_array_new(FALSE, TRUE, sizeof(struct stack_entry));
        if (!stack->entries) {
-               BT_LOGE_STR("Failed to allocate a GPtrArray.");
+               BT_LOGE_STR("Failed to allocate a GArray.");
                goto error;
        }
 
@@ -363,41 +363,41 @@ void stack_destroy(struct stack *stack)
 {
        BT_ASSERT(stack);
        BT_LOGD("Destroying stack: addr=%p", stack);
-       g_ptr_array_free(stack->entries, TRUE);
+
+       if (stack->entries) {
+               g_array_free(stack->entries, TRUE);
+       }
+
        g_free(stack);
 }
 
 static
 int stack_push(struct stack *stack, struct bt_field *base)
 {
-       int ret = 0;
        struct stack_entry *entry;
 
        BT_ASSERT(stack);
        BT_ASSERT(base);
        BT_LOGV("Pushing base field on stack: stack-addr=%p, "
-               "stack-size-before=%u, stack-size-after=%u",
-               stack, stack->entries->len, stack->entries->len + 1);
-       entry = g_new0(struct stack_entry, 1);
-       if (!entry) {
-               BT_LOGE_STR("Failed to allocate one stack entry.");
-               ret = -1;
-               goto end;
+               "stack-size-before=%zu, stack-size-after=%zu",
+               stack, stack->size, stack->size + 1);
+
+       if (stack->entries->len == stack->size) {
+               g_array_set_size(stack->entries, stack->size + 1);
        }
 
+       entry = &g_array_index(stack->entries, struct stack_entry, stack->size);
        entry->base = base;
-       g_ptr_array_add(stack->entries, entry);
-
-end:
-       return ret;
+       entry->index = 0;
+       stack->size++;
+       return 0;
 }
 
 static inline
 unsigned int stack_size(struct stack *stack)
 {
        BT_ASSERT(stack);
-
-       return stack->entries->len;
+       return stack->size;
 }
 
 static
@@ -406,9 +406,9 @@ void stack_pop(struct stack *stack)
        BT_ASSERT(stack);
        BT_ASSERT(stack_size(stack));
        BT_LOGV("Popping from stack: "
-               "stack-addr=%p, stack-size-before=%u, stack-size-after=%u",
-               stack, stack->entries->len, stack->entries->len - 1);
-       g_ptr_array_remove_index(stack->entries, stack->entries->len - 1);
+               "stack-addr=%p, stack-size-before=%zu, stack-size-after=%zu",
+               stack, stack->size, stack->size - 1);
+       stack->size--;
 }
 
 static inline
@@ -416,8 +416,8 @@ struct stack_entry *stack_top(struct stack *stack)
 {
        BT_ASSERT(stack);
        BT_ASSERT(stack_size(stack));
-
-       return g_ptr_array_index(stack->entries, stack->entries->len - 1);
+       return &g_array_index(stack->entries, struct stack_entry,
+               stack->size - 1);
 }
 
 static inline
@@ -430,14 +430,7 @@ static
 void stack_clear(struct stack *stack)
 {
        BT_ASSERT(stack);
-
-       if (!stack_empty(stack)) {
-               BT_LOGV("Clearing stack: stack-addr=%p, stack-size=%u",
-                       stack, stack->entries->len);
-               g_ptr_array_remove_range(stack->entries, 0, stack_size(stack));
-       }
-
-       BT_ASSERT(stack_empty(stack));
+       stack->size = 0;
 }
 
 static inline
@@ -1582,8 +1575,10 @@ enum bt_notif_iter_status set_current_event_notification(
                notit, notit->meta.event_class,
                bt_event_class_get_name(notit->meta.event_class),
                notit->packet);
-       notif = bt_notification_event_create(notit->meta.event_class,
-               notit->packet, notit->meta.cc_prio_map);
+       BT_ASSERT(notit->graph);
+       notif = bt_notification_event_create(notit->graph,
+               notit->meta.event_class, notit->packet,
+               notit->meta.cc_prio_map);
        if (!notif) {
                BT_LOGE("Cannot create event notification: "
                        "notit-addr=%p, ec-addr=%p, ec-name=\"%s\", "
@@ -2759,7 +2754,8 @@ void notify_new_stream(struct bt_notif_iter *notit,
        }
 
        BT_ASSERT(notit->stream);
-       ret = bt_notification_stream_begin_create(notit->stream);
+       BT_ASSERT(notit->graph);
+       ret = bt_notification_stream_begin_create(notit->graph, notit->stream);
        if (!ret) {
                BT_LOGE("Cannot create stream beginning notification: "
                        "notit-addr=%p, stream-addr=%p",
@@ -2783,7 +2779,8 @@ void notify_end_of_stream(struct bt_notif_iter *notit,
                return;
        }
 
-       ret = bt_notification_stream_end_create(notit->stream);
+       BT_ASSERT(notit->graph);
+       ret = bt_notification_stream_end_create(notit->graph, notit->stream);
        if (!ret) {
                BT_LOGE("Cannot create stream beginning notification: "
                        "notit-addr=%p, stream-addr=%p",
@@ -2844,7 +2841,9 @@ void notify_new_packet(struct bt_notif_iter *notit,
                        notit->dscopes.stream_packet_context);
        }
 
-       notif = bt_notification_packet_begin_create(notit->packet);
+       BT_ASSERT(notit->graph);
+       notif = bt_notification_packet_begin_create(notit->graph,
+               notit->packet);
        if (!notif) {
                BT_LOGE("Cannot create packet beginning notification: "
                        "notit-addr=%p, packet-addr=%p",
@@ -2866,7 +2865,8 @@ void notify_end_of_packet(struct bt_notif_iter *notit,
                return;
        }
 
-       notif = bt_notification_packet_end_create(notit->packet);
+       BT_ASSERT(notit->graph);
+       notif = bt_notification_packet_end_create(notit->graph, notit->packet);
        if (!notif) {
                BT_LOGE("Cannot create packet end notification: "
                        "notit-addr=%p, packet-addr=%p",
@@ -3051,6 +3051,7 @@ void bt_notif_iter_destroy(struct bt_notif_iter *notit)
 enum bt_notif_iter_status bt_notif_iter_get_next_notification(
                struct bt_notif_iter *notit,
                struct bt_clock_class_priority_map *cc_prio_map,
+               struct bt_graph *graph,
                struct bt_notification **notification)
 {
        int ret;
@@ -3059,15 +3060,17 @@ enum bt_notif_iter_status bt_notif_iter_get_next_notification(
        BT_ASSERT(notit);
        BT_ASSERT(notification);
 
+       if (notit->state == STATE_DONE) {
+               status = BT_NOTIF_ITER_STATUS_EOF;
+               goto end;
+       }
+
        if (cc_prio_map != notit->meta.cc_prio_map) {
                bt_put(notit->meta.cc_prio_map);
                notit->meta.cc_prio_map = bt_get(cc_prio_map);
        }
 
-       if (notit->state == STATE_DONE) {
-               status = BT_NOTIF_ITER_STATUS_EOF;
-               goto end;
-       }
+       notit->graph = graph;
 
        BT_LOGV("Getting next notification: notit-addr=%p, cc-prio-map-addr=%p",
                notit, cc_prio_map);
@@ -3135,7 +3138,8 @@ enum bt_notif_iter_status bt_notif_iter_get_next_notification(
                                goto end;
                        }
 
-                       BT_MOVE(*notification, notit->event_notif);
+                       *notification = notit->event_notif;
+                       notit->event_notif = NULL;
                        goto end;
                case STATE_EMIT_NOTIF_END_OF_PACKET:
                        /* Update clock with timestamp_end field. */
This page took 0.027526 seconds and 4 git commands to generate.