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/assert.h"
16 #include "include/buffer.h"
17 #include "include/encoding.h"
22 #include <boost/static_assert.hpp>
26 template <uint8_t _bit_count>
30 static const uint8_t BITS_PER_BYTE = 8;
31 static const uint32_t ELEMENTS_PER_BLOCK = BITS_PER_BYTE / _bit_count;
32 static const uint8_t MASK = static_cast<uint8_t>((1 << _bit_count) - 1);
35 BOOST_STATIC_ASSERT((_bit_count != 0) && !(_bit_count & (_bit_count - 1)));
36 BOOST_STATIC_ASSERT(_bit_count <= BITS_PER_BYTE);
38 static const uint32_t BLOCK_SIZE;
40 class ConstReference {
42 operator uint8_t() const;
44 friend class BitVector;
45 const BitVector &m_bit_vector;
48 ConstReference(const BitVector &bit_vector, uint64_t offset);
53 operator uint8_t() const;
54 Reference& operator=(uint8_t v);
56 friend class BitVector;
57 BitVector &m_bit_vector;
60 Reference(BitVector &bit_vector, uint64_t offset);
63 static const uint8_t BIT_COUNT = _bit_count;
67 void set_crc_enabled(bool enabled) {
68 m_crc_enabled = enabled;
72 void resize(uint64_t elements);
73 uint64_t size() const;
75 const bufferlist& get_data() const;
77 Reference operator[](uint64_t offset);
78 ConstReference operator[](uint64_t offset) const;
80 void encode_header(bufferlist& bl) const;
81 void decode_header(bufferlist::iterator& it);
82 uint64_t get_header_length() const;
84 void encode_data(bufferlist& bl, uint64_t byte_offset,
85 uint64_t byte_length) const;
86 void decode_data(bufferlist::iterator& it, uint64_t byte_offset);
87 void get_data_extents(uint64_t offset, uint64_t length,
88 uint64_t *byte_offset, uint64_t *byte_length) const;
90 void encode_footer(bufferlist& bl) const;
91 void decode_footer(bufferlist::iterator& it);
92 uint64_t get_footer_offset() const;
94 void encode(bufferlist& bl) const;
95 void decode(bufferlist::iterator& it);
96 void dump(Formatter *f) const;
98 bool operator==(const BitVector &b) const;
100 static void generate_test_instances(std::list<BitVector *> &o);
107 mutable __u32 m_header_crc;
108 mutable std::vector<__u32> m_data_crcs;
110 static void compute_index(uint64_t offset, uint64_t *index, uint64_t *shift);
114 template <uint8_t _b>
115 const uint32_t BitVector<_b>::BLOCK_SIZE = 4096;
117 template <uint8_t _b>
118 BitVector<_b>::BitVector() : m_size(0), m_crc_enabled(true), m_header_crc(0)
122 template <uint8_t _b>
123 void BitVector<_b>::clear() {
130 template <uint8_t _b>
131 void BitVector<_b>::resize(uint64_t size) {
132 uint64_t buffer_size = (size + ELEMENTS_PER_BLOCK - 1) / ELEMENTS_PER_BLOCK;
133 if (buffer_size > m_data.length()) {
134 m_data.append_zero(buffer_size - m_data.length());
135 } else if (buffer_size < m_data.length()) {
137 bl.substr_of(m_data, 0, buffer_size);
142 uint64_t block_count = (buffer_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
143 m_data_crcs.resize(block_count);
146 template <uint8_t _b>
147 uint64_t BitVector<_b>::size() const {
151 template <uint8_t _b>
152 const bufferlist& BitVector<_b>::get_data() const {
156 template <uint8_t _b>
157 void BitVector<_b>::compute_index(uint64_t offset, uint64_t *index, uint64_t *shift) {
158 *index = offset / ELEMENTS_PER_BLOCK;
159 *shift = ((ELEMENTS_PER_BLOCK - 1) - (offset % ELEMENTS_PER_BLOCK)) * _b;
162 template <uint8_t _b>
163 void BitVector<_b>::encode_header(bufferlist& bl) const {
164 bufferlist header_bl;
165 ENCODE_START(1, 1, header_bl);
166 ::encode(m_size, header_bl);
167 ENCODE_FINISH(header_bl);
168 m_header_crc = header_bl.crc32c(0);
170 ::encode(header_bl, bl);
173 template <uint8_t _b>
174 void BitVector<_b>::decode_header(bufferlist::iterator& it) {
175 bufferlist header_bl;
176 ::decode(header_bl, it);
178 bufferlist::iterator header_it = header_bl.begin();
180 DECODE_START(1, header_it);
181 ::decode(size, header_it);
182 DECODE_FINISH(header_it);
185 m_header_crc = header_bl.crc32c(0);
188 template <uint8_t _b>
189 uint64_t BitVector<_b>::get_header_length() const {
190 // 4 byte bl length, 6 byte encoding header, 8 byte size
194 template <uint8_t _b>
195 void BitVector<_b>::encode_data(bufferlist& bl, uint64_t byte_offset,
196 uint64_t byte_length) const {
197 assert(byte_offset % BLOCK_SIZE == 0);
198 assert(byte_offset + byte_length == m_data.length() ||
199 byte_length % BLOCK_SIZE == 0);
201 uint64_t end_offset = byte_offset + byte_length;
202 while (byte_offset < end_offset) {
203 uint64_t len = MIN(BLOCK_SIZE, end_offset - byte_offset);
206 bit.substr_of(m_data, byte_offset, len);
207 m_data_crcs[byte_offset / BLOCK_SIZE] = bit.crc32c(0);
209 bl.claim_append(bit);
210 byte_offset += BLOCK_SIZE;
214 template <uint8_t _b>
215 void BitVector<_b>::decode_data(bufferlist::iterator& it, uint64_t byte_offset) {
216 assert(byte_offset % BLOCK_SIZE == 0);
221 uint64_t end_offset = byte_offset + it.get_remaining();
222 if (end_offset > m_data.length()) {
223 throw buffer::end_of_buffer();
227 if (byte_offset > 0) {
228 data.substr_of(m_data, 0, byte_offset);
231 while (byte_offset < end_offset) {
232 uint64_t len = MIN(BLOCK_SIZE, end_offset - byte_offset);
235 it.copy_deep(len, ptr);
240 m_data_crcs[byte_offset / BLOCK_SIZE] != bit.crc32c(0)) {
241 throw buffer::malformed_input("invalid data block CRC");
244 byte_offset += bit.length();
247 if (m_data.length() > end_offset) {
249 tail.substr_of(m_data, end_offset, m_data.length() - end_offset);
252 assert(data.length() == m_data.length());
256 template <uint8_t _b>
257 void BitVector<_b>::get_data_extents(uint64_t offset, uint64_t length,
258 uint64_t *byte_offset,
259 uint64_t *byte_length) const {
260 // read BLOCK_SIZE-aligned chunks
261 assert(length > 0 && offset + length <= m_size);
263 compute_index(offset, byte_offset, &shift);
264 *byte_offset -= (*byte_offset % BLOCK_SIZE);
267 compute_index(offset + length - 1, &end_offset, &shift);
268 end_offset += (BLOCK_SIZE - (end_offset % BLOCK_SIZE));
269 assert(*byte_offset <= end_offset);
271 *byte_length = end_offset - *byte_offset;
272 if (*byte_offset + *byte_length > m_data.length()) {
273 *byte_length = m_data.length() - *byte_offset;
277 template <uint8_t _b>
278 void BitVector<_b>::encode_footer(bufferlist& bl) const {
279 bufferlist footer_bl;
281 ::encode(m_header_crc, footer_bl);
282 ::encode(m_data_crcs, footer_bl);
284 ::encode(footer_bl, bl);
287 template <uint8_t _b>
288 void BitVector<_b>::decode_footer(bufferlist::iterator& it) {
289 bufferlist footer_bl;
290 ::decode(footer_bl, it);
292 m_crc_enabled = (footer_bl.length() > 0);
294 bufferlist::iterator footer_it = footer_bl.begin();
297 ::decode(header_crc, footer_it);
298 if (m_header_crc != header_crc) {
299 throw buffer::malformed_input("incorrect header CRC");
302 uint64_t block_count = (m_data.length() + BLOCK_SIZE - 1) / BLOCK_SIZE;
303 ::decode(m_data_crcs, footer_it);
304 if (m_data_crcs.size() != block_count) {
305 throw buffer::malformed_input("invalid data block CRCs");
310 template <uint8_t _b>
311 uint64_t BitVector<_b>::get_footer_offset() const {
312 return get_header_length() + m_data.length();
315 template <uint8_t _b>
316 void BitVector<_b>::encode(bufferlist& bl) const {
318 encode_data(bl, 0, m_data.length());
322 template <uint8_t _b>
323 void BitVector<_b>::decode(bufferlist::iterator& it) {
327 if (m_data.length() > 0) {
328 it.copy(m_data.length(), data_bl);
333 bufferlist::iterator data_it = data_bl.begin();
334 decode_data(data_it, 0);
337 template <uint8_t _b>
338 void BitVector<_b>::dump(Formatter *f) const {
339 f->dump_unsigned("size", m_size);
340 f->open_array_section("bit_table");
341 for (unsigned i = 0; i < m_data.length(); ++i) {
342 f->dump_format("byte", "0x%02hhX", m_data[i]);
347 template <uint8_t _b>
348 bool BitVector<_b>::operator==(const BitVector &b) const {
349 return (this->m_size == b.m_size && this->m_data == b.m_data);
352 template <uint8_t _b>
353 typename BitVector<_b>::Reference BitVector<_b>::operator[](uint64_t offset) {
354 return Reference(*this, offset);
357 template <uint8_t _b>
358 typename BitVector<_b>::ConstReference BitVector<_b>::operator[](uint64_t offset) const {
359 return ConstReference(*this, offset);
362 template <uint8_t _b>
363 BitVector<_b>::ConstReference::ConstReference(const BitVector<_b> &bit_vector,
365 : m_bit_vector(bit_vector), m_offset(offset)
369 template <uint8_t _b>
370 BitVector<_b>::ConstReference::operator uint8_t() const {
373 this->m_bit_vector.compute_index(this->m_offset, &index, &shift);
375 return (this->m_bit_vector.m_data[index] >> shift) & MASK;
378 template <uint8_t _b>
379 BitVector<_b>::Reference::Reference(BitVector<_b> &bit_vector, uint64_t offset)
380 : m_bit_vector(bit_vector), m_offset(offset)
384 template <uint8_t _b>
385 BitVector<_b>::Reference::operator uint8_t() const {
388 this->m_bit_vector.compute_index(this->m_offset, &index, &shift);
390 return (this->m_bit_vector.m_data[index] >> shift) & MASK;
393 template <uint8_t _b>
394 typename BitVector<_b>::Reference& BitVector<_b>::Reference::operator=(uint8_t v) {
397 this->m_bit_vector.compute_index(this->m_offset, &index, &shift);
399 uint8_t mask = MASK << shift;
400 char packed_value = (this->m_bit_vector.m_data[index] & ~mask) |
401 ((v << shift) & mask);
402 this->m_bit_vector.m_data.copy_in(index, 1, &packed_value);
406 template <uint8_t _b>
407 void BitVector<_b>::generate_test_instances(std::list<BitVector *> &o) {
408 o.push_back(new BitVector());
410 BitVector *b = new BitVector();
411 const uint64_t radix = 1 << b->BIT_COUNT;
412 const uint64_t size = 1024;
415 for (uint64_t i = 0; i < size; ++i) {
416 (*b)[i] = rand() % radix;
423 WRITE_CLASS_ENCODER(ceph::BitVector<2>)
425 template <uint8_t _b>
426 inline std::ostream& operator<<(std::ostream& out, const ceph::BitVector<_b> &b)
428 out << "ceph::BitVector<" << _b << ">(size=" << b.size() << ", data="
429 << b.get_data() << ")";
433 #endif // BIT_VECTOR_HPP