]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/bit_str: Helper functions to output bitmask's flags
authorJoao Eduardo Luis <joao@suse.de>
Wed, 27 Jan 2016 10:26:13 +0000 (10:26 +0000)
committerJoao Eduardo Luis <joao@suse.de>
Sat, 29 Oct 2016 03:10:22 +0000 (04:10 +0100)
These functions will take a given function 'f' as parameter, and will
call 'f' for each bit that is set on a provided bitmask. The
responsibility of printing the appropriate flag name falls on 'f'.

Signed-off-by: Joao Eduardo Luis <joao@suse.de>
src/CMakeLists.txt
src/common/bit_str.cc [new file with mode: 0644]
src/common/bit_str.h [new file with mode: 0644]

index c42af1d1208a4a437a6898e460f69e9ade442328..ce4056c9c4842d65e1dcd0c00c862b34a8586d63 100644 (file)
@@ -436,6 +436,7 @@ set(libcommon_files
   common/TracepointProvider.cc
   common/Cycles.cc
   common/scrub_types.cc
+  common/bit_str.cc
   osdc/Striper.cc
   osdc/Objecter.cc
   common/Graylog.cc
diff --git a/src/common/bit_str.cc b/src/common/bit_str.cc
new file mode 100644 (file)
index 0000000..9802f4a
--- /dev/null
@@ -0,0 +1,61 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2016 SUSE LINUX GmbH
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+#include "common/bit_str.h"
+#include "common/Formatter.h"
+#include "include/assert.h"
+
+static void _dump_bit_str(
+    uint64_t bits,
+    std::ostream *out,
+    ceph::Formatter *f,
+    std::function<const char*(uint64_t)> func)
+{
+  uint64_t b = bits;
+  int cnt = 0;
+  bool outted = false;
+
+  while (b && cnt < 64) {
+    uint64_t r = bits & (1ULL << cnt++);
+    if (r) {
+      if (out) {
+        if (outted)
+          *out << ",";
+        *out << func(r);
+      } else {
+        assert(f != NULL);
+        f->dump_stream("bit_flag") << func(r);
+      }
+      outted = true;
+    }
+    b >>= 1;
+  }
+  if (!outted && out)
+      *out << "none";
+}
+
+void print_bit_str(
+    uint64_t bits,
+    std::ostream &out,
+    std::function<const char*(uint64_t)> func)
+{
+  _dump_bit_str(bits, &out, NULL, func);
+}
+
+void dump_bit_str(
+    uint64_t bits,
+    ceph::Formatter *f,
+    std::function<const char*(uint64_t)> func)
+{
+  _dump_bit_str(bits, NULL, f, func);
+}
diff --git a/src/common/bit_str.h b/src/common/bit_str.h
new file mode 100644 (file)
index 0000000..6567454
--- /dev/null
@@ -0,0 +1,35 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2016 SUSE LINUX GmbH
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+#ifndef CEPH_COMMON_BIT_STR_H
+#define CEPH_COMMON_BIT_STR_H
+
+#include <ostream>
+#include <functional>
+#include <stdint.h>
+
+namespace ceph {
+  class Formatter;
+}
+
+extern void print_bit_str(
+    uint64_t bits,
+    std::ostream &out,
+    std::function<const char*(uint64_t)> func);
+
+extern void dump_bit_str(
+    uint64_t bits,
+    ceph::Formatter *f,
+    std::function<const char*(uint64_t)> func);
+
+#endif /* CEPH_COMMON_BIT_STR_H */