From: Haomai Wang Date: Wed, 27 Jan 2016 14:53:03 +0000 (+0800) Subject: buffer: add ptr move construct X-Git-Tag: v10.0.4~61^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=dd4e6e18d6f9420fa1f0a716b03a7ab501050fb6;p=ceph.git buffer: add ptr move construct Signed-off-by: Haomai Wang --- diff --git a/src/common/buffer.cc b/src/common/buffer.cc index cbb9549c3e34..f12771493d06 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -708,6 +708,11 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; bdout << "ptr " << this << " get " << _raw << bendl; } } + buffer::ptr::ptr(ptr&& p) : _raw(p._raw), _off(p._off), _len(p._len) + { + p._raw = nullptr; + p._off = p._len = 0; + } buffer::ptr::ptr(const ptr& p, unsigned o, unsigned l) : _raw(p._raw), _off(p._off + o), _len(l) { @@ -1583,6 +1588,12 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; push_back(bp); } + void buffer::list::append(ptr&& bp) + { + if (bp.length()) + push_back(std::move(bp)); + } + void buffer::list::append(const ptr& bp, unsigned off, unsigned len) { assert(len+off <= bp.length()); @@ -1597,8 +1608,7 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; } } // add new item to list - ptr tempbp(bp, off, len); - push_back(tempbp); + push_back(ptr(bp, off, len)); } void buffer::list::append(const list& bl) @@ -1625,7 +1635,7 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; { ptr bp(len); bp.zero(); - append(bp); + append(std::move(bp)); } @@ -1834,7 +1844,7 @@ void buffer::list::encode_base64(buffer::list& o) bufferptr bp(length() * 4 / 3 + 3); int l = ceph_armor(bp.c_str(), bp.c_str() + bp.length(), c_str(), c_str() + length()); bp.set_length(l); - o.push_back(bp); + o.push_back(std::move(bp)); } void buffer::list::decode_base64(buffer::list& e) @@ -1849,7 +1859,7 @@ void buffer::list::decode_base64(buffer::list& e) } assert(l <= (int)bp.length()); bp.set_length(l); - push_back(bp); + push_back(std::move(bp)); } @@ -1903,7 +1913,7 @@ ssize_t buffer::list::read_fd(int fd, size_t len) ssize_t ret = safe_read(fd, (void*)bp.c_str(), len); if (ret >= 0) { bp.set_length(ret); - append(bp); + append(std::move(bp)); } return ret; } @@ -1912,8 +1922,7 @@ int buffer::list::read_fd_zero_copy(int fd, size_t len) { #ifdef CEPH_HAVE_SPLICE try { - bufferptr bp = buffer::create_zero_copy(len, fd, NULL); - append(bp); + append(buffer::create_zero_copy(len, fd, NULL)); } catch (buffer::error_code &e) { return e.code; } catch (buffer::malformed_input &e) { diff --git a/src/include/buffer.h b/src/include/buffer.h index 9bbe79d039ac..cf3198652704 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -174,6 +174,7 @@ namespace buffer CEPH_BUFFER_API { ptr(unsigned l); ptr(const char *d, unsigned l); ptr(const ptr& p); + ptr(ptr&& p); ptr(const ptr& p, unsigned o, unsigned l); ptr& operator= (const ptr& p); ~ptr() { @@ -417,9 +418,14 @@ namespace buffer CEPH_BUFFER_API { _buffers.push_front(bp); _len += bp.length(); } + void push_front(ptr&& bp) { + if (bp.length() == 0) + return; + _len += bp.length(); + _buffers.push_front(std::move(bp)); + } void push_front(raw *r) { - ptr bp(r); - push_front(bp); + push_front(ptr(r)); } void push_back(const ptr& bp) { if (bp.length() == 0) @@ -427,9 +433,14 @@ namespace buffer CEPH_BUFFER_API { _buffers.push_back(bp); _len += bp.length(); } + void push_back(ptr&& bp) { + if (bp.length() == 0) + return; + _len += bp.length(); + _buffers.push_back(std::move(bp)); + } void push_back(raw *r) { - ptr bp(r); - push_back(bp); + push_back(ptr(r)); } void zero(); @@ -500,6 +511,7 @@ namespace buffer CEPH_BUFFER_API { append(s.data(), s.length()); } void append(const ptr& bp); + void append(ptr&& bp); void append(const ptr& bp, unsigned off, unsigned len); void append(const list& bl); void append(std::istream& in);