From 3d8b9f2f614d45486c82801073a20b1aa2905046 Mon Sep 17 00:00:00 2001 From: simransinghal Date: Wed, 21 Mar 2018 01:49:35 +0530 Subject: [PATCH] common: Use ARRAY_SIZE macro Use ARRAY_SIZE macro instead of sizeof(x)/sizeof(x[0]) to improve code readability. Defined it in appropriate header file. Signed-off-by: Simran Singhal --- src/common/entity_name.cc | 2 +- src/common/entity_name.h | 2 ++ src/common/utf8.c | 2 +- src/common/utf8.h | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/common/entity_name.cc b/src/common/entity_name.cc index 7c9cd78485824..918361604423c 100644 --- a/src/common/entity_name.cc +++ b/src/common/entity_name.cc @@ -168,7 +168,7 @@ std::ostream& operator<<(std::ostream& out, const EntityName& n) uint32_t str_to_ceph_entity_type(const char * str) { size_t i; - for (i = 0; i < sizeof(STR_TO_ENTITY_TYPE)/sizeof(STR_TO_ENTITY_TYPE[0]); ++i) { + for (i = 0; i < ARRAY_SIZE(STR_TO_ENTITY_TYPE); ++i) { if (strcmp(str, STR_TO_ENTITY_TYPE[i].str) == 0) return STR_TO_ENTITY_TYPE[i].type; } diff --git a/src/common/entity_name.h b/src/common/entity_name.h index 44edebb04863e..ea4125c5fb030 100644 --- a/src/common/entity_name.h +++ b/src/common/entity_name.h @@ -19,6 +19,8 @@ #include "msg/msg_types.h" +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a)) + /* Represents a Ceph entity name. * * For example, mds.0 is the name of the first metadata server. diff --git a/src/common/utf8.c b/src/common/utf8.c index 5a8592f1cc5e2..d8663aee57d0f 100644 --- a/src/common/utf8.c +++ b/src/common/utf8.c @@ -38,7 +38,7 @@ int encode_utf8(unsigned long u, unsigned char *buf) 0x0000007ful, 0x000007fful, 0x0000fffful, 0x001ffffful, 0x03fffffful, 0x7ffffffful }; - static const int MAX_VAL_SZ = sizeof(max_val) / sizeof(max_val[0]); + static const int MAX_VAL_SZ = ARRAY_SIZE(max_val); for (i = 0; i < MAX_VAL_SZ; ++i) { if (u <= max_val[i]) diff --git a/src/common/utf8.h b/src/common/utf8.h index 83efe6fd6dad4..cf8daeed1786a 100644 --- a/src/common/utf8.h +++ b/src/common/utf8.h @@ -17,6 +17,7 @@ #define MAX_UTF8_SZ 6 #define INVALID_UTF8_CHAR 0xfffffffful +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a)) #ifdef __cplusplus extern "C" { -- 2.39.5