We aren't interested in the data previousl read, hence free() followed
by malloc() is typically better since it means libc doesn't have to
restore the contained data needlessly.
struct stat st;
size_t n, size;
int n_retries;
- char *p;
assert(ret_contents);
if (size > READ_FULL_BYTES_MAX)
return -E2BIG;
- p = realloc(buf, size + 1);
- if (!p)
+ buf = malloc(size + 1);
+ if (!buf)
return -ENOMEM;
- buf = TAKE_PTR(p);
for (;;) {
ssize_t k;
if (lseek(fd, 0, SEEK_SET) < 0)
return -errno;
+
+ buf = mfree(buf);
}
if (n < size) {
+ char *p;
+
+ /* Return rest of the buffer to libc */
p = realloc(buf, n + 1);
if (!p)
return -ENOMEM;
+
buf = TAKE_PTR(p);
}