[gtk/css-lookup-caching: 91/97] Make lookups refcounted



commit 5511c6c2388015eb71406ca5783b20ed50d30eb6
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Feb 10 18:10:42 2020 -0500

    Make lookups refcounted
    
    This will be useful when we share lookups.

 gtk/gtkcsslookup.c        | 37 ++++++++++++++++++++++++++++++++-----
 gtk/gtkcsslookupprivate.h |  7 +++++--
 gtk/gtkcssstaticstyle.c   | 10 +++++-----
 3 files changed, 42 insertions(+), 12 deletions(-)
---
diff --git a/gtk/gtkcsslookup.c b/gtk/gtkcsslookup.c
index 68551fb3c4..11f15e837f 100644
--- a/gtk/gtkcsslookup.c
+++ b/gtk/gtkcsslookup.c
@@ -24,18 +24,45 @@
 #include "gtkprivatetypebuiltins.h"
 #include "gtkprivate.h"
 
-void
-gtk_css_lookup_init (GtkCssLookup     *lookup)
+GtkCssLookup *
+gtk_css_lookup_new (void)
 {
-  memset (lookup, 0, sizeof (*lookup));
+  GtkCssLookup *lookup = g_new0 (GtkCssLookup, 1);
 
+  lookup->ref_count = 1;
   lookup->set_values = _gtk_bitmask_new ();
+
+  return lookup;
 }
 
-void
-gtk_css_lookup_destroy (GtkCssLookup *lookup)
+static void
+gtk_css_lookup_free (GtkCssLookup *lookup)
 {
   _gtk_bitmask_free (lookup->set_values);
+  g_free (lookup);
+}
+
+GtkCssLookup *
+gtk_css_lookup_ref (GtkCssLookup *lookup)
+{
+  gtk_internal_return_val_if_fail (lookup != NULL, NULL);
+  gtk_internal_return_val_if_fail (lookup->ref_count > 0, NULL);
+
+  lookup->ref_count++;
+
+  return lookup;
+}
+
+void
+gtk_css_lookup_unref (GtkCssLookup *lookup)
+{
+  gtk_internal_return_if_fail (lookup != NULL);
+  gtk_internal_return_if_fail (lookup->ref_count > 0);
+
+  lookup->ref_count--;
+
+  if (lookup->ref_count == 0)
+    gtk_css_lookup_free (lookup);
 }
 
 gboolean
diff --git a/gtk/gtkcsslookupprivate.h b/gtk/gtkcsslookupprivate.h
index 344995810a..aab2a3d197 100644
--- a/gtk/gtkcsslookupprivate.h
+++ b/gtk/gtkcsslookupprivate.h
@@ -36,12 +36,15 @@ typedef struct {
 } GtkCssLookupValue;
 
 struct _GtkCssLookup {
+  int ref_count;
   GtkBitmask *set_values;
   GtkCssLookupValue  values[GTK_CSS_PROPERTY_N_PROPERTIES];
 };
 
-void                     gtk_css_lookup_init                    (GtkCssLookup               *lookup);
-void                     gtk_css_lookup_destroy                 (GtkCssLookup               *lookup);
+GtkCssLookup *           gtk_css_lookup_new (void);
+GtkCssLookup *           gtk_css_lookup_ref (GtkCssLookup *lookup);
+void                     gtk_css_lookup_unref (GtkCssLookup *lookup);
+
 gboolean                 gtk_css_lookup_is_missing              (const GtkCssLookup         *lookup,
                                                                  guint                       id);
 void                     gtk_css_lookup_set                     (GtkCssLookup               *lookup,
diff --git a/gtk/gtkcssstaticstyle.c b/gtk/gtkcssstaticstyle.c
index adc8eb524a..e8d7d6367d 100644
--- a/gtk/gtkcssstaticstyle.c
+++ b/gtk/gtkcssstaticstyle.c
@@ -993,16 +993,16 @@ gtk_css_static_style_new_compute (GtkStyleProvider             *provider,
                                   GtkCssChange                  change)
 {
   GtkCssStaticStyle *result;
-  GtkCssLookup lookup;
+  GtkCssLookup *lookup;
   GtkCssNode *parent;
 
-  gtk_css_lookup_init (&lookup);
+  lookup = gtk_css_lookup_new ();
 
   if (node)
     gtk_style_provider_lookup (provider,
                                filter,
                                node,
-                               &lookup,
+                               lookup,
                                change == 0 ? &change : NULL);
 
   result = g_object_new (GTK_TYPE_CSS_STATIC_STYLE, NULL);
@@ -1014,12 +1014,12 @@ gtk_css_static_style_new_compute (GtkStyleProvider             *provider,
   else
     parent = NULL;
 
-  gtk_css_lookup_resolve (&lookup,
+  gtk_css_lookup_resolve (lookup,
                           provider,
                           result,
                           parent ? gtk_css_node_get_style (parent) : NULL);
 
-  gtk_css_lookup_destroy (&lookup);
+  gtk_css_lookup_unref (lookup);
 
   return GTK_CSS_STYLE (result);
 }


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