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) 2014 Red Hat <contact@redhat.com>
8 * LGPL2.1 (see COPYING-LGPL2.1) or later
11 #ifndef BIT_VECTOR_HPP
12 #define BIT_VECTOR_HPP
14 #include "common/Formatter.h"
15 #include "include/ceph_assert.h"
16 #include "include/encoding.h"
23 template <uint8_t _bit_count>
27 static const uint8_t BITS_PER_BYTE = 8;
28 static const uint32_t ELEMENTS_PER_BLOCK = BITS_PER_BYTE / _bit_count;
29 static const uint8_t MASK = static_cast<uint8_t>((1 << _bit_count) - 1);
32 BOOST_STATIC_ASSERT((_bit_count != 0) && !(_bit_count & (_bit_count - 1)));
33 BOOST_STATIC_ASSERT(_bit_count <= BITS_PER_BYTE);
35 template <typename DataIterator>
38 DataIterator m_data_iterator;
41 ReferenceImpl(const DataIterator& data_iterator, uint64_t shift)
42 : m_data_iterator(data_iterator), m_shift(shift) {
44 ReferenceImpl(DataIterator&& data_iterator, uint64_t shift)
45 : m_data_iterator(std::move(data_iterator)), m_shift(shift) {
49 inline operator uint8_t() const {
50 return (*m_data_iterator >> m_shift) & MASK;
56 class ConstReference : public ReferenceImpl<bufferlist::const_iterator> {
58 friend class BitVector;
60 ConstReference(const bufferlist::const_iterator& data_iterator,
62 : ReferenceImpl<bufferlist::const_iterator>(data_iterator, shift) {
64 ConstReference(bufferlist::const_iterator&& data_iterator, uint64_t shift)
65 : ReferenceImpl<bufferlist::const_iterator>(std::move(data_iterator),
70 class Reference : public ReferenceImpl<bufferlist::iterator> {
72 Reference& operator=(uint8_t v);
75 friend class BitVector;
77 Reference(const bufferlist::iterator& data_iterator, uint64_t shift)
78 : ReferenceImpl<bufferlist::iterator>(data_iterator, shift) {
80 Reference(bufferlist::iterator&& data_iterator, uint64_t shift)
81 : ReferenceImpl<bufferlist::iterator>(std::move(data_iterator), shift) {
86 template <typename BitVectorT, typename DataIterator>
89 friend class BitVector;
91 uint64_t m_offset = 0;
92 BitVectorT *m_bit_vector;
94 // cached derived values
97 DataIterator m_data_iterator;
99 IteratorImpl(BitVectorT *bit_vector, uint64_t offset)
100 : m_bit_vector(bit_vector),
101 m_data_iterator(bit_vector->m_data.begin()) {
106 inline IteratorImpl& operator++() {
110 compute_index(m_offset, &index, &m_shift);
112 ceph_assert(index == m_index || index == m_index + 1);
113 if (index > m_index) {
119 inline IteratorImpl& operator+=(uint64_t offset) {
121 compute_index(m_offset, &m_index, &m_shift);
122 if (m_offset < m_bit_vector->size()) {
123 m_data_iterator.seek(m_index);
125 m_data_iterator = m_bit_vector->m_data.end();
130 inline IteratorImpl operator++(int) {
131 IteratorImpl iterator_impl(*this);
133 return iterator_impl;
135 inline IteratorImpl operator+(uint64_t offset) {
136 IteratorImpl iterator_impl(*this);
137 iterator_impl += offset;
138 return iterator_impl;
141 inline bool operator==(const IteratorImpl& rhs) const {
142 return (m_offset == rhs.m_offset && m_bit_vector == rhs.m_bit_vector);
144 inline bool operator!=(const IteratorImpl& rhs) const {
145 return (m_offset != rhs.m_offset || m_bit_vector != rhs.m_bit_vector);
148 inline ConstReference operator*() const {
149 return ConstReference(m_data_iterator, m_shift);
151 inline Reference operator*() {
152 return Reference(m_data_iterator, m_shift);
156 typedef IteratorImpl<const BitVector,
157 bufferlist::const_iterator> ConstIterator;
158 typedef IteratorImpl<BitVector, bufferlist::iterator> Iterator;
160 static const uint32_t BLOCK_SIZE;
161 static const uint8_t BIT_COUNT = _bit_count;
165 inline ConstIterator begin() const {
166 return ConstIterator(this, 0);
168 inline ConstIterator end() const {
169 return ConstIterator(this, m_size);
171 inline Iterator begin() {
172 return Iterator(this, 0);
174 inline Iterator end() {
175 return Iterator(this, m_size);
178 void set_crc_enabled(bool enabled) {
179 m_crc_enabled = enabled;
183 void resize(uint64_t elements);
184 uint64_t size() const;
186 const bufferlist& get_data() const;
188 Reference operator[](uint64_t offset);
189 ConstReference operator[](uint64_t offset) const;
191 void encode_header(bufferlist& bl) const;
192 void decode_header(bufferlist::const_iterator& it);
193 uint64_t get_header_length() const;
195 void encode_data(bufferlist& bl, uint64_t data_byte_offset,
196 uint64_t byte_length) const;
197 void decode_data(bufferlist::const_iterator& it, uint64_t data_byte_offset);
198 void get_data_extents(uint64_t offset, uint64_t length,
199 uint64_t *data_byte_offset,
200 uint64_t *object_byte_offset,
201 uint64_t *byte_length) const;
203 void encode_footer(bufferlist& bl) const;
204 void decode_footer(bufferlist::const_iterator& it);
205 uint64_t get_footer_offset() const;
207 void decode_header_crc(bufferlist::const_iterator& it);
208 void get_header_crc_extents(uint64_t *byte_offset,
209 uint64_t *byte_length) const;
211 void encode_data_crcs(bufferlist& bl, uint64_t offset,
212 uint64_t length) const;
213 void decode_data_crcs(bufferlist::const_iterator& it, uint64_t offset);
214 void get_data_crcs_extents(uint64_t offset, uint64_t length,
215 uint64_t *byte_offset,
216 uint64_t *byte_length) const;
218 void encode(bufferlist& bl) const;
219 void decode(bufferlist::const_iterator& it);
220 void dump(Formatter *f) const;
222 bool operator==(const BitVector &b) const;
224 static void generate_test_instances(std::list<BitVector *> &o);
230 mutable __u32 m_header_crc;
232 // inhibit value-initialization when used in std::vector
237 mutable std::vector<u32_struct> m_data_crcs;
239 void resize(uint64_t elements, bool zero);
241 static void compute_index(uint64_t offset, uint64_t *index, uint64_t *shift);
245 template <uint8_t _b>
246 const uint32_t BitVector<_b>::BLOCK_SIZE = 4096;
248 template <uint8_t _b>
249 BitVector<_b>::BitVector() : m_size(0), m_crc_enabled(true), m_header_crc(0)
253 template <uint8_t _b>
254 void BitVector<_b>::clear() {
261 template <uint8_t _b>
262 void BitVector<_b>::resize(uint64_t size) {
266 template <uint8_t _b>
267 void BitVector<_b>::resize(uint64_t size, bool zero) {
268 uint64_t buffer_size = (size + ELEMENTS_PER_BLOCK - 1) / ELEMENTS_PER_BLOCK;
269 if (buffer_size > m_data.length()) {
271 m_data.append_zero(buffer_size - m_data.length());
273 m_data.append(buffer::ptr(buffer_size - m_data.length()));
275 } else if (buffer_size < m_data.length()) {
277 bl.substr_of(m_data, 0, buffer_size);
282 uint64_t block_count = (buffer_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
283 m_data_crcs.resize(block_count);
286 template <uint8_t _b>
287 uint64_t BitVector<_b>::size() const {
291 template <uint8_t _b>
292 const bufferlist& BitVector<_b>::get_data() const {
296 template <uint8_t _b>
297 void BitVector<_b>::compute_index(uint64_t offset, uint64_t *index, uint64_t *shift) {
298 *index = offset / ELEMENTS_PER_BLOCK;
299 *shift = ((ELEMENTS_PER_BLOCK - 1) - (offset % ELEMENTS_PER_BLOCK)) * _b;
302 template <uint8_t _b>
303 void BitVector<_b>::encode_header(bufferlist& bl) const {
304 bufferlist header_bl;
305 ENCODE_START(1, 1, header_bl);
306 encode(m_size, header_bl);
307 ENCODE_FINISH(header_bl);
308 m_header_crc = header_bl.crc32c(0);
310 encode(header_bl, bl);
313 template <uint8_t _b>
314 void BitVector<_b>::decode_header(bufferlist::const_iterator& it) {
316 bufferlist header_bl;
317 decode(header_bl, it);
319 auto header_it = header_bl.cbegin();
321 DECODE_START(1, header_it);
322 decode(size, header_it);
323 DECODE_FINISH(header_it);
326 m_header_crc = header_bl.crc32c(0);
329 template <uint8_t _b>
330 uint64_t BitVector<_b>::get_header_length() const {
331 // 4 byte bl length, 6 byte encoding header, 8 byte size
335 template <uint8_t _b>
336 void BitVector<_b>::encode_data(bufferlist& bl, uint64_t data_byte_offset,
337 uint64_t byte_length) const {
338 ceph_assert(data_byte_offset % BLOCK_SIZE == 0);
339 ceph_assert(data_byte_offset + byte_length == m_data.length() ||
340 byte_length % BLOCK_SIZE == 0);
342 uint64_t end_offset = data_byte_offset + byte_length;
343 while (data_byte_offset < end_offset) {
344 uint64_t len = std::min<uint64_t>(BLOCK_SIZE,
345 end_offset - data_byte_offset);
348 bit.substr_of(m_data, data_byte_offset, len);
349 m_data_crcs[data_byte_offset / BLOCK_SIZE].val = bit.crc32c(0);
351 bl.claim_append(bit);
352 data_byte_offset += BLOCK_SIZE;
356 template <uint8_t _b>
357 void BitVector<_b>::decode_data(bufferlist::const_iterator& it,
358 uint64_t data_byte_offset) {
359 ceph_assert(data_byte_offset % BLOCK_SIZE == 0);
364 uint64_t end_offset = data_byte_offset + it.get_remaining();
365 if (end_offset > m_data.length()) {
366 throw buffer::end_of_buffer();
370 if (data_byte_offset > 0) {
371 data.substr_of(m_data, 0, data_byte_offset);
374 while (data_byte_offset < end_offset) {
375 uint64_t len = std::min<uint64_t>(BLOCK_SIZE, end_offset - data_byte_offset);
378 it.copy_deep(len, ptr);
383 m_data_crcs[data_byte_offset / BLOCK_SIZE].val != bit.crc32c(0)) {
384 throw buffer::malformed_input("invalid data block CRC");
387 data_byte_offset += bit.length();
390 if (m_data.length() > end_offset) {
392 tail.substr_of(m_data, end_offset, m_data.length() - end_offset);
395 ceph_assert(data.length() == m_data.length());
399 template <uint8_t _b>
400 void BitVector<_b>::get_data_extents(uint64_t offset, uint64_t length,
401 uint64_t *data_byte_offset,
402 uint64_t *object_byte_offset,
403 uint64_t *byte_length) const {
404 // read BLOCK_SIZE-aligned chunks
405 ceph_assert(length > 0 && offset + length <= m_size);
407 compute_index(offset, data_byte_offset, &shift);
408 *data_byte_offset -= (*data_byte_offset % BLOCK_SIZE);
411 compute_index(offset + length - 1, &end_offset, &shift);
412 end_offset += (BLOCK_SIZE - (end_offset % BLOCK_SIZE));
413 ceph_assert(*data_byte_offset <= end_offset);
415 *object_byte_offset = get_header_length() + *data_byte_offset;
416 *byte_length = end_offset - *data_byte_offset;
417 if (*data_byte_offset + *byte_length > m_data.length()) {
418 *byte_length = m_data.length() - *data_byte_offset;
422 template <uint8_t _b>
423 void BitVector<_b>::encode_footer(bufferlist& bl) const {
425 bufferlist footer_bl;
427 encode(m_header_crc, footer_bl);
429 __u32 size = m_data_crcs.size();
430 encode(size, footer_bl);
431 encode_data_crcs(footer_bl, 0, m_size);
433 encode(footer_bl, bl);
436 template <uint8_t _b>
437 void BitVector<_b>::decode_footer(bufferlist::const_iterator& it) {
439 bufferlist footer_bl;
440 decode(footer_bl, it);
442 m_crc_enabled = (footer_bl.length() > 0);
444 auto footer_it = footer_bl.cbegin();
445 decode_header_crc(footer_it);
448 decode(data_src_size, footer_it);
449 decode_data_crcs(footer_it, 0);
451 uint64_t block_count = (m_data.length() + BLOCK_SIZE - 1) / BLOCK_SIZE;
452 if (m_data_crcs.size() != block_count) {
453 throw buffer::malformed_input("invalid data block CRCs");
458 template <uint8_t _b>
459 uint64_t BitVector<_b>::get_footer_offset() const {
460 return get_header_length() + m_data.length();
463 template <uint8_t _b>
464 void BitVector<_b>::decode_header_crc(bufferlist::const_iterator& it) {
465 if (it.get_remaining() > 0) {
467 ceph::decode(header_crc, it);
468 if (m_header_crc != header_crc) {
469 throw buffer::malformed_input("incorrect header CRC");
474 template <uint8_t _b>
475 void BitVector<_b>::get_header_crc_extents(uint64_t *byte_offset,
476 uint64_t *byte_length) const {
477 // footer is prefixed with a bufferlist length
478 *byte_offset = get_footer_offset() + sizeof(__u32);
479 *byte_length = sizeof(__u32);
482 template <uint8_t _b>
483 void BitVector<_b>::encode_data_crcs(bufferlist& bl, uint64_t offset,
484 uint64_t length) const {
491 compute_index(offset, &index, &shift);
492 uint64_t crc_index = index / BLOCK_SIZE;
494 compute_index(offset + length - 1, &index, &shift);
495 uint64_t end_crc_index = index / BLOCK_SIZE;
496 while (crc_index <= end_crc_index) {
497 __u32 crc = m_data_crcs[crc_index++].val;
498 ceph::encode(crc, bl);
502 template <uint8_t _b>
503 void BitVector<_b>::decode_data_crcs(bufferlist::const_iterator& it,
511 compute_index(offset, &index, &shift);
513 uint64_t crc_index = index / BLOCK_SIZE;
514 uint64_t remaining = it.get_remaining() / sizeof(__u32);
515 while (remaining > 0) {
517 ceph::decode(crc, it);
518 m_data_crcs[crc_index++].val = crc;
523 template <uint8_t _b>
524 void BitVector<_b>::get_data_crcs_extents(uint64_t offset, uint64_t length,
525 uint64_t *byte_offset,
526 uint64_t *byte_length) const {
527 // data CRCs immediately follow the header CRC
528 get_header_crc_extents(byte_offset, byte_length);
529 *byte_offset += *byte_length;
531 // skip past data CRC vector size
532 *byte_offset += sizeof(__u32);
534 // CRCs are computed over BLOCK_SIZE chunks
535 ceph_assert(length > 0 && offset + length <= m_size);
538 compute_index(offset, &index, &shift);
539 uint64_t start_byte_offset =
540 *byte_offset + ((index / BLOCK_SIZE) * sizeof(__u32));
542 compute_index(offset + length, &index, &shift);
543 uint64_t end_byte_offset =
544 *byte_offset + (((index / BLOCK_SIZE) + 1) * sizeof(__u32));
545 ceph_assert(start_byte_offset < end_byte_offset);
547 *byte_offset = start_byte_offset;
548 *byte_length = end_byte_offset - start_byte_offset;
551 template <uint8_t _b>
552 void BitVector<_b>::encode(bufferlist& bl) const {
554 encode_data(bl, 0, m_data.length());
558 template <uint8_t _b>
559 void BitVector<_b>::decode(bufferlist::const_iterator& it) {
563 if (m_data.length() > 0) {
564 it.copy(m_data.length(), data_bl);
569 auto data_it = data_bl.cbegin();
570 decode_data(data_it, 0);
573 template <uint8_t _b>
574 void BitVector<_b>::dump(Formatter *f) const {
575 f->dump_unsigned("size", m_size);
576 f->open_array_section("bit_table");
577 for (unsigned i = 0; i < m_data.length(); ++i) {
578 f->dump_format("byte", "0x%02hhX", m_data[i]);
583 template <uint8_t _b>
584 bool BitVector<_b>::operator==(const BitVector &b) const {
585 return (this->m_size == b.m_size && this->m_data == b.m_data);
588 template <uint8_t _b>
589 typename BitVector<_b>::Reference BitVector<_b>::operator[](uint64_t offset) {
592 compute_index(offset, &index, &shift);
594 bufferlist::iterator data_iterator(m_data.begin());
595 data_iterator.seek(index);
596 return Reference(std::move(data_iterator), shift);
599 template <uint8_t _b>
600 typename BitVector<_b>::ConstReference BitVector<_b>::operator[](uint64_t offset) const {
603 compute_index(offset, &index, &shift);
605 bufferlist::const_iterator data_iterator(m_data.begin());
606 data_iterator.seek(index);
607 return ConstReference(std::move(data_iterator), shift);
610 template <uint8_t _b>
611 typename BitVector<_b>::Reference& BitVector<_b>::Reference::operator=(uint8_t v) {
612 uint8_t mask = MASK << this->m_shift;
613 char packed_value = (*this->m_data_iterator & ~mask) |
614 ((v << this->m_shift) & mask);
615 bufferlist::iterator it(this->m_data_iterator);
616 it.copy_in(1, &packed_value, true);
620 template <uint8_t _b>
621 void BitVector<_b>::generate_test_instances(std::list<BitVector *> &o) {
622 o.push_back(new BitVector());
624 BitVector *b = new BitVector();
625 const uint64_t radix = 1 << b->BIT_COUNT;
626 const uint64_t size = 1024;
628 b->resize(size, false);
629 for (uint64_t i = 0; i < size; ++i) {
630 (*b)[i] = rand() % radix;
636 WRITE_CLASS_ENCODER(ceph::BitVector<2>)
638 template <uint8_t _b>
639 inline std::ostream& operator<<(std::ostream& out, const ceph::BitVector<_b> &b)
641 out << "ceph::BitVector<" << _b << ">(size=" << b.size() << ", data="
642 << b.get_data() << ")";
647 #endif // BIT_VECTOR_HPP