]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
buffer: add iterator copy_shallow() to ptr
authorSage Weil <sage@redhat.com>
Mon, 19 Sep 2016 00:10:17 +0000 (19:10 -0500)
committerSage Weil <sage@redhat.com>
Sun, 16 Oct 2016 14:32:49 +0000 (10:32 -0400)
If the length resides within the bufferlist's current ptr, return a ptr
to the same buffer.

Signed-off-by: Sage Weil <sage@redhat.com>
src/common/buffer.cc
src/include/buffer.h
src/test/bufferlist.cc

index c3ce4f199403fb2476a368e2c703a51736972b69..59f0fde44dd3ac6516cd9b57ba96b3024f2126ac 100644 (file)
@@ -1157,6 +1157,22 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER;
     dest = create(len);
     copy(len, dest.c_str());
   }
+  template<bool is_const>
+  void buffer::list::iterator_impl<is_const>::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<bool is_const>
   void buffer::list::iterator_impl<is_const>::copy(unsigned len, list &dest)
index 6a23feca730ab1994415b0af10828b524b92b098..e900fc7ca2655924d0a28272641b0800387663bb 100644 (file)
@@ -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);
index 63f174358c3b2d018799a5b243ad020ce102f2df..fc7627aeded43486530a476f90d5e587ca3ef83b 100644 (file)
@@ -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)
   //
   {