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.
15 #include "include/compat.h"
16 #include "include/mempool.h"
18 #include "common/environment.h"
19 #include "common/errno.h"
20 #include "common/safe_io.h"
21 #include "common/simple_spin.h"
22 #include "common/strtol.h"
23 #include "common/likely.h"
24 #include "common/valgrind.h"
25 #include "common/deleter.h"
26 #include "include/atomic.h"
27 #include "common/RWLock.h"
28 #include "include/types.h"
29 #include "include/compat.h"
30 #include "include/inline_memory.h"
31 #include "include/scope_guard.h"
33 #include "msg/xio/XioMsg.h"
45 #define CEPH_BUFFER_ALLOC_UNIT (MIN(CEPH_PAGE_SIZE, 4096))
46 #define CEPH_BUFFER_APPEND_SIZE (CEPH_BUFFER_ALLOC_UNIT - sizeof(raw_combined))
49 static std::atomic_flag buffer_debug_lock
= ATOMIC_FLAG_INIT
;
50 # define bdout { simple_spin_lock(&buffer_debug_lock); std::cout
51 # define bendl std::endl; simple_spin_unlock(&buffer_debug_lock); }
53 # define bdout if (0) { std::cout
54 # define bendl std::endl; }
57 static atomic_t buffer_total_alloc
;
58 static atomic64_t buffer_history_alloc_bytes
;
59 static atomic64_t buffer_history_alloc_num
;
60 const bool buffer_track_alloc
= get_env_bool("CEPH_BUFFER_TRACK");
63 void inc_total_alloc(unsigned len
) {
64 if (buffer_track_alloc
)
65 buffer_total_alloc
.add(len
);
68 void dec_total_alloc(unsigned len
) {
69 if (buffer_track_alloc
)
70 buffer_total_alloc
.sub(len
);
73 void inc_history_alloc(uint64_t len
) {
74 if (buffer_track_alloc
) {
75 buffer_history_alloc_bytes
.add(len
);
76 buffer_history_alloc_num
.inc();
82 int buffer::get_total_alloc() {
83 return buffer_total_alloc
.read();
85 uint64_t buffer::get_history_alloc_bytes() {
86 return buffer_history_alloc_bytes
.read();
88 uint64_t buffer::get_history_alloc_num() {
89 return buffer_history_alloc_num
.read();
92 static atomic_t buffer_cached_crc
;
93 static atomic_t buffer_cached_crc_adjusted
;
94 static atomic_t buffer_missed_crc
;
95 static bool buffer_track_crc
= get_env_bool("CEPH_BUFFER_TRACK");
97 void buffer::track_cached_crc(bool b
) {
100 int buffer::get_cached_crc() {
101 return buffer_cached_crc
.read();
103 int buffer::get_cached_crc_adjusted() {
104 return buffer_cached_crc_adjusted
.read();
107 int buffer::get_missed_crc() {
108 return buffer_missed_crc
.read();
111 static atomic_t buffer_c_str_accesses
;
112 static bool buffer_track_c_str
= get_env_bool("CEPH_BUFFER_TRACK");
114 void buffer::track_c_str(bool b
) {
115 buffer_track_c_str
= b
;
117 int buffer::get_c_str_accesses() {
118 return buffer_c_str_accesses
.read();
121 static atomic_t buffer_max_pipe_size
;
122 int update_max_pipe_size() {
123 #ifdef CEPH_HAVE_SETPIPE_SZ
127 struct stat stat_result
;
128 if (::stat(PROCPREFIX
"/proc/sys/fs/pipe-max-size", &stat_result
) == -1)
130 r
= safe_read_file(PROCPREFIX
"/proc/sys/fs/", "pipe-max-size",
131 buf
, sizeof(buf
) - 1);
135 size_t size
= strict_strtol(buf
, 10, &err
);
138 buffer_max_pipe_size
.set(size
);
143 size_t get_max_pipe_size() {
144 #ifdef CEPH_HAVE_SETPIPE_SZ
145 size_t size
= buffer_max_pipe_size
.read();
148 if (update_max_pipe_size() == 0)
149 return buffer_max_pipe_size
.read();
151 // this is the max size hardcoded in linux before 2.6.35
155 const char * buffer::error::what() const throw () {
156 return "buffer::exception";
158 const char * buffer::bad_alloc::what() const throw () {
159 return "buffer::bad_alloc";
161 const char * buffer::end_of_buffer::what() const throw () {
162 return "buffer::end_of_buffer";
164 const char * buffer::malformed_input::what() const throw () {
167 buffer::error_code::error_code(int error
) :
168 buffer::malformed_input(cpp_strerror(error
).c_str()), code(error
) {}
176 mutable std::atomic_flag crc_spinlock
= ATOMIC_FLAG_INIT
;
177 map
<pair
<size_t, size_t>, pair
<uint32_t, uint32_t> > crc_map
;
179 explicit raw(unsigned l
)
180 : data(NULL
), len(l
), nref(0)
182 raw(char *c
, unsigned l
)
183 : data(c
), len(l
), nref(0)
188 // cppcheck-suppress noExplicitConstructor
189 raw(const raw
&other
);
190 const raw
& operator=(const raw
&other
);
192 virtual char *get_data() {
195 virtual raw
* clone_empty() = 0;
197 raw
*c
= clone_empty();
198 memcpy(c
->data
, data
, len
);
201 virtual bool can_zero_copy() const {
204 virtual int zero_copy_to_fd(int fd
, loff_t
*offset
) {
207 virtual bool is_page_aligned() {
208 return ((long)data
& ~CEPH_PAGE_MASK
) == 0;
210 bool is_n_page_sized() {
211 return (len
& ~CEPH_PAGE_MASK
) == 0;
213 virtual bool is_shareable() {
214 // true if safe to reference/share the existing buffer copy
215 // false if it is not safe to share the buffer, e.g., due to special
216 // and/or registered memory that is scarce
219 bool get_crc(const pair
<size_t, size_t> &fromto
,
220 pair
<uint32_t, uint32_t> *crc
) const {
221 simple_spin_lock(&crc_spinlock
);
222 map
<pair
<size_t, size_t>, pair
<uint32_t, uint32_t> >::const_iterator i
=
223 crc_map
.find(fromto
);
224 if (i
== crc_map
.end()) {
225 simple_spin_unlock(&crc_spinlock
);
229 simple_spin_unlock(&crc_spinlock
);
232 void set_crc(const pair
<size_t, size_t> &fromto
,
233 const pair
<uint32_t, uint32_t> &crc
) {
234 simple_spin_lock(&crc_spinlock
);
235 crc_map
[fromto
] = crc
;
236 simple_spin_unlock(&crc_spinlock
);
238 void invalidate_crc() {
239 simple_spin_lock(&crc_spinlock
);
240 if (crc_map
.size() != 0) {
243 simple_spin_unlock(&crc_spinlock
);
247 MEMPOOL_DEFINE_FACTORY(char, char, buffer_data
);
250 * raw_combined is always placed within a single allocation along
251 * with the data buffer. the data goes at the beginning, and
252 * raw_combined at the end.
254 class buffer::raw_combined
: public buffer::raw
{
257 raw_combined(char *dataptr
, unsigned l
, unsigned align
=0)
260 inc_total_alloc(len
);
261 inc_history_alloc(len
);
263 ~raw_combined() override
{
264 dec_total_alloc(len
);
266 raw
* clone_empty() override
{
267 return create(len
, alignment
);
270 static raw_combined
*create(unsigned len
, unsigned align
=0) {
272 align
= sizeof(size_t);
273 size_t rawlen
= ROUND_UP_TO(sizeof(buffer::raw_combined
),
274 alignof(buffer::raw_combined
));
275 size_t datalen
= ROUND_UP_TO(len
, alignof(buffer::raw_combined
));
277 char *ptr
= mempool::buffer_data::alloc_char
.allocate_aligned(
278 rawlen
+ datalen
, align
);
282 // actual data first, since it has presumably larger alignment restriction
283 // then put the raw_combined at the end
284 return new (ptr
+ datalen
) raw_combined(ptr
, len
, align
);
287 static void operator delete(void *ptr
) {
288 raw_combined
*raw
= (raw_combined
*)ptr
;
289 size_t rawlen
= ROUND_UP_TO(sizeof(buffer::raw_combined
),
290 alignof(buffer::raw_combined
));
291 size_t datalen
= ROUND_UP_TO(raw
->len
, alignof(buffer::raw_combined
));
292 mempool::buffer_data::alloc_char
.deallocate_aligned(
293 raw
->data
, rawlen
+ datalen
);
297 class buffer::raw_malloc
: public buffer::raw
{
299 MEMPOOL_CLASS_HELPERS();
301 explicit raw_malloc(unsigned l
) : raw(l
) {
303 data
= (char *)malloc(len
);
309 inc_total_alloc(len
);
310 inc_history_alloc(len
);
311 bdout
<< "raw_malloc " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
313 raw_malloc(unsigned l
, char *b
) : raw(b
, l
) {
314 inc_total_alloc(len
);
315 bdout
<< "raw_malloc " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
317 ~raw_malloc() override
{
319 dec_total_alloc(len
);
320 bdout
<< "raw_malloc " << this << " free " << (void *)data
<< " " << buffer::get_total_alloc() << bendl
;
322 raw
* clone_empty() override
{
323 return new raw_malloc(len
);
328 class buffer::raw_mmap_pages
: public buffer::raw
{
330 MEMPOOL_CLASS_HELPERS();
332 explicit raw_mmap_pages(unsigned l
) : raw(l
) {
333 data
= (char*)::mmap(NULL
, len
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
|MAP_ANON
, -1, 0);
336 inc_total_alloc(len
);
337 inc_history_alloc(len
);
338 bdout
<< "raw_mmap " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
340 ~raw_mmap_pages() override
{
342 dec_total_alloc(len
);
343 bdout
<< "raw_mmap " << this << " free " << (void *)data
<< " " << buffer::get_total_alloc() << bendl
;
345 raw
* clone_empty() override
{
346 return new raw_mmap_pages(len
);
350 class buffer::raw_posix_aligned
: public buffer::raw
{
353 MEMPOOL_CLASS_HELPERS();
355 raw_posix_aligned(unsigned l
, unsigned _align
) : raw(l
) {
357 assert((align
>= sizeof(void *)) && (align
& (align
- 1)) == 0);
358 data
= mempool::buffer_data::alloc_char
.allocate_aligned(len
, align
);
361 inc_total_alloc(len
);
362 inc_history_alloc(len
);
363 bdout
<< "raw_posix_aligned " << this << " alloc " << (void *)data
<< " l=" << l
<< ", align=" << align
<< " total_alloc=" << buffer::get_total_alloc() << bendl
;
365 ~raw_posix_aligned() override
{
366 mempool::buffer_data::alloc_char
.deallocate_aligned(data
, len
);
367 dec_total_alloc(len
);
368 bdout
<< "raw_posix_aligned " << this << " free " << (void *)data
<< " " << buffer::get_total_alloc() << bendl
;
370 raw
* clone_empty() override
{
371 return new raw_posix_aligned(len
, align
);
377 class buffer::raw_hack_aligned
: public buffer::raw
{
381 raw_hack_aligned(unsigned l
, unsigned _align
) : raw(l
) {
383 realdata
= new char[len
+align
-1];
384 unsigned off
= ((unsigned)realdata
) & (align
-1);
386 data
= realdata
+ align
- off
;
389 inc_total_alloc(len
+align
-1);
390 inc_history_alloc(len
+align
-1);
391 //cout << "hack aligned " << (unsigned)data
392 //<< " in raw " << (unsigned)realdata
393 //<< " off " << off << std::endl;
394 assert(((unsigned)data
& (align
-1)) == 0);
396 ~raw_hack_aligned() {
398 dec_total_alloc(len
+align
-1);
401 return new raw_hack_aligned(len
, align
);
406 #ifdef CEPH_HAVE_SPLICE
407 class buffer::raw_pipe
: public buffer::raw
{
409 MEMPOOL_CLASS_HELPERS();
411 explicit raw_pipe(unsigned len
) : raw(len
), source_consumed(false) {
412 size_t max
= get_max_pipe_size();
414 bdout
<< "raw_pipe: requested length " << len
415 << " > max length " << max
<< bendl
;
416 throw malformed_input("length larger than max pipe size");
422 if (::pipe(pipefds
) == -1) {
424 bdout
<< "raw_pipe: error creating pipe: " << cpp_strerror(r
) << bendl
;
428 r
= set_nonblocking(pipefds
);
430 bdout
<< "raw_pipe: error setting nonblocking flag on temp pipe: "
431 << cpp_strerror(r
) << bendl
;
435 r
= set_pipe_size(pipefds
, len
);
437 bdout
<< "raw_pipe: could not set pipe size" << bendl
;
438 // continue, since the pipe should become large enough as needed
441 inc_total_alloc(len
);
442 inc_history_alloc(len
);
443 bdout
<< "raw_pipe " << this << " alloc " << len
<< " "
444 << buffer::get_total_alloc() << bendl
;
447 ~raw_pipe() override
{
451 dec_total_alloc(len
);
452 bdout
<< "raw_pipe " << this << " free " << (void *)data
<< " "
453 << buffer::get_total_alloc() << bendl
;
456 bool can_zero_copy() const override
{
460 int set_source(int fd
, loff_t
*off
) {
461 int flags
= SPLICE_F_NONBLOCK
;
462 ssize_t r
= safe_splice(fd
, off
, pipefds
[1], NULL
, len
, flags
);
464 bdout
<< "raw_pipe: error splicing into pipe: " << cpp_strerror(r
)
468 // update length with actual amount read
473 int zero_copy_to_fd(int fd
, loff_t
*offset
) override
{
474 assert(!source_consumed
);
475 int flags
= SPLICE_F_NONBLOCK
;
476 ssize_t r
= safe_splice_exact(pipefds
[0], NULL
, fd
, offset
, len
, flags
);
478 bdout
<< "raw_pipe: error splicing from pipe to fd: "
479 << cpp_strerror(r
) << bendl
;
482 source_consumed
= true;
486 buffer::raw
* clone_empty() override
{
487 // cloning doesn't make sense for pipe-based buffers,
488 // and is only used by unit tests for other types of buffers
492 char *get_data() override
{
495 return copy_pipe(pipefds
);
499 int set_pipe_size(int *fds
, long length
) {
500 #ifdef CEPH_HAVE_SETPIPE_SZ
501 if (::fcntl(fds
[1], F_SETPIPE_SZ
, length
) == -1) {
504 // pipe limit must have changed - EPERM means we requested
505 // more than the maximum size as an unprivileged user
506 update_max_pipe_size();
507 throw malformed_input("length larger than new max pipe size");
515 int set_nonblocking(int *fds
) {
516 if (::fcntl(fds
[0], F_SETFL
, O_NONBLOCK
) == -1)
518 if (::fcntl(fds
[1], F_SETFL
, O_NONBLOCK
) == -1)
523 static void close_pipe(const int *fds
) {
525 VOID_TEMP_FAILURE_RETRY(::close(fds
[0]));
527 VOID_TEMP_FAILURE_RETRY(::close(fds
[1]));
529 char *copy_pipe(int *fds
) {
530 /* preserve original pipe contents by copying into a temporary
531 * pipe before reading.
536 assert(!source_consumed
);
539 if (::pipe(tmpfd
) == -1) {
541 bdout
<< "raw_pipe: error creating temp pipe: " << cpp_strerror(r
)
545 auto sg
= make_scope_guard([=] { close_pipe(tmpfd
); });
546 r
= set_nonblocking(tmpfd
);
548 bdout
<< "raw_pipe: error setting nonblocking flag on temp pipe: "
549 << cpp_strerror(r
) << bendl
;
552 r
= set_pipe_size(tmpfd
, len
);
554 bdout
<< "raw_pipe: error setting pipe size on temp pipe: "
555 << cpp_strerror(r
) << bendl
;
557 int flags
= SPLICE_F_NONBLOCK
;
558 if (::tee(fds
[0], tmpfd
[1], len
, flags
) == -1) {
560 bdout
<< "raw_pipe: error tee'ing into temp pipe: " << cpp_strerror(r
)
564 data
= (char *)malloc(len
);
568 r
= safe_read(tmpfd
[0], data
, len
);
569 if (r
< (ssize_t
)len
) {
570 bdout
<< "raw_pipe: error reading from temp pipe:" << cpp_strerror(r
)
578 bool source_consumed
;
581 #endif // CEPH_HAVE_SPLICE
584 * primitive buffer types
586 class buffer::raw_char
: public buffer::raw
{
588 MEMPOOL_CLASS_HELPERS();
590 explicit raw_char(unsigned l
) : raw(l
) {
592 data
= mempool::buffer_data::alloc_char
.allocate(len
);
595 inc_total_alloc(len
);
596 inc_history_alloc(len
);
597 bdout
<< "raw_char " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
599 raw_char(unsigned l
, char *b
) : raw(b
, l
) {
600 inc_total_alloc(len
);
601 bdout
<< "raw_char " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
603 ~raw_char() override
{
605 mempool::buffer_data::alloc_char
.deallocate(data
, len
);
606 dec_total_alloc(len
);
607 bdout
<< "raw_char " << this << " free " << (void *)data
<< " " << buffer::get_total_alloc() << bendl
;
609 raw
* clone_empty() override
{
610 return new raw_char(len
);
614 class buffer::raw_unshareable
: public buffer::raw
{
616 MEMPOOL_CLASS_HELPERS();
618 explicit raw_unshareable(unsigned l
) : raw(l
) {
620 data
= new char[len
];
624 raw_unshareable(unsigned l
, char *b
) : raw(b
, l
) {
626 raw
* clone_empty() override
{
627 return new raw_char(len
);
629 bool is_shareable() override
{
630 return false; // !shareable, will force make_shareable()
632 ~raw_unshareable() override
{
637 class buffer::raw_static
: public buffer::raw
{
639 MEMPOOL_CLASS_HELPERS();
641 raw_static(const char *d
, unsigned l
) : raw((char*)d
, l
) { }
642 ~raw_static() override
{}
643 raw
* clone_empty() override
{
644 return new buffer::raw_char(len
);
648 class buffer::raw_claim_buffer
: public buffer::raw
{
651 raw_claim_buffer(const char *b
, unsigned l
, deleter d
)
652 : raw((char*)b
, l
), del(std::move(d
)) { }
653 ~raw_claim_buffer() override
{}
654 raw
* clone_empty() override
{
655 return new buffer::raw_char(len
);
659 #if defined(HAVE_XIO)
660 class buffer::xio_msg_buffer
: public buffer::raw
{
662 XioDispatchHook
* m_hook
;
664 xio_msg_buffer(XioDispatchHook
* _m_hook
, const char *d
,
666 raw((char*)d
, l
), m_hook(_m_hook
->get()) {}
668 bool is_shareable() { return false; }
669 static void operator delete(void *p
)
671 xio_msg_buffer
*buf
= static_cast<xio_msg_buffer
*>(p
);
672 // return hook ref (counts against pool); it appears illegal
673 // to do this in our dtor, because this fires after that
677 return new buffer::raw_char(len
);
681 class buffer::xio_mempool
: public buffer::raw
{
683 struct xio_reg_mem
*mp
;
684 xio_mempool(struct xio_reg_mem
*_mp
, unsigned l
) :
685 raw((char*)_mp
->addr
, l
), mp(_mp
)
689 return new buffer::raw_char(len
);
693 struct xio_reg_mem
* get_xio_mp(const buffer::ptr
& bp
)
695 buffer::xio_mempool
*mb
= dynamic_cast<buffer::xio_mempool
*>(bp
.get_raw());
702 buffer::raw
* buffer::create_msg(
703 unsigned len
, char *buf
, XioDispatchHook
* m_hook
) {
704 XioPool
& pool
= m_hook
->get_pool();
706 static_cast<buffer::raw
*>(pool
.alloc(sizeof(xio_msg_buffer
)));
707 new (bp
) xio_msg_buffer(m_hook
, buf
, len
);
710 #endif /* HAVE_XIO */
712 buffer::raw
* buffer::copy(const char *c
, unsigned len
) {
713 raw
* r
= buffer::create_aligned(len
, sizeof(size_t));
714 memcpy(r
->data
, c
, len
);
718 buffer::raw
* buffer::create(unsigned len
) {
719 return buffer::create_aligned(len
, sizeof(size_t));
721 buffer::raw
* buffer::claim_char(unsigned len
, char *buf
) {
722 return new raw_char(len
, buf
);
724 buffer::raw
* buffer::create_malloc(unsigned len
) {
725 return new raw_malloc(len
);
727 buffer::raw
* buffer::claim_malloc(unsigned len
, char *buf
) {
728 return new raw_malloc(len
, buf
);
730 buffer::raw
* buffer::create_static(unsigned len
, char *buf
) {
731 return new raw_static(buf
, len
);
733 buffer::raw
* buffer::claim_buffer(unsigned len
, char *buf
, deleter del
) {
734 return new raw_claim_buffer(buf
, len
, std::move(del
));
737 buffer::raw
* buffer::create_aligned(unsigned len
, unsigned align
) {
738 // If alignment is a page multiple, use a separate buffer::raw to
739 // avoid fragmenting the heap.
741 // Somewhat unexpectedly, I see consistently better performance
742 // from raw_combined than from raw even when the allocation size is
743 // a page multiple (but alignment is not).
745 // I also see better performance from a separate buffer::raw once the
747 if ((align
& ~CEPH_PAGE_MASK
) == 0 ||
748 len
>= CEPH_PAGE_SIZE
* 2) {
750 return new raw_posix_aligned(len
, align
);
752 return new raw_hack_aligned(len
, align
);
755 return raw_combined::create(len
, align
);
758 buffer::raw
* buffer::create_page_aligned(unsigned len
) {
759 return create_aligned(len
, CEPH_PAGE_SIZE
);
762 buffer::raw
* buffer::create_zero_copy(unsigned len
, int fd
, int64_t *offset
) {
763 #ifdef CEPH_HAVE_SPLICE
764 buffer::raw_pipe
* buf
= new raw_pipe(len
);
765 int r
= buf
->set_source(fd
, (loff_t
*)offset
);
772 throw error_code(-ENOTSUP
);
776 buffer::raw
* buffer::create_unshareable(unsigned len
) {
777 return new raw_unshareable(len
);
780 buffer::ptr::ptr(raw
*r
) : _raw(r
), _off(0), _len(r
->len
) // no lock needed; this is an unref raw.
783 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
785 buffer::ptr::ptr(unsigned l
) : _off(0), _len(l
)
789 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
791 buffer::ptr::ptr(const char *d
, unsigned l
) : _off(0), _len(l
) // ditto.
795 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
797 buffer::ptr::ptr(const ptr
& p
) : _raw(p
._raw
), _off(p
._off
), _len(p
._len
)
801 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
804 buffer::ptr::ptr(ptr
&& p
) noexcept
: _raw(p
._raw
), _off(p
._off
), _len(p
._len
)
809 buffer::ptr::ptr(const ptr
& p
, unsigned o
, unsigned l
)
810 : _raw(p
._raw
), _off(p
._off
+ o
), _len(l
)
812 assert(o
+l
<= p
._len
);
815 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
817 buffer::ptr
& buffer::ptr::operator= (const ptr
& p
)
821 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
823 buffer::raw
*raw
= p
._raw
;
834 buffer::ptr
& buffer::ptr::operator= (ptr
&& p
) noexcept
837 buffer::raw
*raw
= p
._raw
;
850 buffer::raw
*buffer::ptr::clone()
852 return _raw
->clone();
855 buffer::ptr
& buffer::ptr::make_shareable() {
856 if (_raw
&& !_raw
->is_shareable()) {
857 buffer::raw
*tr
= _raw
;
860 if (unlikely(tr
->nref
.dec() == 0)) {
861 ANNOTATE_HAPPENS_AFTER(&tr
->nref
);
862 ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&tr
->nref
);
865 ANNOTATE_HAPPENS_BEFORE(&tr
->nref
);
871 void buffer::ptr::swap(ptr
& other
)
884 void buffer::ptr::release()
887 bdout
<< "ptr " << this << " release " << _raw
<< bendl
;
888 if (_raw
->nref
.dec() == 0) {
889 //cout << "hosing raw " << (void*)_raw << " len " << _raw->len << std::endl;
890 ANNOTATE_HAPPENS_AFTER(&_raw
->nref
);
891 ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&_raw
->nref
);
892 delete _raw
; // dealloc old (if any)
894 ANNOTATE_HAPPENS_BEFORE(&_raw
->nref
);
900 bool buffer::ptr::at_buffer_tail() const { return _off
+ _len
== _raw
->len
; }
902 const char *buffer::ptr::c_str() const {
904 if (buffer_track_c_str
)
905 buffer_c_str_accesses
.inc();
906 return _raw
->get_data() + _off
;
908 char *buffer::ptr::c_str() {
910 if (buffer_track_c_str
)
911 buffer_c_str_accesses
.inc();
912 return _raw
->get_data() + _off
;
914 const char *buffer::ptr::end_c_str() const {
916 if (buffer_track_c_str
)
917 buffer_c_str_accesses
.inc();
918 return _raw
->get_data() + _off
+ _len
;
920 char *buffer::ptr::end_c_str() {
922 if (buffer_track_c_str
)
923 buffer_c_str_accesses
.inc();
924 return _raw
->get_data() + _off
+ _len
;
927 unsigned buffer::ptr::unused_tail_length() const
930 return _raw
->len
- (_off
+_len
);
934 const char& buffer::ptr::operator[](unsigned n
) const
938 return _raw
->get_data()[_off
+ n
];
940 char& buffer::ptr::operator[](unsigned n
)
944 return _raw
->get_data()[_off
+ n
];
947 const char *buffer::ptr::raw_c_str() const { assert(_raw
); return _raw
->data
; }
948 unsigned buffer::ptr::raw_length() const { assert(_raw
); return _raw
->len
; }
949 int buffer::ptr::raw_nref() const { assert(_raw
); return _raw
->nref
.read(); }
951 void buffer::ptr::copy_out(unsigned o
, unsigned l
, char *dest
) const {
954 throw end_of_buffer();
955 char* src
= _raw
->data
+ _off
+ o
;
956 maybe_inline_memcpy(dest
, src
, l
, 8);
959 unsigned buffer::ptr::wasted()
962 return _raw
->len
- _len
;
965 int buffer::ptr::cmp(const ptr
& o
) const
967 int l
= _len
< o
._len
? _len
: o
._len
;
969 int r
= memcmp(c_str(), o
.c_str(), l
);
980 bool buffer::ptr::is_zero() const
982 return mem_is_zero(c_str(), _len
);
985 unsigned buffer::ptr::append(char c
)
988 assert(1 <= unused_tail_length());
989 char* ptr
= _raw
->data
+ _off
+ _len
;
995 unsigned buffer::ptr::append(const char *p
, unsigned l
)
998 assert(l
<= unused_tail_length());
999 char* c
= _raw
->data
+ _off
+ _len
;
1000 maybe_inline_memcpy(c
, p
, l
, 32);
1005 void buffer::ptr::copy_in(unsigned o
, unsigned l
, const char *src
)
1007 copy_in(o
, l
, src
, true);
1010 void buffer::ptr::copy_in(unsigned o
, unsigned l
, const char *src
, bool crc_reset
)
1014 assert(o
+l
<= _len
);
1015 char* dest
= _raw
->data
+ _off
+ o
;
1017 _raw
->invalidate_crc();
1018 maybe_inline_memcpy(dest
, src
, l
, 64);
1021 void buffer::ptr::zero()
1026 void buffer::ptr::zero(bool crc_reset
)
1029 _raw
->invalidate_crc();
1030 memset(c_str(), 0, _len
);
1033 void buffer::ptr::zero(unsigned o
, unsigned l
)
1038 void buffer::ptr::zero(unsigned o
, unsigned l
, bool crc_reset
)
1040 assert(o
+l
<= _len
);
1042 _raw
->invalidate_crc();
1043 memset(c_str()+o
, 0, l
);
1045 bool buffer::ptr::can_zero_copy() const
1047 return _raw
->can_zero_copy();
1050 int buffer::ptr::zero_copy_to_fd(int fd
, int64_t *offset
) const
1052 return _raw
->zero_copy_to_fd(fd
, (loff_t
*)offset
);
1055 // -- buffer::list::iterator --
1057 buffer::list::iterator operator=(const buffer::list::iterator& other)
1059 if (this != &other) {
1064 p_off = other.p_off;
1069 template<bool is_const
>
1070 buffer::list::iterator_impl
<is_const
>::iterator_impl(bl_t
*l
, unsigned o
)
1071 : bl(l
), ls(&bl
->_buffers
), off(0), p(ls
->begin()), p_off(0)
1076 template<bool is_const
>
1077 buffer::list::iterator_impl
<is_const
>::iterator_impl(const buffer::list::iterator
& i
)
1078 : iterator_impl
<is_const
>(i
.bl
, i
.off
, i
.p
, i
.p_off
) {}
1080 template<bool is_const
>
1081 void buffer::list::iterator_impl
<is_const
>::advance(int o
)
1083 //cout << this << " advance " << o << " from " << off << " (p_off " << p_off << " in " << p->length() << ")" << std::endl;
1088 throw end_of_buffer();
1089 if (p_off
>= p
->length()) {
1091 p_off
-= p
->length();
1094 // somewhere in this buffer!
1109 } else if (off
> 0) {
1110 assert(p
!= ls
->begin());
1112 p_off
= p
->length();
1114 throw end_of_buffer();
1119 template<bool is_const
>
1120 void buffer::list::iterator_impl
<is_const
>::seek(unsigned o
)
1127 template<bool is_const
>
1128 char buffer::list::iterator_impl
<is_const
>::operator*() const
1131 throw end_of_buffer();
1135 template<bool is_const
>
1136 buffer::list::iterator_impl
<is_const
>&
1137 buffer::list::iterator_impl
<is_const
>::operator++()
1140 throw end_of_buffer();
1145 template<bool is_const
>
1146 buffer::ptr
buffer::list::iterator_impl
<is_const
>::get_current_ptr() const
1149 throw end_of_buffer();
1150 return ptr(*p
, p_off
, p
->length() - p_off
);
1154 // note that these all _append_ to dest!
1155 template<bool is_const
>
1156 void buffer::list::iterator_impl
<is_const
>::copy(unsigned len
, char *dest
)
1158 if (p
== ls
->end()) seek(off
);
1161 throw end_of_buffer();
1162 assert(p
->length() > 0);
1164 unsigned howmuch
= p
->length() - p_off
;
1165 if (len
< howmuch
) howmuch
= len
;
1166 p
->copy_out(p_off
, howmuch
, dest
);
1174 template<bool is_const
>
1175 void buffer::list::iterator_impl
<is_const
>::copy(unsigned len
, ptr
&dest
)
1177 copy_deep(len
, dest
);
1180 template<bool is_const
>
1181 void buffer::list::iterator_impl
<is_const
>::copy_deep(unsigned len
, ptr
&dest
)
1187 throw end_of_buffer();
1188 assert(p
->length() > 0);
1190 copy(len
, dest
.c_str());
1192 template<bool is_const
>
1193 void buffer::list::iterator_impl
<is_const
>::copy_shallow(unsigned len
,
1200 throw end_of_buffer();
1201 assert(p
->length() > 0);
1202 unsigned howmuch
= p
->length() - p_off
;
1203 if (howmuch
< len
) {
1205 copy(len
, dest
.c_str());
1207 dest
= ptr(*p
, p_off
, len
);
1212 template<bool is_const
>
1213 void buffer::list::iterator_impl
<is_const
>::copy(unsigned len
, list
&dest
)
1219 throw end_of_buffer();
1221 unsigned howmuch
= p
->length() - p_off
;
1224 dest
.append(*p
, p_off
, howmuch
);
1231 template<bool is_const
>
1232 void buffer::list::iterator_impl
<is_const
>::copy(unsigned len
, std::string
&dest
)
1238 throw end_of_buffer();
1240 unsigned howmuch
= p
->length() - p_off
;
1241 const char *c_str
= p
->c_str();
1244 dest
.append(c_str
+ p_off
, howmuch
);
1251 template<bool is_const
>
1252 void buffer::list::iterator_impl
<is_const
>::copy_all(list
&dest
)
1259 assert(p
->length() > 0);
1261 unsigned howmuch
= p
->length() - p_off
;
1262 const char *c_str
= p
->c_str();
1263 dest
.append(c_str
+ p_off
, howmuch
);
1269 template<bool is_const
>
1270 size_t buffer::list::iterator_impl
<is_const
>::get_ptr_and_advance(
1271 size_t want
, const char **data
)
1273 if (p
== ls
->end()) {
1275 if (p
== ls
->end()) {
1279 *data
= p
->c_str() + p_off
;
1280 size_t l
= MIN(p
->length() - p_off
, want
);
1282 if (p_off
== p
->length()) {
1290 template<bool is_const
>
1291 uint32_t buffer::list::iterator_impl
<is_const
>::crc32c(
1292 size_t length
, uint32_t crc
)
1294 length
= MIN( length
, get_remaining());
1295 while (length
> 0) {
1297 size_t l
= get_ptr_and_advance(length
, &p
);
1298 crc
= ceph_crc32c(crc
, (unsigned char*)p
, l
);
1304 // explicitly instantiate only the iterator types we need, so we can hide the
1305 // details in this compilation unit without introducing unnecessary link time
1307 template class buffer::list::iterator_impl
<true>;
1308 template class buffer::list::iterator_impl
<false>;
1310 buffer::list::iterator::iterator(bl_t
*l
, unsigned o
)
1311 : iterator_impl(l
, o
)
1314 buffer::list::iterator::iterator(bl_t
*l
, unsigned o
, list_iter_t ip
, unsigned po
)
1315 : iterator_impl(l
, o
, ip
, po
)
1318 void buffer::list::iterator::advance(int o
)
1320 buffer::list::iterator_impl
<false>::advance(o
);
1323 void buffer::list::iterator::seek(unsigned o
)
1325 buffer::list::iterator_impl
<false>::seek(o
);
1328 char buffer::list::iterator::operator*()
1330 if (p
== ls
->end()) {
1331 throw end_of_buffer();
1336 buffer::list::iterator
& buffer::list::iterator::operator++()
1338 buffer::list::iterator_impl
<false>::operator++();
1342 buffer::ptr
buffer::list::iterator::get_current_ptr()
1344 if (p
== ls
->end()) {
1345 throw end_of_buffer();
1347 return ptr(*p
, p_off
, p
->length() - p_off
);
1350 void buffer::list::iterator::copy(unsigned len
, char *dest
)
1352 return buffer::list::iterator_impl
<false>::copy(len
, dest
);
1355 void buffer::list::iterator::copy(unsigned len
, ptr
&dest
)
1357 return buffer::list::iterator_impl
<false>::copy_deep(len
, dest
);
1360 void buffer::list::iterator::copy_deep(unsigned len
, ptr
&dest
)
1362 buffer::list::iterator_impl
<false>::copy_deep(len
, dest
);
1365 void buffer::list::iterator::copy_shallow(unsigned len
, ptr
&dest
)
1367 buffer::list::iterator_impl
<false>::copy_shallow(len
, dest
);
1370 void buffer::list::iterator::copy(unsigned len
, list
&dest
)
1372 buffer::list::iterator_impl
<false>::copy(len
, dest
);
1375 void buffer::list::iterator::copy(unsigned len
, std::string
&dest
)
1377 buffer::list::iterator_impl
<false>::copy(len
, dest
);
1380 void buffer::list::iterator::copy_all(list
&dest
)
1382 buffer::list::iterator_impl
<false>::copy_all(dest
);
1385 void buffer::list::iterator::copy_in(unsigned len
, const char *src
)
1387 copy_in(len
, src
, true);
1391 void buffer::list::iterator::copy_in(unsigned len
, const char *src
, bool crc_reset
)
1398 throw end_of_buffer();
1400 unsigned howmuch
= p
->length() - p_off
;
1403 p
->copy_in(p_off
, howmuch
, src
, crc_reset
);
1411 void buffer::list::iterator::copy_in(unsigned len
, const list
& otherl
)
1415 unsigned left
= len
;
1416 for (std::list
<ptr
>::const_iterator i
= otherl
._buffers
.begin();
1417 i
!= otherl
._buffers
.end();
1419 unsigned l
= (*i
).length();
1422 copy_in(l
, i
->c_str());
1429 // -- buffer::list --
1431 buffer::list::list(list
&& other
)
1432 : _buffers(std::move(other
._buffers
)),
1434 _memcopy_count(other
._memcopy_count
),
1436 append_buffer
.swap(other
.append_buffer
);
1440 void buffer::list::swap(list
& other
)
1442 std::swap(_len
, other
._len
);
1443 std::swap(_memcopy_count
, other
._memcopy_count
);
1444 _buffers
.swap(other
._buffers
);
1445 append_buffer
.swap(other
.append_buffer
);
1446 //last_p.swap(other.last_p);
1448 other
.last_p
= other
.begin();
1451 bool buffer::list::contents_equal(buffer::list
& other
)
1453 return static_cast<const buffer::list
*>(this)->contents_equal(other
);
1456 bool buffer::list::contents_equal(const ceph::buffer::list
& other
) const
1458 if (length() != other
.length())
1461 // buffer-wise comparison
1463 std::list
<ptr
>::const_iterator a
= _buffers
.begin();
1464 std::list
<ptr
>::const_iterator b
= other
._buffers
.begin();
1465 unsigned aoff
= 0, boff
= 0;
1466 while (a
!= _buffers
.end()) {
1467 unsigned len
= a
->length() - aoff
;
1468 if (len
> b
->length() - boff
)
1469 len
= b
->length() - boff
;
1470 if (memcmp(a
->c_str() + aoff
, b
->c_str() + boff
, len
) != 0)
1473 if (aoff
== a
->length()) {
1478 if (boff
== b
->length()) {
1483 assert(b
== other
._buffers
.end());
1487 // byte-wise comparison
1489 bufferlist::const_iterator me
= begin();
1490 bufferlist::const_iterator him
= other
.begin();
1501 bool buffer::list::can_zero_copy() const
1503 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1504 it
!= _buffers
.end();
1506 if (!it
->can_zero_copy())
1511 bool buffer::list::is_provided_buffer(const char *dst
) const
1513 if (_buffers
.empty())
1515 return (is_contiguous() && (_buffers
.front().c_str() == dst
));
1518 bool buffer::list::is_aligned(unsigned align
) const
1520 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1521 it
!= _buffers
.end();
1523 if (!it
->is_aligned(align
))
1528 bool buffer::list::is_n_align_sized(unsigned align
) const
1530 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1531 it
!= _buffers
.end();
1533 if (!it
->is_n_align_sized(align
))
1538 bool buffer::list::is_aligned_size_and_memory(unsigned align_size
,
1539 unsigned align_memory
) const
1541 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1542 it
!= _buffers
.end();
1544 if (!it
->is_aligned(align_memory
) || !it
->is_n_align_sized(align_size
))
1550 bool buffer::list::is_zero() const {
1551 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1552 it
!= _buffers
.end();
1554 if (!it
->is_zero()) {
1561 void buffer::list::zero()
1563 for (std::list
<ptr
>::iterator it
= _buffers
.begin();
1564 it
!= _buffers
.end();
1569 void buffer::list::zero(unsigned o
, unsigned l
)
1571 assert(o
+l
<= _len
);
1573 for (std::list
<ptr
>::iterator it
= _buffers
.begin();
1574 it
!= _buffers
.end();
1576 if (p
+ it
->length() > o
) {
1577 if (p
>= o
&& p
+it
->length() <= o
+l
) {
1578 // 'o'------------- l -----------|
1579 // 'p'-- it->length() --|
1581 } else if (p
>= o
) {
1582 // 'o'------------- l -----------|
1583 // 'p'------- it->length() -------|
1585 } else if (p
+ it
->length() <= o
+l
) {
1586 // 'o'------------- l -----------|
1587 // 'p'------- it->length() -------|
1588 it
->zero(o
-p
, it
->length()-(o
-p
));
1590 // 'o'----------- l -----------|
1591 // 'p'---------- it->length() ----------|
1601 bool buffer::list::is_contiguous() const
1603 return &(*_buffers
.begin()) == &(*_buffers
.rbegin());
1606 bool buffer::list::is_n_page_sized() const
1608 return is_n_align_sized(CEPH_PAGE_SIZE
);
1611 bool buffer::list::is_page_aligned() const
1613 return is_aligned(CEPH_PAGE_SIZE
);
1616 void buffer::list::rebuild()
1623 if ((_len
& ~CEPH_PAGE_MASK
) == 0)
1624 nb
= buffer::create_page_aligned(_len
);
1626 nb
= buffer::create(_len
);
1630 void buffer::list::rebuild(ptr
& nb
)
1633 for (std::list
<ptr
>::iterator it
= _buffers
.begin();
1634 it
!= _buffers
.end();
1636 nb
.copy_in(pos
, it
->length(), it
->c_str(), false);
1637 pos
+= it
->length();
1639 _memcopy_count
+= pos
;
1642 _buffers
.push_back(nb
);
1647 bool buffer::list::rebuild_aligned(unsigned align
)
1649 return rebuild_aligned_size_and_memory(align
, align
);
1652 bool buffer::list::rebuild_aligned_size_and_memory(unsigned align_size
,
1653 unsigned align_memory
)
1655 unsigned old_memcopy_count
= _memcopy_count
;
1656 std::list
<ptr
>::iterator p
= _buffers
.begin();
1657 while (p
!= _buffers
.end()) {
1658 // keep anything that's already align and sized aligned
1659 if (p
->is_aligned(align_memory
) && p
->is_n_align_sized(align_size
)) {
1660 /*cout << " segment " << (void*)p->c_str()
1661 << " offset " << ((unsigned long)p->c_str() & (align - 1))
1662 << " length " << p->length()
1663 << " " << (p->length() & (align - 1)) << " ok" << std::endl;
1669 // consolidate unaligned items, until we get something that is sized+aligned
1671 unsigned offset
= 0;
1673 /*cout << " segment " << (void*)p->c_str()
1674 << " offset " << ((unsigned long)p->c_str() & (align - 1))
1675 << " length " << p->length() << " " << (p->length() & (align - 1))
1676 << " overall offset " << offset << " " << (offset & (align - 1))
1677 << " not ok" << std::endl;
1679 offset
+= p
->length();
1680 unaligned
.push_back(*p
);
1681 _buffers
.erase(p
++);
1682 } while (p
!= _buffers
.end() &&
1683 (!p
->is_aligned(align_memory
) ||
1684 !p
->is_n_align_sized(align_size
) ||
1685 (offset
% align_size
)));
1686 if (!(unaligned
.is_contiguous() && unaligned
._buffers
.front().is_aligned(align_memory
))) {
1687 ptr
nb(buffer::create_aligned(unaligned
._len
, align_memory
));
1688 unaligned
.rebuild(nb
);
1689 _memcopy_count
+= unaligned
._len
;
1691 _buffers
.insert(p
, unaligned
._buffers
.front());
1695 return (old_memcopy_count
!= _memcopy_count
);
1698 bool buffer::list::rebuild_page_aligned()
1700 return rebuild_aligned(CEPH_PAGE_SIZE
);
1703 // sort-of-like-assignment-op
1704 void buffer::list::claim(list
& bl
, unsigned int flags
)
1708 claim_append(bl
, flags
);
1711 void buffer::list::claim_append(list
& bl
, unsigned int flags
)
1713 // steal the other guy's buffers
1715 if (!(flags
& CLAIM_ALLOW_NONSHAREABLE
))
1716 bl
.make_shareable();
1717 _buffers
.splice(_buffers
.end(), bl
._buffers
);
1719 bl
.last_p
= bl
.begin();
1722 void buffer::list::claim_prepend(list
& bl
, unsigned int flags
)
1724 // steal the other guy's buffers
1726 if (!(flags
& CLAIM_ALLOW_NONSHAREABLE
))
1727 bl
.make_shareable();
1728 _buffers
.splice(_buffers
.begin(), bl
._buffers
);
1730 bl
.last_p
= bl
.begin();
1733 void buffer::list::copy(unsigned off
, unsigned len
, char *dest
) const
1735 if (off
+ len
> length())
1736 throw end_of_buffer();
1737 if (last_p
.get_off() != off
)
1739 last_p
.copy(len
, dest
);
1742 void buffer::list::copy(unsigned off
, unsigned len
, list
&dest
) const
1744 if (off
+ len
> length())
1745 throw end_of_buffer();
1746 if (last_p
.get_off() != off
)
1748 last_p
.copy(len
, dest
);
1751 void buffer::list::copy(unsigned off
, unsigned len
, std::string
& dest
) const
1753 if (last_p
.get_off() != off
)
1755 return last_p
.copy(len
, dest
);
1758 void buffer::list::copy_in(unsigned off
, unsigned len
, const char *src
)
1760 copy_in(off
, len
, src
, true);
1763 void buffer::list::copy_in(unsigned off
, unsigned len
, const char *src
, bool crc_reset
)
1765 if (off
+ len
> length())
1766 throw end_of_buffer();
1768 if (last_p
.get_off() != off
)
1770 last_p
.copy_in(len
, src
, crc_reset
);
1773 void buffer::list::copy_in(unsigned off
, unsigned len
, const list
& src
)
1775 if (last_p
.get_off() != off
)
1777 last_p
.copy_in(len
, src
);
1780 void buffer::list::append(char c
)
1782 // put what we can into the existing append_buffer.
1783 unsigned gap
= append_buffer
.unused_tail_length();
1785 // make a new append_buffer!
1786 append_buffer
= raw_combined::create(CEPH_BUFFER_APPEND_SIZE
);
1787 append_buffer
.set_length(0); // unused, so far.
1789 append(append_buffer
, append_buffer
.append(c
) - 1, 1); // add segment to the list
1792 void buffer::list::append(const char *data
, unsigned len
)
1795 // put what we can into the existing append_buffer.
1796 unsigned gap
= append_buffer
.unused_tail_length();
1798 if (gap
> len
) gap
= len
;
1799 //cout << "append first char is " << data[0] << ", last char is " << data[len-1] << std::endl;
1800 append_buffer
.append(data
, gap
);
1801 append(append_buffer
, append_buffer
.length() - gap
, gap
); // add segment to the list
1808 // make a new append_buffer. fill out a complete page, factoring in the
1809 // raw_combined overhead.
1810 size_t need
= ROUND_UP_TO(len
, sizeof(size_t)) + sizeof(raw_combined
);
1811 size_t alen
= ROUND_UP_TO(need
, CEPH_BUFFER_ALLOC_UNIT
) -
1812 sizeof(raw_combined
);
1813 append_buffer
= raw_combined::create(alen
);
1814 append_buffer
.set_length(0); // unused, so far.
1818 void buffer::list::append(const ptr
& bp
)
1824 void buffer::list::append(ptr
&& bp
)
1827 push_back(std::move(bp
));
1830 void buffer::list::append(const ptr
& bp
, unsigned off
, unsigned len
)
1832 assert(len
+off
<= bp
.length());
1833 if (!_buffers
.empty()) {
1834 ptr
&l
= _buffers
.back();
1835 if (l
.get_raw() == bp
.get_raw() &&
1836 l
.end() == bp
.start() + off
) {
1837 // yay contiguous with tail bp!
1838 l
.set_length(l
.length()+len
);
1843 // add new item to list
1844 push_back(ptr(bp
, off
, len
));
1847 void buffer::list::append(const list
& bl
)
1850 for (std::list
<ptr
>::const_iterator p
= bl
._buffers
.begin();
1851 p
!= bl
._buffers
.end();
1853 _buffers
.push_back(*p
);
1856 void buffer::list::append(std::istream
& in
)
1861 append(s
.c_str(), s
.length());
1867 void buffer::list::prepend_zero(unsigned len
)
1872 _buffers
.emplace_front(std::move(bp
));
1875 void buffer::list::append_zero(unsigned len
)
1879 append(std::move(bp
));
1886 const char& buffer::list::operator[](unsigned n
) const
1889 throw end_of_buffer();
1891 for (std::list
<ptr
>::const_iterator p
= _buffers
.begin();
1892 p
!= _buffers
.end();
1894 if (n
>= p
->length()) {
1904 * return a contiguous ptr to whole bufferlist contents.
1906 char *buffer::list::c_str()
1908 if (_buffers
.empty())
1909 return 0; // no buffers
1911 std::list
<ptr
>::const_iterator iter
= _buffers
.begin();
1914 if (iter
!= _buffers
.end())
1916 return _buffers
.front().c_str(); // good, we're already contiguous.
1919 string
buffer::list::to_str() const {
1921 s
.reserve(length());
1922 for (std::list
<ptr
>::const_iterator p
= _buffers
.begin();
1923 p
!= _buffers
.end();
1926 s
.append(p
->c_str(), p
->length());
1932 char *buffer::list::get_contiguous(unsigned orig_off
, unsigned len
)
1934 if (orig_off
+ len
> length())
1935 throw end_of_buffer();
1941 unsigned off
= orig_off
;
1942 std::list
<ptr
>::iterator curbuf
= _buffers
.begin();
1943 while (off
> 0 && off
>= curbuf
->length()) {
1944 off
-= curbuf
->length();
1948 if (off
+ len
> curbuf
->length()) {
1950 unsigned l
= off
+ len
;
1953 if (l
>= curbuf
->length())
1954 l
-= curbuf
->length();
1957 tmp
.append(*curbuf
);
1958 curbuf
= _buffers
.erase(curbuf
);
1960 } while (curbuf
!= _buffers
.end() && l
> 0);
1965 _buffers
.insert(curbuf
, tmp
._buffers
.front());
1966 return tmp
.c_str() + off
;
1969 last_p
= begin(); // we modified _buffers
1971 return curbuf
->c_str() + off
;
1974 void buffer::list::substr_of(const list
& other
, unsigned off
, unsigned len
)
1976 if (off
+ len
> other
.length())
1977 throw end_of_buffer();
1982 std::list
<ptr
>::const_iterator curbuf
= other
._buffers
.begin();
1984 off
>= curbuf
->length()) {
1986 //cout << "skipping over " << *curbuf << std::endl;
1987 off
-= (*curbuf
).length();
1990 assert(len
== 0 || curbuf
!= other
._buffers
.end());
1994 if (off
+ len
< curbuf
->length()) {
1995 //cout << "copying partial of " << *curbuf << std::endl;
1996 _buffers
.push_back( ptr( *curbuf
, off
, len
) );
2002 //cout << "copying end (all?) of " << *curbuf << std::endl;
2003 unsigned howmuch
= curbuf
->length() - off
;
2004 _buffers
.push_back( ptr( *curbuf
, off
, howmuch
) );
2013 void buffer::list::splice(unsigned off
, unsigned len
, list
*claim_by
/*, bufferlist& replace_with */)
2018 if (off
>= length())
2019 throw end_of_buffer();
2022 //cout << "splice off " << off << " len " << len << " ... mylen = " << length() << std::endl;
2025 std::list
<ptr
>::iterator curbuf
= _buffers
.begin();
2027 assert(curbuf
!= _buffers
.end());
2028 if (off
>= (*curbuf
).length()) {
2030 //cout << "off = " << off << " skipping over " << *curbuf << std::endl;
2031 off
-= (*curbuf
).length();
2034 // somewhere in this buffer!
2035 //cout << "off = " << off << " somewhere in " << *curbuf << std::endl;
2041 // add a reference to the front bit
2042 // insert it before curbuf (which we'll hose)
2043 //cout << "keeping front " << off << " of " << *curbuf << std::endl;
2044 _buffers
.insert( curbuf
, ptr( *curbuf
, 0, off
) );
2050 if (off
+ len
< (*curbuf
).length()) {
2051 //cout << "keeping end of " << *curbuf << ", losing first " << off+len << std::endl;
2053 claim_by
->append( *curbuf
, off
, len
);
2054 (*curbuf
).set_offset( off
+len
+ (*curbuf
).offset() ); // ignore beginning big
2055 (*curbuf
).set_length( (*curbuf
).length() - (len
+off
) );
2057 //cout << " now " << *curbuf << std::endl;
2061 // hose though the end
2062 unsigned howmuch
= (*curbuf
).length() - off
;
2063 //cout << "discarding " << howmuch << " of " << *curbuf << std::endl;
2065 claim_by
->append( *curbuf
, off
, howmuch
);
2066 _len
-= (*curbuf
).length();
2067 _buffers
.erase( curbuf
++ );
2072 // splice in *replace (implement me later?)
2074 last_p
= begin(); // just in case we were in the removed region.
2077 void buffer::list::write(int off
, int len
, std::ostream
& out
) const
2080 s
.substr_of(*this, off
, len
);
2081 for (std::list
<ptr
>::const_iterator it
= s
._buffers
.begin();
2082 it
!= s
._buffers
.end();
2085 out
.write(it
->c_str(), it
->length());
2086 /*iterator p(this, off);
2087 while (len > 0 && !p.end()) {
2088 int l = p.left_in_this_buf();
2091 out.write(p.c_str(), l);
2096 void buffer::list::encode_base64(buffer::list
& o
)
2098 bufferptr
bp(length() * 4 / 3 + 3);
2099 int l
= ceph_armor(bp
.c_str(), bp
.c_str() + bp
.length(), c_str(), c_str() + length());
2101 o
.push_back(std::move(bp
));
2104 void buffer::list::decode_base64(buffer::list
& e
)
2106 bufferptr
bp(4 + ((e
.length() * 3) / 4));
2107 int l
= ceph_unarmor(bp
.c_str(), bp
.c_str() + bp
.length(), e
.c_str(), e
.c_str() + e
.length());
2109 std::ostringstream oss
;
2110 oss
<< "decode_base64: decoding failed:\n";
2112 throw buffer::malformed_input(oss
.str().c_str());
2114 assert(l
<= (int)bp
.length());
2116 push_back(std::move(bp
));
2121 int buffer::list::read_file(const char *fn
, std::string
*error
)
2123 int fd
= TEMP_FAILURE_RETRY(::open(fn
, O_RDONLY
));
2126 std::ostringstream oss
;
2127 oss
<< "can't open " << fn
<< ": " << cpp_strerror(err
);
2133 memset(&st
, 0, sizeof(st
));
2134 if (::fstat(fd
, &st
) < 0) {
2136 std::ostringstream oss
;
2137 oss
<< "bufferlist::read_file(" << fn
<< "): stat error: "
2138 << cpp_strerror(err
);
2140 VOID_TEMP_FAILURE_RETRY(::close(fd
));
2144 ssize_t ret
= read_fd(fd
, st
.st_size
);
2146 std::ostringstream oss
;
2147 oss
<< "bufferlist::read_file(" << fn
<< "): read error:"
2148 << cpp_strerror(ret
);
2150 VOID_TEMP_FAILURE_RETRY(::close(fd
));
2153 else if (ret
!= st
.st_size
) {
2155 // Perhaps the file changed between stat() and read()?
2156 std::ostringstream oss
;
2157 oss
<< "bufferlist::read_file(" << fn
<< "): warning: got premature EOF.";
2159 // not actually an error, but weird
2161 VOID_TEMP_FAILURE_RETRY(::close(fd
));
2165 ssize_t
buffer::list::read_fd(int fd
, size_t len
)
2167 // try zero copy first
2168 if (false && read_fd_zero_copy(fd
, len
) == 0) {
2169 // TODO fix callers to not require correct read size, which is not
2170 // available for raw_pipe until we actually inspect the data
2173 bufferptr bp
= buffer::create(len
);
2174 ssize_t ret
= safe_read(fd
, (void*)bp
.c_str(), len
);
2177 append(std::move(bp
));
2182 int buffer::list::read_fd_zero_copy(int fd
, size_t len
)
2184 #ifdef CEPH_HAVE_SPLICE
2186 append(buffer::create_zero_copy(len
, fd
, NULL
));
2187 } catch (buffer::error_code
&e
) {
2189 } catch (buffer::malformed_input
&e
) {
2198 int buffer::list::write_file(const char *fn
, int mode
)
2200 int fd
= TEMP_FAILURE_RETRY(::open(fn
, O_WRONLY
|O_CREAT
|O_TRUNC
, mode
));
2203 cerr
<< "bufferlist::write_file(" << fn
<< "): failed to open file: "
2204 << cpp_strerror(err
) << std::endl
;
2207 int ret
= write_fd(fd
);
2209 cerr
<< "bufferlist::write_fd(" << fn
<< "): write_fd error: "
2210 << cpp_strerror(ret
) << std::endl
;
2211 VOID_TEMP_FAILURE_RETRY(::close(fd
));
2214 if (TEMP_FAILURE_RETRY(::close(fd
))) {
2216 cerr
<< "bufferlist::write_file(" << fn
<< "): close error: "
2217 << cpp_strerror(err
) << std::endl
;
2223 static int do_writev(int fd
, struct iovec
*vec
, uint64_t offset
, unsigned veclen
, unsigned bytes
)
2228 r
= ::pwritev(fd
, vec
, veclen
, offset
);
2230 r
= ::lseek64(fd
, offset
, SEEK_SET
);
2235 r
= ::writev(fd
, vec
, veclen
);
2245 if (bytes
== 0) break;
2248 if (vec
[0].iov_len
<= (size_t)r
) {
2249 // drain this whole item
2250 r
-= vec
[0].iov_len
;
2254 vec
[0].iov_base
= (char *)vec
[0].iov_base
+ r
;
2255 vec
[0].iov_len
-= r
;
2263 int buffer::list::write_fd(int fd
) const
2265 if (can_zero_copy())
2266 return write_fd_zero_copy(fd
);
2273 std::list
<ptr
>::const_iterator p
= _buffers
.begin();
2274 while (p
!= _buffers
.end()) {
2275 if (p
->length() > 0) {
2276 iov
[iovlen
].iov_base
= (void *)p
->c_str();
2277 iov
[iovlen
].iov_len
= p
->length();
2278 bytes
+= p
->length();
2283 if (iovlen
== IOV_MAX
-1 ||
2284 p
== _buffers
.end()) {
2289 wrote
= ::writev(fd
, start
, num
);
2296 if (wrote
< bytes
) {
2297 // partial write, recover!
2298 while ((size_t)wrote
>= start
[0].iov_len
) {
2299 wrote
-= start
[0].iov_len
;
2300 bytes
-= start
[0].iov_len
;
2305 start
[0].iov_len
-= wrote
;
2306 start
[0].iov_base
= (char *)start
[0].iov_base
+ wrote
;
2318 int buffer::list::write_fd(int fd
, uint64_t offset
) const
2322 std::list
<ptr
>::const_iterator p
= _buffers
.begin();
2323 uint64_t left_pbrs
= _buffers
.size();
2326 unsigned iovlen
= 0;
2327 uint64_t size
= MIN(left_pbrs
, IOV_MAX
);
2330 iov
[iovlen
].iov_base
= (void *)p
->c_str();
2331 iov
[iovlen
].iov_len
= p
->length();
2333 bytes
+= p
->length();
2338 int r
= do_writev(fd
, iov
, offset
, iovlen
, bytes
);
2346 int buffer::list::write_fd_zero_copy(int fd
) const
2348 if (!can_zero_copy())
2350 /* pass offset to each call to avoid races updating the fd seek
2351 * position, since the I/O may be non-blocking
2353 int64_t offset
= ::lseek(fd
, 0, SEEK_CUR
);
2354 int64_t *off_p
= &offset
;
2355 if (offset
< 0 && errno
!= ESPIPE
)
2357 if (errno
== ESPIPE
)
2359 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
2360 it
!= _buffers
.end(); ++it
) {
2361 int r
= it
->zero_copy_to_fd(fd
, off_p
);
2365 offset
+= it
->length();
2370 __u32
buffer::list::crc32c(__u32 crc
) const
2372 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
2373 it
!= _buffers
.end();
2376 raw
*r
= it
->get_raw();
2377 pair
<size_t, size_t> ofs(it
->offset(), it
->offset() + it
->length());
2378 pair
<uint32_t, uint32_t> ccrc
;
2379 if (r
->get_crc(ofs
, &ccrc
)) {
2380 if (ccrc
.first
== crc
) {
2383 if (buffer_track_crc
)
2384 buffer_cached_crc
.inc();
2386 /* If we have cached crc32c(buf, v) for initial value v,
2387 * we can convert this to a different initial value v' by:
2388 * crc32c(buf, v') = crc32c(buf, v) ^ adjustment
2389 * where adjustment = crc32c(0*len(buf), v ^ v')
2391 * http://crcutil.googlecode.com/files/crc-doc.1.0.pdf
2392 * note, u for our crc32c implementation is 0
2394 crc
= ccrc
.second
^ ceph_crc32c(ccrc
.first
^ crc
, NULL
, it
->length());
2395 if (buffer_track_crc
)
2396 buffer_cached_crc_adjusted
.inc();
2399 if (buffer_track_crc
)
2400 buffer_missed_crc
.inc();
2401 uint32_t base
= crc
;
2402 crc
= ceph_crc32c(crc
, (unsigned char*)it
->c_str(), it
->length());
2403 r
->set_crc(ofs
, make_pair(base
, crc
));
2410 void buffer::list::invalidate_crc()
2412 for (std::list
<ptr
>::const_iterator p
= _buffers
.begin(); p
!= _buffers
.end(); ++p
) {
2413 raw
*r
= p
->get_raw();
2415 r
->invalidate_crc();
2421 * Binary write all contents to a C++ stream
2423 void buffer::list::write_stream(std::ostream
&out
) const
2425 for (std::list
<ptr
>::const_iterator p
= _buffers
.begin(); p
!= _buffers
.end(); ++p
) {
2426 if (p
->length() > 0) {
2427 out
.write(p
->c_str(), p
->length());
2433 void buffer::list::hexdump(std::ostream
&out
, bool trailing_newline
) const
2438 std::ios_base::fmtflags original_flags
= out
.flags();
2440 // do our best to match the output of hexdump -C, for better
2443 out
.setf(std::ios::right
);
2447 bool was_zeros
= false, did_star
= false;
2448 for (unsigned o
=0; o
<length(); o
+= per
) {
2449 bool row_is_zeros
= false;
2450 if (o
+ per
< length()) {
2451 row_is_zeros
= true;
2452 for (unsigned i
=0; i
<per
&& o
+i
<length(); i
++) {
2454 row_is_zeros
= false;
2473 out
<< std::hex
<< std::setw(8) << o
<< " ";
2476 for (i
=0; i
<per
&& o
+i
<length(); i
++) {
2479 out
<< " " << std::setw(2) << ((unsigned)(*this)[o
+i
] & 0xff);
2481 for (; i
<per
; i
++) {
2488 for (i
=0; i
<per
&& o
+i
<length(); i
++) {
2489 char c
= (*this)[o
+i
];
2490 if (isupper(c
) || islower(c
) || isdigit(c
) || c
== ' ' || ispunct(c
))
2495 out
<< '|' << std::dec
;
2497 if (trailing_newline
) {
2498 out
<< "\n" << std::hex
<< std::setw(8) << length();
2502 out
.flags(original_flags
);
2505 std::ostream
& buffer::operator<<(std::ostream
& out
, const buffer::raw
&r
) {
2506 return out
<< "buffer::raw(" << (void*)r
.data
<< " len " << r
.len
<< " nref " << r
.nref
.read() << ")";
2509 std::ostream
& buffer::operator<<(std::ostream
& out
, const buffer::ptr
& bp
) {
2511 out
<< "buffer::ptr(" << bp
.offset() << "~" << bp
.length()
2512 << " " << (void*)bp
.c_str()
2513 << " in raw " << (void*)bp
.raw_c_str()
2514 << " len " << bp
.raw_length()
2515 << " nref " << bp
.raw_nref() << ")";
2517 out
<< "buffer:ptr(" << bp
.offset() << "~" << bp
.length() << " no raw)";
2521 std::ostream
& buffer::operator<<(std::ostream
& out
, const buffer::list
& bl
) {
2522 out
<< "buffer::list(len=" << bl
.length() << "," << std::endl
;
2524 std::list
<buffer::ptr
>::const_iterator it
= bl
.buffers().begin();
2525 while (it
!= bl
.buffers().end()) {
2527 if (++it
== bl
.buffers().end()) break;
2528 out
<< "," << std::endl
;
2530 out
<< std::endl
<< ")";
2534 std::ostream
& buffer::operator<<(std::ostream
& out
, const buffer::error
& e
)
2536 return out
<< e
.what();
2539 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_malloc
, buffer_raw_malloc
,
2541 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_mmap_pages
, buffer_raw_mmap_pagse
,
2543 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_posix_aligned
,
2544 buffer_raw_posix_aligned
, buffer_meta
);
2545 #ifdef CEPH_HAVE_SPLICE
2546 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_pipe
, buffer_raw_pipe
, buffer_meta
);
2548 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_char
, buffer_raw_char
, buffer_meta
);
2549 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_unshareable
, buffer_raw_unshareable
,
2551 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_static
, buffer_raw_static
,