[dpdk-dev] [PATCH 1/3] app: use RTE_DIM to calculate array size

David Marchand david.marchand at redhat.com
Thu Oct 31 10:20:55 CET 2019


On Mon, Oct 28, 2019 at 10:09 AM <pbhagavatula at marvell.com> wrote:
>
> From: Pavan Nikhilesh <pbhagavatula at marvell.com>
>
> Use RTE_DIM macro to calculate array size
>
> Suggested-by: David Marchand <david.marchand at redhat.com>
> Signed-off-by: Pavan Nikhilesh <pbhagavatula at marvell.com>

Ah, I was not expecting this soon.
Thanks for looking at it.


> ---
>  app/test-pmd/cmdline.c            |  2 +-
>  app/test-pmd/icmpecho.c           |  2 +-
>  app/test-pmd/testpmd.c            |  2 +-
>  app/test/test.c                   |  2 +-
>  app/test/test_cmdline_etheraddr.c | 10 +++-------
>  app/test/test_cmdline_ipaddr.c    | 18 ++++++------------
>  app/test/test_cmdline_num.c       | 16 +++++-----------
>  app/test/test_cmdline_portlist.c  | 12 ++++--------
>  app/test/test_cmdline_string.c    | 15 +++++----------
>  app/test/test_debug.c             |  2 +-
>  app/test/test_eal_flags.c         |  9 ++++-----
>  app/test/test_errno.c             |  4 ++--
>  app/test/test_lpm.c               |  2 +-
>  app/test/test_lpm6.c              |  2 +-
>  app/test/test_lpm6_data.h         |  3 +--
>  app/test/test_malloc.c            |  2 +-
>  app/test/test_memcpy.c            |  2 +-
>  app/test/test_memcpy_perf.c       |  4 ++--
>  app/test/test_mp_secondary.c      |  3 +--
>  app/test/test_pdump.c             |  3 +--
>  app/test/test_pmd_ring_perf.c     |  2 +-
>  app/test/test_ring_perf.c         |  6 +++---
>  app/test/test_timer_secondary.c   |  3 +--
>  23 files changed, 48 insertions(+), 78 deletions(-)
>
> diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
> index 447806991..c6b4e44a2 100644
> --- a/app/test-pmd/cmdline.c
> +++ b/app/test-pmd/cmdline.c
> @@ -5490,7 +5490,7 @@ cmd_show_bypass_config_parsed(void *parsed_result,
>                 "OS/board off",
>                 "power supply off",
>                 "timeout"};
> -       int num_events = (sizeof events) / (sizeof events[0]);
> +       int num_events = RTE_DIM(events);
>
>         /* Display the bypass mode.*/
>         if (rte_pmd_ixgbe_bypass_state_show(port_id, &bypass_mode) != 0) {
> diff --git a/app/test-pmd/icmpecho.c b/app/test-pmd/icmpecho.c
> index 2d359c943..65aece16c 100644
> --- a/app/test-pmd/icmpecho.c
> +++ b/app/test-pmd/icmpecho.c
> @@ -188,7 +188,7 @@ ip_proto_name(uint16_t ip_proto)
>                 "PIM",        /**< Protocol Independent Mcast */
>         };
>
> -       if (ip_proto < sizeof(ip_proto_names) / sizeof(ip_proto_names[0]))
> +       if (ip_proto < RTE_DIM(ip_proto_names))
>                 return ip_proto_names[ip_proto];
>         switch (ip_proto) {
>  #ifdef IPPROTO_PGM
> diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
> index 38acbc58a..1103db629 100644
> --- a/app/test-pmd/testpmd.c
> +++ b/app/test-pmd/testpmd.c
> @@ -2584,7 +2584,7 @@ struct pmd_test_command {
>         cmd_func_t cmd_func;
>  };
>
> -#define PMD_TEST_CMD_NB (sizeof(pmd_test_menu) / sizeof(pmd_test_menu[0]))
> +#define PMD_TEST_CMD_NB RTE_DIM(pmd_test_menu)

This macro is just unused and can be shot.


>
>  /* Check the link status of all ports in up to 9s, and print them finally */
>  static void
> diff --git a/app/test/test.c b/app/test/test.c
> index cd7aaf645..784535095 100644
> --- a/app/test/test.c
> +++ b/app/test/test.c
> @@ -74,7 +74,7 @@ do_recursive_call(void)
>
>         if (recursive_call == NULL)
>                 return -1;
> -       for (i = 0; i < sizeof(actions)/sizeof(actions[0]); i++) {
> +       for (i = 0; i < RTE_DIM(actions); i++) {
>                 if (strcmp(actions[i].env_var, recursive_call) == 0)
>                         return (actions[i].action_fn)();
>         }
> diff --git a/app/test/test_cmdline_etheraddr.c b/app/test/test_cmdline_etheraddr.c
> index 9a32fd7ec..dc7fa944b 100644
> --- a/app/test/test_cmdline_etheraddr.c
> +++ b/app/test/test_cmdline_etheraddr.c
> @@ -72,13 +72,9 @@ const char * ether_addr_invalid_strs[] = {
>                 " ",
>  };
>
> -#define ETHERADDR_VALID_STRS_SIZE \
> -       (sizeof(ether_addr_valid_strs) / sizeof(ether_addr_valid_strs[0]))
> -#define ETHERADDR_GARBAGE_STRS_SIZE \
> -       (sizeof(ether_addr_garbage_strs) / sizeof(ether_addr_garbage_strs[0]))
> -#define ETHERADDR_INVALID_STRS_SIZE \
> -       (sizeof(ether_addr_invalid_strs) / sizeof(ether_addr_invalid_strs[0]))
> -
> +#define ETHERADDR_VALID_STRS_SIZE RTE_DIM(ether_addr_valid_strs)
> +#define ETHERADDR_GARBAGE_STRS_SIZE RTE_DIM(ether_addr_garbage_strs)
> +#define ETHERADDR_INVALID_STRS_SIZE RTE_DIM(ether_addr_invalid_strs)

Those macros are just used once, in loops enumerating the array entries.
Let's remove them and replace inline:

        /* test full strings */
-       for (i = 0; i < ETHERADDR_VALID_STRS_SIZE; i++) {
+       for (i = 0; i < RTE_DIM(ether_addr_valid_strs); i++) {

etc...

>
>
>  static int
> diff --git a/app/test/test_cmdline_ipaddr.c b/app/test/test_cmdline_ipaddr.c
> index 2d11ce936..0c90162e8 100644
> --- a/app/test/test_cmdline_ipaddr.c
> +++ b/app/test/test_cmdline_ipaddr.c
> @@ -264,18 +264,12 @@ const char * ipaddr_invalid_strs[] = {
>                 " ",
>  };
>
> -#define IPADDR_VALID_STRS_SIZE \
> -       (sizeof(ipaddr_valid_strs) / sizeof(ipaddr_valid_strs[0]))
> -#define IPADDR_GARBAGE_ADDR4_STRS_SIZE \
> -       (sizeof(ipaddr_garbage_addr4_strs) / sizeof(ipaddr_garbage_addr4_strs[0]))
> -#define IPADDR_GARBAGE_ADDR6_STRS_SIZE \
> -       (sizeof(ipaddr_garbage_addr6_strs) / sizeof(ipaddr_garbage_addr6_strs[0]))
> -#define IPADDR_GARBAGE_NETWORK4_STRS_SIZE \
> -       (sizeof(ipaddr_garbage_network4_strs) / sizeof(ipaddr_garbage_network4_strs[0]))
> -#define IPADDR_GARBAGE_NETWORK6_STRS_SIZE \
> -       (sizeof(ipaddr_garbage_network6_strs) / sizeof(ipaddr_garbage_network6_strs[0]))
> -#define IPADDR_INVALID_STRS_SIZE \
> -       (sizeof(ipaddr_invalid_strs) / sizeof(ipaddr_invalid_strs[0]))
> +#define IPADDR_VALID_STRS_SIZE RTE_DIM(ipaddr_valid_strs)
> +#define IPADDR_GARBAGE_ADDR4_STRS_SIZE RTE_DIM(ipaddr_garbage_addr4_strs)
> +#define IPADDR_GARBAGE_ADDR6_STRS_SIZE RTE_DIM(ipaddr_garbage_addr6_strs)
> +#define IPADDR_GARBAGE_NETWORK4_STRS_SIZE RTE_DIM(ipaddr_garbage_network4_strs)
> +#define IPADDR_GARBAGE_NETWORK6_STRS_SIZE RTE_DIM(ipaddr_garbage_network6_strs)
> +#define IPADDR_INVALID_STRS_SIZE RTE_DIM(ipaddr_invalid_strs)

Idem.

>
>  static void
>  dump_addr(cmdline_ipaddr_t addr)
> diff --git a/app/test/test_cmdline_num.c b/app/test/test_cmdline_num.c
> index 4c97caf3d..1daeaf840 100644
> --- a/app/test/test_cmdline_num.c
> +++ b/app/test/test_cmdline_num.c
> @@ -216,17 +216,11 @@ const char * num_invalid_strs[] = {
>                 "\0",
>  };
>
> -#define NUM_POSITIVE_STRS_SIZE \
> -       (sizeof(num_valid_positive_strs) / sizeof(num_valid_positive_strs[0]))
> -#define NUM_NEGATIVE_STRS_SIZE \
> -       (sizeof(num_valid_negative_strs) / sizeof(num_valid_negative_strs[0]))
> -#define NUM_POSITIVE_GARBAGE_STRS_SIZE \
> -       (sizeof(num_garbage_positive_strs) / sizeof(num_garbage_positive_strs[0]))
> -#define NUM_NEGATIVE_GARBAGE_STRS_SIZE \
> -       (sizeof(num_garbage_negative_strs) / sizeof(num_garbage_negative_strs[0]))
> -#define NUM_INVALID_STRS_SIZE \
> -       (sizeof(num_invalid_strs) / sizeof(num_invalid_strs[0]))
> -
> +#define NUM_POSITIVE_STRS_SIZE RTE_DIM(num_valid_positive_strs)
> +#define NUM_NEGATIVE_STRS_SIZE RTE_DIM(num_valid_negative_strs)
> +#define NUM_POSITIVE_GARBAGE_STRS_SIZE RTE_DIM(num_garbage_positive_strs)
> +#define NUM_NEGATIVE_GARBAGE_STRS_SIZE RTE_DIM(num_garbage_negative_strs)
> +#define NUM_INVALID_STRS_SIZE RTE_DIM(num_invalid_strs)

Idem.
Stopping at this, I suppose you get the idea for the rest of the changes.


Thanks.

-- 
David Marchand



More information about the dev mailing list