From db3b6ca546fef0629127fc8dcc45d9a5a0c2e17a Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Mon, 30 Apr 2018 08:57:50 -0700 Subject: [PATCH] common: refactor for array size Signed-off-by: Patrick Donnelly --- src/common/entity_name.cc | 49 +++++++++++++++------------------------ src/common/entity_name.h | 15 ++++++------ src/common/utf8.c | 6 ++--- src/common/utf8.h | 1 - src/mon/ConfigMap.cc | 2 +- 5 files changed, 31 insertions(+), 42 deletions(-) diff --git a/src/common/entity_name.cc b/src/common/entity_name.cc index 918361604423c..b9c33d7d824de 100644 --- a/src/common/entity_name.cc +++ b/src/common/entity_name.cc @@ -20,25 +20,14 @@ using std::string; extern const char *ceph_entity_type_name(int type); -struct str_to_entity_type_t { - uint32_t type; - const char *str; -}; - -static const str_to_entity_type_t STR_TO_ENTITY_TYPE[] = { +const std::array EntityName::STR_TO_ENTITY_TYPE = {{ { CEPH_ENTITY_TYPE_AUTH, "auth" }, { CEPH_ENTITY_TYPE_MON, "mon" }, { CEPH_ENTITY_TYPE_OSD, "osd" }, { CEPH_ENTITY_TYPE_MDS, "mds" }, { CEPH_ENTITY_TYPE_MGR, "mgr" }, { CEPH_ENTITY_TYPE_CLIENT, "client" }, -}; - -EntityName:: -EntityName() - : type(0) -{ -} +}}; const std::string& EntityName:: to_str() const @@ -144,15 +133,25 @@ has_default_id() const std::string EntityName:: get_valid_types_as_str() { - std::string out; + std::ostringstream out; + size_t i; + for (i = 0; i < STR_TO_ENTITY_TYPE.size(); ++i) { + if (i > 0) { + out << ", "; + } + out << STR_TO_ENTITY_TYPE[i].str; + } + return out.str(); +} + +uint32_t EntityName::str_to_ceph_entity_type(std::string_view s) +{ size_t i; - std::string sep(""); - for (i = 0; i < sizeof(STR_TO_ENTITY_TYPE)/sizeof(STR_TO_ENTITY_TYPE[0]); ++i) { - out += sep; - out += STR_TO_ENTITY_TYPE[i].str; - sep = ", "; + for (i = 0; i < STR_TO_ENTITY_TYPE.size(); ++i) { + if (s == STR_TO_ENTITY_TYPE[i].str) + return STR_TO_ENTITY_TYPE[i].type; } - return out; + return CEPH_ENTITY_TYPE_ANY; } bool operator<(const EntityName& a, const EntityName& b) @@ -164,13 +163,3 @@ std::ostream& operator<<(std::ostream& out, const EntityName& n) { return out << n.to_str(); } - -uint32_t str_to_ceph_entity_type(const char * str) -{ - size_t 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; - } - return CEPH_ENTITY_TYPE_ANY; -} diff --git a/src/common/entity_name.h b/src/common/entity_name.h index ea4125c5fb030..05f99145b4846 100644 --- a/src/common/entity_name.h +++ b/src/common/entity_name.h @@ -19,8 +19,6 @@ #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. @@ -28,8 +26,6 @@ */ struct EntityName { - EntityName(); - void encode(bufferlist& bl) const { using ceph::encode; encode(type, bl); @@ -68,6 +64,7 @@ struct EntityName bool has_default_id() const; static std::string get_valid_types_as_str(); + static uint32_t str_to_ceph_entity_type(std::string_view); friend bool operator<(const EntityName& a, const EntityName& b); friend std::ostream& operator<<(std::ostream& out, const EntityName& n); @@ -75,13 +72,17 @@ struct EntityName friend bool operator!=(const EntityName& a, const EntityName& b); private: - uint32_t type; + struct str_to_entity_type_t { + uint32_t type; + const char *str; + }; + static const std::array STR_TO_ENTITY_TYPE; + + uint32_t type = 0; std::string id; std::string type_id; }; -uint32_t str_to_ceph_entity_type(const char * str); - WRITE_CLASS_ENCODER(EntityName) WRITE_EQ_OPERATORS_2(EntityName, type, id) diff --git a/src/common/utf8.c b/src/common/utf8.c index d8663aee57d0f..9b7aaf5fdac96 100644 --- a/src/common/utf8.c +++ b/src/common/utf8.c @@ -33,13 +33,13 @@ static int high_bits_set(int c) */ int encode_utf8(unsigned long u, unsigned char *buf) { - int i; - unsigned long max_val[MAX_UTF8_SZ] = { + static const unsigned long max_val[MAX_UTF8_SZ] = { 0x0000007ful, 0x000007fful, 0x0000fffful, 0x001ffffful, 0x03fffffful, 0x7ffffffful }; - static const int MAX_VAL_SZ = ARRAY_SIZE(max_val); + static const int MAX_VAL_SZ = sizeof(max_val)/sizeof(max_val[0]); + int i; for (i = 0; i < MAX_VAL_SZ; ++i) { if (u <= max_val[i]) break; diff --git a/src/common/utf8.h b/src/common/utf8.h index cf8daeed1786a..83efe6fd6dad4 100644 --- a/src/common/utf8.h +++ b/src/common/utf8.h @@ -17,7 +17,6 @@ #define MAX_UTF8_SZ 6 #define INVALID_UTF8_CHAR 0xfffffffful -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a)) #ifdef __cplusplus extern "C" { diff --git a/src/mon/ConfigMap.cc b/src/mon/ConfigMap.cc index 3ac72f6dd342a..43d72ea391d0d 100644 --- a/src/mon/ConfigMap.cc +++ b/src/mon/ConfigMap.cc @@ -161,7 +161,7 @@ bool ConfigMap::parse_mask( } else { type = i; } - if (str_to_ceph_entity_type(type.c_str()) == CEPH_ENTITY_TYPE_ANY) { + if (EntityName::str_to_ceph_entity_type(type) == CEPH_ENTITY_TYPE_ANY) { return false; } *section = i; -- 2.39.5