From: Radoslaw Zarzynski Date: Mon, 22 Oct 2018 16:22:52 +0000 (+0200) Subject: common: introduce obtain_contiguous_space() to bufferlist. X-Git-Tag: v14.1.0~222^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5b905b836477a4df5d2b362374f9941e5ec2300a;p=ceph.git common: introduce obtain_contiguous_space() to bufferlist. Signed-off-by: Radoslaw Zarzynski --- diff --git a/src/common/buffer.cc b/src/common/buffer.cc index 6a79d2431da..3c85f749243 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -1453,6 +1453,34 @@ static ceph::spinlock debug_lock; } } + buffer::list::reserve_t buffer::list::obtain_contiguous_space( + const unsigned len) + { + // note: if len < the normal append_buffer size it *might* + // be better to allocate a normal-sized append_buffer and + // use part of it. however, that optimizes for the case of + // old-style types including new-style types. and in most + // such cases, this won't be the very first thing encoded to + // the list, so append_buffer will already be allocated. + // OTOH if everything is new-style, we *should* allocate + // only what we need and conserve memory. + if (unlikely(get_append_buffer_unused_tail_length() < len)) { + auto new_back = \ + buffer::ptr_node::create(buffer::create(len)).release(); + new_back->set_length(0); // unused, so far. + _buffers.push_back(*new_back); + _carriage = new_back; + return { new_back->c_str(), &new_back->_len, &_len }; + } else { + if (unlikely(_carriage != &_buffers.back())) { + auto bptr = ptr_node::create(*_carriage, _carriage->length(), 0); + _carriage = bptr.get(); + _buffers.push_back(*bptr.release()); + } + return { _carriage->end_c_str(), &_carriage->_len, &_len }; + } + } + void buffer::list::append(const ptr& bp) { push_back(bp); diff --git a/src/include/buffer.h b/src/include/buffer.h index 334a594ab23..15ad5a025d9 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -762,6 +762,12 @@ namespace buffer CEPH_BUFFER_API { void copy_in(unsigned len, const list& otherl); }; + struct reserve_t { + char* bp_data; + unsigned* bp_len; + unsigned* bl_len; + }; + class contiguous_appender { bufferlist *pbl; char *pos; @@ -1209,6 +1215,8 @@ namespace buffer CEPH_BUFFER_API { void append_zero(unsigned len); void prepend_zero(unsigned len); + reserve_t obtain_contiguous_space(unsigned len); + /* * get a char */