From 32a3eff7f5191d76a82be7db1190a98ed9ef55d0 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 28 Aug 2010 16:22:14 +0200 Subject: [PATCH] read-file: Don't occupy too much unused memory. --- ChangeLog | 5 +++++ lib/read-file.c | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/ChangeLog b/ChangeLog index 8a32339035..d4eaed748a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2010-08-28 Bruno Haible + + 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 Eric Blake Bruno Haible diff --git a/lib/read-file.c b/lib/read-file.c index 27241b4504..0a15c5a457 100644 --- a/lib/read-file.c +++ b/lib/read-file.c @@ -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; -- 2.30.2