From 716bb331b9cee828ea8249265cf50355d961859f Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Fri, 26 Sep 2014 21:35:25 +0100 Subject: [PATCH] Use the nelems macro to compute the size of arrays. Copied from Perforce Change: 187055 ServerID: perforce.ravenbrook.com --- mps/code/amcssth.c | 4 ++-- mps/code/djbench.c | 2 +- mps/code/eventsql.c | 6 +++--- mps/code/gcbench.c | 2 +- mps/code/misc.h | 4 ++-- mps/code/steptest.c | 4 +--- mps/code/testlib.c | 8 ++++---- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/mps/code/amcssth.c b/mps/code/amcssth.c index f4dedd55f7f..59b48ff27c4 100644 --- a/mps/code/amcssth.c +++ b/mps/code/amcssth.c @@ -222,7 +222,7 @@ static void test_pool(mps_pool_t pool, size_t roots_count, int mode) cl.pool = pool; cl.roots_count = roots_count; - for (i = 0; i < sizeof(kids)/sizeof(kids[0]); ++i) + for (i = 0; i < NELEMS(kids); ++i) testthr_create(&kids[i], kid_thread, &cl); die(mps_ap_create(&ap, pool, mps_rank_exact()), "BufferCreate"); @@ -306,7 +306,7 @@ static void test_pool(mps_pool_t pool, size_t roots_count, int mode) mps_ap_destroy(busy_ap); mps_ap_destroy(ap); - for (i = 0; i < sizeof(kids)/sizeof(kids[0]); ++i) + for (i = 0; i < NELEMS(kids); ++i) testthr_join(&kids[i], NULL); } diff --git a/mps/code/djbench.c b/mps/code/djbench.c index 2eafe724522..9379a259669 100644 --- a/mps/code/djbench.c +++ b/mps/code/djbench.c @@ -356,7 +356,7 @@ int main(int argc, char *argv[]) { (void)fflush(stdout); while (argc > 0) { - for (i = 0; i < sizeof(pools) / sizeof(pools[0]); ++i) + for (i = 0; i < NELEMS(pools); ++i) if (strcmp(argv[0], pools[i].name) == 0) goto found; fprintf(stderr, "unknown pool test \"%s\"\n", argv[0]); diff --git a/mps/code/eventsql.c b/mps/code/eventsql.c index e1c9ea22381..08838620777 100644 --- a/mps/code/eventsql.c +++ b/mps/code/eventsql.c @@ -386,7 +386,7 @@ static void testTableExists(sqlite3 *db) size_t i; int defects = 0; int tests = 0; - for (i=0; i < (sizeof(tableTests)/sizeof(tableTests[0])); ++i) { + for (i=0; i < NELEMS(tableTests); ++i) { const char *name = tableTests[i].name; int exists = tableExists(db, name); if (exists) @@ -566,7 +566,7 @@ static void makeTables(sqlite3 *db) size_t i; evlog(LOG_SOMETIMES, "Creating tables."); - for (i=0; i < (sizeof(createStatements)/sizeof(createStatements[0])); ++i) { + for (i=0; i < NELEMS(createStatements); ++i) { runStatement(db, createStatements[i], "Table creation"); } } @@ -585,7 +585,7 @@ static void dropGlueTables(sqlite3 *db) evlog(LOG_ALWAYS, "Dropping glue tables so they are rebuilt."); - for (i=0; i < (sizeof(glueTables)/sizeof(glueTables[0])); ++i) { + for (i=0; i < NELEMS(glueTables); ++i) { evlog(LOG_SOMETIMES, "Dropping table %s", glueTables[i]); sprintf(sql, "DROP TABLE %s", glueTables[i]); res = sqlite3_exec(db, diff --git a/mps/code/gcbench.c b/mps/code/gcbench.c index 0dc3ef3ded8..0f02f8040db 100644 --- a/mps/code/gcbench.c +++ b/mps/code/gcbench.c @@ -449,7 +449,7 @@ int main(int argc, char *argv[]) { (void)fflush(stdout); while (argc > 0) { - for (i = 0; i < sizeof(pools) / sizeof(pools[0]); ++i) + for (i = 0; i < NELEMS(pools); ++i) if (strcmp(argv[0], pools[i].name) == 0) goto found; fprintf(stderr, "unknown pool test \"%s\"\n", argv[0]); diff --git a/mps/code/misc.h b/mps/code/misc.h index c07647f54db..9b22522558a 100644 --- a/mps/code/misc.h +++ b/mps/code/misc.h @@ -102,8 +102,8 @@ typedef const struct SrcIdStruct { * NELEMS(a) expands into an expression that is the number * of elements in the array a. * - * WARNING: expands a more than once (you'd have to write obviously - * perverse code for this to matter though). + * WARNING: expands a more than once (but only in the context of + * sizeof, so does not cause double evaluation). */ #define NELEMS(a) (sizeof(a)/sizeof((a)[0])) diff --git a/mps/code/steptest.c b/mps/code/steptest.c index 546eaa7e417..a66b85f1195 100644 --- a/mps/code/steptest.c +++ b/mps/code/steptest.c @@ -56,8 +56,6 @@ static unsigned long step_frequencies[] = { 1000000000, /* one billion */ }; -#define TESTS (sizeof(step_frequencies) / sizeof(step_frequencies[0])) - static unsigned test_number = 0; @@ -496,7 +494,7 @@ int main(int argc, char *argv[]) testlib_init(argc, argv); - while (test_number < TESTS) { + while (test_number < NELEMS(step_frequencies)) { mps_arena_t arena; mps_thr_t thread; void *r; diff --git a/mps/code/testlib.c b/mps/code/testlib.c index 5b8b17d5ef2..401c4059938 100644 --- a/mps/code/testlib.c +++ b/mps/code/testlib.c @@ -325,9 +325,9 @@ void rnd_state_set_v2(unsigned long seed0_v2) static struct { const char *ident; const char *doc; -} res_strings[] = { -#define RES_STRINGS_ROW(X, ident, doc) {#ident, #doc}, -_mps_RES_ENUM(RES_STRINGS_ROW, X) +} const res_strings[] = { +#define RES_STRINGS_ROW(X, ident, doc) {#ident, doc}, + _mps_RES_ENUM(RES_STRINGS_ROW, X) }; @@ -372,7 +372,7 @@ void error(const char *format, ...) void die_expect(mps_res_t res, mps_res_t expected, const char *s) { if (res != expected) { - if (0 <= res && (unsigned)res < sizeof(res_strings) / sizeof(res_strings[0])) + if (0 <= res && (unsigned)res < NELEMS(res_strings)) error("\n%s: %s: %s\n", s, res_strings[res].ident, res_strings[res].doc); else error("\n%s: %d: unknown result code\n", s, res);