1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
17 #if defined(__linux__) || defined(__FreeBSD__)
23 # define _XOPEN_SOURCE 600
29 #if defined(__linux__) // For malloc(2).
38 # include <sys/mman.h>
46 #if __cplusplus >= 201703L
47 #include <string_view>
48 #endif // __cplusplus >= 201703L
51 #include <type_traits>
55 #include "buffer_fwd.h"
58 # include "include/ceph_assert.h"
63 #include "inline_memory.h"
65 #define CEPH_BUFFER_API
69 template <typename T
> class temporary_buffer
;
74 #endif // HAVE_SEASTAR
77 template<typename T
> class DencDumper
;
83 void operator()(T
*) {}
86 // This is not unique_ptr-like smart pointer! It just signalizes ownership
87 // but DOES NOT manage the resource. It WILL LEAK if not manually deleted.
88 // It's rather a replacement for raw pointer than any other smart one.
90 // Considered options:
91 // * unique_ptr with custom deleter implemented in .cc (would provide
92 // the non-zero-cost resource management),
93 // * GSL's owner<T*> (pretty neat but would impose an extra depedency),
94 // * unique_ptr with nop deleter,
95 // * raw pointer (doesn't embed ownership enforcement - std::move).
97 struct unique_leakable_ptr
: public std::unique_ptr
<T
, ceph::nop_delete
<T
>> {
98 using std::unique_ptr
<T
, ceph::nop_delete
<T
>>::unique_ptr
;
101 namespace buffer CEPH_BUFFER_API
{
102 inline namespace v15_2_0
{
108 struct error
: public std::exception
{
109 const char *what() const throw () override
;
111 struct bad_alloc
: public error
{
112 const char *what() const throw () override
;
114 struct end_of_buffer
: public error
{
115 const char *what() const throw () override
;
117 struct malformed_input
: public error
{
118 explicit malformed_input(const std::string
& w
) {
119 snprintf(buf
, sizeof(buf
), "buffer::malformed_input: %s", w
.c_str());
121 const char *what() const throw () override
;
125 struct error_code
: public malformed_input
{
126 explicit error_code(int error
);
131 /// count of cached crc hits (matching input)
132 int get_cached_crc();
133 /// count of cached crc hits (mismatching input, required adjustment)
134 int get_cached_crc_adjusted();
135 /// count of crc cache misses
136 int get_missed_crc();
137 /// enable/disable tracking of cached crcs
138 void track_cached_crc(bool b
);
141 * an abstract raw buffer. with a reference count.
146 class raw_posix_aligned
;
147 class raw_hack_aligned
;
149 class raw_claimed_char
;
150 class raw_unshareable
; // diagnostic, unshareable char buffer
152 class raw_claim_buffer
;
158 ceph::unique_leakable_ptr
<raw
> copy(const char *c
, unsigned len
);
159 ceph::unique_leakable_ptr
<raw
> create(unsigned len
);
160 ceph::unique_leakable_ptr
<raw
> create_in_mempool(unsigned len
, int mempool
);
161 ceph::unique_leakable_ptr
<raw
> claim_char(unsigned len
, char *buf
);
162 ceph::unique_leakable_ptr
<raw
> create_malloc(unsigned len
);
163 ceph::unique_leakable_ptr
<raw
> claim_malloc(unsigned len
, char *buf
);
164 ceph::unique_leakable_ptr
<raw
> create_static(unsigned len
, char *buf
);
165 ceph::unique_leakable_ptr
<raw
> create_aligned(unsigned len
, unsigned align
);
166 ceph::unique_leakable_ptr
<raw
> create_aligned_in_mempool(unsigned len
, unsigned align
, int mempool
);
167 ceph::unique_leakable_ptr
<raw
> create_page_aligned(unsigned len
);
168 ceph::unique_leakable_ptr
<raw
> create_small_page_aligned(unsigned len
);
169 ceph::unique_leakable_ptr
<raw
> claim_buffer(unsigned len
, char *buf
, deleter del
);
172 /// create a raw buffer to wrap seastar cpu-local memory, using foreign_ptr to
173 /// make it safe to share between cpus
174 ceph::unique_leakable_ptr
<buffer::raw
> create_foreign(seastar::temporary_buffer
<char>&& buf
);
175 /// create a raw buffer to wrap seastar cpu-local memory, without the safety
176 /// of foreign_ptr. the caller must otherwise guarantee that the buffer ptr is
177 /// destructed on this cpu
178 ceph::unique_leakable_ptr
<buffer::raw
> create(seastar::temporary_buffer
<char>&& buf
);
182 * a buffer pointer. references (a subsequence of) a raw buffer.
184 class CEPH_BUFFER_API ptr
{
193 template<bool is_const
>
194 class iterator_impl
{
195 const ptr
*bp
; ///< parent ptr
196 const char *start
; ///< starting pointer into bp->c_str()
197 const char *pos
; ///< pointer into bp->c_str()
198 const char *end_ptr
; ///< pointer to bp->end_c_str()
199 const bool deep
; ///< if true, do not allow shallow ptr copies
201 iterator_impl(typename
std::conditional
<is_const
, const ptr
*, ptr
*>::type p
,
202 size_t offset
, bool d
)
204 start(p
->c_str() + offset
),
206 end_ptr(p
->end_c_str()),
213 using pointer
= typename
std::conditional
<is_const
, const char*, char *>::type
;
214 pointer
get_pos_add(size_t n
) {
219 ptr
get_ptr(size_t len
) {
221 return buffer::copy(get_pos_add(len
), len
);
223 size_t off
= pos
- bp
->c_str();
225 return ptr(*bp
, off
, len
);
229 iterator_impl
& operator+=(size_t len
) {
232 throw end_of_buffer();
236 const char *get_pos() {
239 const char *get_end() {
243 size_t get_offset() {
248 return pos
== end_ptr
;
253 using const_iterator
= iterator_impl
<true>;
254 using iterator
= iterator_impl
<false>;
256 ptr() : _raw(nullptr), _off(0), _len(0) {}
257 ptr(ceph::unique_leakable_ptr
<raw
> r
);
258 // cppcheck-suppress noExplicitConstructor
260 ptr(const char *d
, unsigned l
);
262 ptr(ptr
&& p
) noexcept
;
263 ptr(const ptr
& p
, unsigned o
, unsigned l
);
264 ptr(const ptr
& p
, ceph::unique_leakable_ptr
<raw
> r
);
265 ptr
& operator= (const ptr
& p
);
266 ptr
& operator= (ptr
&& p
) noexcept
;
268 // BE CAREFUL: this destructor is called also for hypercombined ptr_node.
269 // After freeing underlying raw, `*this` can become inaccessible as well!
273 bool have_raw() const { return _raw
? true:false; }
275 ceph::unique_leakable_ptr
<raw
> clone();
276 void swap(ptr
& other
) noexcept
;
278 iterator
begin(size_t offset
=0) {
279 return iterator(this, offset
, false);
281 const_iterator
begin(size_t offset
=0) const {
282 return const_iterator(this, offset
, false);
284 const_iterator
cbegin() const {
287 const_iterator
begin_deep(size_t offset
=0) const {
288 return const_iterator(this, offset
, true);
292 bool is_aligned(unsigned align
) const {
293 return ((long)c_str() & (align
-1)) == 0;
295 bool is_page_aligned() const { return is_aligned(CEPH_PAGE_SIZE
); }
296 bool is_n_align_sized(unsigned align
) const
298 return (length() % align
) == 0;
300 bool is_n_page_sized() const { return is_n_align_sized(CEPH_PAGE_SIZE
); }
301 bool is_partial() const {
302 return have_raw() && (start() > 0 || end() < raw_length());
305 int get_mempool() const;
306 void reassign_to_mempool(int pool
);
307 void try_assign_to_mempool(int pool
);
310 const char *c_str() const;
312 const char *end_c_str() const;
314 unsigned length() const { return _len
; }
315 unsigned offset() const { return _off
; }
316 unsigned start() const { return _off
; }
317 unsigned end() const { return _off
+ _len
; }
318 unsigned unused_tail_length() const;
319 const char& operator[](unsigned n
) const;
320 char& operator[](unsigned n
);
322 const char *raw_c_str() const;
323 unsigned raw_length() const;
324 int raw_nref() const;
326 void copy_out(unsigned o
, unsigned l
, char *dest
) const;
328 unsigned wasted() const;
330 int cmp(const ptr
& o
) const;
331 bool is_zero() const;
334 void set_offset(unsigned o
) {
336 ceph_assert(raw_length() >= o
);
338 assert(raw_length() >= o
);
342 void set_length(unsigned l
) {
344 ceph_assert(raw_length() >= l
);
346 assert(raw_length() >= l
);
351 unsigned append(char c
);
352 unsigned append(const char *p
, unsigned l
);
353 #if __cplusplus >= 201703L
354 inline unsigned append(std::string_view s
) {
355 return append(s
.data(), s
.length());
357 #endif // __cplusplus >= 201703L
358 void copy_in(unsigned o
, unsigned l
, const char *src
, bool crc_reset
= true);
359 void zero(bool crc_reset
= true);
360 void zero(unsigned o
, unsigned l
, bool crc_reset
= true);
361 unsigned append_zeros(unsigned l
);
364 /// create a temporary_buffer, copying the ptr as its deleter
365 operator seastar::temporary_buffer
<char>() &;
366 /// convert to temporary_buffer, stealing the ptr as its deleter
367 operator seastar::temporary_buffer
<char>() &&;
368 #endif // HAVE_SEASTAR
374 mutable ptr_hook
* next
;
376 ptr_hook() = default;
377 ptr_hook(ptr_hook
* const next
)
382 class ptr_node
: public ptr_hook
, public ptr
{
385 ptr_node
* operator()(const ptr_node
& clone_this
);
388 void operator()(ptr_node
* const delete_this
) {
389 if (!dispose_if_hypercombined(delete_this
)) {
395 ~ptr_node() = default;
397 static std::unique_ptr
<ptr_node
, disposer
>
398 create(ceph::unique_leakable_ptr
<raw
> r
) {
399 return create_hypercombined(std::move(r
));
401 static std::unique_ptr
<ptr_node
, disposer
>
402 create(const unsigned l
) {
403 return create_hypercombined(buffer::create(l
));
405 template <class... Args
>
406 static std::unique_ptr
<ptr_node
, disposer
>
407 create(Args
&&... args
) {
408 return std::unique_ptr
<ptr_node
, disposer
>(
409 new ptr_node(std::forward
<Args
>(args
)...));
412 static ptr_node
* copy_hypercombined(const ptr_node
& copy_this
);
415 template <class... Args
>
416 ptr_node(Args
&&... args
) : ptr(std::forward
<Args
>(args
)...) {
418 ptr_node(const ptr_node
&) = default;
420 ptr
& operator= (const ptr
& p
) = delete;
421 ptr
& operator= (ptr
&& p
) noexcept
= delete;
422 ptr_node
& operator= (const ptr_node
& p
) = delete;
423 ptr_node
& operator= (ptr_node
&& p
) noexcept
= delete;
424 void swap(ptr
& other
) noexcept
= delete;
425 void swap(ptr_node
& other
) noexcept
= delete;
427 static bool dispose_if_hypercombined(ptr_node
* delete_this
);
428 static std::unique_ptr
<ptr_node
, disposer
> create_hypercombined(
429 ceph::unique_leakable_ptr
<raw
> r
);
432 * list - the useful bit!
435 class CEPH_BUFFER_API list
{
437 // this the very low-level implementation of singly linked list
438 // ceph::buffer::list is built on. We don't use intrusive slist
439 // of Boost (or any other 3rd party) to save extra dependencies
440 // in our public headers.
442 // _root.next can be thought as _head
448 class buffers_iterator
{
449 typename
std::conditional
<
450 std::is_const
<T
>::value
, const ptr_hook
*, ptr_hook
*>::type cur
;
451 template <class U
> friend class buffers_iterator
;
453 using value_type
= T
;
454 using reference
= typename
std::add_lvalue_reference
<T
>::type
;
455 using pointer
= typename
std::add_pointer
<T
>::type
;
456 using difference_type
= std::ptrdiff_t;
457 using iterator_category
= std::forward_iterator_tag
;
460 buffers_iterator(U
* const p
)
464 buffers_iterator(const buffers_iterator
<U
>& other
)
467 buffers_iterator() = default;
469 T
& operator*() const {
470 return *reinterpret_cast<T
*>(cur
);
472 T
* operator->() const {
473 return reinterpret_cast<T
*>(cur
);
476 buffers_iterator
& operator++() {
480 buffers_iterator
operator++(int) {
481 const auto temp(*this);
487 buffers_iterator
& operator=(buffers_iterator
<U
>& other
) {
492 bool operator==(const buffers_iterator
& rhs
) const {
493 return cur
== rhs
.cur
;
495 bool operator!=(const buffers_iterator
& rhs
) const {
496 return !(*this==rhs
);
499 using citer_t
= buffers_iterator
<typename
std::add_const
<T
>::type
>;
500 operator citer_t() const {
505 typedef buffers_iterator
<const ptr_node
> const_iterator
;
506 typedef buffers_iterator
<ptr_node
> iterator
;
508 typedef const ptr_node
& const_reference
;
509 typedef ptr_node
& reference
;
515 buffers_t(const buffers_t
&) = delete;
516 buffers_t(buffers_t
&& other
)
517 : _root(other
._root
.next
== &other
._root
? &_root
: other
._root
.next
),
518 _tail(other
._tail
== &other
._root
? &_root
: other
._tail
) {
519 other
._root
.next
= &other
._root
;
520 other
._tail
= &other
._root
;
522 _tail
->next
= &_root
;
524 buffers_t
& operator=(buffers_t
&& other
) {
525 if (&other
!= this) {
532 void push_back(reference item
) {
534 // this updates _root.next when called on empty
539 void push_front(reference item
) {
540 item
.next
= _root
.next
;
542 _tail
= _tail
== &_root
? &item
: _tail
;
546 iterator
erase_after(const_iterator it
) {
547 const auto* to_erase
= it
->next
;
549 it
->next
= to_erase
->next
;
550 _root
.next
= _root
.next
== to_erase
? to_erase
->next
: _root
.next
;
551 _tail
= _tail
== to_erase
? (ptr_hook
*)&*it
: _tail
;
555 void insert_after(const_iterator it
, reference item
) {
556 item
.next
= it
->next
;
558 _root
.next
= it
== end() ? &item
: _root
.next
;
559 _tail
= const_iterator(_tail
) == it
? &item
: _tail
;
562 void splice_back(buffers_t
& other
) {
567 other
._tail
->next
= &_root
;
568 // will update root.next if empty() == true
569 _tail
->next
= other
._root
.next
;
572 other
._root
.next
= &other
._root
;
573 other
._tail
= &other
._root
;
576 bool empty() const { return _tail
== &_root
; }
578 const_iterator
begin() const {
581 const_iterator
before_begin() const {
584 const_iterator
end() const {
590 iterator
before_begin() {
598 return reinterpret_cast<reference
>(*_root
.next
);
601 return reinterpret_cast<reference
>(*_tail
);
603 const_reference
front() const {
604 return reinterpret_cast<const_reference
>(*_root
.next
);
606 const_reference
back() const {
607 return reinterpret_cast<const_reference
>(*_tail
);
610 void clone_from(const buffers_t
& other
) {
612 for (auto& node
: other
) {
613 ptr_node
* clone
= ptr_node::cloner()(node
);
617 void clear_and_dispose() {
618 for (auto it
= begin(); it
!= end(); /* nop */) {
621 ptr_node::disposer()(&node
);
626 iterator
erase_after_and_dispose(iterator it
) {
627 auto* to_dispose
= &*std::next(it
);
628 auto ret
= erase_after(it
);
629 ptr_node::disposer()(to_dispose
);
633 void swap(buffers_t
& other
) {
634 const auto copy_root
= _root
;
636 other
._root
.next
== &other
._root
? &this->_root
: other
._root
.next
;
638 copy_root
.next
== &_root
? &other
._root
: copy_root
.next
;
640 const auto copy_tail
= _tail
;
641 _tail
= other
._tail
== &other
._root
? &this->_root
: other
._tail
;
642 other
._tail
= copy_tail
== &_root
? &other
._root
: copy_tail
;
644 _tail
->next
= &_root
;
645 other
._tail
->next
= &other
._root
;
655 // track bufferptr we can modify (especially ::append() to). Not all bptrs
656 // bufferlist holds have this trait -- if somebody ::push_back(const ptr&),
657 // he expects it won't change.
661 template <bool is_const
>
662 class CEPH_BUFFER_API iterator_impl
{
664 typedef typename
std::conditional
<is_const
,
667 typedef typename
std::conditional
<is_const
,
669 buffers_t
>::type list_t
;
670 typedef typename
std::conditional
<is_const
,
671 typename
buffers_t::const_iterator
,
672 typename
buffers_t::iterator
>::type list_iter_t
;
674 list_t
* ls
; // meh.. just here to avoid an extra pointer dereference..
676 unsigned off
; // in bl
677 unsigned p_off
; // in *p
678 friend class iterator_impl
<true>;
681 using iterator_category
= std::forward_iterator_tag
;
682 using value_type
= typename
std::conditional
<is_const
, const char, char>::type
;
683 using difference_type
= std::ptrdiff_t;
684 using pointer
= typename
std::add_pointer
<value_type
>::type
;
685 using reference
= typename
std::add_lvalue_reference
<value_type
>::type
;
687 // constructor. position.
689 : bl(0), ls(0), off(0), p_off(0) {}
690 iterator_impl(bl_t
*l
, unsigned o
=0);
691 iterator_impl(bl_t
*l
, unsigned o
, list_iter_t ip
, unsigned po
)
692 : bl(l
), ls(&bl
->_buffers
), p(ip
), off(o
), p_off(po
) {}
693 iterator_impl(const list::iterator
& i
);
695 /// get current iterator offset in buffer::list
696 unsigned get_off() const { return off
; }
698 /// get number of bytes remaining from iterator position to the end of the buffer::list
699 unsigned get_remaining() const { return bl
->length() - off
; }
701 /// true if iterator is at the end of the buffer::list
703 return p
== ls
->end();
704 //return off == bl->length();
706 void seek(unsigned o
);
707 char operator*() const;
708 iterator_impl
& operator+=(unsigned o
);
709 iterator_impl
& operator++();
710 ptr
get_current_ptr() const;
711 bool is_pointing_same_raw(const ptr
& other
) const;
713 bl_t
& get_bl() const { return *bl
; }
716 // note that these all _append_ to dest!
717 void copy(unsigned len
, char *dest
);
718 // deprecated, use copy_deep()
719 void copy(unsigned len
, ptr
&dest
) __attribute__((deprecated
));
720 void copy_deep(unsigned len
, ptr
&dest
);
721 void copy_shallow(unsigned len
, ptr
&dest
);
722 void copy(unsigned len
, list
&dest
);
723 void copy(unsigned len
, std::string
&dest
);
724 void copy_all(list
&dest
);
726 // get a pointer to the currenet iterator position, return the
727 // number of bytes we can read from that position (up to want),
728 // and advance the iterator by that amount.
729 size_t get_ptr_and_advance(size_t want
, const char **p
);
731 /// calculate crc from iterator position
732 uint32_t crc32c(size_t length
, uint32_t crc
);
734 friend bool operator==(const iterator_impl
& lhs
,
735 const iterator_impl
& rhs
) {
736 return &lhs
.get_bl() == &rhs
.get_bl() && lhs
.get_off() == rhs
.get_off();
738 friend bool operator!=(const iterator_impl
& lhs
,
739 const iterator_impl
& rhs
) {
740 return &lhs
.get_bl() != &rhs
.get_bl() || lhs
.get_off() != rhs
.get_off();
745 typedef iterator_impl
<true> const_iterator
;
747 class CEPH_BUFFER_API iterator
: public iterator_impl
<false> {
749 iterator() = default;
750 iterator(bl_t
*l
, unsigned o
=0);
751 iterator(bl_t
*l
, unsigned o
, list_iter_t ip
, unsigned po
);
753 void copy_in(unsigned len
, const char *src
, bool crc_reset
= true);
754 void copy_in(unsigned len
, const list
& otherl
);
763 class contiguous_appender
{
764 ceph::bufferlist
& bl
;
765 ceph::bufferlist::reserve_t space
;
769 /// running count of bytes appended that are not reflected by @pos
770 size_t out_of_band_offset
= 0;
772 contiguous_appender(bufferlist
& bl
, size_t len
, bool d
)
774 space(bl
.obtain_contiguous_space(len
)),
779 void flush_and_continue() {
780 const size_t l
= pos
- space
.bp_data
;
787 template<typename Type
> friend class ::DencDumper
;
790 ~contiguous_appender() {
791 flush_and_continue();
794 size_t get_out_of_band_offset() const {
795 return out_of_band_offset
;
797 void append(const char* __restrict__ p
, size_t l
) {
798 maybe_inline_memcpy(pos
, p
, l
, 16);
801 char *get_pos_add(size_t len
) {
806 char *get_pos() const {
810 void append(const bufferptr
& p
) {
811 const auto plen
= p
.length();
816 append(p
.c_str(), plen
);
818 flush_and_continue();
820 space
= bl
.obtain_contiguous_space(0);
821 out_of_band_offset
+= plen
;
824 void append(const bufferlist
& l
) {
826 for (const auto &p
: l
._buffers
) {
827 append(p
.c_str(), p
.length());
830 flush_and_continue();
832 space
= bl
.obtain_contiguous_space(0);
833 out_of_band_offset
+= l
.length();
837 size_t get_logical_offset() const {
838 return out_of_band_offset
+ (pos
- space
.bp_data
);
842 contiguous_appender
get_contiguous_appender(size_t len
, bool deep
=false) {
843 return contiguous_appender(*this, len
, deep
);
846 class contiguous_filler
{
850 contiguous_filler(char* const pos
) : pos(pos
) {}
853 void advance(const unsigned len
) {
856 void copy_in(const unsigned len
, const char* const src
) {
857 memcpy(pos
, src
, len
);
864 // The contiguous_filler is supposed to be not costlier than a single
865 // pointer. Keep it dumb, please.
866 static_assert(sizeof(contiguous_filler
) == sizeof(char*),
867 "contiguous_filler should be no costlier than pointer");
869 class page_aligned_appender
{
875 page_aligned_appender(list
*l
, unsigned min_pages
)
877 min_alloc(min_pages
* CEPH_PAGE_SIZE
),
878 pos(nullptr), end(nullptr) {}
883 ~page_aligned_appender() {
888 if (pos
&& pos
!= buffer
.c_str()) {
889 size_t len
= pos
- buffer
.c_str();
890 pbl
->append(buffer
, 0, len
);
891 buffer
.set_length(buffer
.length() - len
);
892 buffer
.set_offset(buffer
.offset() + len
);
896 void append(const char *buf
, size_t len
) {
899 size_t alloc
= (len
+ CEPH_PAGE_SIZE
- 1) & CEPH_PAGE_MASK
;
900 if (alloc
< min_alloc
) {
903 buffer
= create_page_aligned(alloc
);
904 pos
= buffer
.c_str();
905 end
= buffer
.end_c_str();
908 if (l
> (size_t)(end
- pos
)) {
916 pbl
->append(buffer
, 0, buffer
.length());
923 page_aligned_appender
get_page_aligned_appender(unsigned min_pages
=1) {
924 return page_aligned_appender(this, min_pages
);
928 // always_empty_bptr has no underlying raw but its _len is always 0.
929 // This is useful for e.g. get_append_buffer_unused_tail_length() as
930 // it allows to avoid conditionals on hot paths.
931 static ptr always_empty_bptr
;
932 ptr_node
& refill_append_space(const unsigned len
);
937 : _carriage(&always_empty_bptr
),
941 // cppcheck-suppress noExplicitConstructor
942 // cppcheck-suppress noExplicitConstructor
943 list(unsigned prealloc
)
944 : _carriage(&always_empty_bptr
),
950 list(const list
& other
)
951 : _carriage(&always_empty_bptr
),
954 _buffers
.clone_from(other
._buffers
);
957 list(list
&& other
) noexcept
958 : _buffers(std::move(other
._buffers
)),
959 _carriage(other
._carriage
),
966 _buffers
.clear_and_dispose();
969 list
& operator= (const list
& other
) {
970 if (this != &other
) {
971 _carriage
= &always_empty_bptr
;
972 _buffers
.clone_from(other
._buffers
);
978 list
& operator= (list
&& other
) noexcept
{
979 _buffers
= std::move(other
._buffers
);
980 _carriage
= other
._carriage
;
987 uint64_t get_wasted_space() const;
988 unsigned get_num_buffers() const { return _num
; }
989 const ptr_node
& front() const { return _buffers
.front(); }
990 const ptr_node
& back() const { return _buffers
.back(); }
992 int get_mempool() const;
993 void reassign_to_mempool(int pool
);
994 void try_assign_to_mempool(int pool
);
996 size_t get_append_buffer_unused_tail_length() const {
997 return _carriage
->unused_tail_length();
1000 const buffers_t
& buffers() const { return _buffers
; }
1001 void swap(list
& other
) noexcept
;
1002 unsigned length() const {
1004 // DEBUG: verify _len
1006 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1007 it
!= _buffers
.end();
1009 len
+= (*it
).length();
1012 ceph_assert(len
== _len
);
1014 assert(len
== _len
);
1020 bool contents_equal(const buffer::list
& other
) const;
1021 bool contents_equal(const void* other
, size_t length
) const;
1023 bool is_provided_buffer(const char *dst
) const;
1024 bool is_aligned(unsigned align
) const;
1025 bool is_page_aligned() const;
1026 bool is_n_align_sized(unsigned align
) const;
1027 bool is_n_page_sized() const;
1028 bool is_aligned_size_and_memory(unsigned align_size
,
1029 unsigned align_memory
) const;
1031 bool is_zero() const;
1034 void clear() noexcept
{
1035 _carriage
= &always_empty_bptr
;
1036 _buffers
.clear_and_dispose();
1040 void push_back(const ptr
& bp
) {
1041 if (bp
.length() == 0)
1043 _buffers
.push_back(*ptr_node::create(bp
).release());
1044 _len
+= bp
.length();
1047 void push_back(ptr
&& bp
) {
1048 if (bp
.length() == 0)
1050 _len
+= bp
.length();
1052 _buffers
.push_back(*ptr_node::create(std::move(bp
)).release());
1053 _carriage
= &always_empty_bptr
;
1055 void push_back(const ptr_node
&) = delete;
1056 void push_back(ptr_node
&) = delete;
1057 void push_back(ptr_node
&&) = delete;
1058 void push_back(std::unique_ptr
<ptr_node
, ptr_node::disposer
> bp
) {
1059 if (bp
->length() == 0)
1061 _carriage
= bp
.get();
1062 _len
+= bp
->length();
1064 _buffers
.push_back(*bp
.release());
1066 void push_back(raw
* const r
) = delete;
1067 void push_back(ceph::unique_leakable_ptr
<raw
> r
) {
1068 _buffers
.push_back(*ptr_node::create(std::move(r
)).release());
1069 _carriage
= &_buffers
.back();
1070 _len
+= _buffers
.back().length();
1075 void zero(unsigned o
, unsigned l
);
1077 bool is_contiguous() const;
1079 void rebuild(std::unique_ptr
<ptr_node
, ptr_node::disposer
> nb
);
1080 bool rebuild_aligned(unsigned align
);
1081 // max_buffers = 0 mean don't care _buffers.size(), other
1082 // must make _buffers.size() <= max_buffers after rebuilding.
1083 bool rebuild_aligned_size_and_memory(unsigned align_size
,
1084 unsigned align_memory
,
1085 unsigned max_buffers
= 0);
1086 bool rebuild_page_aligned();
1088 void reserve(size_t prealloc
);
1090 void claim(list
& bl
);
1091 void claim_append(list
& bl
);
1092 // only for bl is bufferlist::page_aligned_appender
1093 void claim_append_piecewise(list
& bl
);
1095 // copy with explicit volatile-sharing semantics
1096 void share(const list
& bl
)
1100 for (const auto& bp
: bl
._buffers
) {
1101 _buffers
.push_back(*ptr_node::create(bp
).release());
1109 /// convert the bufferlist into a network packet
1110 operator seastar::net::packet() &&;
1113 iterator
begin(size_t offset
=0) {
1114 return iterator(this, offset
);
1117 return iterator(this, _len
, _buffers
.end(), 0);
1120 const_iterator
begin(size_t offset
=0) const {
1121 return const_iterator(this, offset
);
1123 const_iterator
cbegin(size_t offset
=0) const {
1124 return begin(offset
);
1126 const_iterator
end() const {
1127 return const_iterator(this, _len
, _buffers
.end(), 0);
1130 void append(char c
);
1131 void append(const char *data
, unsigned len
);
1132 void append(std::string s
) {
1133 append(s
.data(), s
.length());
1135 #if __cplusplus >= 201703L
1136 // To forcibly disambiguate between string and string_view in the
1138 template<std::size_t N
>
1139 void append(const char (&s
)[N
]) {
1142 void append(const char* s
) {
1143 append(s
, strlen(s
));
1145 void append(std::string_view s
) {
1146 append(s
.data(), s
.length());
1148 #endif // __cplusplus >= 201703L
1149 void append(const ptr
& bp
);
1150 void append(ptr
&& bp
);
1151 void append(const ptr
& bp
, unsigned off
, unsigned len
);
1152 void append(const list
& bl
);
1153 void append(std::istream
& in
);
1154 contiguous_filler
append_hole(unsigned len
);
1155 void append_zero(unsigned len
);
1156 void prepend_zero(unsigned len
);
1158 reserve_t
obtain_contiguous_space(unsigned len
);
1163 const char& operator[](unsigned n
) const;
1165 std::string
to_str() const;
1167 void substr_of(const list
& other
, unsigned off
, unsigned len
);
1170 void splice(unsigned off
, unsigned len
, list
*claim_by
=0 /*, bufferlist& replace_with */);
1171 void write(int off
, int len
, std::ostream
& out
) const;
1173 void encode_base64(list
& o
);
1174 void decode_base64(list
& o
);
1176 void write_stream(std::ostream
&out
) const;
1177 void hexdump(std::ostream
&out
, bool trailing_newline
= true) const;
1178 ssize_t
pread_file(const char *fn
, uint64_t off
, uint64_t len
, std::string
*error
);
1179 int read_file(const char *fn
, std::string
*error
);
1180 ssize_t
read_fd(int fd
, size_t len
);
1181 int write_file(const char *fn
, int mode
=0644);
1182 int write_fd(int fd
) const;
1183 int write_fd(int fd
, uint64_t offset
) const;
1184 template<typename VectorT
>
1185 void prepare_iov(VectorT
*piov
) const {
1187 ceph_assert(_num
<= IOV_MAX
);
1189 assert(_num
<= IOV_MAX
);
1193 for (auto& p
: _buffers
) {
1194 (*piov
)[n
].iov_base
= (void *)p
.c_str();
1195 (*piov
)[n
].iov_len
= p
.length();
1199 uint32_t crc32c(uint32_t crc
) const;
1200 void invalidate_crc();
1202 // These functions return a bufferlist with a pointer to a single
1203 // static buffer. They /must/ not outlive the memory they
1205 static list
static_from_mem(char* c
, size_t l
);
1206 static list
static_from_cstring(char* c
);
1207 static list
static_from_string(std::string
& s
);
1210 } // inline namespace v15_2_0
1213 * efficient hash of one or more bufferlists
1221 // cppcheck-suppress noExplicitConstructor
1222 hash(uint32_t init
) : crc(init
) { }
1224 void update(const buffer::list
& bl
) {
1225 crc
= bl
.crc32c(crc
);
1233 inline bool operator>(bufferlist
& l
, bufferlist
& r
) {
1234 for (unsigned p
= 0; ; p
++) {
1235 if (l
.length() > p
&& r
.length() == p
) return true;
1236 if (l
.length() == p
) return false;
1237 if (l
[p
] > r
[p
]) return true;
1238 if (l
[p
] < r
[p
]) return false;
1241 inline bool operator>=(bufferlist
& l
, bufferlist
& r
) {
1242 for (unsigned p
= 0; ; p
++) {
1243 if (l
.length() > p
&& r
.length() == p
) return true;
1244 if (r
.length() == p
&& l
.length() == p
) return true;
1245 if (l
.length() == p
&& r
.length() > p
) return false;
1246 if (l
[p
] > r
[p
]) return true;
1247 if (l
[p
] < r
[p
]) return false;
1251 inline bool operator==(const bufferlist
&l
, const bufferlist
&r
) {
1252 if (l
.length() != r
.length())
1254 for (unsigned p
= 0; p
< l
.length(); p
++) {
1260 inline bool operator<(bufferlist
& l
, bufferlist
& r
) {
1263 inline bool operator<=(bufferlist
& l
, bufferlist
& r
) {
1268 std::ostream
& operator<<(std::ostream
& out
, const buffer::ptr
& bp
);
1270 std::ostream
& operator<<(std::ostream
& out
, const buffer::raw
&r
);
1272 std::ostream
& operator<<(std::ostream
& out
, const buffer::list
& bl
);
1274 std::ostream
& operator<<(std::ostream
& out
, const buffer::error
& e
);
1276 inline bufferhash
& operator<<(bufferhash
& l
, const bufferlist
&r
) {
1281 } // namespace buffer