[gnome-menus] Memory leak fixes



commit 7d90221e27fcc72de497f45db4f3ef3c2d623fd6
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Feb 1 21:46:13 2013 -0500

    Memory leak fixes
    
    Based on a patch by William Jon McCann <william jon mccann gmail com>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=349695

 libmenu/desktop-entries.c   |   10 ++-
 libmenu/entry-directories.c |  167 ++++++++++++++++++++++++++++---------------
 libmenu/gmenu-tree.c        |   14 +++--
 3 files changed, 124 insertions(+), 67 deletions(-)
---
diff --git a/libmenu/desktop-entries.c b/libmenu/desktop-entries.c
index a8c13d9..1e23bf2 100644
--- a/libmenu/desktop-entries.c
+++ b/libmenu/desktop-entries.c
@@ -406,7 +406,7 @@ desktop_entry_ref (DesktopEntry *entry)
   g_return_val_if_fail (entry != NULL, NULL);
   g_return_val_if_fail (entry->refcount > 0, NULL);
 
-  entry->refcount += 1;
+  g_atomic_int_inc (&entry->refcount);
 
   return entry;
 }
@@ -699,7 +699,7 @@ desktop_entry_set_ref (DesktopEntrySet *set)
   g_return_val_if_fail (set != NULL, NULL);
   g_return_val_if_fail (set->refcount > 0, NULL);
 
-  set->refcount += 1;
+  g_atomic_int_inc (&set->refcount);
 
   return set;
 }
@@ -707,11 +707,13 @@ desktop_entry_set_ref (DesktopEntrySet *set)
 void
 desktop_entry_set_unref (DesktopEntrySet *set)
 {
+  gboolean is_zero;
+
   g_return_if_fail (set != NULL);
   g_return_if_fail (set->refcount > 0);
 
-  set->refcount -= 1;
-  if (set->refcount == 0)
+  is_zero = g_atomic_int_dec_and_test (&set->refcount);
+  if (is_zero)
     {
       menu_verbose (" Deleting entry set %p\n", set);
 
diff --git a/libmenu/entry-directories.c b/libmenu/entry-directories.c
index ee22a55..67869b4 100644
--- a/libmenu/entry-directories.c
+++ b/libmenu/entry-directories.c
@@ -40,12 +40,12 @@ struct EntryDirectory
 
   guint entry_type : 2;
   guint is_legacy : 1;
-  guint refcount : 24;
+  volatile gint refcount;
 };
 
 struct EntryDirectoryList
 {
-  int    refcount;
+  volatile int refcount;
   int    length;
   GList *dirs;
 };
@@ -64,7 +64,10 @@ struct CachedDir
   guint have_read_entries : 1;
   guint deleted : 1;
 
-  guint references : 28;
+  GFunc    notify;
+  gpointer notify_data;
+
+  volatile gint references;
 };
 
 struct CachedDirMonitor
@@ -79,6 +82,12 @@ static void     cached_dir_remove_reference       (CachedDir *dir);
 static void     cached_dir_free                   (CachedDir  *dir);
 static gboolean cached_dir_load_entries_recursive (CachedDir  *dir,
                                                    const char *dirname);
+static void     cached_dir_unref                  (CachedDir *dir);
+static CachedDir * cached_dir_add_subdir          (CachedDir  *dir,
+                                                   const char *basename,
+                                                   const char *path);
+static gboolean cached_dir_remove_subdir          (CachedDir  *dir,
+                                                   const char *basename);
 
 static void handle_cached_dir_changed (MenuMonitor      *monitor,
 				       MenuMonitorEvent  event,
@@ -91,18 +100,39 @@ static void handle_cached_dir_changed (MenuMonitor      *monitor,
 
 static CachedDir *dir_cache = NULL;
 
+static void
+clear_cache (CachedDir *dir,
+             gpointer  *cache)
+{
+  *cache = NULL;
+}
+
 static CachedDir *
 cached_dir_new (const char *name)
 {
   CachedDir *dir;
 
   dir = g_new0 (CachedDir, 1);
-
   dir->name = g_strdup (name);
 
   return dir;
 }
 
+static CachedDir *
+cached_dir_new_full (const char *name,
+                     GFunc       notify,
+                     gpointer    notify_data)
+{
+  CachedDir *dir;
+
+  dir = cached_dir_new (name);
+
+  dir->notify = notify;
+  dir->notify_data = notify_data;
+
+  return dir;
+}
+
 static void
 cached_dir_free (CachedDir *dir)
 {
@@ -126,7 +156,7 @@ cached_dir_free (CachedDir *dir)
   dir->entries = NULL;
 
   g_slist_foreach (dir->subdirs,
-                   (GFunc) cached_dir_free,
+                   (GFunc) cached_dir_unref,
                    NULL);
   g_slist_free (dir->subdirs);
   dir->subdirs = NULL;
@@ -135,6 +165,36 @@ cached_dir_free (CachedDir *dir)
   g_free (dir);
 }
 
+static CachedDir *
+cached_dir_ref (CachedDir *dir)
+{
+  g_atomic_int_inc (&dir->references);
+
+  return dir;
+}
+
+static void
+cached_dir_unref (CachedDir *dir)
+{
+  gboolean is_zero;
+
+  is_zero = g_atomic_int_dec_and_test (&dir->references);
+  if (is_zero)
+    {
+      CachedDir *parent;
+
+      parent = dir->parent;
+
+      if (parent != NULL)
+        cached_dir_remove_subdir (parent, dir->name);
+
+      if (dir->notify)
+        dir->notify (dir, dir->notify_data);
+
+      cached_dir_free (dir);
+    }
+}
+
 static inline CachedDir *
 find_subdir (CachedDir  *dir,
              const char *subdir)
@@ -164,8 +224,13 @@ find_entry (CachedDir  *dir,
   tmp = dir->entries;
   while (tmp != NULL)
     {
-      if (strcmp (desktop_entry_get_basename (tmp->data), basename) == 0)
-        return tmp->data;
+      const char *entry_basename;
+
+      entry_basename = desktop_entry_get_basename (tmp->data);
+      if (strcmp (entry_basename, basename) == 0)
+        {
+          return tmp->data;
+        }
 
       tmp = tmp->next;
     }
@@ -213,7 +278,9 @@ cached_dir_lookup (const char *canonical)
   int         i;
 
   if (dir_cache == NULL)
-    dir_cache = cached_dir_new ("/");
+    dir_cache = cached_dir_new_full ("/",
+                                     (GFunc) clear_cache,
+                                     &dir_cache);
   dir = dir_cache;
 
   g_assert (canonical != NULL && canonical[0] == G_DIR_SEPARATOR);
@@ -227,12 +294,7 @@ cached_dir_lookup (const char *canonical)
     {
       CachedDir *subdir;
 
-      if ((subdir = find_subdir (dir, split[i])) == NULL)
-        {
-          subdir = cached_dir_new (split[i]);
-          dir->subdirs = g_slist_prepend (dir->subdirs, subdir);
-          subdir->parent = dir;
-        }
+      subdir = cached_dir_add_subdir (dir, split[i], NULL);
 
       dir = subdir;
 
@@ -272,7 +334,9 @@ cached_dir_update_entry (CachedDir  *dir,
   tmp = dir->entries;
   while (tmp != NULL)
     {
-      if (strcmp (desktop_entry_get_basename (tmp->data), basename) == 0)
+      const char *entry_basename;
+      entry_basename = desktop_entry_get_basename (tmp->data);
+      if (strcmp (entry_basename, basename) == 0)
         {
           if (!desktop_entry_reload (tmp->data))
 	    {
@@ -297,7 +361,10 @@ cached_dir_remove_entry (CachedDir  *dir,
   tmp = dir->entries;
   while (tmp != NULL)
     {
-      if (strcmp (desktop_entry_get_basename (tmp->data), basename) == 0)
+      const char *entry_basename;
+      entry_basename = desktop_entry_get_basename (tmp->data);
+
+      if (strcmp (entry_basename, basename) == 0)
         {
           desktop_entry_unref (tmp->data);
           dir->entries = g_slist_delete_link (dir->entries, tmp);
@@ -310,7 +377,7 @@ cached_dir_remove_entry (CachedDir  *dir,
   return FALSE;
 }
 
-static gboolean
+static CachedDir *
 cached_dir_add_subdir (CachedDir  *dir,
                        const char *basename,
                        const char *path)
@@ -322,23 +389,23 @@ cached_dir_add_subdir (CachedDir  *dir,
   if (subdir != NULL)
     {
       subdir->deleted = FALSE;
-      return TRUE;
+      return subdir;
     }
 
   subdir = cached_dir_new (basename);
 
-  if (!cached_dir_load_entries_recursive (subdir, path))
+  if (path != NULL && !cached_dir_load_entries_recursive (subdir, path))
     {
       cached_dir_free (subdir);
-      return FALSE;
+      return NULL;
     }
 
   menu_verbose ("Caching dir \"%s\"\n", basename);
 
   subdir->parent = dir;
-  dir->subdirs = g_slist_prepend (dir->subdirs, subdir);
+  dir->subdirs = g_slist_prepend (dir->subdirs, cached_dir_ref (subdir));
 
-  return TRUE;
+  return subdir;
 }
 
 static gboolean
@@ -355,7 +422,7 @@ cached_dir_remove_subdir (CachedDir  *dir,
 
       if (subdir->references == 0)
         {
-          cached_dir_free (subdir);
+          cached_dir_unref (subdir);
           dir->subdirs = g_slist_remove (dir->subdirs, subdir);
         }
 
@@ -496,7 +563,7 @@ handle_cached_dir_changed (MenuMonitor      *monitor,
       switch (event)
         {
         case MENU_MONITOR_EVENT_CREATED:
-          handled = cached_dir_add_subdir (dir, basename, path);
+          handled = cached_dir_add_subdir (dir, basename, path) != NULL;
           break;
 
         case MENU_MONITOR_EVENT_CHANGED:
@@ -668,7 +735,7 @@ cached_dir_remove_monitor (CachedDir                 *dir,
 static void
 cached_dir_add_reference (CachedDir *dir)
 {
-  dir->references++;
+  cached_dir_ref (dir);
 
   if (dir->parent != NULL)
     {
@@ -683,29 +750,7 @@ cached_dir_remove_reference (CachedDir *dir)
 
   parent = dir->parent;
 
-  if (--dir->references == 0 && dir->deleted)
-    {
-      if (dir->parent != NULL)
-	{
-	  GSList *tmp;
-
-	  tmp = parent->subdirs;
-	  while (tmp != NULL)
-	    {
-	      CachedDir *subdir = tmp->data;
-
-	      if (!strcmp (subdir->name, dir->name))
-		{
-		  parent->subdirs = g_slist_delete_link (parent->subdirs, tmp);
-		  break;
-		}
-
-	      tmp = tmp->next;
-	    }
-	}
-
-      cached_dir_free (dir);
-    }
+  cached_dir_unref (dir);
 
   if (parent != NULL)
     {
@@ -777,7 +822,7 @@ entry_directory_ref (EntryDirectory *ed)
   g_return_val_if_fail (ed != NULL, NULL);
   g_return_val_if_fail (ed->refcount > 0, NULL);
 
-  ed->refcount++;
+  g_atomic_int_inc (&ed->refcount);
 
   return ed;
 }
@@ -785,10 +830,13 @@ entry_directory_ref (EntryDirectory *ed)
 void
 entry_directory_unref (EntryDirectory *ed)
 {
+  gboolean is_zero;
+
   g_return_if_fail (ed != NULL);
   g_return_if_fail (ed->refcount > 0);
 
-  if (--ed->refcount == 0)
+  is_zero = g_atomic_int_dec_and_test (&ed->refcount);
+  if (is_zero)
     {
       cached_dir_remove_reference (ed->dir);
 
@@ -904,11 +952,12 @@ entry_directory_foreach_recursive (EntryDirectory            *ed,
 
       if (desktop_entry_get_type (entry) == ed->entry_type)
         {
-          gboolean  ret;
-          char     *file_id;
+          gboolean    ret;
+          char       *file_id;
+          const char *basename;
 
-          g_string_append (relative_path,
-                           desktop_entry_get_basename (entry));
+          basename = desktop_entry_get_basename (entry);
+          g_string_append (relative_path, basename);
 
 	  file_id = get_desktop_file_id_from_path (ed,
 						   ed->entry_type,
@@ -988,7 +1037,7 @@ entry_directory_get_flat_contents (EntryDirectory   *ed,
       DesktopEntry *entry = tmp->data;
       const char   *basename;
 
-      basename = desktop_entry_get_basename (entry);
+      basename = desktop_entry_get_path (entry);
 
       if (desktop_entries &&
           desktop_entry_get_type (entry) == DESKTOP_ENTRY_DESKTOP)
@@ -1061,7 +1110,7 @@ entry_directory_list_ref (EntryDirectoryList *list)
   g_return_val_if_fail (list != NULL, NULL);
   g_return_val_if_fail (list->refcount > 0, NULL);
 
-  list->refcount += 1;
+  g_atomic_int_inc (&list->refcount);
 
   return list;
 }
@@ -1069,11 +1118,13 @@ entry_directory_list_ref (EntryDirectoryList *list)
 void
 entry_directory_list_unref (EntryDirectoryList *list)
 {
+  gboolean is_zero;
+
   g_return_if_fail (list != NULL);
   g_return_if_fail (list->refcount > 0);
 
-  list->refcount -= 1;
-  if (list->refcount == 0)
+  is_zero = g_atomic_int_dec_and_test (&list->refcount);
+  if (is_zero)
     {
       g_list_foreach (list->dirs, (GFunc) entry_directory_unref, NULL);
       g_list_free (list->dirs);
diff --git a/libmenu/gmenu-tree.c b/libmenu/gmenu-tree.c
index 8e01af4..4b760c5 100644
--- a/libmenu/gmenu-tree.c
+++ b/libmenu/gmenu-tree.c
@@ -163,7 +163,7 @@ static void      gmenu_tree_resolve_files        (GMenuTree       *tree,
 						  MenuLayoutNode  *layout);
 static void      gmenu_tree_force_recanonicalize (GMenuTree       *tree);
 static void      gmenu_tree_invoke_monitors      (GMenuTree       *tree);
-     
+
 static void gmenu_tree_item_unref_and_unset_parent (gpointer itemp);
 
 typedef enum
@@ -1242,8 +1242,12 @@ gmenu_tree_directory_make_path (GMenuTreeDirectory *directory,
   append_directory_path (directory, path);
 
   if (entry != NULL)
-    g_string_append (path,
-		     desktop_entry_get_basename (entry->desktop_entry));
+    {
+      const char *basename;
+
+      basename = desktop_entry_get_basename (entry->desktop_entry);
+      g_string_append (path, basename);
+    }
 
   return g_string_free (path, FALSE);
 }
@@ -1273,7 +1277,7 @@ gmenu_tree_entry_get_desktop_file_path (GMenuTreeEntry *entry)
 const char *
 gmenu_tree_entry_get_desktop_file_id (GMenuTreeEntry *entry)
 {
-  g_return_val_if_fail (entry != NULL, NULL);
+  g_return_val_if_fail (entry != NULL, FALSE);
 
   return entry->desktop_file_id;
 }
@@ -1485,7 +1489,7 @@ gmenu_tree_directory_finalize (GMenuTreeDirectory *directory)
 		   NULL);
   g_slist_free (directory->contents);
   directory->contents = NULL;
-  
+
   g_slist_foreach (directory->default_layout_info,
 		   (GFunc) menu_layout_node_unref,
 		   NULL);



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