[gtk/strdup-avoidance: 2/3] eventcontroller: Allow static names




commit b6a965a67c5f2e34975e5d0781bd0a0ceccc4096
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Aug 27 21:59:02 2022 -0400

    eventcontroller: Allow static names
    
    These names are a debug-only feature, and we
    use string literals everywhere, so avoid the
    overhead of copying them.

 gtk/gtkeventcontroller.c | 33 ++++++++++++++++++++++++++++++---
 gtk/gtkeventcontroller.h |  3 +++
 2 files changed, 33 insertions(+), 3 deletions(-)
---
diff --git a/gtk/gtkeventcontroller.c b/gtk/gtkeventcontroller.c
index f4aec29eda..df6a9eebcc 100644
--- a/gtk/gtkeventcontroller.c
+++ b/gtk/gtkeventcontroller.c
@@ -66,6 +66,7 @@ struct _GtkEventControllerPrivate
   char *name;
   GtkWidget *target;
   GdkEvent *event;
+  unsigned int name_is_static : 1;
 };
 
 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkEventController, gtk_event_controller, G_TYPE_OBJECT)
@@ -173,8 +174,9 @@ gtk_event_controller_finalize (GObject *object)
 {
   GtkEventController *self = GTK_EVENT_CONTROLLER (object);
   GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (self);
-  
-  g_free (priv->name);
+
+  if (!priv->name_is_static)
+    g_free (priv->name);
 
   G_OBJECT_CLASS (gtk_event_controller_parent_class)->finalize (object);
 }
@@ -571,8 +573,33 @@ gtk_event_controller_set_name (GtkEventController *controller,
 
   g_return_if_fail (GTK_IS_EVENT_CONTROLLER (controller));
 
-  g_free (priv->name);
+  if (!priv->name_is_static)
+    g_free (priv->name);
   priv->name = g_strdup (name);
+  priv->name_is_static = FALSE;
+}
+
+/**
+ * gtk_event_controller_set_static_name:
+ * @controller: a `GtkEventController`
+ * @name: (nullable): a name for @controller, must be a static string
+ *
+ * Sets a name on the controller that can be used for debugging.
+ *
+ * Since: 4.8
+ */
+void
+gtk_event_controller_set_static_name (GtkEventController *controller,
+                                      const char         *name)
+{
+  GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (controller);
+
+  g_return_if_fail (GTK_IS_EVENT_CONTROLLER (controller));
+
+  if (!priv->name_is_static)
+    g_free (priv->name);
+  priv->name = (char *)name;
+  priv->name_is_static = TRUE;
 }
 
 GtkWidget *
diff --git a/gtk/gtkeventcontroller.h b/gtk/gtkeventcontroller.h
index e564b3d9bc..084e7a4f42 100644
--- a/gtk/gtkeventcontroller.h
+++ b/gtk/gtkeventcontroller.h
@@ -68,6 +68,9 @@ const char *        gtk_event_controller_get_name              (GtkEventControll
 GDK_AVAILABLE_IN_ALL
 void                gtk_event_controller_set_name              (GtkEventController *controller,
                                                                 const char         *name);
+GDK_AVAILABLE_IN_4_8
+void                gtk_event_controller_set_static_name       (GtkEventController *controller,
+                                                                const char         *name);
 
 GDK_AVAILABLE_IN_ALL
 GdkEvent *          gtk_event_controller_get_current_event    (GtkEventController *controller);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]