[nautilus] window: Streamline RestoreTabData memory management



commit 5561219e34d0925ea752ffd87c13cd95258e3172
Author: António Fernandes <antoniof gnome org>
Date:   Wed Jul 8 14:44:38 2020 +0100

    window: Streamline RestoreTabData memory management
    
    When restoring the back and forward lists, we make a deep copy only to
    free the data immediately afterwards.
    
    Instead of reallocating the lists unnecessarily, let's just steal them.
    
    Also, use g_queue_free_full() to make free_restore_tab_data() a proper
    GDestroyNotify; also define it in window-slot.c, where it belongs.

 src/nautilus-window-slot.c | 16 ++++++++++++++--
 src/nautilus-window-slot.h |  2 ++
 src/nautilus-window.c      | 20 ++------------------
 3 files changed, 18 insertions(+), 20 deletions(-)
---
diff --git a/src/nautilus-window-slot.c b/src/nautilus-window-slot.c
index 3bf011519..342ff35e2 100644
--- a/src/nautilus-window-slot.c
+++ b/src/nautilus-window-slot.c
@@ -177,6 +177,18 @@ static void real_set_templates_menu (NautilusWindowSlot *self,
 static GMenuModel *real_get_templates_menu (NautilusWindowSlot *self);
 static void nautilus_window_slot_setup_extra_location_widgets (NautilusWindowSlot *self);
 
+void
+free_restore_tab_data (gpointer data)
+{
+    RestoreTabData *tab_data = data;
+
+    g_list_free_full (tab_data->back_list, g_object_unref);
+    g_list_free_full (tab_data->forward_list, g_object_unref);
+    nautilus_file_unref (tab_data->file);
+
+    g_free (tab_data);
+}
+
 void
 nautilus_window_slot_restore_from_data (NautilusWindowSlot *self,
                                         RestoreTabData     *data)
@@ -185,9 +197,9 @@ nautilus_window_slot_restore_from_data (NautilusWindowSlot *self,
 
     priv = nautilus_window_slot_get_instance_private (self);
 
-    priv->back_list = g_list_copy_deep (data->back_list, (GCopyFunc) g_object_ref, NULL);
+    priv->back_list = g_steal_pointer (&data->back_list);
 
-    priv->forward_list = g_list_copy_deep (data->forward_list, (GCopyFunc) g_object_ref, NULL);
+    priv->forward_list = g_steal_pointer (&data->forward_list);
 
     priv->view_mode_before_search = data->view_before_search;
 
diff --git a/src/nautilus-window-slot.h b/src/nautilus-window-slot.h
index daa2f335a..42bf092ff 100644
--- a/src/nautilus-window-slot.h
+++ b/src/nautilus-window-slot.h
@@ -132,3 +132,5 @@ NautilusQueryEditor *nautilus_window_slot_get_query_editor (NautilusWindowSlot *
 
 /* Only used by slot-dnd */
 NautilusView*  nautilus_window_slot_get_current_view       (NautilusWindowSlot *slot);
+
+void free_restore_tab_data                                 (gpointer data);
diff --git a/src/nautilus-window.c b/src/nautilus-window.c
index 1723d8a81..c7f076fee 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -84,8 +84,6 @@ static GtkWidget *nautilus_window_ensure_location_entry (NautilusWindow *window)
 static void close_slot (NautilusWindow     *window,
                         NautilusWindowSlot *slot,
                         gboolean            remove_from_notebook);
-static void free_restore_tab_data (gpointer data,
-                                   gpointer user_data);
 
 /* Sanity check: highest mouse button value I could find was 14. 5 is our
  * lower threshold (well-documented to be the one of the button events for the
@@ -1236,7 +1234,7 @@ action_restore_tab (GSimpleAction *action,
     nautilus_window_slot_open_location_full (slot, location, flags, NULL);
     nautilus_window_slot_restore_from_data (slot, data);
 
-    free_restore_tab_data (data, NULL);
+    free_restore_tab_data (data);
 }
 
 static guint
@@ -2334,19 +2332,6 @@ nautilus_window_dispose (GObject *object)
     G_OBJECT_CLASS (nautilus_window_parent_class)->dispose (object);
 }
 
-static void
-free_restore_tab_data (gpointer data,
-                       gpointer user_data)
-{
-    RestoreTabData *tab_data = data;
-
-    g_list_free_full (tab_data->back_list, g_object_unref);
-    g_list_free_full (tab_data->forward_list, g_object_unref);
-    nautilus_file_unref (tab_data->file);
-
-    g_free (tab_data);
-}
-
 static void
 nautilus_window_finalize (GObject *object)
 {
@@ -2379,8 +2364,7 @@ nautilus_window_finalize (GObject *object)
                                           G_CALLBACK (nautilus_window_on_undo_changed),
                                           window);
 
-    g_queue_foreach (window->tab_data_queue, (GFunc) free_restore_tab_data, NULL);
-    g_queue_free (window->tab_data_queue);
+    g_queue_free_full (window->tab_data_queue, free_restore_tab_data);
 
     g_object_unref (window->pad_controller);
 


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