Browse Source

*: use array_size() helper macro

Use the array_size() helper macro.  Replaces several instances of local
macros with the same definition.

Reviewed-by: Scott Feldman <sfeldma@cumulusnetworks.com>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Balaji.G 6 years ago
parent
commit
837d16ccbe

+ 1 - 1
babeld/babel_main.c

@@ -444,7 +444,7 @@ babel_init_signals(void)
         },
     };
 
-    signal_init (master, Q_SIGC(babel_signals), babel_signals);
+    signal_init (master, array_size(babel_signals), babel_signals);
 }
 
 static void

+ 2 - 3
bgpd/bgp_attr.c

@@ -62,7 +62,7 @@ static const struct message attr_str [] =
   { BGP_ATTR_AS4_AGGREGATOR,   "AS4_AGGREGATOR" }, 
   { BGP_ATTR_AS_PATHLIMIT,     "AS_PATHLIMIT" },
 };
-static const int attr_str_max = sizeof(attr_str)/sizeof(attr_str[0]);
+static const int attr_str_max = array_size(attr_str);
 
 static const struct message attr_flag_str[] =
 {
@@ -72,8 +72,7 @@ static const struct message attr_flag_str[] =
   /* bgp_attr_flags_diagnose() relies on this bit being last in this list */
   { BGP_ATTR_FLAG_EXTLEN,   "Extended Length" },
 };
-static const size_t attr_flag_str_max =
-  sizeof (attr_flag_str) / sizeof (attr_flag_str[0]);
+static const size_t attr_flag_str_max = array_size(attr_flag_str);
 
 static struct hash *cluster_hash;
 

+ 2 - 2
bgpd/bgp_main.c

@@ -135,7 +135,7 @@ struct zebra_privs_t bgpd_privs =
   .vty_group = VTY_GROUP,
 #endif
   .caps_p = _caps_p,
-  .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
+  .cap_num_p = array_size(_caps_p),
   .cap_num_i = 0,
 };
 
@@ -422,7 +422,7 @@ main (int argc, char **argv)
 
   /* Initializations. */
   srand (time (NULL));
-  signal_init (master, Q_SIGC(bgp_signals), bgp_signals);
+  signal_init (master, array_size(bgp_signals), bgp_signals);
   zprivs_init (&bgpd_privs);
   cmd_init (1);
   vty_init (master);

+ 3 - 5
bgpd/bgp_open.c

@@ -187,8 +187,7 @@ static const struct message orf_type_str[] =
   { ORF_TYPE_PREFIX,		"Prefixlist"		},
   { ORF_TYPE_PREFIX_OLD,	"Prefixlist (old)"	},
 };
-static const int orf_type_str_max
-	= sizeof(orf_type_str)/sizeof(orf_type_str[0]);
+static const int orf_type_str_max = array_size(orf_type_str);
 
 static const struct message orf_mode_str[] =
 {
@@ -196,8 +195,7 @@ static const struct message orf_mode_str[] =
   { ORF_MODE_SEND,	"Send"		},
   { ORF_MODE_BOTH,	"Both"		},
 };
-static const int orf_mode_str_max
-	 = sizeof(orf_mode_str)/sizeof(orf_mode_str[0]);
+static const int orf_mode_str_max = array_size(orf_mode_str);
 
 static int
 bgp_capability_orf_entry (struct peer *peer, struct capability_header *hdr)
@@ -449,7 +447,7 @@ static const struct message capcode_str[] =
   { CAPABILITY_CODE_REFRESH_OLD,	"Route Refresh (Old)"		},
   { CAPABILITY_CODE_ORF_OLD,		"ORF (Old)"			},
 };
-static const int capcode_str_max = sizeof(capcode_str)/sizeof(capcode_str[0]);
+static const int capcode_str_max = array_size(capcode_str);
 
 /* Minimum sizes for length field of each cap (so not inc. the header) */
 static const size_t cap_minsizes[] = 

+ 1 - 1
isisd/isis_main.c

@@ -325,7 +325,7 @@ main (int argc, char **argv, char **envp)
    *  initializations
    */
   zprivs_init (&isisd_privs);
-  signal_init (master, Q_SIGC (isisd_signals), isisd_signals);
+  signal_init (master, array_size (isisd_signals), isisd_signals);
   cmd_init (1);
   vty_init (master);
   memory_init ();

+ 1 - 1
lib/buffer.c

@@ -262,7 +262,7 @@ buffer_flush_window (struct buffer *b, int fd, int width, int height,
   /* For erase and more data add two to b's buffer_data count.*/
   if (b->head->next == NULL)
     {
-      iov_alloc = sizeof(small_iov)/sizeof(small_iov[0]);
+      iov_alloc = array_size(small_iov);
       iov = small_iov;
     }
   else

+ 10 - 14
lib/log.c

@@ -443,8 +443,8 @@ zlog_backtrace_sigsafe(int priority, void *program_counter)
 #define LOC s,buf+sizeof(buf)-s
 
 #ifdef HAVE_GLIBC_BACKTRACE
-  if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
-      ((size_t)size > sizeof(array)/sizeof(array[0])))
+  if (((size = backtrace(array,array_size(array)) <= 0) ||
+      ((size_t)size > array_size(array))))
     return;
 
 #define DUMP(FD) { \
@@ -526,12 +526,12 @@ zlog_backtrace(int priority)
   int size, i;
   char **strings;
 
-  if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
-      ((size_t)size > sizeof(array)/sizeof(array[0])))
+  if (((size = backtrace(array,array_size(array))) <= 0) ||
+      ((size_t)size > array_size(array)))
     {
       zlog_err("Cannot get backtrace, returned invalid # of frames %d "
 	       "(valid range is between 1 and %lu)",
-	       size, (unsigned long)(sizeof(array)/sizeof(array[0])));
+	       size, (unsigned long)(array_size(array)));
       return;
     }
   zlog(NULL, priority, "Backtrace for %d stack frames:", size);
@@ -636,7 +636,7 @@ openzlog (const char *progname, zlog_proto_t protocol,
   zl->syslog_options = syslog_flags;
 
   /* Set default logging levels. */
-  for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
+  for (i = 0; i < array_size(zl->maxlvl); i++)
     zl->maxlvl[i] = ZLOG_DISABLED;
   zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
   zl->default_lvl = LOG_DEBUG;
@@ -855,14 +855,14 @@ zroute_lookup(u_int zroute)
 {
   u_int i;
 
-  if (zroute >= sizeof(route_types)/sizeof(route_types[0]))
+  if (zroute >= array_size(route_types))
     {
       zlog_err("unknown zebra route type: %u", zroute);
       return &unknown;
     }
   if (zroute == route_types[zroute].type)
     return &route_types[zroute];
-  for (i = 0; i < sizeof(route_types)/sizeof(route_types[0]); i++)
+  for (i = 0; i < array_size(route_types); i++)
     {
       if (zroute == route_types[i].type)
         {
@@ -890,7 +890,7 @@ zebra_route_char(u_int zroute)
 const char *
 zserv_command_string (unsigned int command)
 {
-  if (command >= sizeof(command_types)/sizeof(command_types[0]))
+  if (command >= array_size(command_types))
     {
       zlog_err ("unknown zserv command type: %u", command);
       return unknown.string;
@@ -898,21 +898,17 @@ zserv_command_string (unsigned int command)
   return command_types[command].string;
 }
 
-#define RTSIZE	(sizeof(route_types)/sizeof(route_types[0]))
-
 int
 proto_name2num(const char *s)
 {
    unsigned i;
 
-   for (i=0; i<RTSIZE; ++i)
+   for (i=0; i<array_size(route_types); ++i)
      if (strcasecmp(s, route_types[i].string) == 0)
        return route_types[i].type;
    return -1;
 }
 
-#undef RTSIZE
-
 int
 proto_redistnum(int afi, const char *s)
 {

+ 5 - 4
lib/sigevent.c

@@ -22,6 +22,7 @@
 #include <zebra.h>
 #include <sigevent.h>
 #include <log.h>
+#include <memory.h>
 
 #ifdef SA_SIGINFO
 #ifdef HAVE_UCONTEXT_H
@@ -266,13 +267,13 @@ trap_default_signals(void)
 #endif
 		   );
   } sigmap[] = {
-    { core_signals, sizeof(core_signals)/sizeof(core_signals[0]), core_handler},
-    { exit_signals, sizeof(exit_signals)/sizeof(exit_signals[0]), exit_handler},
-    { ignore_signals, sizeof(ignore_signals)/sizeof(ignore_signals[0]), NULL},
+    { core_signals, array_size(core_signals), core_handler},
+    { exit_signals, array_size(exit_signals), exit_handler},
+    { ignore_signals, array_size(ignore_signals), NULL},
   };
   u_int i;
 
-  for (i = 0; i < sizeof(sigmap)/sizeof(sigmap[0]); i++)
+  for (i = 0; i < array_size(sigmap); i++)
     {
       u_int j;
 

+ 0 - 1
lib/sigevent.h

@@ -27,7 +27,6 @@
 #include <thread.h>
 
 #define QUAGGA_SIGNAL_TIMER_INTERVAL 2L
-#define Q_SIGC(sig) (sizeof(sig)/sizeof(sig[0]))
 
 struct quagga_signal_t
 {

+ 1 - 1
ospf6d/ospf6_main.c

@@ -308,7 +308,7 @@ main (int argc, char *argv[], char *envp[])
                            LOG_DAEMON);
   zprivs_init (&ospf6d_privs);
   /* initialize zebra libraries */
-  signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals);
+  signal_init (master, array_size(ospf6_signals), ospf6_signals);
   cmd_init (1);
   vty_init (master);
   memory_init ();

+ 1 - 2
ospf6d/ospf6_message.c

@@ -58,8 +58,7 @@ static const struct message ospf6_message_type_str [] =
   { OSPF6_MESSAGE_TYPE_LSUPDATE, "LSUpdate" },
   { OSPF6_MESSAGE_TYPE_LSACK,    "LSAck"    },
 };
-static const size_t ospf6_message_type_str_max =
-  sizeof (ospf6_message_type_str) / sizeof (ospf6_message_type_str[0]);
+static const size_t ospf6_message_type_str_max = array_size(ospf6_message_type_str);
 
 /* Minimum (besides the standard OSPF packet header) lengths for OSPF
    packets of particular types, offset is the "type" field. */

+ 2 - 2
ospfd/ospf_api.c

@@ -155,7 +155,7 @@ ospf_api_typename (int msgtype)
     { MSG_NSM_CHANGE,            "NSM change",             },
   };
 
-  int i, n = sizeof (NameTab) / sizeof (NameTab[0]);
+  int i, n = array_size(NameTab);
   const char *name = NULL;
 
   for (i = 0; i < n; i++)
@@ -187,7 +187,7 @@ ospf_api_errname (int errcode)
     { OSPF_API_UNDEF,                   "Undefined",                  },
   };
 
-  int i, n = sizeof (NameTab) / sizeof (NameTab[0]);
+  int i, n = array_size(NameTab);
   const char *name = NULL;
 
   for (i = 0; i < n; i++)

+ 2 - 2
ospfd/ospf_main.c

@@ -68,7 +68,7 @@ struct zebra_privs_t ospfd_privs =
   .vty_group = VTY_GROUP,
 #endif
   .caps_p = _caps_p,
-  .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
+  .cap_num_p = array_size(_caps_p),
   .cap_num_i = 0
 };
 
@@ -285,7 +285,7 @@ main (int argc, char **argv)
 
   /* Library inits. */
   zprivs_init (&ospfd_privs);
-  signal_init (master, Q_SIGC(ospf_signals), ospf_signals);
+  signal_init (master, array_size(ospf_signals), ospf_signals);
   cmd_init (1);
   debug_init ();
   vty_init (master);

+ 1 - 1
ripd/rip_main.c

@@ -275,7 +275,7 @@ main (int argc, char **argv)
 
   /* Library initialization. */
   zprivs_init (&ripd_privs);
-  signal_init (master, Q_SIGC(ripd_signals), ripd_signals);
+  signal_init (master, array_size(ripd_signals), ripd_signals);
   cmd_init (1);
   vty_init (master);
   memory_init ();

+ 1 - 1
ripngd/ripng_main.c

@@ -272,7 +272,7 @@ main (int argc, char **argv)
 
   /* Library inits. */
   zprivs_init (&ripngd_privs);
-  signal_init (master, Q_SIGC(ripng_signals), ripng_signals);
+  signal_init (master, array_size(ripng_signals), ripng_signals);
   cmd_init (1);
   vty_init (master);
   memory_init ();

+ 1 - 1
tests/test-sig.c

@@ -43,7 +43,7 @@ int
 main (void)
 {
   master = thread_master_create ();
-  signal_init (master, Q_SIGC(sigs), sigs);
+  signal_init (master, array_size(sigs), sigs);
   
   zlog_default = openzlog("testsig", ZLOG_NONE,
                           LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);

+ 10 - 11
vtysh/vtysh.c

@@ -61,7 +61,6 @@ struct vtysh_client
   { .fd = -1, .name = "babeld", .flag = VTYSH_BABELD, .path = BABEL_VTYSH_PATH},
 };
 
-#define VTYSH_INDEX_MAX (sizeof(vtysh_client)/sizeof(vtysh_client[0]))
 
 /* We need direct access to ripd to implement vtysh_exit_ripd_only. */
 static struct vtysh_client *ripd_client = NULL;
@@ -374,7 +373,7 @@ vtysh_execute_func (const char *line, int pager)
 
 	if (! strcmp(cmd->string,"configure terminal"))
 	  {
-	    for (i = 0; i < VTYSH_INDEX_MAX; i++)
+	    for (i = 0; i < array_size(vtysh_client); i++)
 	      {
 	        cmd_stat = vtysh_client_execute(&vtysh_client[i], line, fp);
 		if (cmd_stat == CMD_WARNING)
@@ -413,7 +412,7 @@ vtysh_execute_func (const char *line, int pager)
 	  }
 
 	cmd_stat = CMD_SUCCESS;
-	for (i = 0; i < VTYSH_INDEX_MAX; i++)
+	for (i = 0; i < array_size(vtysh_client); i++)
 	  {
 	    if (cmd->daemon & vtysh_client[i].flag)
 	      {
@@ -525,7 +524,7 @@ vtysh_config_from_file (struct vty *vty, FILE *fp)
 	    u_int i;
 	    int cmd_stat = CMD_SUCCESS;
 
-	    for (i = 0; i < VTYSH_INDEX_MAX; i++)
+	    for (i = 0; i < array_size(vtysh_client); i++)
 	      {
 	        if (cmd->daemon & vtysh_client[i].flag)
 		  {
@@ -1354,7 +1353,7 @@ DEFUN (vtysh_show_memory,
   int ret = CMD_SUCCESS;
   char line[] = "show memory\n";
   
-  for (i = 0; i < VTYSH_INDEX_MAX; i++)
+  for (i = 0; i < array_size(vtysh_client); i++)
     if ( vtysh_client[i].fd >= 0 )
       {
         fprintf (stdout, "Memory statistics for %s:\n", 
@@ -1377,7 +1376,7 @@ DEFUN (vtysh_show_logging,
   int ret = CMD_SUCCESS;
   char line[] = "show logging\n";
   
-  for (i = 0; i < VTYSH_INDEX_MAX; i++)
+  for (i = 0; i < array_size(vtysh_client); i++)
     if ( vtysh_client[i].fd >= 0 )
       {
         fprintf (stdout,"Logging configuration for %s:\n", 
@@ -1733,7 +1732,7 @@ DEFUN (vtysh_write_terminal,
 	   VTY_NEWLINE);
   vty_out (vty, "!%s", VTY_NEWLINE);
 
-  for (i = 0; i < VTYSH_INDEX_MAX; i++)
+  for (i = 0; i < array_size(vtysh_client); i++)
     ret = vtysh_client_config (&vtysh_client[i], line);
 
   /* Integrate vtysh specific configuration. */
@@ -1807,7 +1806,7 @@ write_config_integrated(void)
       return CMD_SUCCESS;
     }
 
-  for (i = 0; i < VTYSH_INDEX_MAX; i++)
+  for (i = 0; i < array_size(vtysh_client); i++)
     ret = vtysh_client_config (&vtysh_client[i], line);
 
   vtysh_config_dump (fp);
@@ -1844,7 +1843,7 @@ DEFUN (vtysh_write_memory,
 
   fprintf (stdout,"Building Configuration...\n");
 	  
-  for (i = 0; i < VTYSH_INDEX_MAX; i++)
+  for (i = 0; i < array_size(vtysh_client); i++)
     ret = vtysh_client_execute (&vtysh_client[i], line, stdout);
   
   fprintf (stdout,"[OK]\n");
@@ -1934,7 +1933,7 @@ DEFUN (vtysh_show_daemons,
 {
   u_int i;
 
-  for (i = 0; i < VTYSH_INDEX_MAX; i++)
+  for (i = 0; i < array_size(vtysh_client); i++)
     if ( vtysh_client[i].fd >= 0 )
       vty_out(vty, " %s", vtysh_client[i].name);
   vty_out(vty, "%s", VTY_NEWLINE);
@@ -2184,7 +2183,7 @@ vtysh_connect_all(const char *daemon_name)
   int rc = 0;
   int matches = 0;
 
-  for (i = 0; i < VTYSH_INDEX_MAX; i++)
+  for (i = 0; i < array_size(vtysh_client); i++)
     {
       if (!daemon_name || !strcmp(daemon_name, vtysh_client[i].name))
 	{

+ 2 - 1
watchquagga/watchquagga.c

@@ -27,6 +27,7 @@
 #include <getopt.h>
 #include <sys/un.h>
 #include <sys/wait.h>
+#include <memory.h>
 
 #ifndef MIN
 #define MIN(X,Y) (((X) <= (Y)) ? (X) : (Y))
@@ -1279,7 +1280,7 @@ main(int argc, char **argv)
       
   gs.restart.interval = gs.min_restart_interval;
   master = thread_master_create();
-  signal_init (master, Q_SIGC(my_signals), my_signals);
+  signal_init (master, array_size(my_signals), my_signals);
   srandom(time(NULL));
 
   {

+ 2 - 2
zebra/main.c

@@ -104,7 +104,7 @@ struct zebra_privs_t zserv_privs =
   .vty_group = VTY_GROUP,
 #endif
   .caps_p = _caps_p,
-  .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
+  .cap_num_p = array_size(_caps_p),
   .cap_num_i = 0
 };
 
@@ -313,7 +313,7 @@ main (int argc, char **argv)
   zprivs_init (&zserv_privs);
 
   /* Vty related initialize. */
-  signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
+  signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
   cmd_init (1);
   vty_init (zebrad.master);
   memory_init ();

+ 1 - 1
zebra/rt_netlink.c

@@ -1985,7 +1985,7 @@ static void netlink_install_filter (int sock, __u32 pid)
   };
 
   struct sock_fprog prog = {
-    .len = sizeof(filter) / sizeof(filter[0]),
+    .len = array_size(filter),
     .filter = filter,
   };
 

+ 1 - 1
zebra/test_main.c

@@ -280,7 +280,7 @@ main (int argc, char **argv)
   zebrad.master = thread_master_create ();
 
   /* Vty related initialize. */
-  signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
+  signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
   cmd_init (1);
   vty_init (zebrad.master);
   memory_init ();

+ 1 - 1
zebra/zebra_rib.c

@@ -1546,7 +1546,7 @@ rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
   /* Set default distance by route type. */
   if (distance == 0)
     {
-      if ((unsigned)type >= sizeof(route_info) / sizeof(route_info[0]))
+      if ((unsigned)type >= array_size(route_info))
 	distance = 150;
       else
         distance = route_info[type].distance;