Replace SIZE() with COUNTOF() and countof()
Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/1617f762 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/1617f762 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/1617f762 Branch: refs/heads/sphinx-docs Commit: 1617f7620e652623779c28e29685f0f264190da5 Parents: 207ac9a Author: James Peach <[email protected]> Authored: Fri Apr 26 10:49:20 2013 -0700 Committer: James Peach <[email protected]> Committed: Fri Apr 26 10:53:17 2013 -0700 ---------------------------------------------------------------------- CHANGES | 2 ++ iocore/cache/Cache.cc | 5 ++--- iocore/cluster/ClusterHandler.cc | 2 +- iocore/cluster/P_ClusterCache.h | 2 +- iocore/dns/DNS.cc | 6 +++--- iocore/net/NetVCTest.cc | 2 +- iocore/net/P_NetVCTest.h | 2 +- lib/ts/ink_defs.h | 14 +++++++++++--- mgmt/cli/TrafficLine.cc | 2 +- mgmt/cli/cliMain.cc | 3 +-- proxy/Main.cc | 7 +++---- proxy/StatSystem.cc | 4 ++-- proxy/logcat.cc | 5 ++--- proxy/logstats.cc | 8 +++----- proxy/sac.cc | 4 +--- 15 files changed, 35 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 9f48947..e13b24a 100644 --- a/CHANGES +++ b/CHANGES @@ -2,6 +2,8 @@ Changes with Apache Traffic Server 3.3.3 + *) [TS-1856] Replace SIZE() macro with COUNTOF() and countof(). + *) [TS-1853] Fix formatting in logstats to be consistenly 2 decimals. *) [TS-1852] Fix missing AC_PROG_SED on CentOS 5.9. http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/iocore/cache/Cache.cc ---------------------------------------------------------------------- diff --git a/iocore/cache/Cache.cc b/iocore/cache/Cache.cc index 9676291..bc273d3 100644 --- a/iocore/cache/Cache.cc +++ b/iocore/cache/Cache.cc @@ -859,7 +859,7 @@ CacheProcessor::cacheInitialized() break; } } - // let us cocalate the Size + // let us calculate the Size if (cache_config_ram_cache_size == AUTO_SIZE_RAM_CACHE) { Debug("cache_init", "CacheProcessor::cacheInitialized - cache_config_ram_cache_size == AUTO_SIZE_RAM_CACHE"); for (i = 0; i < gnvol; i++) { @@ -1125,7 +1125,6 @@ Vol::init(char *s, off_t blocks, off_t dir_skip, bool clear) len = blocks * STORE_BLOCK_SIZE; ink_assert(len <= MAX_VOL_SIZE); skip = dir_skip; - int i; prev_recover_pos = 0; // successive approximation, directory/meta data eats up some storage @@ -1167,7 +1166,7 @@ Vol::init(char *s, off_t blocks, off_t dir_skip, bool clear) init_info->vol_aio[2].aiocb.aio_offset = bs; init_info->vol_aio[3].aiocb.aio_offset = bs + footer_offset; - for (i = 0; i < 4; i++) { + for (unsigned i = 0; i < countof(init_info->vol_aio); i++) { AIOCallback *aio = &(init_info->vol_aio[i]); aio->aiocb.aio_fildes = fd; aio->aiocb.aio_buf = &(init_info->vol_h_f[i * STORE_BLOCK_SIZE]); http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/iocore/cluster/ClusterHandler.cc ---------------------------------------------------------------------- diff --git a/iocore/cluster/ClusterHandler.cc b/iocore/cluster/ClusterHandler.cc index 6e4c5ca..770aa89 100644 --- a/iocore/cluster/ClusterHandler.cc +++ b/iocore/cluster/ClusterHandler.cc @@ -34,7 +34,7 @@ // Global Data /*************************************************************************/ // Initialize clusterFunction[] size -int SIZE_clusterFunction = SIZE(clusterFunction); +unsigned SIZE_clusterFunction = countof(clusterFunction); // hook for testing ClusterFunction *ptest_ClusterFunction = NULL; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/iocore/cluster/P_ClusterCache.h ---------------------------------------------------------------------- diff --git a/iocore/cluster/P_ClusterCache.h b/iocore/cluster/P_ClusterCache.h index 9fc001f..f885689 100644 --- a/iocore/cluster/P_ClusterCache.h +++ b/iocore/cluster/P_ClusterCache.h @@ -866,7 +866,7 @@ ClusterFunctionDescriptor clusterFunction[] #endif ; -extern int SIZE_clusterFunction; // clusterFunction[] entries +extern unsigned SIZE_clusterFunction; // clusterFunction[] entries ////////////////////////////////////////////////////////////// // Map from Cluster Function code to send queue priority http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/iocore/dns/DNS.cc ---------------------------------------------------------------------- diff --git a/iocore/dns/DNS.cc b/iocore/dns/DNS.cc index 2d99eea..3593cd4 100644 --- a/iocore/dns/DNS.cc +++ b/iocore/dns/DNS.cc @@ -569,7 +569,7 @@ DNSHandler::retry_named(int ndx, ink_hrtime t, bool reopen) char buffer[MAX_DNS_PACKET_LEN]; Debug("dns", "trying to resolve '%s' from DNS connection, ndx %d", try_server_names[try_servers], ndx); int r = _ink_res_mkquery(m_res, try_server_names[try_servers], T_A, buffer); - try_servers = (try_servers + 1) % SIZE(try_server_names); + try_servers = (try_servers + 1) % countof(try_server_names); ink_assert(r >= 0); if (r >= 0) { // looking for a bounce int res = socketManager.send(con[ndx].fd, buffer, r, 0); @@ -597,7 +597,7 @@ DNSHandler::try_primary_named(bool reopen) if (local_num_entries < DEFAULT_NUM_TRY_SERVER) try_servers = (try_servers + 1) % local_num_entries; else - try_servers = (try_servers + 1) % SIZE(try_server_names); + try_servers = (try_servers + 1) % countof(try_server_names); ink_assert(r >= 0); if (r >= 0) { // looking for a bounce int res = socketManager.send(con[0].fd, buffer, r, 0); @@ -1421,7 +1421,7 @@ dns_process(DNSHandler *handler, HostEnt *buf, int len) // e->qname_len, no ? if (local_num_entries >= DEFAULT_NUM_TRY_SERVER) { if ((attempt_num_entries % 50) == 0) { - try_servers = (try_servers + 1) % SIZE(try_server_names); + try_servers = (try_servers + 1) % countof(try_server_names); ink_strlcpy(try_server_names[try_servers], e->qname, MAXDNAME); memset(&try_server_names[try_servers][strlen(e->qname)], 0, 1); attempt_num_entries = 0; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/iocore/net/NetVCTest.cc ---------------------------------------------------------------------- diff --git a/iocore/net/NetVCTest.cc b/iocore/net/NetVCTest.cc index 8903e72..55844f7 100644 --- a/iocore/net/NetVCTest.cc +++ b/iocore/net/NetVCTest.cc @@ -95,7 +95,7 @@ NVC_test_def netvc_tests_def[] = { {"overflow", 50, 50, 0, 35000, 1024, 35, EVENT_NONE, VC_EVENT_WRITE_COMPLETE} }; -int num_netvc_tests = SIZE(netvc_tests_def); +const unsigned num_netvc_tests = countof(netvc_tests_def); NetVCTest::NetVCTest(): http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/iocore/net/P_NetVCTest.h ---------------------------------------------------------------------- diff --git a/iocore/net/P_NetVCTest.h b/iocore/net/P_NetVCTest.h index a950f2d..14f4ea1 100644 --- a/iocore/net/P_NetVCTest.h +++ b/iocore/net/P_NetVCTest.h @@ -69,7 +69,7 @@ struct NVC_test_def }; extern NVC_test_def netvc_tests_def[]; -extern int num_netvc_tests; +extern const unsigned num_netvc_tests; class NetTestDriver:public Continuation { http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/lib/ts/ink_defs.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_defs.h b/lib/ts/ink_defs.h index 5df5dcc..7aa4c10 100644 --- a/lib/ts/ink_defs.h +++ b/lib/ts/ink_defs.h @@ -85,9 +85,17 @@ // interpreted as 32 bits #define NULL_PTR static_cast<void*>(0) -/* Some popular defines -*/ -#define SIZE(x) (sizeof(x)/sizeof((x)[0])) +// Determine the element count for an array. +#define COUNTOF(x) ((unsigned)(sizeof(x)/sizeof((x)[0]))) + +#ifdef __cplusplus +template<typename T, unsigned N> +static inline unsigned +countof(const T (&)[N]) { + return N; +} +#endif + #define SOCKOPT_ON ((char*)&on) #define SOCKOPT_OFF ((char*)&off) http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/mgmt/cli/TrafficLine.cc ---------------------------------------------------------------------- diff --git a/mgmt/cli/TrafficLine.cc b/mgmt/cli/TrafficLine.cc index 16c6a2f..4c318ea 100644 --- a/mgmt/cli/TrafficLine.cc +++ b/mgmt/cli/TrafficLine.cc @@ -190,7 +190,7 @@ main(int argc, char **argv) }; // Process command line arguments and dump into variables - process_args(argument_descriptions, SIZE(argument_descriptions), argv); + process_args(argument_descriptions, countof(argument_descriptions), argv); // check for the version number request if (VersionFlag) { http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/mgmt/cli/cliMain.cc ---------------------------------------------------------------------- diff --git a/mgmt/cli/cliMain.cc b/mgmt/cli/cliMain.cc index 11d242f..671eba8 100644 --- a/mgmt/cli/cliMain.cc +++ b/mgmt/cli/cliMain.cc @@ -64,11 +64,10 @@ main(int argc, char *argv[]) {"version", 'V', "Print Version Id", "T", &version_flag, NULL, NULL} }; - int n_argument_descriptions = SIZE(argument_descriptions); NOWARN_UNUSED(argc); // Process command line arguments and dump into variables - process_args(argument_descriptions, n_argument_descriptions, argv); + process_args(argument_descriptions, countof(argument_descriptions), argv); // check for the version number request if (version_flag) { http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/proxy/Main.cc ---------------------------------------------------------------------- diff --git a/proxy/Main.cc b/proxy/Main.cc index 9d04841..793add0 100644 --- a/proxy/Main.cc +++ b/proxy/Main.cc @@ -243,7 +243,6 @@ static const ArgumentDescription argument_descriptions[] = { NULL, NULL}, {"help", 'h', "HELP!", NULL, NULL, NULL, usage}, }; -static const unsigned n_argument_descriptions = SIZE(argument_descriptions); // // Initialize operating system related information/services @@ -707,7 +706,7 @@ static int cmd_index(char *p) { p += strspn(p, " \t"); - for (unsigned c = 0; c < SIZE(commands); c++) { + for (unsigned c = 0; c < countof(commands); c++) { const char *l = commands[c].n; while (l) { const char *s = strchr(l, '/'); @@ -729,7 +728,7 @@ cmd_help(char *cmd) printf("HELP\n\n"); cmd = skip(cmd, true); if (!cmd) { - for (unsigned i = 0; i < SIZE(commands); i++) { + for (unsigned i = 0; i < countof(commands); i++) { printf("%15s %s\n", commands[i].n, commands[i].d); } } else { @@ -1336,7 +1335,7 @@ main(int argc, char **argv) ink_strlcpy(management_directory, Layout::get()->sysconfdir, sizeof(management_directory)); chdir_root(); // change directory to the install root of traffic server. - process_args(argument_descriptions, n_argument_descriptions, argv); + process_args(argument_descriptions, countof(argument_descriptions), argv); // Check for version number request if (version_flag) { http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/proxy/StatSystem.cc ---------------------------------------------------------------------- diff --git a/proxy/StatSystem.cc b/proxy/StatSystem.cc index 480dfc9..f52bd57 100644 --- a/proxy/StatSystem.cc +++ b/proxy/StatSystem.cc @@ -167,12 +167,12 @@ static int persistent_stat(int i) { #ifndef DEFAULT_PERSISTENT - for (int j = 0; j < (int) SIZE(persistent_stats); j++) + for (unsigned j = 0; j < countof(persistent_stats); j++) if (persistent_stats[j] == i) return 1; return 0; #else - for (int j = 0; j < (int) SIZE(non_persistent_stats); j++) + for (unsigned j = 0; j < countof(non_persistent_stats); j++) if (non_persistent_stats[j] == i) return 0; return 1; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/proxy/logcat.cc ---------------------------------------------------------------------- diff --git a/proxy/logcat.cc b/proxy/logcat.cc index eab1a8d..fe9bd3e 100644 --- a/proxy/logcat.cc +++ b/proxy/logcat.cc @@ -77,7 +77,6 @@ static const ArgumentDescription argument_descriptions[] = { {"elf2", '2', "Convert to Extended2 Logging Format", "T", &elf2_flag, NULL, NULL} }; -static const int n_argument_descriptions = SIZE(argument_descriptions); static const char *USAGE_LINE = "Usage: " PROGRAM_NAME " [-o output-file | -a] [-CEhS" #ifdef DEBUG @@ -241,7 +240,7 @@ main(int /* argc ATS_UNUSED */, char *argv[]) // process command-line arguments // output_file[0] = 0; - process_args(argument_descriptions, n_argument_descriptions, argv, USAGE_LINE); + process_args(argument_descriptions, countof(argument_descriptions), argv, USAGE_LINE); // check for the version number request // @@ -252,7 +251,7 @@ main(int /* argc ATS_UNUSED */, char *argv[]) // check for help request // if (help) { - usage(argument_descriptions, n_argument_descriptions, USAGE_LINE); + usage(argument_descriptions, countof(argument_descriptions), USAGE_LINE); _exit(NO_ERROR); } // check that only one of the -o and -a options was specified http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/proxy/logstats.cc ---------------------------------------------------------------------- diff --git a/proxy/logstats.cc b/proxy/logstats.cc index e99b2e1..dc5815a 100644 --- a/proxy/logstats.cc +++ b/proxy/logstats.cc @@ -658,8 +658,6 @@ static ArgumentDescription argument_descriptions[] = { {"version", 'V', "Print Version Id", "T", &cl.version, NULL, NULL}, }; -static int n_argument_descriptions = SIZE(argument_descriptions); - static const char *USAGE_LINE = "Usage: " PROGRAM_NAME " [-f logfile] [-o origin[,...]] [-O originfile] [-m minhits] [-inshv]"; @@ -667,7 +665,7 @@ void CommandLineArgs::parse_arguments(char** argv) { // process command-line arguments - process_args(argument_descriptions, n_argument_descriptions, argv, USAGE_LINE); + process_args(argument_descriptions, countof(argument_descriptions), argv, USAGE_LINE); // Process as "CGI" ? if (strstr(argv[0], ".cgi") || cgi) { @@ -720,7 +718,7 @@ CommandLineArgs::parse_arguments(char** argv) // check for help request if (help) { - usage(argument_descriptions, n_argument_descriptions, USAGE_LINE); + usage(argument_descriptions, countof(argument_descriptions), USAGE_LINE); _exit(0); } } @@ -2302,7 +2300,7 @@ main(int /* argc ATS_UNUSED */, char *argv[]) fs.open(cl.origin_file, std::ios::in); if (!fs.is_open()) { std::cerr << "can't read " << cl.origin_file << std::endl; - usage(argument_descriptions, n_argument_descriptions, USAGE_LINE); + usage(argument_descriptions, countof(argument_descriptions), USAGE_LINE); _exit(0); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1617f762/proxy/sac.cc ---------------------------------------------------------------------- diff --git a/proxy/sac.cc b/proxy/sac.cc index 11f9f43..1925113 100644 --- a/proxy/sac.cc +++ b/proxy/sac.cc @@ -70,8 +70,6 @@ ArgumentDescription argument_descriptions[] = { #endif {"help", 'h', "HELP!", NULL, NULL, NULL, usage}, }; -int n_argument_descriptions = SIZE(argument_descriptions); - /*------------------------------------------------------------------------- main @@ -90,7 +88,7 @@ main(int /* argc ATS_UNUSED */, char *argv[]) // take care of command-line arguments // snprintf(configDirectoryType, sizeof(configDirectoryType), "S%d", PATH_NAME_MAX - 1); - process_args(argument_descriptions, n_argument_descriptions, argv); + process_args(argument_descriptions, countof(argument_descriptions), argv); // Get log directory ink_strlcpy(system_log_dir, Layout::get()->logdir, sizeof(system_log_dir));
