From: Adam C. Emerson Date: Mon, 1 May 2017 20:59:36 +0000 (-0400) Subject: buffer: Make the use of static areas more convenient X-Git-Tag: v12.1.0~10^2~88^2~5 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=8416f1c569a2b8ac7a25fc43f7e9e955b49bfd62;p=ceph.git buffer: Make the use of static areas more convenient Make three wrapper functions to tidy up the process of making a bufferlist holding a single static buffer. The lack of any decent handling of const in buffer::list makes me wax wroth, but it's a bit much to fix right now. Signed-off-by: Adam C. Emerson --- diff --git a/src/common/buffer.cc b/src/common/buffer.cc index 77dcc685caaa..5d8dcf5c27d7 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -2498,6 +2498,25 @@ void buffer::list::hexdump(std::ostream &out, bool trailing_newline) const out.flags(original_flags); } + +buffer::list buffer::list::static_from_mem(char* c, size_t l) { + list bl; + bl.push_back(ptr(create_static(l, c))); + return bl; +} + +buffer::list buffer::list::static_from_cstring(char* c) { + return static_from_mem(c, std::strlen(c)); +} + +buffer::list buffer::list::static_from_string(string& s) { + // C++14 just has string::data return a char* from a non-const + // string. + return static_from_mem(const_cast(s.data()), s.length()); + // But the way buffer::list mostly doesn't work in a sane way with + // const makes me generally sad. +} + std::ostream& buffer::operator<<(std::ostream& out, const buffer::raw &r) { return out << "buffer::raw(" << (void*)r.data << " len " << r.len << " nref " << r.nref.read() << ")"; } diff --git a/src/include/buffer.h b/src/include/buffer.h index d8f8999b6ce9..177e95f8a875 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -883,6 +883,13 @@ namespace buffer CEPH_BUFFER_API { } uint32_t crc32c(uint32_t crc) const; void invalidate_crc(); + + // These functions return a bufferlist with a pointer to a single + // static buffer. They /must/ not outlive the memory they + // reference. + static list static_from_mem(char* c, size_t l); + static list static_from_cstring(char* c); + static list static_from_string(std::string& s); }; /*