]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
add size() member function to buffer::list
authorJesse F. Williamson <jfw@ibm.com>
Mon, 9 Feb 2026 18:12:54 +0000 (10:12 -0800)
committerJesse F. Williamson <jfw@ibm.com>
Wed, 18 Feb 2026 19:15:01 +0000 (11:15 -0800)
Mostly to allow use with std::size().

Signed-off-by: Jesse F. Williamson <jfw@ibm.com>
src/include/buffer.h
src/test/bufferlist.cc

index 1f1604e43bceb4d6e341ceaa156010ed6fa7bb49..517b76bfdfacd524822fc4ab5897d66ae731e7be 100644 (file)
@@ -1006,6 +1006,7 @@ struct error_code;
     const buffers_t& buffers() const { return _buffers; }
     buffers_t& mut_buffers() { return _buffers; }
     void swap(list& other) noexcept;
+
     unsigned length() const {
 #if 0
       // DEBUG: verify _len
@@ -1023,6 +1024,11 @@ struct error_code;
 #endif
       return _len;
     }
+   
+    // Allow use with std::size(); like std::string, this is the same as length(): 
+    unsigned size() const {
+      return length();
+    }
 
     bool contents_equal(const buffer::list& other) const;
     bool contents_equal(const void* other, size_t length) const;
index ba83f532934437898ccab65fa5c553164b433728..3d8b3f81822a5ca5a48b8b831090675aa1b07d34 100644 (file)
@@ -2316,9 +2316,11 @@ TEST(BufferList, append_zero) {
   bl.append('A');
   EXPECT_EQ((unsigned)1, bl.get_num_buffers());
   EXPECT_EQ((unsigned)1, bl.length());
+  EXPECT_EQ(bl.size(), bl.length());
   bl.append_zero(1);
   EXPECT_EQ((unsigned)1, bl.get_num_buffers());
   EXPECT_EQ((unsigned)2, bl.length());
+  EXPECT_EQ(bl.size(), bl.length());
   EXPECT_EQ('\0', bl[1]);
 }
 
@@ -2407,8 +2409,10 @@ TEST(BufferList, splice) {
   bl.splice(4, 4, &other);
   EXPECT_EQ((unsigned)3, other.get_num_buffers());
   EXPECT_EQ((unsigned)5, other.length());
+  EXPECT_EQ(other.size(), other.length());
   EXPECT_EQ(0, ::memcmp("XEFGH", other.c_str(), other.length()));
   EXPECT_EQ((unsigned)8, bl.length());
+  EXPECT_EQ(bl.size(), bl.length());
   {
     bufferlist tmp(bl);
     EXPECT_EQ(0, ::memcmp("ABCDIJKL", tmp.c_str(), tmp.length()));
@@ -2416,6 +2420,7 @@ TEST(BufferList, splice) {
 
   bl.splice(4, 4);
   EXPECT_EQ((unsigned)4, bl.length());
+  EXPECT_EQ(bl.size(), bl.length());
   EXPECT_EQ(0, ::memcmp("ABCD", bl.c_str(), bl.length()));
 
   {
@@ -2428,6 +2433,9 @@ TEST(BufferList, splice) {
     bl.splice(10, 4, &other);
     EXPECT_EQ((unsigned)11, bl.length());
     EXPECT_EQ(0, ::memcmp("fghi", other.c_str(), other.length()));
+
+    EXPECT_EQ(bl.size(), bl.length());
+    EXPECT_EQ(other.size(), other.length());
   }
 }
 
@@ -2507,6 +2515,7 @@ TEST(BufferList, read_fd) {
   EXPECT_EQ(len, (unsigned)bl.read_fd(fd, len));
   //EXPECT_EQ(CEPH_BUFFER_APPEND_SIZE - len, bl.front().unused_tail_length());
   EXPECT_EQ(len, bl.length());
+  EXPECT_EQ(bl.size(), bl.length());
   ::close(fd);
   ::unlink(FILENAME);
 }
@@ -2904,6 +2913,8 @@ TEST(BufferList, InternalCarriage) {
     EXPECT_EQ(bl_with_foo.length(), 3u);
     EXPECT_EQ(bl_with_foo.get_num_buffers(), 1u);
 
+    EXPECT_EQ(bl_with_foo.size(), bl_with_foo.length());
+
     bl.append(bl_with_foo);
     EXPECT_EQ(bl.get_num_buffers(), 2u);
   }
@@ -2940,8 +2951,10 @@ TEST(BufferList, ContiguousAppender) {
     denc(int64_t(24), ap);
     EXPECT_EQ(bl.get_num_buffers(), 3u);
     EXPECT_EQ(bl.length(), sizeof(int64_t) + 3u);
+    EXPECT_EQ(bl.size(), bl.length());
   }
   EXPECT_EQ(bl.length(), 2u * sizeof(int64_t) + 3u);
+  EXPECT_EQ(bl.size(), bl.length());
 }
 
 TEST(BufferList, TestPtrAppend) {
@@ -2997,6 +3010,7 @@ TEST(BufferList, TestCopyAll) {
   bufferlist::iterator i = bl.begin();
   bufferlist bl2;
   i.copy_all(bl2);
+  ASSERT_EQ(bl2.size(), BIG_SZ);
   ASSERT_EQ(bl2.length(), BIG_SZ);
   std::shared_ptr <unsigned char> big2(
       (unsigned char*)malloc(BIG_SZ), free);