From 316b966bd0eb15f41201d05cc1dc5a3d03389bb4 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 3 Feb 2018 10:42:22 +0100 Subject: [PATCH] Introduce and use ARRAY_SIZE macro --- ccache.h | 1 + compopt.c | 12 ++++++------ unittest/test_util.c | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/ccache.h b/ccache.h index 6fdd0d401..dec11fa21 100644 --- a/ccache.h +++ b/ccache.h @@ -76,6 +76,7 @@ enum stats { #define str_endswith(s, suffix) \ (strlen(s) >= strlen(suffix) \ && str_eq((s) + strlen(s) - strlen(suffix), (suffix))) +#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) // Buffer size for I/O operations. Should be a multiple of 4 KiB. #define READ_BUFFER_SIZE 65536 diff --git a/compopt.c b/compopt.c index 89976a4a8..aab9fe552 100644 --- a/compopt.c +++ b/compopt.c @@ -1,4 +1,4 @@ -// Copyright (C) 2010-2016 Joel Rosdahl +// Copyright (C) 2010-2018 Joel Rosdahl // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free @@ -113,8 +113,8 @@ find(const char *option) struct compopt key; key.name = option; return bsearch( - &key, compopts, sizeof(compopts) / sizeof(compopts[0]), - sizeof(compopts[0]), compare_compopts); + &key, compopts, ARRAY_SIZE(compopts), sizeof(compopts[0]), + compare_compopts); } static const struct compopt * @@ -123,8 +123,8 @@ find_prefix(const char *option) struct compopt key; key.name = option; return bsearch( - &key, compopts, sizeof(compopts) / sizeof(compopts[0]), - sizeof(compopts[0]), compare_prefix_compopts); + &key, compopts, ARRAY_SIZE(compopts), sizeof(compopts[0]), + compare_prefix_compopts); } // Runs fn on the first two characters of option. @@ -141,7 +141,7 @@ compopt_short(bool (*fn)(const char *), const char *option) bool compopt_verify_sortedness(void) { - for (size_t i = 1; i < sizeof(compopts)/sizeof(compopts[0]); i++) { + for (size_t i = 1; i < ARRAY_SIZE(compopts); i++) { if (strcmp(compopts[i-1].name, compopts[i].name) >= 0) { fprintf(stderr, "compopt_verify_sortedness: %s >= %s\n", diff --git a/unittest/test_util.c b/unittest/test_util.c index dca718c34..b6c4c2eb8 100644 --- a/unittest/test_util.c +++ b/unittest/test_util.c @@ -1,4 +1,4 @@ -// Copyright (C) 2010-2016 Joel Rosdahl +// Copyright (C) 2010-2018 Joel Rosdahl // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free @@ -193,7 +193,7 @@ TEST(parse_size_with_suffix) }; - for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); ++i) { + for (i = 0; i < ARRAY_SIZE(sizes); ++i) { CHECKM(parse_size_with_suffix(sizes[i].size, &size), sizes[i].size); CHECK_INT_EQ(sizes[i].expected, size); } -- 2.47.2