lib: use object pool for event and packet notifications
[babeltrace.git] / lib / graph / notification / packet.c
index fb590c3c512c9611fc8c68c722ab77456af44f9f..17b00d463ee42a6c5341f25a98b3ffea0800cc61 100644 (file)
 #include <babeltrace/ctf-ir/stream-class.h>
 #include <babeltrace/ctf-ir/stream.h>
 #include <babeltrace/ctf-ir/stream-internal.h>
+#include <babeltrace/graph/graph-internal.h>
 #include <babeltrace/graph/notification-packet-internal.h>
 #include <babeltrace/assert-internal.h>
 #include <babeltrace/assert-pre-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;
+       struct bt_notification_packet_begin *notification;
 
-       BT_LOGD("Destroying packet beginning 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;
+       }
 
-static
-void bt_notification_packet_end_destroy(struct bt_object *obj)
-{
-       struct bt_notification_packet_end *notification =
-                       (struct bt_notification_packet_end *) obj;
+       bt_notification_init(&notification->parent,
+                       BT_NOTIFICATION_TYPE_PACKET_BEGIN,
+                       (bt_object_release_func) bt_notification_packet_begin_recycle,
+                       graph);
+       goto end;
 
-       BT_LOGD("Destroying packet end notification: addr=%p",
-               notification);
-       BT_LOGD_STR("Putting packet.");
-       BT_PUT(notification->packet);
-       g_free(notification);
+error:
+       BT_PUT(notification);
+
+end:
+       return (void *) notification;
 }
 
 struct bt_notification *bt_notification_packet_begin_create(
-               struct bt_packet *packet)
+               struct bt_graph *graph, struct bt_packet *packet)
 {
        struct bt_notification_packet_begin *notification;
        struct bt_stream *stream;
@@ -84,15 +83,13 @@ struct bt_notification *bt_notification_packet_begin_create(
                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);
+       notification = (void *) bt_notification_create_from_pool(
+               &graph->packet_begin_notif_pool, graph);
        if (!notification) {
-               BT_LOGE_STR("Failed to allocate one packet beginning notification.");
+               /* bt_notification_create_from_pool() logs errors */
                goto error;
        }
 
-       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\", "
@@ -102,9 +99,45 @@ struct bt_notification *bt_notification_packet_begin_create(
                stream_class,
                bt_stream_class_get_name(stream_class),
                bt_stream_class_get_id(stream_class), notification);
-       return &notification->parent;
+       goto end;
+
 error:
-       return NULL;
+       BT_PUT(notification);
+
+end:
+       return (void *) notification;
+}
+
+BT_HIDDEN
+void bt_notification_packet_begin_destroy(struct bt_notification *notif)
+{
+       struct bt_notification_packet_begin *packet_begin_notif = (void *) notif;
+
+       BT_LOGD("Destroying packet beginning notification: addr=%p", notif);
+       BT_LOGD_STR("Putting packet.");
+       BT_PUT(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 (!notif->graph) {
+               bt_notification_packet_begin_destroy(notif);
+               return;
+       }
+
+       BT_LOGD("Recycling packet beginning notification: addr=%p", notif);
+       bt_notification_reset(notif);
+       BT_PUT(packet_begin_notif->packet);
+       graph = notif->graph;
+       notif->graph = NULL;
+       bt_object_pool_recycle_object(&graph->packet_begin_notif_pool, notif);
 }
 
 struct bt_packet *bt_notification_packet_begin_borrow_packet(
@@ -120,8 +153,32 @@ struct bt_packet *bt_notification_packet_begin_borrow_packet(
        return packet_begin->packet;
 }
 
+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_PUT(notification);
+
+end:
+       return (void *) notification;
+}
+
 struct bt_notification *bt_notification_packet_end_create(
-               struct bt_packet *packet)
+               struct bt_graph *graph, struct bt_packet *packet)
 {
        struct bt_notification_packet_end *notification;
        struct bt_stream *stream;
@@ -140,15 +197,13 @@ struct bt_notification *bt_notification_packet_end_create(
                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);
+       notification = (void *) bt_notification_create_from_pool(
+               &graph->packet_end_notif_pool, graph);
        if (!notification) {
-               BT_LOGE_STR("Failed to allocate one packet end notification.");
+               /* bt_notification_create_from_pool() logs errors */
                goto error;
        }
 
-       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\", "
@@ -158,9 +213,45 @@ struct bt_notification *bt_notification_packet_end_create(
                stream_class,
                bt_stream_class_get_name(stream_class),
                bt_stream_class_get_id(stream_class), notification);
-       return &notification->parent;
+       goto end;
+
 error:
-       return NULL;
+       BT_PUT(notification);
+
+end:
+       return (void *) notification;
+}
+
+BT_HIDDEN
+void bt_notification_packet_end_destroy(struct bt_notification *notif)
+{
+       struct bt_notification_packet_end *packet_end_notif = (void *) notif;
+
+       BT_LOGD("Destroying packet end notification: addr=%p", notif);
+       BT_LOGD_STR("Putting packet.");
+       BT_PUT(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_LOGD("Recycling packet end notification: addr=%p", notif);
+       bt_notification_reset(notif);
+       BT_PUT(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_notification_packet_end_borrow_packet(
This page took 0.027306 seconds and 4 git commands to generate.