changeset 13594:4cd1a9ea04e8

read-file: Don't occupy too much unused memory.
author Bruno Haible <bruno@clisp.org>
date Sat, 28 Aug 2010 16:22:14 +0200
parents 9134588a0ed4
children fc90387175d8
files ChangeLog lib/read-file.c
diffstat 2 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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>
--- a/lib/read-file.c
+++ b/lib/read-file.c
@@ -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;