read-file: Don't occupy too much unused memory.
authorBruno Haible <bruno@clisp.org>
Sat, 28 Aug 2010 14:22:14 +0000 (16:22 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 28 Aug 2010 14:22:14 +0000 (16:22 +0200)
ChangeLog
lib/read-file.c

index 8a32339..d4eaed7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2010-08-28  Bruno Haible  <bruno@clisp.org>
+
+       read-file: Don't occupy too much unused memory.
+       * lib/read-file.c (fread_file): Shrink the buffer at the end.
+
 2010-08-28  Giuseppe Scrivano  <gscrivano@gnu.org>
             Eric Blake  <eblake@redhat.com>
             Bruno Haible  <bruno@clisp.org>
index 27241b4..0a15c5a 100644 (file)
@@ -119,6 +119,15 @@ fread_file (FILE * stream, size_t * length)
             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;