[pacman-dev] [PATCH v5 1/2] Use ARRAYSIZE macro for non-string array size computation

Pierre Neidhardt ambrevar at gmail.com
Wed Oct 21 09:11:17 UTC 2015


Signed-off-by: Pierre Neidhardt <ambrevar at gmail.com>
---
 lib/libalpm/pkghash.c    | 2 +-
 src/common/util-common.h | 2 ++
 src/pacman/pacman.c      | 2 +-
 src/pacman/sync.c        | 2 +-
 src/pacman/util.c        | 2 +-
 5 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/lib/libalpm/pkghash.c b/lib/libalpm/pkghash.c
index 989a8c1..5328ce4 100644
--- a/lib/libalpm/pkghash.c
+++ b/lib/libalpm/pkghash.c
@@ -66,7 +66,7 @@ alpm_pkghash_t *_alpm_pkghash_create(unsigned int size)
 	CALLOC(hash, 1, sizeof(alpm_pkghash_t), return NULL);
 	size = size / initial_hash_load + 1;
 
-	loopsize = sizeof(prime_list) / sizeof(*prime_list);
+	loopsize = ARRAYSIZE(prime_list);
 	for(i = 0; i < loopsize; i++) {
 		if(prime_list[i] > size) {
 			hash->buckets = prime_list[i];
diff --git a/src/common/util-common.h b/src/common/util-common.h
index a2093be..373db19 100644
--- a/src/common/util-common.h
+++ b/src/common/util-common.h
@@ -34,6 +34,8 @@ char *safe_fgets(char *s, int size, FILE *stream);
 char *strndup(const char *s, size_t n);
 #endif
 
+#define ARRAYSIZE(a) (sizeof (a) / sizeof (a[0]))
+
 #endif /* _PM_UTIL_COMMON_H */
 
 /* vim: set noet: */
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index c680067..b71ac43 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -1136,7 +1136,7 @@ int main(int argc, char *argv[])
 	new_action.sa_flags = SA_RESTART;
 
 	/* assign our handler to any signals we care about */
-	for(i = 0; i < sizeof(signals) / sizeof(signals[0]); i++) {
+	for(i = 0; i < ARRAYSIZE(signals); i++) {
 		int signal = signals[i];
 		sigaction(signal, NULL, &old_action);
 		if(old_action.sa_handler != SIG_IGN) {
diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index 3c20199..2a0f4b5 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -228,7 +228,7 @@ static int sync_cleancache(int level)
 				};
 				size_t j;
 
-				for(j = 0; j < sizeof(glob_skips) / sizeof(glob_skips[0]); j++) {
+				for(j = 0; j < ARRAYSIZE(glob_skips); j++) {
 					if(fnmatch(glob_skips[j], ent->d_name, 0) == 0) {
 						delete = 0;
 						break;
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 3d71d8b..53f7f6e 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -1114,7 +1114,7 @@ double humanize_size(off_t bytes, const char target_unit, int precision,
 {
 	static const char *labels[] = {"B", "KiB", "MiB", "GiB",
 		"TiB", "PiB", "EiB", "ZiB", "YiB"};
-	static const int unitcount = sizeof(labels) / sizeof(labels[0]);
+	static const int unitcount = ARRAYSIZE(labels);
 
 	double val = (double)bytes;
 	int index;
-- 
2.6.1


More information about the pacman-dev mailing list