bug-gnulib
[Top][All Lists]
Advanced

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

read-file: don't occupy too much unused memory


From: Bruno Haible
Subject: read-file: don't occupy too much unused memory
Date: Sat, 28 Aug 2010 16:22:29 +0200
User-agent: KMail/1.9.9

When read_file returns a buffer that contains a 201KB file, you don't want
that buffer to be and to stay 300KB large - that's just a waste of memory.
This fixes it.


2010-08-28  Bruno Haible  <address@hidden>

        read-file: Don't occupy too much unused memory.
        * lib/read-file.c (fread_file): Shrink the buffer at the end.

--- lib/read-file.c.orig        Sat Aug 28 16:21:31 2010
+++ lib/read-file.c     Sat Aug 28 16:17:46 2010
@@ -119,6 +119,15 @@
             save_errno = errno;
             if (ferror (stream))
               break;
+
+            /* Shrink the allocated memory if possible.  */
+            if (size + 1 < alloc)
+              {
+                char *smaller_buf = realloc (buf, size + 1);
+                if (smaller_buf != NULL)
+                  buf = smaller_buf;
+              }
+
             buf[size] = '\0';
             *length = size;
             return buf;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]