From fc8e3c354be7f6bb25462b104bde46879aeb21cc Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 8 Apr 2010 08:49:27 -0700 Subject: [PATCH] buffer: move to ceph namespace --- src/common/buffer.cc | 5 +- src/include/buffer.h | 7 +- src/include/encoding.h | 187 +++++++++++++++++++++-------------------- 3 files changed, 105 insertions(+), 94 deletions(-) diff --git a/src/common/buffer.cc b/src/common/buffer.cc index bb47673b3a6b..b6318c3f828b 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -20,10 +20,11 @@ #include #include +namespace ceph { + Spinlock buffer_lock("buffer_lock"); atomic_t buffer_total_alloc; - void buffer::list::encode_base64(buffer::list& o) { bufferptr bp(length() * 4 / 3 + 1); @@ -127,3 +128,5 @@ void buffer::list::hexdump(std::ostream &out) const } out.unsetf(std::ios::right); } + +} diff --git a/src/include/buffer.h b/src/include/buffer.h index f6faa129a68a..b311c4fd67e1 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -58,7 +58,6 @@ using std::string; #include "crc32c.h" #include "assert.h" -extern atomic_t buffer_total_alloc; //#define BUFFER_DEBUG @@ -72,6 +71,10 @@ extern Spinlock buffer_lock; # define bendl std::endl; } #endif +namespace ceph { + +extern atomic_t buffer_total_alloc; + class buffer { /* * exceptions @@ -1205,4 +1208,6 @@ inline ostream& operator<<(ostream& out, buffer::error& e) return out << e.what(); } +} + #endif diff --git a/src/include/encoding.h b/src/include/encoding.h index 776e209d6cf5..16d08799c88a 100644 --- a/src/include/encoding.h +++ b/src/include/encoding.h @@ -19,6 +19,8 @@ #include "byteorder.h" #include "buffer.h" +using namespace ceph; + // -------------------------------------- // base types @@ -91,6 +93,30 @@ WRITE_INTTYPE_ENCODER(s16, le16) inline void decode(cl &c, bufferlist::iterator &p) { c.decode(p); } +// string +inline void encode(const std::string& s, bufferlist& bl) +{ + __u32 len = s.length(); + encode(len, bl); + bl.append(s.data(), len); +} +inline void decode(std::string& s, bufferlist::iterator& p) +{ + __u32 len; + decode(len, p); + s.clear(); + p.copy(len, s); +} + +// const char* (encode only, string compatible) +inline void encode(const char *s, bufferlist& bl) +{ + __u32 len = strlen(s); + encode(len, bl); + bl.append(s, len); +} + + // array template inline void encode_array_nohead(const A a[], int n, bufferlist &bl) @@ -107,6 +133,75 @@ inline void decode_array_nohead(A a[], int n, bufferlist::iterator &p) +// ----------------------------- +// buffers + +// bufferptr (encapsulated) +inline void encode(const buffer::ptr& bp, bufferlist& bl) +{ + __u32 len = bp.length(); + encode(len, bl); + if (len) + bl.append(bp); +} +inline void decode(buffer::ptr& bp, bufferlist::iterator& p) +{ + __u32 len; + decode(len, p); + + bufferlist s; + p.copy(len, s); + + if (len) { + if (s.buffers().size() == 1) + bp = s.buffers().front(); + else + bp = buffer::copy(s.c_str(), s.length()); + } +} + +// bufferlist (encapsulated) +inline void encode(const bufferlist& s, bufferlist& bl) +{ + __u32 len = s.length(); + encode(len, bl); + bl.append(s); +} +inline void encode_destructively(bufferlist& s, bufferlist& bl) +{ + __u32 len = s.length(); + encode(len, bl); + bl.claim_append(s); +} +inline void decode(bufferlist& s, bufferlist::iterator& p) +{ + __u32 len; + decode(len, p); + s.clear(); + p.copy(len, s); +} + +inline void encode_nohead(const bufferlist& s, bufferlist& bl) +{ + bl.append(s); +} +inline void decode_nohead(int len, bufferlist& s, bufferlist::iterator& p) +{ + s.clear(); + p.copy(len, s); +} + + +// full bl decoder +template +inline void decode(T o, bufferlist& bl) +{ + bufferlist::iterator p = bl.begin(); + decode(o, p); + assert(p.end()); +} + + // ----------------------------- // STL container types @@ -422,97 +517,5 @@ inline void decode(__gnu_cxx::hash_set& m, bufferlist::iterator& p) } } -// string -inline void encode(const std::string& s, bufferlist& bl) -{ - __u32 len = s.length(); - encode(len, bl); - bl.append(s.data(), len); -} -inline void decode(std::string& s, bufferlist::iterator& p) -{ - __u32 len; - decode(len, p); - s.clear(); - p.copy(len, s); -} - -// const char* (encode only, string compatible) -inline void encode(const char *s, bufferlist& bl) -{ - __u32 len = strlen(s); - encode(len, bl); - bl.append(s, len); -} - - - -// ----------------------------- -// buffers - -// bufferptr (encapsulated) -inline void encode(const buffer::ptr& bp, bufferlist& bl) -{ - __u32 len = bp.length(); - encode(len, bl); - if (len) - bl.append(bp); -} -inline void decode(buffer::ptr& bp, bufferlist::iterator& p) -{ - __u32 len; - decode(len, p); - - bufferlist s; - p.copy(len, s); - - if (len) { - if (s.buffers().size() == 1) - bp = s.buffers().front(); - else - bp = buffer::copy(s.c_str(), s.length()); - } -} - -// bufferlist (encapsulated) -inline void encode(const bufferlist& s, bufferlist& bl) -{ - __u32 len = s.length(); - encode(len, bl); - bl.append(s); -} -inline void encode_destructively(bufferlist& s, bufferlist& bl) -{ - __u32 len = s.length(); - encode(len, bl); - bl.claim_append(s); -} -inline void decode(bufferlist& s, bufferlist::iterator& p) -{ - __u32 len; - decode(len, p); - s.clear(); - p.copy(len, s); -} - -inline void encode_nohead(const bufferlist& s, bufferlist& bl) -{ - bl.append(s); -} -inline void decode_nohead(int len, bufferlist& s, bufferlist::iterator& p) -{ - s.clear(); - p.copy(len, s); -} - - -// full bl decoder -template -inline void decode(T o, bufferlist& bl) -{ - bufferlist::iterator p = bl.begin(); - decode(o, p); - assert(p.end()); -} #endif -- 2.47.3