]> git.ipfire.org Git - thirdparty/rsync.git/commitdiff
Use mallinfo2, when available, and use %zd for size_t values on C99.
authorWayne Davison <wayne@opencoder.net>
Sun, 26 Dec 2021 22:23:19 +0000 (14:23 -0800)
committerWayne Davison <wayne@opencoder.net>
Sun, 26 Dec 2021 22:25:53 +0000 (14:25 -0800)
An exhanced version of pull request #265.

configure.ac
io.c
main.c
rsync.h

index 84111de8b0326ac9011fe873f7114835c487be8c..b30eeb9335941bd783123fa2d70b34e40cbd3a86 100644 (file)
@@ -829,7 +829,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd chown chmod lchmod mknod mkfifo \
     fchmod fstat ftruncate strchr readlink link utime utimes lutimes strftime \
     chflags getattrlist mktime innetgr linkat \
     memmove lchown vsnprintf snprintf vasprintf asprintf setsid strpbrk \
-    strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
+    strlcat strlcpy strtol mallinfo mallinfo2 getgroups setgroups geteuid getegid \
     setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
     seteuid strerror putenv iconv_open locale_charset nl_langinfo getxattr \
     extattr_get_link sigaction sigprocmask setattrlist getgrouplist \
diff --git a/io.c b/io.c
index cc70ce88702c8505c8f1f92543fc597ce33d8771..abb0f7ab1e0b2da6589778affb82f3dc8a02e866 100644 (file)
--- a/io.c
+++ b/io.c
@@ -264,15 +264,18 @@ static size_t safe_read(int fd, char *buf, size_t len)
                        rprintf(FINFO, "select exception on fd %d\n", fd); */
 
                if (FD_ISSET(fd, &r_fds)) {
-                       int n = read(fd, buf + got, len - got);
-                       if (DEBUG_GTE(IO, 2))
-                               rprintf(FINFO, "[%s] safe_read(%d)=%ld\n", who_am_i(), fd, (long)n);
+                       ssize_t n = read(fd, buf + got, len - got);
+                       if (DEBUG_GTE(IO, 2)) {
+                               rprintf(FINFO, "[%s] safe_read(%d)=%" SIZE_T_FMT_MOD "d\n",
+                                       who_am_i(), fd, (SIZE_T_FMT_CAST)n);
+                       }
                        if (n == 0)
                                break;
                        if (n < 0) {
                                if (errno == EINTR)
                                        continue;
-                               rsyserr(FERROR, errno, "safe_read failed to read %ld bytes", (long)len);
+                               rsyserr(FERROR, errno, "safe_read failed to read %" SIZE_T_FMT_MOD "d bytes",
+                                       (SIZE_T_FMT_CAST)len);
                                exit_cleanup(RERR_STREAMIO);
                        }
                        if ((got += (size_t)n) == len)
@@ -304,7 +307,7 @@ static const char *what_fd_is(int fd)
  * is not used on the socket except very early in the transfer. */
 static void safe_write(int fd, const char *buf, size_t len)
 {
-       int n;
+       ssize_t n;
 
        assert(fd != iobuf.out_fd);
 
@@ -315,8 +318,8 @@ static void safe_write(int fd, const char *buf, size_t len)
                if (errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN) {
                  write_failed:
                        rsyserr(FERROR, errno,
-                               "safe_write failed to write %ld bytes to %s",
-                               (long)len, what_fd_is(fd));
+                               "safe_write failed to write %" SIZE_T_FMT_MOD "d bytes to %s",
+                               (SIZE_T_FMT_CAST)len, what_fd_is(fd));
                        exit_cleanup(RERR_STREAMIO);
                }
        } else {
@@ -362,7 +365,7 @@ static void safe_write(int fd, const char *buf, size_t len)
  * a chunk of data and put it into the output buffer. */
 static void forward_filesfrom_data(void)
 {
-       int len;
+       ssize_t len;
 
        len = read(ff_forward_fd, ff_xb.buf + ff_xb.len, ff_xb.size - ff_xb.len);
        if (len <= 0) {
@@ -377,8 +380,10 @@ static void forward_filesfrom_data(void)
                return;
        }
 
-       if (DEBUG_GTE(IO, 2))
-               rprintf(FINFO, "[%s] files-from read=%ld\n", who_am_i(), (long)len);
+       if (DEBUG_GTE(IO, 2)) {
+               rprintf(FINFO, "[%s] files-from read=%" SIZE_T_FMT_MOD "d\n",
+                       who_am_i(), (SIZE_T_FMT_CAST)len);
+       }
 
 #ifdef ICONV_OPTION
        len += ff_xb.len;
@@ -562,52 +567,59 @@ static char *perform_io(size_t needed, int flags)
        case PIO_NEED_INPUT:
                /* We never resize the circular input buffer. */
                if (iobuf.in.size < needed) {
-                       rprintf(FERROR, "need to read %ld bytes, iobuf.in.buf is only %ld bytes.\n",
-                               (long)needed, (long)iobuf.in.size);
+                       rprintf(FERROR, "need to read %" SIZE_T_FMT_MOD "d bytes,"
+                                       " iobuf.in.buf is only %" SIZE_T_FMT_MOD "d bytes.\n",
+                               (SIZE_T_FMT_CAST)needed, (SIZE_T_FMT_CAST)iobuf.in.size);
                        exit_cleanup(RERR_PROTOCOL);
                }
 
                if (msgs2stderr == 1 && DEBUG_GTE(IO, 3)) {
-                       rprintf(FINFO, "[%s] perform_io(%ld, %sinput)\n",
-                               who_am_i(), (long)needed, flags & PIO_CONSUME_INPUT ? "consume&" : "");
+                       rprintf(FINFO, "[%s] perform_io(%" SIZE_T_FMT_MOD "d, %sinput)\n",
+                               who_am_i(), (SIZE_T_FMT_CAST)needed, flags & PIO_CONSUME_INPUT ? "consume&" : "");
                }
                break;
 
        case PIO_NEED_OUTROOM:
                /* We never resize the circular output buffer. */
                if (iobuf.out.size - iobuf.out_empty_len < needed) {
-                       fprintf(stderr, "need to write %ld bytes, iobuf.out.buf is only %ld bytes.\n",
-                               (long)needed, (long)(iobuf.out.size - iobuf.out_empty_len));
+                       fprintf(stderr, "need to write %" SIZE_T_FMT_MOD "d bytes,"
+                                       " iobuf.out.buf is only %" SIZE_T_FMT_MOD "d bytes.\n",
+                               (SIZE_T_FMT_CAST)needed, (SIZE_T_FMT_CAST)(iobuf.out.size - iobuf.out_empty_len));
                        exit_cleanup(RERR_PROTOCOL);
                }
 
                if (msgs2stderr == 1 && DEBUG_GTE(IO, 3)) {
-                       rprintf(FINFO, "[%s] perform_io(%ld, outroom) needs to flush %ld\n",
-                               who_am_i(), (long)needed,
+                       rprintf(FINFO, "[%s] perform_io(%" SIZE_T_FMT_MOD "d,"
+                                      " outroom) needs to flush %" SIZE_T_FMT_MOD "d\n",
+                               who_am_i(), (SIZE_T_FMT_CAST)needed,
                                iobuf.out.len + needed > iobuf.out.size
-                               ? (long)(iobuf.out.len + needed - iobuf.out.size) : 0L);
+                               ? (SIZE_T_FMT_CAST)(iobuf.out.len + needed - iobuf.out.size) : (SIZE_T_FMT_CAST)0);
                }
                break;
 
        case PIO_NEED_MSGROOM:
                /* We never resize the circular message buffer. */
                if (iobuf.msg.size < needed) {
-                       fprintf(stderr, "need to write %ld bytes, iobuf.msg.buf is only %ld bytes.\n",
-                               (long)needed, (long)iobuf.msg.size);
+                       fprintf(stderr, "need to write %" SIZE_T_FMT_MOD "d bytes,"
+                                       " iobuf.msg.buf is only %" SIZE_T_FMT_MOD "d bytes.\n",
+                               (SIZE_T_FMT_CAST)needed, (SIZE_T_FMT_CAST)iobuf.msg.size);
                        exit_cleanup(RERR_PROTOCOL);
                }
 
                if (msgs2stderr == 1 && DEBUG_GTE(IO, 3)) {
-                       rprintf(FINFO, "[%s] perform_io(%ld, msgroom) needs to flush %ld\n",
-                               who_am_i(), (long)needed,
+                       rprintf(FINFO, "[%s] perform_io(%" SIZE_T_FMT_MOD "d,"
+                                      " msgroom) needs to flush %" SIZE_T_FMT_MOD "d\n",
+                               who_am_i(), (SIZE_T_FMT_CAST)needed,
                                iobuf.msg.len + needed > iobuf.msg.size
-                               ? (long)(iobuf.msg.len + needed - iobuf.msg.size) : 0L);
+                               ? (SIZE_T_FMT_CAST)(iobuf.msg.len + needed - iobuf.msg.size) : (SIZE_T_FMT_CAST)0);
                }
                break;
 
        case 0:
-               if (msgs2stderr == 1 && DEBUG_GTE(IO, 3))
-                       rprintf(FINFO, "[%s] perform_io(%ld, %d)\n", who_am_i(), (long)needed, flags);
+               if (msgs2stderr == 1 && DEBUG_GTE(IO, 3)) {
+                       rprintf(FINFO, "[%s] perform_io(%" SIZE_T_FMT_MOD "d, %d)\n",
+                               who_am_i(), (SIZE_T_FMT_CAST)needed, flags);
+               }
                break;
 
        default:
@@ -665,8 +677,8 @@ static char *perform_io(size_t needed, int flags)
                                              ((MPLEX_BASE + (int)MSG_DATA)<<24) + iobuf.out.len - 4);
 
                                        if (msgs2stderr == 1 && DEBUG_GTE(IO, 1)) {
-                                               rprintf(FINFO, "[%s] send_msg(%d, %ld)\n",
-                                                       who_am_i(), (int)MSG_DATA, (long)iobuf.out.len - 4);
+                                               rprintf(FINFO, "[%s] send_msg(%d, %" SIZE_T_FMT_MOD "d)\n",
+                                                       who_am_i(), (int)MSG_DATA, (SIZE_T_FMT_CAST)iobuf.out.len - 4);
                                        }
 
                                        /* reserve room for the next MSG_DATA header */
@@ -757,7 +769,7 @@ static char *perform_io(size_t needed, int flags)
 
                if (iobuf.in_fd >= 0 && FD_ISSET(iobuf.in_fd, &r_fds)) {
                        size_t len, pos = iobuf.in.pos + iobuf.in.len;
-                       int n;
+                       ssize_t n;
                        if (pos >= iobuf.in.size) {
                                pos -= iobuf.in.size;
                                len = iobuf.in.size - iobuf.in.len;
@@ -784,8 +796,10 @@ static char *perform_io(size_t needed, int flags)
                                        exit_cleanup(RERR_SOCKETIO);
                                }
                        }
-                       if (msgs2stderr == 1 && DEBUG_GTE(IO, 2))
-                               rprintf(FINFO, "[%s] recv=%ld\n", who_am_i(), (long)n);
+                       if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) {
+                               rprintf(FINFO, "[%s] recv=%" SIZE_T_FMT_MOD "d\n",
+                                       who_am_i(), (SIZE_T_FMT_CAST)n);
+                       }
 
                        if (io_timeout || stop_at_utime) {
                                last_io_in = time(NULL);
@@ -803,7 +817,7 @@ static char *perform_io(size_t needed, int flags)
 
                if (out && FD_ISSET(iobuf.out_fd, &w_fds)) {
                        size_t len = iobuf.raw_flushing_ends_before ? iobuf.raw_flushing_ends_before - out->pos : out->len;
-                       int n;
+                       ssize_t n;
 
                        if (bwlimit_writemax && len > bwlimit_writemax)
                                len = bwlimit_writemax;
@@ -824,8 +838,8 @@ static char *perform_io(size_t needed, int flags)
                                }
                        }
                        if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) {
-                               rprintf(FINFO, "[%s] %s sent=%ld\n",
-                                       who_am_i(), out == &iobuf.out ? "out" : "msg", (long)n);
+                               rprintf(FINFO, "[%s] %s sent=%" SIZE_T_FMT_MOD "d\n",
+                                       who_am_i(), out == &iobuf.out ? "out" : "msg", (SIZE_T_FMT_CAST)n);
                        }
 
                        if (io_timeout)
@@ -945,8 +959,10 @@ int send_msg(enum msgcode code, const char *buf, size_t len, int convert)
        if (!OUT_MULTIPLEXED)
                return 0;
 
-       if (want_debug)
-               rprintf(FINFO, "[%s] send_msg(%d, %ld)\n", who_am_i(), (int)code, (long)len);
+       if (want_debug) {
+               rprintf(FINFO, "[%s] send_msg(%d, %" SIZE_T_FMT_MOD "d)\n",
+                       who_am_i(), (int)code, (SIZE_T_FMT_CAST)len);
+       }
 
        /* When checking for enough free space for this message, we need to
         * make sure that there is space for the 4-byte header, plus we'll
@@ -1021,8 +1037,10 @@ int send_msg(enum msgcode code, const char *buf, size_t len, int convert)
 
        SIVAL(hdr, 0, ((MPLEX_BASE + (int)code)<<24) + len);
 
-       if (want_debug && convert > 0)
-               rprintf(FINFO, "[%s] converted msg len=%ld\n", who_am_i(), (long)len);
+       if (want_debug && convert > 0) {
+               rprintf(FINFO, "[%s] converted msg len=%" SIZE_T_FMT_MOD "d\n",
+                       who_am_i(), (SIZE_T_FMT_CAST)len);
+       }
 
        return 1;
 }
@@ -1436,8 +1454,10 @@ static void read_a_msg(void)
        msg_bytes = tag & 0xFFFFFF;
        tag = (tag >> 24) - MPLEX_BASE;
 
-       if (msgs2stderr == 1 && DEBUG_GTE(IO, 1))
-               rprintf(FINFO, "[%s] got msg=%d, len=%ld\n", who_am_i(), (int)tag, (long)msg_bytes);
+       if (msgs2stderr == 1 && DEBUG_GTE(IO, 1)) {
+               rprintf(FINFO, "[%s] got msg=%d, len=%" SIZE_T_FMT_MOD "d\n",
+                       who_am_i(), (int)tag, (SIZE_T_FMT_CAST)msg_bytes);
+       }
 
        switch (tag) {
        case MSG_DATA:
@@ -1613,8 +1633,10 @@ static void read_a_msg(void)
                else
                        goto invalid_msg;
                iobuf.in_multiplexed = 1;
-               if (DEBUG_GTE(EXIT, 3))
-                       rprintf(FINFO, "[%s] got MSG_ERROR_EXIT with %ld bytes\n", who_am_i(), (long)msg_bytes);
+               if (DEBUG_GTE(EXIT, 3)) {
+                       rprintf(FINFO, "[%s] got MSG_ERROR_EXIT with %" SIZE_T_FMT_MOD "d bytes\n",
+                                       who_am_i(), (SIZE_T_FMT_CAST)msg_bytes);
+               }
                if (msg_bytes == 0) {
                        if (!am_sender && !am_generator) {
                                if (DEBUG_GTE(EXIT, 3)) {
diff --git a/main.c b/main.c
index 013a05553c9f77a1b39b0140fbede56707452f92..8b145867d10a136ba8512f540469f04d2206b474 100644 (file)
--- a/main.c
+++ b/main.c
@@ -467,38 +467,33 @@ static void output_summary(void)
  **/
 static void show_malloc_stats(void)
 {
-#ifdef HAVE_MALLINFO
-       struct mallinfo mi;
-
-       mi = mallinfo();
+#ifdef MEM_ALLOC_INFO
+       struct MEM_ALLOC_INFO mi = MEM_ALLOC_INFO(); /* mallinfo or mallinfo2 */
 
        rprintf(FCLIENT, "\n");
        rprintf(FINFO, RSYNC_NAME "[%d] (%s%s%s) heap statistics:\n",
                (int)getpid(), am_server ? "server " : "",
                am_daemon ? "daemon " : "", who_am_i());
-       rprintf(FINFO, "  arena:     %10ld   (bytes from sbrk)\n",
-               (long)mi.arena);
-       rprintf(FINFO, "  ordblks:   %10ld   (chunks not in use)\n",
-               (long)mi.ordblks);
-       rprintf(FINFO, "  smblks:    %10ld\n",
-               (long)mi.smblks);
-       rprintf(FINFO, "  hblks:     %10ld   (chunks from mmap)\n",
-               (long)mi.hblks);
-       rprintf(FINFO, "  hblkhd:    %10ld   (bytes from mmap)\n",
-               (long)mi.hblkhd);
-       rprintf(FINFO, "  allmem:    %10ld   (bytes from sbrk + mmap)\n",
-               (long)mi.arena + mi.hblkhd);
-       rprintf(FINFO, "  usmblks:   %10ld\n",
-               (long)mi.usmblks);
-       rprintf(FINFO, "  fsmblks:   %10ld\n",
-               (long)mi.fsmblks);
-       rprintf(FINFO, "  uordblks:  %10ld   (bytes used)\n",
-               (long)mi.uordblks);
-       rprintf(FINFO, "  fordblks:  %10ld   (bytes free)\n",
-               (long)mi.fordblks);
-       rprintf(FINFO, "  keepcost:  %10ld   (bytes in releasable chunk)\n",
-               (long)mi.keepcost);
-#endif /* HAVE_MALLINFO */
+
+#define PRINT_ALLOC_NUM(title, descr, num) \
+       rprintf(FINFO, "  %-11s%10" SIZE_T_FMT_MOD "d   (" descr ")\n", \
+              title ":", (SIZE_T_FMT_CAST)(num));
+
+       PRINT_ALLOC_NUM("arena", "bytes from sbrk", mi.arena);
+       PRINT_ALLOC_NUM("ordblks", "chunks not in use", mi.ordblks);
+       PRINT_ALLOC_NUM("smblks", "free fastbin blocks", mi.smblks);
+       PRINT_ALLOC_NUM("hblks", "chunks from mmap", mi.hblks);
+       PRINT_ALLOC_NUM("hblkhd", "bytes from mmap", mi.hblkhd);
+       PRINT_ALLOC_NUM("allmem", "bytes from sbrk + mmap", mi.arena + mi.hblkhd);
+       PRINT_ALLOC_NUM("usmblks", "always 0", mi.usmblks);
+       PRINT_ALLOC_NUM("fsmblks", "bytes in freed fastbin blocks", mi.fsmblks);
+       PRINT_ALLOC_NUM("uordblks", "bytes used", mi.uordblks);
+       PRINT_ALLOC_NUM("fordblks", "bytes free", mi.fordblks);
+       PRINT_ALLOC_NUM("keepcost", "bytes in releasable chunk", mi.keepcost);
+
+#undef PRINT_ALLOC_NUM
+
+#endif /* MEM_ALLOC_INFO */
 }
 
 
diff --git a/rsync.h b/rsync.h
index 86105afe34865da5de7bfe26355a990e5baa0002..ea1bfea6a84338116257ac142b5486f2c00a343e 100644 (file)
--- a/rsync.h
+++ b/rsync.h
@@ -780,6 +780,11 @@ struct ht_int64_node {
 
 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
 #define USE_FLEXIBLE_ARRAY 1
+#define SIZE_T_FMT_MOD "z" /* printf supports %zd */
+#define SIZE_T_FMT_CAST size_t
+#else
+#define SIZE_T_FMT_MOD "l" /* printf supports %ld */
+#define SIZE_T_FMT_CAST long
 #endif
 
 union file_extras {
@@ -1473,3 +1478,9 @@ const char *get_panic_action(void);
     exit_cleanup(RERR_UNSUPPORTED); \
 } while (0)
 #endif  /* AVX2_ASM */
+
+#ifdef HAVE_MALLINFO2
+#define MEM_ALLOC_INFO mallinfo2
+#elif defined HAVE_MALLINFO
+#define MEM_ALLOC_INFO mallinfo
+#endif