From aa38700396ed766cf1876040b8502962f07fbddc Mon Sep 17 00:00:00 2001 From: Michal Jarzabek Date: Sun, 22 Nov 2015 12:12:47 +0000 Subject: [PATCH] common/buffer: changed buffer from class to namespace Since the buffer only contained static functions it doesn't make sense for it to be a class. Changing it to a namespace will allow bufferlist, bufferptr and bufferhash to be forward declared. This wasn't possible when they were nested classes. Function declarations for inc_total_alloc and dec_total_alloc were removed from the header file and changed to static, to simulate the private access specifier, which they were under. Moved the operators inside the buffer namespace to take advantage of the Argument Dependent Lookup. Moved typedefs to buffer_fwd header file which we can include when only the forward declarations are needed. Since the buffer is not a class anymore all the instances of using ceph::buffer were removed. Signed-off-by: Michal Jarzabek --- src/common/buffer.cc | 21 +++++++---- src/include/Makefile.am | 2 + src/include/buffer.h | 69 ++++++++++++++-------------------- src/include/buffer_fwd.h | 17 +++++++++ src/test/librados/cls.cc | 1 - src/test/librados/cmd.cc | 1 - src/test/librados/misc.cc | 1 - src/test/librados/tier.cc | 1 - src/test/multi_stress_watch.cc | 1 - src/test/test_stress_watch.cc | 1 - 10 files changed, 60 insertions(+), 55 deletions(-) create mode 100644 src/include/buffer_fwd.h diff --git a/src/common/buffer.cc b/src/common/buffer.cc index 9e0b12329deaa..10e01a00457d6 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -52,20 +52,26 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; static atomic64_t buffer_history_alloc_num; const bool buffer_track_alloc = get_env_bool("CEPH_BUFFER_TRACK"); - void buffer::inc_total_alloc(unsigned len) { + namespace { + void inc_total_alloc(unsigned len) { if (buffer_track_alloc) buffer_total_alloc.add(len); } - void buffer::dec_total_alloc(unsigned len) { + + void dec_total_alloc(unsigned len) { if (buffer_track_alloc) buffer_total_alloc.sub(len); } - void buffer::inc_history_alloc(uint64_t len) { + + void inc_history_alloc(uint64_t len) { if (buffer_track_alloc) { buffer_history_alloc_bytes.add(len); buffer_history_alloc_num.inc(); } } + } + + int buffer::get_total_alloc() { return buffer_total_alloc.read(); } @@ -2118,11 +2124,11 @@ void buffer::list::hexdump(std::ostream &out) const out.flags(original_flags); } -std::ostream& operator<<(std::ostream& out, const buffer::raw &r) { +std::ostream& buffer::operator<<(std::ostream& out, const buffer::raw &r) { return out << "buffer::raw(" << (void*)r.data << " len " << r.len << " nref " << r.nref.read() << ")"; } -std::ostream& operator<<(std::ostream& out, const buffer::ptr& bp) { +std::ostream& buffer::operator<<(std::ostream& out, const buffer::ptr& bp) { if (bp.have_raw()) out << "buffer::ptr(" << bp.offset() << "~" << bp.length() << " " << (void*)bp.c_str() @@ -2134,7 +2140,7 @@ std::ostream& operator<<(std::ostream& out, const buffer::ptr& bp) { return out; } -std::ostream& operator<<(std::ostream& out, const buffer::list& bl) { +std::ostream& buffer::operator<<(std::ostream& out, const buffer::list& bl) { out << "buffer::list(len=" << bl.length() << "," << std::endl; std::list::const_iterator it = bl.buffers().begin(); @@ -2147,9 +2153,8 @@ std::ostream& operator<<(std::ostream& out, const buffer::list& bl) { return out; } -std::ostream& operator<<(std::ostream& out, const buffer::error& e) +std::ostream& buffer::operator<<(std::ostream& out, const buffer::error& e) { return out << e.what(); } - } diff --git a/src/include/Makefile.am b/src/include/Makefile.am index a364b295d6c25..ffbdc35b3ba73 100644 --- a/src/include/Makefile.am +++ b/src/include/Makefile.am @@ -9,6 +9,7 @@ rados_include_DATA = \ $(srcdir)/include/rados/rados_types.hpp \ $(srcdir)/include/rados/librados.hpp \ $(srcdir)/include/buffer.h \ + $(srcdir)/include/buffer_fwd.h \ $(srcdir)/include/page.h \ $(srcdir)/include/crc32c.h \ $(srcdir)/include/memory.h @@ -52,6 +53,7 @@ noinst_HEADERS += \ include/bitmapper.h \ include/blobhash.h \ include/buffer.h \ + include/buffer_fwd.h \ include/byteorder.h \ include/cephfs/libcephfs.h \ include/ceph_features.h \ diff --git a/src/include/buffer.h b/src/include/buffer.h index 2478d30b01ba1..2f01260c151cb 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -47,6 +47,7 @@ #include "page.h" #include "crc32c.h" +#include "include/buffer_fwd.h" #ifdef __CEPH__ # include "include/assert.h" @@ -69,12 +70,11 @@ namespace ceph { const static int CEPH_BUFFER_APPEND_SIZE(4096); -class CEPH_BUFFER_API buffer { +namespace buffer CEPH_BUFFER_API { /* * exceptions */ -public: struct error : public std::exception{ const char *what() const throw (); }; @@ -99,35 +99,28 @@ public: /// total bytes allocated - static int get_total_alloc(); + int get_total_alloc(); /// history total bytes allocated - static uint64_t get_history_alloc_bytes(); + uint64_t get_history_alloc_bytes(); /// total num allocated - static uint64_t get_history_alloc_num(); + uint64_t get_history_alloc_num(); /// enable/disable alloc tracking - static void track_alloc(bool b); + void track_alloc(bool b); /// count of cached crc hits (matching input) - static int get_cached_crc(); + int get_cached_crc(); /// count of cached crc hits (mismatching input, required adjustment) - static int get_cached_crc_adjusted(); + int get_cached_crc_adjusted(); /// enable/disable tracking of cached crcs - static void track_cached_crc(bool b); + void track_cached_crc(bool b); /// count of calls to buffer::ptr::c_str() - static int get_c_str_accesses(); + int get_c_str_accesses(); /// enable/disable tracking of buffer::ptr::c_str() calls - static void track_c_str(bool b); - -private: - - /* hack for memory utilization debugging. */ - static void inc_total_alloc(unsigned len); - static void inc_history_alloc(uint64_t len); - static void dec_total_alloc(unsigned len); + void track_c_str(bool b); /* * an abstract raw buffer. with a reference count. @@ -142,25 +135,23 @@ private: class raw_pipe; class raw_unshareable; // diagnostic, unshareable char buffer - friend std::ostream& operator<<(std::ostream& out, const raw &r); -public: class xio_mempool; class xio_msg_buffer; /* * named constructors */ - static raw* copy(const char *c, unsigned len); - static raw* create(unsigned len); - static raw* claim_char(unsigned len, char *buf); - static raw* create_malloc(unsigned len); - static raw* claim_malloc(unsigned len, char *buf); - static raw* create_static(unsigned len, char *buf); - static raw* create_aligned(unsigned len, unsigned align); - static raw* create_page_aligned(unsigned len); - static raw* create_zero_copy(unsigned len, int fd, int64_t *offset); - static raw* create_unshareable(unsigned len); + raw* copy(const char *c, unsigned len); + raw* create(unsigned len); + raw* claim_char(unsigned len, char *buf); + raw* create_malloc(unsigned len); + raw* claim_malloc(unsigned len, char *buf); + raw* create_static(unsigned len, char *buf); + raw* create_aligned(unsigned len, unsigned align); + raw* create_page_aligned(unsigned len); + raw* create_zero_copy(unsigned len, int fd, int64_t *offset); + raw* create_unshareable(unsigned len); #if defined(HAVE_XIO) static raw* create_msg(unsigned len, char *buf, XioDispatchHook *m_hook); @@ -254,7 +245,6 @@ public: }; - friend std::ostream& operator<<(std::ostream& out, const buffer::ptr& bp); /* * list - the useful bit! @@ -562,16 +552,6 @@ public: return crc; } }; -}; - -#if defined(HAVE_XIO) -xio_reg_mem* get_xio_mp(const buffer::ptr& bp); -#endif - -typedef buffer::ptr bufferptr; -typedef buffer::list bufferlist; -typedef buffer::hash bufferhash; - inline bool operator>(bufferlist& l, bufferlist& r) { for (unsigned p = 0; ; p++) { @@ -610,6 +590,7 @@ inline bool operator<=(bufferlist& l, bufferlist& r) { std::ostream& operator<<(std::ostream& out, const buffer::ptr& bp); +std::ostream& operator<<(std::ostream& out, const raw &r); std::ostream& operator<<(std::ostream& out, const buffer::list& bl); @@ -621,4 +602,10 @@ inline bufferhash& operator<<(bufferhash& l, bufferlist &r) { } } +#if defined(HAVE_XIO) +xio_reg_mem* get_xio_mp(const buffer::ptr& bp); +#endif + +} + #endif diff --git a/src/include/buffer_fwd.h b/src/include/buffer_fwd.h new file mode 100644 index 0000000000000..1646eff8be45d --- /dev/null +++ b/src/include/buffer_fwd.h @@ -0,0 +1,17 @@ +#ifndef BUFFER_FWD_H +#define BUFFER_FWD_H + +namespace ceph { + namespace buffer { + class ptr; + class list; + class hash; + } + + using bufferptr = buffer::ptr; + using bufferlist = buffer::list; + using bufferhash = buffer::hash; +} + +#endif + diff --git a/src/test/librados/cls.cc b/src/test/librados/cls.cc index 1f61664b48782..10744b2b9b899 100644 --- a/src/test/librados/cls.cc +++ b/src/test/librados/cls.cc @@ -9,7 +9,6 @@ #include using namespace librados; -using ceph::buffer; using std::map; using std::ostringstream; using std::string; diff --git a/src/test/librados/cmd.cc b/src/test/librados/cmd.cc index 4f327a0e84b2b..d5e9d71fb6a5a 100644 --- a/src/test/librados/cmd.cc +++ b/src/test/librados/cmd.cc @@ -18,7 +18,6 @@ #include using namespace librados; -using ceph::buffer; using std::map; using std::ostringstream; using std::string; diff --git a/src/test/librados/misc.cc b/src/test/librados/misc.cc index 4adaa6bf163f8..0fb67e0d509b3 100644 --- a/src/test/librados/misc.cc +++ b/src/test/librados/misc.cc @@ -21,7 +21,6 @@ #include using namespace librados; -using ceph::buffer; using std::map; using std::ostringstream; using std::string; diff --git a/src/test/librados/tier.cc b/src/test/librados/tier.cc index 12ccfc29a0177..6517f82b839f5 100644 --- a/src/test/librados/tier.cc +++ b/src/test/librados/tier.cc @@ -26,7 +26,6 @@ #include using namespace librados; -using ceph::buffer; using std::map; using std::ostringstream; using std::string; diff --git a/src/test/multi_stress_watch.cc b/src/test/multi_stress_watch.cc index 25f735558f5c3..4dc5489d51d74 100644 --- a/src/test/multi_stress_watch.cc +++ b/src/test/multi_stress_watch.cc @@ -13,7 +13,6 @@ #include using namespace librados; -using ceph::buffer; using std::map; using std::ostringstream; using std::string; diff --git a/src/test/test_stress_watch.cc b/src/test/test_stress_watch.cc index 9e66f0ecd66ee..6ddfee5047f9d 100644 --- a/src/test/test_stress_watch.cc +++ b/src/test/test_stress_watch.cc @@ -18,7 +18,6 @@ using namespace librados; -using ceph::buffer; using std::map; using std::ostringstream; using std::string; -- 2.39.5