]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: refactor for array size 21010/head
authorPatrick Donnelly <pdonnell@redhat.com>
Mon, 30 Apr 2018 15:57:50 +0000 (08:57 -0700)
committerPatrick Donnelly <pdonnell@redhat.com>
Mon, 30 Apr 2018 20:43:54 +0000 (13:43 -0700)
Signed-off-by: Patrick Donnelly <pdonnell@redhat.com>
src/common/entity_name.cc
src/common/entity_name.h
src/common/utf8.c
src/common/utf8.h
src/mon/ConfigMap.cc

index 918361604423c6c5ab9b7e46ca759ad57dacb355..b9c33d7d824deecfc2ef55c021b27d0335d483d8 100644 (file)
@@ -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_t, 6> 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;
-}
index ea4125c5fb0303943a837ee53ffa793d0c92a121..05f99145b48460e69a6d42a8d0c8cd8a7e2e1a31 100644 (file)
@@ -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_t, 6> 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)
index d8663aee57d0f1a52f87faef83714380cbf89804..9b7aaf5fdac968531a4a4184f390c13f5c332602 100644 (file)
@@ -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;
index cf8daeed1786af6f1aa7629b7d172f0bcf796182..83efe6fd6dad43c81e417208102c39f15e75dba6 100644 (file)
@@ -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" {
index 3ac72f6dd342af09934a48e01f6d3d9314d110dd..43d72ea391d0dcaaa2c2bba03585638a9e583810 100644 (file)
@@ -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;