]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
tools/ceph-dencoder: extract denc_plugin.h out
authorKefu Chai <kchai@redhat.com>
Tue, 3 Aug 2021 10:30:34 +0000 (18:30 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 3 Aug 2021 12:51:43 +0000 (20:51 +0800)
to prepare for the upcoming change to hold the encoders in DencoderPlugin

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/tools/ceph-dencoder/ceph_dencoder.cc
src/tools/ceph-dencoder/denc_plugin.h [new file with mode: 0644]

index eb42d9e22604447e13e26f9e5b7909629873876e..30e7eef945a7d19ec1ea942f7decb64625994f29 100644 (file)
@@ -13,7 +13,6 @@
  */
 
 
-#include <dlfcn.h>
 #include <errno.h>
 
 #include <filesystem>
@@ -24,6 +23,7 @@
 #include "common/Formatter.h"
 #include "common/ceph_argparse.h"
 #include "common/errno.h"
+#include "denc_plugin.h"
 #include "denc_registry.h"
 
 #define MB(m) ((m) * 1024 * 1024)
@@ -60,44 +60,6 @@ void usage(ostream &out)
   out << "  is_deterministic    exit w/ success if type encodes deterministically\n";
 }
 
-static constexpr string_view REGISTER_DENCODERS_FUNCTION = "register_dencoders\0";
-
-class DencoderPlugin {
-public:
-  DencoderPlugin(const fs::path& path) {
-    mod = dlopen(path.c_str(), RTLD_NOW);
-    if (mod == nullptr) {
-      std::cerr << "failed to dlopen(" << path << "): " << dlerror() << std::endl;
-    }
-  }
-  ~DencoderPlugin() {
-#if !defined(__FreeBSD__)
-    if (mod) {
-      dlclose(mod);
-    }
-#endif
-  }
-  int register_dencoders(DencoderRegistry& registry) {
-    assert(mod);
-    using register_dencoders_t = void (*)(DencoderRegistry&);
-    const auto do_register =
-      reinterpret_cast<register_dencoders_t>(dlsym(mod, REGISTER_DENCODERS_FUNCTION.data()));
-    if (do_register == nullptr) {
-      std::cerr << "failed to dlsym(" << REGISTER_DENCODERS_FUNCTION << ")" << std::endl;
-      return -1;
-    }
-    const unsigned nr_before = registry.get().size();
-    do_register(registry);
-    const unsigned nr_after = registry.get().size();
-    return nr_after - nr_before;
-  }
-  bool good() const {
-    return mod != nullptr;
-  }
-private:
-  void *mod = nullptr;
-};
-
 vector<DencoderPlugin> load_plugins(DencoderRegistry& registry)
 {
   fs::path mod_dir{CEPH_DENC_MOD_DIR};
@@ -126,6 +88,7 @@ vector<DencoderPlugin> load_plugins(DencoderRegistry& registry)
       std::cerr << "fail to load dencoders from " << entry << std::endl;
       continue;
     }
+    std::cout << "load " << n << " from " << entry << std::endl;
     dencoder_plugins.push_back(std::move(plugin));
   }
   return dencoder_plugins;
diff --git a/src/tools/ceph-dencoder/denc_plugin.h b/src/tools/ceph-dencoder/denc_plugin.h
new file mode 100644 (file)
index 0000000..1a0834e
--- /dev/null
@@ -0,0 +1,44 @@
+#include <dlfcn.h>
+#include <filesystem>
+
+#include "denc_registry.h"
+
+namespace fs = std::filesystem;
+
+class DencoderPlugin {
+public:
+  DencoderPlugin(const fs::path& path) {
+    mod = dlopen(path.c_str(), RTLD_NOW);
+    if (mod == nullptr) {
+      std::cerr << "failed to dlopen(" << path << "): " << dlerror() << std::endl;
+    }
+  }
+  ~DencoderPlugin() {
+#if !defined(__FreeBSD__)
+    if (mod) {
+      dlclose(mod);
+    }
+#endif
+  }
+  int register_dencoders(DencoderRegistry& registry) {
+    static constexpr string_view REGISTER_DENCODERS_FUNCTION = "register_dencoders\0";
+
+    assert(mod);
+    using register_dencoders_t = void (*)(DencoderRegistry&);
+    const auto do_register =
+      reinterpret_cast<register_dencoders_t>(dlsym(mod, REGISTER_DENCODERS_FUNCTION.data()));
+    if (do_register == nullptr) {
+      std::cerr << "failed to dlsym(" << REGISTER_DENCODERS_FUNCTION << ")" << std::endl;
+      return -1;
+    }
+    const unsigned nr_before = registry.get().size();
+    do_register(registry);
+    const unsigned nr_after = registry.get().size();
+    return nr_after - nr_before;
+  }
+  bool good() const {
+    return mod != nullptr;
+  }
+private:
+  void *mod = nullptr;
+};