From 4914607e7a32d10a130d751a1ea8206aebfd5458 Mon Sep 17 00:00:00 2001 From: "Adam C. Emerson" Date: Tue, 30 Jan 2018 16:44:00 -0500 Subject: [PATCH] common/admin_socket: Use unique_ptr instead of new/delete Mostly for the default hooks, plus one Formatter. Signed-off-by: Adam C. Emerson --- src/common/admin_socket.cc | 28 +++++++++++++++------------- src/common/admin_socket.h | 11 ++++++----- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/common/admin_socket.cc b/src/common/admin_socket.cc index 83bb68b541741..4e014ae2d868a 100644 --- a/src/common/admin_socket.cc +++ b/src/common/admin_socket.cc @@ -517,7 +517,8 @@ public: bool call(std::string_view command, const cmdmap_t& cmdmap, std::string_view format, bufferlist& out) override { - auto f = Formatter::create(format, "json-pretty"sv, "json-pretty"sv); + std::unique_ptr f(Formatter::create(format, "json-pretty"sv, + "json-pretty"sv)); f->open_object_section("help"); for (const auto& [command, info] : m_as->hooks) { if (info.help.length()) @@ -527,7 +528,6 @@ public: ostringstream ss; f->flush(ss); out.append(ss.str()); - delete f; return true; } }; @@ -589,15 +589,17 @@ bool AdminSocket::init(const std::string& path) m_shutdown_wr_fd = pipe_wr; m_path = path; - m_version_hook = new VersionHook; - register_command("0", "0", m_version_hook, ""); - register_command("version", "version", m_version_hook, "get ceph version"); - register_command("git_version", "git_version", m_version_hook, "get git sha1"); - m_help_hook = new HelpHook(this); - register_command("help", "help", m_help_hook, "list available commands"); - m_getdescs_hook = new GetdescsHook(this); + version_hook = std::make_unique(); + register_command("0", "0", version_hook.get(), ""); + register_command("version", "version", version_hook.get(), "get ceph version"); + register_command("git_version", "git_version", version_hook.get(), + "get git sha1"); + help_hook = std::make_unique(this); + register_command("help", "help", help_hook.get(), + "list available commands"); + getdescs_hook = std::make_unique(this); register_command("get_command_descriptions", "get_command_descriptions", - m_getdescs_hook, "list available commands"); + getdescs_hook.get(), "list available commands"); th = make_named_thread("admin_socket", &AdminSocket::entry, this); add_cleanup_file(m_path.c_str()); @@ -624,13 +626,13 @@ void AdminSocket::shutdown() unregister_command("version"); unregister_command("git_version"); unregister_command("0"); - delete m_version_hook; + version_hook.reset(); unregister_command("help"); - delete m_help_hook; + help_hook.reset(); unregister_command("get_command_descriptions"); - delete m_getdescs_hook; + getdescs_hook.reset(); remove_cleanup_file(m_path); m_path.clear(); diff --git a/src/common/admin_socket.h b/src/common/admin_socket.h index 1296820e22b0d..2178d711cfeed 100644 --- a/src/common/admin_socket.h +++ b/src/common/admin_socket.h @@ -1,4 +1,4 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- // vim: ts=8 sw=2 smarttab /* * Ceph - scalable distributed file system @@ -7,9 +7,9 @@ * * 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 + * License version 2.1, as published by the Free Software * Foundation. See file COPYING. - * + * */ #ifndef CEPH_COMMON_ADMIN_SOCKET_H @@ -114,8 +114,9 @@ private: bool in_hook = false; std::condition_variable in_hook_cond; std::mutex lock; // protects `hooks` - AdminSocketHook *m_version_hook = nullptr, *m_help_hook = nullptr, - *m_getdescs_hook = nullptr; + std::unique_ptr version_hook; + std::unique_ptr help_hook; + std::unique_ptr getdescs_hook; struct hook_info { AdminSocketHook* hook; -- 2.39.5