lib: make trace IR API const-correct
[babeltrace.git] / lib / graph / notification / packet.c
index 3a6b32376c1f1e71a94ad2365337052c6c2f9196..6e4a5a1b4a5c3f0636a0fa66b68b296a54de5711 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * Babeltrace Plug-in Packet-related Notifications
- *
  * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
  * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
 #include <babeltrace/lib-logging-internal.h>
 
 #include <babeltrace/compiler-internal.h>
-#include <babeltrace/ctf-ir/packet.h>
-#include <babeltrace/ctf-ir/packet-internal.h>
-#include <babeltrace/ctf-ir/stream-class.h>
-#include <babeltrace/ctf-ir/stream.h>
-#include <babeltrace/ctf-ir/stream-internal.h>
+#include <babeltrace/trace-ir/packet.h>
+#include <babeltrace/trace-ir/packet-internal.h>
+#include <babeltrace/trace-ir/stream-class.h>
+#include <babeltrace/trace-ir/stream.h>
+#include <babeltrace/trace-ir/stream-internal.h>
+#include <babeltrace/graph/graph-internal.h>
+#include <babeltrace/graph/private-notification-packet.h>
 #include <babeltrace/graph/notification-packet-internal.h>
+#include <babeltrace/object.h>
 #include <babeltrace/assert-internal.h>
 #include <babeltrace/assert-pre-internal.h>
+#include <babeltrace/object-internal.h>
 #include <inttypes.h>
 
-static
-void bt_notification_packet_begin_destroy(struct bt_object *obj)
+BT_HIDDEN
+struct bt_notification *bt_notification_packet_begin_new(struct bt_graph *graph)
 {
-       struct bt_notification_packet_begin *notification =
-                       (struct bt_notification_packet_begin *) obj;
-
-       BT_LOGD("Destroying packet beginning notification: addr=%p",
-               notification);
-       BT_LOGD_STR("Putting packet.");
-       BT_PUT(notification->packet);
-       g_free(notification);
-}
+       struct bt_notification_packet_begin *notification;
 
-static
-void bt_notification_packet_end_destroy(struct bt_object *obj)
-{
-       struct bt_notification_packet_end *notification =
-                       (struct bt_notification_packet_end *) obj;
-
-       BT_LOGD("Destroying packet end notification: addr=%p",
-               notification);
-       BT_LOGD_STR("Putting packet.");
-       BT_PUT(notification->packet);
-       g_free(notification);
+       notification = g_new0(struct bt_notification_packet_begin, 1);
+       if (!notification) {
+               BT_LOGE_STR("Failed to allocate one packet beginning notification.");
+               goto error;
+       }
+
+       bt_notification_init(&notification->parent,
+                       BT_NOTIFICATION_TYPE_PACKET_BEGIN,
+                       (bt_object_release_func) bt_notification_packet_begin_recycle,
+                       graph);
+       goto end;
+
+error:
+       BT_OBJECT_PUT_REF_AND_RESET(notification);
+
+end:
+       return (void *) notification;
 }
 
-struct bt_notification *bt_notification_packet_begin_create(
+struct bt_private_notification *bt_private_notification_packet_begin_create(
+               struct bt_self_notification_iterator *self_notif_iter,
                struct bt_packet *packet)
 {
-       struct bt_notification_packet_begin *notification;
+       struct bt_self_component_port_input_notification_iterator *notif_iter =
+               (void *) self_notif_iter;
+       struct bt_notification_packet_begin *notification = NULL;
        struct bt_stream *stream;
        struct bt_stream_class *stream_class;
 
+       BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
        BT_ASSERT_PRE_NON_NULL(packet, "Packet");
        stream = bt_packet_borrow_stream(packet);
        BT_ASSERT(stream);
        stream_class = bt_stream_borrow_class(stream);
        BT_ASSERT(stream_class);
-       BT_LOGD("Creating packet beginning notification object: "
-               "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
-               "stream-class-addr=%p, stream-class-name=\"%s\", "
-               "stream-class-id=%" PRId64,
-               packet, stream, bt_stream_get_name(stream),
-               stream_class,
-               bt_stream_class_get_name(stream_class),
-               bt_stream_class_get_id(stream_class));
-       notification = g_new0(struct bt_notification_packet_begin, 1);
+       BT_LIB_LOGD("Creating packet beginning notification object: "
+               "%![packet-]+a, %![stream-]+s, %![sc-]+S",
+               packet, stream, stream_class);
+       notification = (void *) bt_notification_create_from_pool(
+               &notif_iter->graph->packet_begin_notif_pool, notif_iter->graph);
        if (!notification) {
-               BT_LOGE_STR("Failed to allocate one packet beginning notification.");
-               goto error;
+               /* bt_notification_create_from_pool() logs errors */
+               goto end;
        }
 
-       bt_notification_init(&notification->parent,
-                       BT_NOTIFICATION_TYPE_PACKET_BEGIN,
-                       bt_notification_packet_begin_destroy);
-       notification->packet = bt_get(packet);
-       BT_LOGD("Created packet beginning notification object: "
-               "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
-               "stream-class-addr=%p, stream-class-name=\"%s\", "
-               "stream-class-id=%" PRId64 ", addr=%p",
-               packet, stream, bt_stream_get_name(stream),
-               stream_class,
-               bt_stream_class_get_name(stream_class),
-               bt_stream_class_get_id(stream_class), notification);
-       return &notification->parent;
-error:
-       return NULL;
+       BT_ASSERT(!notification->packet);
+       notification->packet = packet;
+       bt_object_get_no_null_check_no_parent_check(
+               &notification->packet->base);
+       bt_packet_set_is_frozen(packet, true);
+       BT_LIB_LOGD("Created packet beginning notification object: "
+               "%![notif-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
+               notification, packet, stream, stream_class);
+       goto end;
+
+end:
+       return (void *) notification;
 }
 
-struct bt_packet *bt_notification_packet_begin_get_packet(
-               struct bt_notification *notification)
+BT_HIDDEN
+void bt_notification_packet_begin_destroy(struct bt_notification *notif)
+{
+       struct bt_notification_packet_begin *packet_begin_notif = (void *) notif;
+
+       BT_LIB_LOGD("Destroying packet beginning notification: %!+n", notif);
+       BT_LIB_LOGD("Putting packet: %!+a", packet_begin_notif->packet);
+       BT_OBJECT_PUT_REF_AND_RESET(packet_begin_notif->packet);
+       g_free(notif);
+}
+
+BT_HIDDEN
+void bt_notification_packet_begin_recycle(struct bt_notification *notif)
+{
+       struct bt_notification_packet_begin *packet_begin_notif = (void *) notif;
+       struct bt_graph *graph;
+
+       BT_ASSERT(packet_begin_notif);
+
+       if (unlikely(!notif->graph)) {
+               bt_notification_packet_begin_destroy(notif);
+               return;
+       }
+
+       BT_LIB_LOGD("Recycling packet beginning notification: %!+n", notif);
+       bt_notification_reset(notif);
+       bt_object_put_no_null_check(&packet_begin_notif->packet->base);
+       packet_begin_notif->packet = NULL;
+       graph = notif->graph;
+       notif->graph = NULL;
+       bt_object_pool_recycle_object(&graph->packet_begin_notif_pool, notif);
+}
+
+struct bt_packet *bt_private_notification_packet_begin_borrow_packet(
+               struct bt_private_notification *notification)
 {
        struct bt_notification_packet_begin *packet_begin;
 
        BT_ASSERT_PRE_NON_NULL(notification, "Notification");
        BT_ASSERT_PRE_NOTIF_IS_TYPE(notification,
                BT_NOTIFICATION_TYPE_PACKET_BEGIN);
-       packet_begin = container_of(notification,
-                       struct bt_notification_packet_begin, parent);
-       return bt_get(packet_begin->packet);
+       packet_begin = (void *) notification;
+       return packet_begin->packet;
 }
 
-struct bt_notification *bt_notification_packet_end_create(
-               struct bt_packet *packet)
+const struct bt_packet *bt_notification_packet_begin_borrow_packet(
+               struct bt_notification *notification)
+{
+       return bt_private_notification_packet_begin_borrow_packet(
+               (void *) notification);
+}
+
+BT_HIDDEN
+struct bt_notification *bt_notification_packet_end_new(struct bt_graph *graph)
 {
        struct bt_notification_packet_end *notification;
+
+       notification = g_new0(struct bt_notification_packet_end, 1);
+       if (!notification) {
+               BT_LOGE_STR("Failed to allocate one packet end notification.");
+               goto error;
+       }
+
+       bt_notification_init(&notification->parent,
+                       BT_NOTIFICATION_TYPE_PACKET_END,
+                       (bt_object_release_func) bt_notification_packet_end_recycle,
+                       graph);
+       goto end;
+
+error:
+       BT_OBJECT_PUT_REF_AND_RESET(notification);
+
+end:
+       return (void *) notification;
+}
+
+struct bt_private_notification *bt_private_notification_packet_end_create(
+               struct bt_self_notification_iterator *self_notif_iter,
+               struct bt_packet *packet)
+{
+       struct bt_self_component_port_input_notification_iterator *notif_iter =
+               (void *) self_notif_iter;
+       struct bt_notification_packet_end *notification = NULL;
        struct bt_stream *stream;
        struct bt_stream_class *stream_class;
 
+       BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
        BT_ASSERT_PRE_NON_NULL(packet, "Packet");
        stream = bt_packet_borrow_stream(packet);
        BT_ASSERT(stream);
        stream_class = bt_stream_borrow_class(stream);
        BT_ASSERT(stream_class);
-       BT_LOGD("Creating packet end notification object: "
-               "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
-               "stream-class-addr=%p, stream-class-name=\"%s\", "
-               "stream-class-id=%" PRId64,
-               packet, stream, bt_stream_get_name(stream),
-               stream_class,
-               bt_stream_class_get_name(stream_class),
-               bt_stream_class_get_id(stream_class));
-       notification = g_new0(struct bt_notification_packet_end, 1);
+       BT_LIB_LOGD("Creating packet end notification object: "
+               "%![packet-]+a, %![stream-]+s, %![sc-]+S",
+               packet, stream, stream_class);
+       notification = (void *) bt_notification_create_from_pool(
+               &notif_iter->graph->packet_end_notif_pool, notif_iter->graph);
        if (!notification) {
-               BT_LOGE_STR("Failed to allocate one packet end notification.");
-               goto error;
+               /* bt_notification_create_from_pool() logs errors */
+               goto end;
        }
 
-       bt_notification_init(&notification->parent,
-                       BT_NOTIFICATION_TYPE_PACKET_END,
-                       bt_notification_packet_end_destroy);
-       notification->packet = bt_get(packet);
-       BT_LOGD("Created packet end notification object: "
-               "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
-               "stream-class-addr=%p, stream-class-name=\"%s\", "
-               "stream-class-id=%" PRId64 ", addr=%p",
-               packet, stream, bt_stream_get_name(stream),
-               stream_class,
-               bt_stream_class_get_name(stream_class),
-               bt_stream_class_get_id(stream_class), notification);
-       return &notification->parent;
-error:
-       return NULL;
+       BT_ASSERT(!notification->packet);
+       notification->packet = packet;
+       bt_object_get_no_null_check_no_parent_check(
+               &notification->packet->base);
+       bt_packet_set_is_frozen(packet, true);
+       BT_LIB_LOGD("Created packet end notification object: "
+               "%![notif-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
+               notification, packet, stream, stream_class);
+       goto end;
+
+end:
+       return (void *) notification;
 }
 
-struct bt_packet *bt_notification_packet_end_get_packet(
-               struct bt_notification *notification)
+BT_HIDDEN
+void bt_notification_packet_end_destroy(struct bt_notification *notif)
+{
+       struct bt_notification_packet_end *packet_end_notif = (void *) notif;
+
+       BT_LIB_LOGD("Destroying packet end notification: %!+n", notif);
+       BT_LIB_LOGD("Putting packet: %!+a", packet_end_notif->packet);
+       BT_OBJECT_PUT_REF_AND_RESET(packet_end_notif->packet);
+       g_free(notif);
+}
+
+BT_HIDDEN
+void bt_notification_packet_end_recycle(struct bt_notification *notif)
+{
+       struct bt_notification_packet_end *packet_end_notif = (void *) notif;
+       struct bt_graph *graph;
+
+       BT_ASSERT(packet_end_notif);
+
+       if (!notif->graph) {
+               bt_notification_packet_end_destroy(notif);
+               return;
+       }
+
+       BT_LIB_LOGD("Recycling packet end notification: %!+n", notif);
+       bt_notification_reset(notif);
+       BT_OBJECT_PUT_REF_AND_RESET(packet_end_notif->packet);
+       graph = notif->graph;
+       notif->graph = NULL;
+       bt_object_pool_recycle_object(&graph->packet_end_notif_pool, notif);
+}
+
+struct bt_packet *bt_private_notification_packet_end_borrow_packet(
+               struct bt_private_notification *notification)
 {
        struct bt_notification_packet_end *packet_end;
 
        BT_ASSERT_PRE_NON_NULL(notification, "Notification");
        BT_ASSERT_PRE_NOTIF_IS_TYPE(notification,
                BT_NOTIFICATION_TYPE_PACKET_END);
-       packet_end = container_of(notification,
-                       struct bt_notification_packet_end, parent);
-       return bt_get(packet_end->packet);
+       packet_end = (void *) notification;
+       return packet_end->packet;
+}
+
+const struct bt_packet *bt_notification_packet_end_borrow_packet(
+               struct bt_notification *notification)
+{
+       return bt_private_notification_packet_end_borrow_packet(
+               (void *) notification);
 }
This page took 0.027278 seconds and 4 git commands to generate.