From 13f926e4e96d0b7178a9762bbbf589961dba47b7 Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Thu, 23 Apr 2015 23:08:51 -0400 Subject: [PATCH] librados_test_stub: cleanup singleton memory allocation Signed-off-by: Jason Dillaman (cherry picked from commit 54c88255b74741d882b88f791497862635357634) --- .../librados_test_stub/LibradosTestStub.cc | 29 +++++++++++++------ .../librados_test_stub/TestWatchNotify.cc | 2 ++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/test/librados_test_stub/LibradosTestStub.cc b/src/test/librados_test_stub/LibradosTestStub.cc index f9cf32bb0797f..f7f597c6f31d2 100644 --- a/src/test/librados_test_stub/LibradosTestStub.cc +++ b/src/test/librados_test_stub/LibradosTestStub.cc @@ -15,6 +15,7 @@ #include "test/librados_test_stub/TestMemRadosClient.h" #include "objclass/objclass.h" #include +#include #include #include #include @@ -22,29 +23,39 @@ #define dout_subsys ceph_subsys_rados +namespace { + +static void DeallocateRadosClient(librados::TestRadosClient* client) +{ + client->put(); +} + +} // anonymous namespace + + static librados::TestClassHandler *get_class_handler() { - static librados::TestClassHandler *s_class_handler = NULL; - if (s_class_handler == NULL) { - s_class_handler = new librados::TestClassHandler(); + static boost::shared_ptr s_class_handler; + if (!s_class_handler) { + s_class_handler.reset(new librados::TestClassHandler()); s_class_handler->open_all_classes(); } - return s_class_handler; + return s_class_handler.get(); } static librados::TestRadosClient *get_rados_client() { // TODO: use factory to allow tests to swap out impl - static librados::TestRadosClient *s_rados_client = NULL; - if (s_rados_client == NULL) { + static boost::shared_ptr s_rados_client; + if (!s_rados_client) { CephInitParameters iparams(CEPH_ENTITY_TYPE_CLIENT); CephContext *cct = common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0); cct->_conf->parse_env(); cct->_conf->apply_changes(NULL); - g_ceph_context = cct; - s_rados_client = new librados::TestMemRadosClient(cct); + s_rados_client.reset(new librados::TestMemRadosClient(cct), + &DeallocateRadosClient); cct->put(); } s_rados_client->get(); - return s_rados_client; + return s_rados_client.get(); } static void do_out_buffer(bufferlist& outbl, char **outbuf, size_t *outbuflen) { diff --git a/src/test/librados_test_stub/TestWatchNotify.cc b/src/test/librados_test_stub/TestWatchNotify.cc index 0a68c3cc42486..6fd77483bf27f 100644 --- a/src/test/librados_test_stub/TestWatchNotify.cc +++ b/src/test/librados_test_stub/TestWatchNotify.cc @@ -12,12 +12,14 @@ namespace librados { TestWatchNotify::TestWatchNotify(CephContext *cct) : m_cct(cct), m_finisher(new Finisher(cct)), m_handle(), m_notify_id(), m_file_watcher_lock("librados::TestWatchNotify::m_file_watcher_lock") { + m_cct->get(); m_finisher->start(); } TestWatchNotify::~TestWatchNotify() { m_finisher->stop(); delete m_finisher; + m_cct->put(); } TestWatchNotify::NotifyHandle::NotifyHandle() -- 2.39.5