]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librados_test_stub: cleanup singleton memory allocation
authorJason Dillaman <dillaman@redhat.com>
Fri, 24 Apr 2015 03:08:51 +0000 (23:08 -0400)
committerJason Dillaman <dillaman@redhat.com>
Fri, 17 Jul 2015 18:17:04 +0000 (14:17 -0400)
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
(cherry picked from commit 54c88255b74741d882b88f791497862635357634)

src/test/librados_test_stub/LibradosTestStub.cc
src/test/librados_test_stub/TestWatchNotify.cc

index f9cf32bb0797f62a7a00be9b6354ddea567f9a9f..f7f597c6f31d2be5167a6fff319b2cc9d3cb47de 100644 (file)
@@ -15,6 +15,7 @@
 #include "test/librados_test_stub/TestMemRadosClient.h"
 #include "objclass/objclass.h"
 #include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
 #include <deque>
 #include <list>
 #include <vector>
 
 #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<librados::TestClassHandler> 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<librados::TestRadosClient> 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) {
index 0a68c3cc424866cba8f66376af629dab570560c8..6fd77483bf27f4884965c24409f7cb8968b3348a 100644 (file)
@@ -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()