aboutsummaryrefslogtreecommitdiff
path: root/malloc/memusage.c
diff options
context:
space:
mode:
authorFrédéric Bérat <fberat@redhat.com>2023-06-01 16:27:43 +0200
committerSiddhesh Poyarekar <siddhesh@sourceware.org>2023-06-01 13:00:03 -0400
commitb97c5efdcae89d17c8ea715a5b814aab9d3cf618 (patch)
tree06968ed0830bfef46900ee809cedac886f4994f6 /malloc/memusage.c
parente76ff03108f427e8629d24599c6c1bad8b8b35dd (diff)
downloadglibc-b97c5efdcae89d17c8ea715a5b814aab9d3cf618.zip
glibc-b97c5efdcae89d17c8ea715a5b814aab9d3cf618.tar.gz
glibc-b97c5efdcae89d17c8ea715a5b814aab9d3cf618.tar.bz2
malloc/{memusage.c, memusagestat.c}: fix warn unused result
Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in glibc. Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
Diffstat (limited to 'malloc/memusage.c')
-rw-r--r--malloc/memusage.c54
1 files changed, 42 insertions, 12 deletions
diff --git a/malloc/memusage.c b/malloc/memusage.c
index 2a3a508..53a3630 100644
--- a/malloc/memusage.c
+++ b/malloc/memusage.c
@@ -18,7 +18,10 @@
#include <assert.h>
#include <dlfcn.h>
+#include <errno.h>
+#include <error.h>
#include <fcntl.h>
+#include <libintl.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
@@ -142,6 +145,27 @@ peak_atomic_max (_Atomic size_t *peak, size_t val)
while (! atomic_compare_exchange_weak (peak, &v, val));
}
+static void
+write_all (int fd, const void *buffer, size_t length)
+{
+ const char *p = buffer;
+ const char *end = p + length;
+ while (p < end)
+ {
+ ssize_t ret = write (fd, p, end - p);
+ if (ret < 0)
+ error (EXIT_FAILURE, errno,
+ gettext ("write of %zu bytes failed after %td: %m"),
+ length, p - (const char *) buffer);
+
+ if (ret == 0)
+ error (EXIT_FAILURE, 0,
+ gettext ("write returned 0 after writing %td bytes of %zu"),
+ p - (const char *) buffer, length);
+ p += ret;
+ }
+}
+
/* Update the global data after a successful function call. */
static void
update_data (struct header *result, size_t len, size_t old_len)
@@ -210,10 +234,11 @@ update_data (struct header *result, size_t len, size_t old_len)
gettime (&buffer[idx]);
/* Write out buffer if it is full. */
- if (idx + 1 == buffer_size)
- write (fd, buffer, buffer_size * sizeof (struct entry));
- else if (idx + 1 == 2 * buffer_size)
- write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry));
+ if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size)
+ {
+ uint32_t write_size = buffer_size * sizeof (buffer[0]);
+ write_all (fd, &buffer[idx + 1 - buffer_size], write_size);
+ }
}
}
@@ -299,8 +324,8 @@ me (void)
first.stack = 0;
gettime (&first);
/* Write it two times since we need the starting and end time. */
- write (fd, &first, sizeof (first));
- write (fd, &first, sizeof (first));
+ write_all (fd, &first, sizeof (first));
+ write_all (fd, &first, sizeof (first));
/* Determine the buffer size. We use the default if the
environment variable is not present. */
@@ -850,24 +875,29 @@ dest (void)
if (fd != -1)
{
/* Write the partially filled buffer. */
+ struct entry *start = buffer;
+ uint32_t write_cnt = buffer_cnt;
+
if (buffer_cnt > buffer_size)
- write (fd, buffer + buffer_size,
- (buffer_cnt - buffer_size) * sizeof (struct entry));
- else
- write (fd, buffer, buffer_cnt * sizeof (struct entry));
+ {
+ start = buffer + buffer_size;
+ write_cnt = buffer_cnt - buffer_size;
+ }
+
+ write_all (fd, start, write_cnt * sizeof (buffer[0]));
/* Go back to the beginning of the file. We allocated two records
here when we opened the file. */
lseek (fd, 0, SEEK_SET);
/* Write out a record containing the total size. */
first.stack = peak_total;
- write (fd, &first, sizeof (struct entry));
+ write_all (fd, &first, sizeof (first));
/* Write out another record containing the maximum for heap and
stack. */
first.heap = peak_heap;
first.stack = peak_stack;
gettime (&first);
- write (fd, &first, sizeof (struct entry));
+ write_all (fd, &first, sizeof (first));
/* Close the file. */
close (fd);