From: Sage Weil Date: Mon, 19 Sep 2016 00:10:17 +0000 (-0500) Subject: buffer: add iterator copy_shallow() to ptr X-Git-Tag: v11.1.0~617^2~25 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=14cad5655dceb73eaf89133a5f39667f2aa8c202;p=ceph-ci.git buffer: add iterator copy_shallow() to ptr If the length resides within the bufferlist's current ptr, return a ptr to the same buffer. Signed-off-by: Sage Weil --- diff --git a/src/common/buffer.cc b/src/common/buffer.cc index c3ce4f19940..59f0fde44dd 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -1157,6 +1157,22 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; dest = create(len); copy(len, dest.c_str()); } + template + void buffer::list::iterator_impl::copy_shallow(unsigned len, + ptr &dest) + { + if (p == ls->end()) + throw end_of_buffer(); + assert(p->length() > 0); + unsigned howmuch = p->length() - p_off; + if (howmuch < len) { + dest = create(len); + copy(len, dest.c_str()); + } else { + dest = ptr(*p, p_off, len); + advance(len); + } + } template void buffer::list::iterator_impl::copy(unsigned len, list &dest) diff --git a/src/include/buffer.h b/src/include/buffer.h index 6a23feca730..e900fc7ca26 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -321,6 +321,7 @@ namespace buffer CEPH_BUFFER_API { // note that these all _append_ to dest! void copy(unsigned len, char *dest); void copy_deep(unsigned len, ptr &dest); + void copy_shallow(unsigned len, ptr &dest); void copy(unsigned len, list &dest); void copy(unsigned len, std::string &dest); void copy_all(list &dest); @@ -361,6 +362,7 @@ namespace buffer CEPH_BUFFER_API { // copy data out void copy(unsigned len, char *dest); void copy_deep(unsigned len, ptr &dest); + void copy_shallow(unsigned len, ptr &dest); void copy(unsigned len, list &dest); void copy(unsigned len, std::string &dest); void copy_all(list &dest); diff --git a/src/test/bufferlist.cc b/src/test/bufferlist.cc index 63f174358c3..fc7627aeded 100644 --- a/src/test/bufferlist.cc +++ b/src/test/bufferlist.cc @@ -1227,6 +1227,17 @@ TEST(BufferListIterator, copy) { EXPECT_EQ('B', ptr[1]); } // + // void buffer::list::iterator::copy_shallow(unsigned len, ptr &dest) + // + { + bufferptr ptr; + bufferlist::iterator i(&bl); + i.copy_shallow(2, ptr); + EXPECT_EQ((unsigned)2, ptr.length()); + EXPECT_EQ('A', ptr[0]); + EXPECT_EQ('B', ptr[1]); + } + // // void buffer::list::iterator::copy(unsigned len, list &dest) // {