return _buffers.front().c_str(); // good, we're already contiguous.
}
+ char *buffer::list::get_contiguous(unsigned orig_off, unsigned len)
+ {
+ if (orig_off + len > length())
+ throw end_of_buffer();
+
+ unsigned off = orig_off;
+ std::list<ptr>::iterator curbuf = _buffers.begin();
+ while (off > 0 && off >= curbuf->length()) {
+ off -= curbuf->length();
+ ++curbuf;
+ }
+
+ if (off + len > curbuf->length()) {
+ // FIXME we'll just rebuild the whole list for now.
+ rebuild();
+ return c_str() + orig_off;
+ }
+
+ return curbuf->c_str() + off;
+ }
+
void buffer::list::substr_of(const list& other, unsigned off, unsigned len)
{
if (off + len > other.length())
throw end_of_buffer();
clear();
-
+
// skip off
std::list<ptr>::const_iterator curbuf = other._buffers.begin();
while (off > 0 &&
char *c_str();
void substr_of(const list& other, unsigned off, unsigned len);
+ /// return a pointer to a contiguous extent of the buffer,
+ /// reallocating as needed
+ char *get_contiguous(unsigned off, ///< offset
+ unsigned len); ///< length
+
// funky modifer
void splice(unsigned off, unsigned len, list *claim_by=0 /*, bufferlist& replace_with */);
void write(int off, int len, std::ostream& out) const;
ASSERT_EQ((unsigned)1, bl.buffers().size());
}
+TEST(BufferList, get_contiguous) {
+ bufferptr a("foobarbaz", 9);
+ bufferptr b("123456789", 9);
+ bufferptr c("ABCDEFGHI", 9);
+ bufferlist bl;
+ bl.append(a);
+ bl.append(b);
+ bl.append(c);
+ ASSERT_EQ(3, bl.buffers().size());
+ ASSERT_EQ(0, memcmp("bar", bl.get_contiguous(3, 3), 3));
+ ASSERT_EQ(0, memcmp("456", bl.get_contiguous(12, 3), 3));
+ ASSERT_EQ(0, memcmp("ABC", bl.get_contiguous(18, 3), 3));
+ ASSERT_EQ(3, bl.buffers().size());
+ ASSERT_EQ(0, memcmp("789ABC", bl.get_contiguous(15, 6), 6));
+ ASSERT_LT(bl.buffers().size(), 3);
+}
+
TEST(BufferList, swap) {
bufferlist b1;
b1.append('A');