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  <[email protected]>

        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