]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
df: reduce memory usage when filtering mount entries
authorAnton Ovchinnikov <revolver112@gmail.com>
Thu, 11 Jul 2013 12:44:24 +0000 (13:44 +0100)
committerPádraig Brady <P@draigBrady.com>
Thu, 11 Jul 2013 12:44:24 +0000 (13:44 +0100)
Avoid Valgrind reports of "definitely lost" items
and while at it, free all discarded mount entries
to minimize the amount of memory used.

* src/df.c (filter_mount_list): Use the newly exported
free_mount_entry() from gnulib to free all mount entries
as they're discarded.

src/df.c

index bb11bb44556534e0b41c7e7d20e0e59888363dbb..e01806490aafed225706196d0fff8e91e94807bc 100644 (file)
--- a/src/df.c
+++ b/src/df.c
@@ -613,10 +613,11 @@ filter_mount_list (void)
   struct devlist *devlist_head = NULL;
 
   /* Sort all 'wanted' entries into the list devlist_head.  */
-  for (me = mount_list; me; me = me->me_next)
+  for (me = mount_list; me;)
     {
       struct stat buf;
       struct devlist *devlist;
+      struct mount_entry *discard_me = NULL;
 
       if (-1 == stat (me->me_mountdir, &buf))
         {
@@ -635,25 +636,36 @@ filter_mount_list (void)
 
               if (devlist)
                 {
+                  discard_me = me;
+
                   /* Let the shorter mountdir win.  */
                   if (! strchr (devlist->me->me_devname, '/')
                       || (strlen (devlist->me->me_mountdir)
                          > strlen (me->me_mountdir)))
                     {
-                       /* FIXME: free ME - the others are also not free()d.  */
+                      discard_me = devlist->me;
                       devlist->me = me;
                     }
-                  continue; /* ... with the loop over the mount_list.  */
                 }
             }
         }
 
-      /* Add the device number to the global list devlist.  */
-      devlist = xmalloc (sizeof *devlist);
-      devlist->me = me;
-      devlist->dev_num = buf.st_dev;
-      devlist->next = devlist_head;
-      devlist_head = devlist;
+      if (discard_me)
+        {
+           me = me->me_next;
+           free_mount_entry (discard_me);
+        }
+      else
+        {
+          /* Add the device number to the global list devlist.  */
+          devlist = xmalloc (sizeof *devlist);
+          devlist->me = me;
+          devlist->dev_num = buf.st_dev;
+          devlist->next = devlist_head;
+          devlist_head = devlist;
+
+          me = me->me_next;
+        }
     }
 
   /* Finally rebuild the mount_list from the devlist.  */