From: Dmitry V. Levin Date: Sun, 2 Jul 2017 00:11:31 +0000 (+0000) Subject: Introduce macros.h X-Git-Tag: v4.18~11 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=38198609dfc6132dceef6b5cd5ee6231dcc4ce2f;p=strace Introduce macros.h Introduce a separate header file for generic macros shared between strace and its tests. Start this unification with ARRAY_SIZE macro. * defs.h: Include "macros.h". (ARRAY_SIZE): Move ... * macros.h: ... to new file. * Makefile.am (strace_SOURCES): Add it. * tests/tests.h: Include "macros.h". (ARRAY_SIZE): Remove. All callers updated. --- diff --git a/Makefile.am b/Makefile.am index 7c0658d5..cb650215 100644 --- a/Makefile.am +++ b/Makefile.am @@ -159,6 +159,7 @@ strace_SOURCES = \ lookup_dcookie.c \ loop.c \ lseek.c \ + macros.h \ mem.c \ membarrier.c \ memfd_create.c \ diff --git a/defs.h b/defs.h index dcb4d947..b3a315f3 100644 --- a/defs.h +++ b/defs.h @@ -54,8 +54,9 @@ #include #include "kernel_types.h" -#include "mpers_type.h" #include "gcc_compat.h" +#include "macros.h" +#include "mpers_type.h" #include "sysent.h" #ifndef HAVE_STRERROR @@ -75,8 +76,6 @@ extern char *stpcpy(char *dst, const char *src); (offsetof(type, member) + sizeof(((type *)NULL)->member)) #endif -#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + MUST_BE_ARRAY(a)) - /* macros */ #ifndef MAX # define MAX(a, b) (((a) > (b)) ? (a) : (b)) diff --git a/macros.h b/macros.h new file mode 100644 index 00000000..218b0572 --- /dev/null +++ b/macros.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2001-2017 The strace developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef STRACE_MACROS_H +#define STRACE_MACROS_H + +#include "gcc_compat.h" + +#define ARRAY_SIZE(a_) (sizeof(a_) / sizeof((a_)[0]) + MUST_BE_ARRAY(a_)) + +#endif /* !STRACE_MACROS_H */ diff --git a/tests/mmsg.c b/tests/mmsg.c index 88805d16..a5cf0e79 100644 --- a/tests/mmsg.c +++ b/tests/mmsg.c @@ -113,14 +113,16 @@ main(void) " | 00000 %-49s %-16s |\n", w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c), - ARRAY_SIZE(w0_iov_), + (unsigned int) ARRAY_SIZE(w0_iov_), LENGTH_OF(w0_c) + LENGTH_OF(w1_c), - w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(w1_iov_), + w2_c, LENGTH_OF(w2_c), (unsigned int) ARRAY_SIZE(w1_iov_), LENGTH_OF(w2_c), n_w_mmh, r, - ARRAY_SIZE(w0_iov_), LENGTH_OF(w0_c), w0_d, w0_c, + (unsigned int) ARRAY_SIZE(w0_iov_), + LENGTH_OF(w0_c), w0_d, w0_c, LENGTH_OF(w1_c), w1_d, w1_c, - ARRAY_SIZE(w1_iov_), LENGTH_OF(w2_c), w2_d, w2_c); + (unsigned int) ARRAY_SIZE(w1_iov_), + LENGTH_OF(w2_c), w2_d, w2_c); const unsigned int w_len = LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c); @@ -184,11 +186,14 @@ main(void) " = %u buffers in vector 1\n" " * %u bytes in buffer 0\n" " | 00000 %-49s %-16s |\n", - r0_c, r_len, ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c), - r1_c, r_len, r_len, ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), + r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_), + LENGTH_OF(r0_c), r1_c, r_len, r_len, + (unsigned int) ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), n_r_mmh, r, - ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c), r0_d, r0_c, - ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), r1_d, r1_c); + (unsigned int) ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c), + r0_d, r0_c, + (unsigned int) ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), + r1_d, r1_c); tprintf("+++ exited with 0 +++\n"); return 0; diff --git a/tests/poll.c b/tests/poll.c index 5c07297f..af66e536 100644 --- a/tests/poll.c +++ b/tests/poll.c @@ -194,7 +194,8 @@ main(int ac, char **av) tprintf("poll("); print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0), ARRAY_SIZE(pfds0), abbrev); - tprintf(", %u, %d) = %d (", ARRAY_SIZE(pfds0), timeout, rc); + tprintf(", %u, %d) = %d (", + (unsigned int) ARRAY_SIZE(pfds0), timeout, rc); print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev); tprintf(")\n"); @@ -207,7 +208,8 @@ main(int ac, char **av) tprintf("poll("); print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0), ARRAY_SIZE(pfds0), abbrev); - tprintf(", %u, %d) = %d (", ARRAY_SIZE(pfds0), timeout, rc); + tprintf(", %u, %d) = %d (", + (unsigned int) ARRAY_SIZE(pfds0), timeout, rc); print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev); tprintf(")\n"); @@ -219,7 +221,8 @@ main(int ac, char **av) tprintf("poll("); print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0), ARRAY_SIZE(pfds0), abbrev); - tprintf(", %u, %d) = %d (", ARRAY_SIZE(pfds0), timeout, rc); + tprintf(", %u, %d) = %d (", + (unsigned int) ARRAY_SIZE(pfds0), timeout, rc); print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev); tprintf(")\n"); @@ -234,7 +237,8 @@ main(int ac, char **av) tprintf("poll("); print_pollfd_array_entering(tail_fds1, ARRAY_SIZE(pfds1), ARRAY_SIZE(pfds1), abbrev); - tprintf(", %u, %d) = %d (Timeout)\n", ARRAY_SIZE(pfds1), timeout, rc); + tprintf(", %u, %d) = %d (Timeout)\n", + (unsigned int) ARRAY_SIZE(pfds1), timeout, rc); const void *const efault = tail_fds0 + ARRAY_SIZE(pfds0); rc = syscall(__NR_poll, efault, 1, 0); diff --git a/tests/preadv-pwritev.c b/tests/preadv-pwritev.c index b2d2a824..ddbd47f6 100644 --- a/tests/preadv-pwritev.c +++ b/tests/preadv-pwritev.c @@ -121,7 +121,7 @@ main(void) " * %u bytes in buffer 2\n" " | 00000 %-49s %-16s |\n", w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c), - w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(w_iov_), w_len, + w2_c, LENGTH_OF(w2_c), (unsigned int) ARRAY_SIZE(w_iov_), w_len, LENGTH_OF(w0_c), w0_d, w0_c, LENGTH_OF(w1_c), w1_d, w1_c, LENGTH_OF(w2_c), w2_d, w2_c); @@ -142,7 +142,8 @@ main(void) tprintf("preadv(0, [{iov_base=\"%s\", iov_len=%u}], %u, 0) = %u\n" " * %u bytes in buffer 0\n" " | 00000 %-49s %-16s |\n", - r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len, r_len, r0_d, r0_c); + r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_), + r_len, r_len, r0_d, r0_c); void *r1 = tail_alloc(r_len); void *r2 = tail_alloc(w_len); @@ -166,7 +167,7 @@ main(void) ", {iov_base=\"\", iov_len=%u}], %u, %u) = %u\n" " * %u bytes in buffer 0\n" " | 00000 %-49s %-16s |\n", - r1_c, r_len, w_len, ARRAY_SIZE(r1_iov_), + r1_c, r_len, w_len, (unsigned int) ARRAY_SIZE(r1_iov_), r_len, w_len - r_len, w_len - r_len, r1_d, r1_c); close(0); diff --git a/tests/preadv.c b/tests/preadv.c index a55909b6..d5ad84f6 100644 --- a/tests/preadv.c +++ b/tests/preadv.c @@ -125,7 +125,7 @@ main(void) perror_msg_and_fail("preadv: expected %u, returned %ld", r_len, rc); printf("preadv(%d, [{iov_base=\"%s\", iov_len=%u}], %u, 0) = %u\n", - fd, r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len); + fd, r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_), r_len); void *r1 = tail_alloc(r_len); void *r2 = tail_alloc(LENGTH_OF(w)); @@ -147,8 +147,9 @@ main(void) (int) LENGTH_OF(w) - r_len, rc); printf("preadv(%d, [{iov_base=\"%s\", iov_len=%u}" ", {iov_base=\"\", iov_len=%u}], %u, %u) = %u\n", - fd, r1_c, r_len, LENGTH_OF(w), ARRAY_SIZE(r1_iov_), - r_len, LENGTH_OF(w) - r_len); + fd, r1_c, r_len, LENGTH_OF(w), + (unsigned int) ARRAY_SIZE(r1_iov_), + r_len, LENGTH_OF(w) - r_len); puts("+++ exited with 0 +++"); return 0; diff --git a/tests/preadv2-pwritev2.c b/tests/preadv2-pwritev2.c index e31e0463..89f37dd6 100644 --- a/tests/preadv2-pwritev2.c +++ b/tests/preadv2-pwritev2.c @@ -127,7 +127,7 @@ dumpio(void) " * %u bytes in buffer 2\n" " | 00000 %-49s %-16s |\n", w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c), - w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(w_iov_), w_len, + w2_c, LENGTH_OF(w2_c), (unsigned int) ARRAY_SIZE(w_iov_), w_len, LENGTH_OF(w0_c), w0_d, w0_c, LENGTH_OF(w1_c), w1_d, w1_c, LENGTH_OF(w2_c), w2_d, w2_c); @@ -148,7 +148,8 @@ dumpio(void) tprintf("preadv2(0, [{iov_base=\"%s\", iov_len=%u}], %u, 0, 0) = %u\n" " * %u bytes in buffer 0\n" " | 00000 %-49s %-16s |\n", - r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len, r_len, r0_d, r0_c); + r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_), + r_len, r_len, r0_d, r0_c); void *r1 = tail_alloc(r_len); void *r2 = tail_alloc(w_len); @@ -172,7 +173,7 @@ dumpio(void) ", {iov_base=\"\", iov_len=%u}], %u, %u, 0) = %u\n" " * %u bytes in buffer 0\n" " | 00000 %-49s %-16s |\n", - r1_c, r_len, w_len, ARRAY_SIZE(r1_iov_), + r1_c, r_len, w_len, (unsigned int) ARRAY_SIZE(r1_iov_), r_len, w_len - r_len, w_len - r_len, r1_d, r1_c); close(0); diff --git a/tests/readv.c b/tests/readv.c index 0e5aab65..93f07605 100644 --- a/tests/readv.c +++ b/tests/readv.c @@ -108,7 +108,7 @@ main(void) " * %u bytes in buffer 2\n" " | 00000 %-49s %-16s |\n", fds[1], w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c), - w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(w_iov_), w_len, + w2_c, LENGTH_OF(w2_c), (unsigned int) ARRAY_SIZE(w_iov_), w_len, LENGTH_OF(w0_c), w0_d, w0_c, LENGTH_OF(w1_c), w1_d, w1_c, LENGTH_OF(w2_c), w2_d, w2_c); @@ -126,8 +126,8 @@ main(void) tprintf("readv(%d, [{iov_base=\"%s\", iov_len=%u}], %u) = %u\n" " * %u bytes in buffer 0\n" " | 00000 %-49s %-16s |\n", - fds[0], - r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len, r_len, r0_d, r0_c); + fds[0], r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_), + r_len, r_len, r0_d, r0_c); void *r1 = tail_alloc(r_len); void *r2 = tail_alloc(w_len); @@ -148,8 +148,8 @@ main(void) ", {iov_base=\"\", iov_len=%u}], %u) = %u\n" " * %u bytes in buffer 0\n" " | 00000 %-49s %-16s |\n", - fds[0], r1_c, r_len, w_len, ARRAY_SIZE(r1_iov_), w_len - r_len, - w_len - r_len, r1_d, r1_c); + fds[0], r1_c, r_len, w_len, (unsigned int) ARRAY_SIZE(r1_iov_), + w_len - r_len, w_len - r_len, r1_d, r1_c); close(fds[0]); tprintf("+++ exited with 0 +++\n"); diff --git a/tests/recvmsg.c b/tests/recvmsg.c index fa41a9d4..2d55d554 100644 --- a/tests/recvmsg.c +++ b/tests/recvmsg.c @@ -100,7 +100,7 @@ main(void) w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c), w2_c, LENGTH_OF(w2_c), - ARRAY_SIZE(w_iov_), w_len, + (unsigned int) ARRAY_SIZE(w_iov_), w_len, LENGTH_OF(w0_c), w0_d, w0_c, LENGTH_OF(w1_c), w1_d, w1_c, LENGTH_OF(w2_c), w2_d, w2_c); @@ -127,7 +127,8 @@ main(void) ", msg_controllen=0, msg_flags=0}, 0) = %u\n" " * %u bytes in buffer 0\n" " | 00000 %-49s %-16s |\n", - r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len, r_len, r0_d, r0_c); + r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_), + r_len, r_len, r0_d, r0_c); void *r1 = tail_alloc(r_len); void *r2 = tail_alloc(w_len); @@ -151,8 +152,8 @@ main(void) ", msg_iovlen=%u, msg_controllen=0, msg_flags=0}, 0) = %u\n" " * %u bytes in buffer 0\n" " | 00000 %-49s %-16s |\n", - r1_c, r_len, w_len, ARRAY_SIZE(r1_iov_), w_len - r_len, - w_len - r_len, r1_d, r1_c); + r1_c, r_len, w_len, (unsigned int) ARRAY_SIZE(r1_iov_), + w_len - r_len, w_len - r_len, r1_d, r1_c); close(0); tprintf("+++ exited with 0 +++\n"); diff --git a/tests/tests.h b/tests/tests.h index 199d21a6..00d10eae 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -36,6 +36,7 @@ # include # include "kernel_types.h" # include "gcc_compat.h" +# include "macros.h" /* * The printf-like function to use in header files @@ -198,7 +199,6 @@ f8ill_ptr_to_kulong(const void *const ptr) ? F8ILL_KULONG_MASK | uptr : (kernel_ulong_t) uptr; } -# define ARRAY_SIZE(arg) ((unsigned int) (sizeof(arg) / sizeof((arg)[0]))) # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1) /* Zero-extend a signed integer type to unsigned long long. */ diff --git a/tests/vmsplice.c b/tests/vmsplice.c index f29962e3..4242d9e6 100644 --- a/tests/vmsplice.c +++ b/tests/vmsplice.c @@ -86,7 +86,7 @@ main(void) " * %u bytes in buffer 2\n" " | 00000 %-49s %-16s |\n", w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c), - w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(iov_), + w2_c, LENGTH_OF(w2_c), (unsigned int) ARRAY_SIZE(iov_), "SPLICE_F_NONBLOCK", len, LENGTH_OF(w0_c), w0_d, w0_c, LENGTH_OF(w1_c), w1_d, w1_c, LENGTH_OF(w2_c), w2_d, w2_c);