2008-05-28 04:35:02 +02:00
|
|
|
/*
|
|
|
|
|
2015-06-03 07:18:48 +02:00
|
|
|
Copyright (c) 2008-2015, Arvid Norberg
|
2008-05-28 04:35:02 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_BITFIELD_HPP_INCLUDED
|
|
|
|
#define TORRENT_BITFIELD_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
#include "libtorrent/config.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/byteswap.hpp"
|
2015-04-26 20:46:45 +02:00
|
|
|
#include "libtorrent/aux_/cpuid.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2009-02-22 22:01:51 +01:00
|
|
|
#include <cstring> // for memset and memcpy
|
2010-05-29 07:46:47 +02:00
|
|
|
#include <cstdlib> // for malloc, free and realloc
|
2012-05-28 19:54:20 +02:00
|
|
|
#include <boost/cstdint.hpp> // uint32_t
|
2014-03-24 05:20:37 +01:00
|
|
|
#include <algorithm> // for min()
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2015-05-18 07:06:07 +02:00
|
|
|
// The bitfiled type stores any number of bits as a bitfield
|
2014-07-28 06:41:40 +02:00
|
|
|
// in a heap allocated array.
|
2008-05-28 04:35:02 +02:00
|
|
|
struct TORRENT_EXPORT bitfield
|
|
|
|
{
|
2013-09-06 04:29:21 +02:00
|
|
|
// constructs a new bitfield. The default constructor creates an empty
|
|
|
|
// bitfield. ``bits`` is the size of the bitfield (specified in bits).
|
2014-04-28 06:46:33 +02:00
|
|
|
// ``val`` is the value to initialize the bits to. If not specified
|
2013-09-06 04:29:21 +02:00
|
|
|
// all bits are initialized to 0.
|
|
|
|
//
|
|
|
|
// The constructor taking a pointer ``b`` and ``bits`` copies a bitfield
|
|
|
|
// from the specified buffer, and ``bits`` number of bits (rounded up to
|
|
|
|
// the nearest byte boundry).
|
2014-07-06 21:18:00 +02:00
|
|
|
bitfield(): m_buf(NULL) {}
|
|
|
|
bitfield(int bits): m_buf(NULL)
|
2008-05-28 04:35:02 +02:00
|
|
|
{ resize(bits); }
|
2014-07-06 21:18:00 +02:00
|
|
|
bitfield(int bits, bool val): m_buf(NULL)
|
2008-05-28 04:35:02 +02:00
|
|
|
{ resize(bits, val); }
|
2014-07-06 21:18:00 +02:00
|
|
|
bitfield(char const* b, int bits): m_buf(NULL)
|
2010-03-06 08:16:39 +01:00
|
|
|
{ assign(b, bits); }
|
2014-07-06 21:18:00 +02:00
|
|
|
bitfield(bitfield const& rhs): m_buf(NULL)
|
2015-05-09 21:07:33 +02:00
|
|
|
{ assign(rhs.data(), rhs.size()); }
|
2013-12-17 01:51:01 +01:00
|
|
|
#if __cplusplus > 199711L
|
2014-07-06 21:18:00 +02:00
|
|
|
bitfield(bitfield&& rhs): m_buf(rhs.m_buf)
|
|
|
|
{ rhs.m_buf = NULL; }
|
2013-12-17 01:51:01 +01:00
|
|
|
#endif
|
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// hidden
|
2008-05-28 04:35:02 +02:00
|
|
|
~bitfield() { dealloc(); }
|
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// copy bitfield from buffer ``b`` of ``bits`` number of bits, rounded up to
|
|
|
|
// the nearest byte boundary.
|
2010-03-06 08:16:39 +01:00
|
|
|
void assign(char const* b, int bits)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
resize(bits);
|
|
|
|
if (bits > 0)
|
|
|
|
{
|
2015-04-18 04:33:39 +02:00
|
|
|
std::memcpy(m_buf, b, size_t((bits + 7) / 8));
|
2014-07-06 21:18:00 +02:00
|
|
|
clear_trailing_bits();
|
|
|
|
}
|
|
|
|
}
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// query bit at ``index``. Returns true if bit is 1, otherwise false.
|
2008-05-28 04:35:02 +02:00
|
|
|
bool operator[](int index) const
|
|
|
|
{ return get_bit(index); }
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
bool get_bit(int index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(index < size());
|
2015-08-02 01:55:28 +02:00
|
|
|
return (m_buf[index / 32] & host_to_network((0x80000000 >> (index & 31)))) != 0;
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// set bit at ``index`` to 0 (clear_bit) or 1 (set_bit).
|
2008-05-28 04:35:02 +02:00
|
|
|
void clear_bit(int index)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(index < size());
|
2015-08-02 01:55:28 +02:00
|
|
|
m_buf[index / 32] &= host_to_network(~(0x80000000 >> (index & 31)));
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
2013-09-06 04:29:21 +02:00
|
|
|
void set_bit(int index)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(index < size());
|
2015-08-02 01:55:28 +02:00
|
|
|
m_buf[index / 32] |= host_to_network((0x80000000 >> (index & 31)));
|
2013-09-06 04:29:21 +02:00
|
|
|
}
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2012-05-02 20:55:58 +02:00
|
|
|
// returns true if all bits in the bitfield are set
|
|
|
|
bool all_set() const
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
const int words = size() / 32;
|
|
|
|
for (int i = 0; i < words; ++i)
|
2012-05-02 20:55:58 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (m_buf[i] != 0xffffffff) return false;
|
2012-05-02 20:55:58 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
int rest = size() & 31;
|
2015-04-29 06:23:00 +02:00
|
|
|
if (rest > 0)
|
|
|
|
{
|
2015-08-02 01:55:28 +02:00
|
|
|
boost::uint32_t mask = host_to_network(0xffffffff << (32-rest));
|
2015-04-29 06:23:00 +02:00
|
|
|
if ((m_buf[words] & mask) != mask) return false;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-02 20:55:58 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool none_set() const
|
|
|
|
{
|
|
|
|
const int words = num_words();
|
|
|
|
for (int i = 0; i < words; ++i)
|
2012-05-02 20:55:58 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (m_buf[i] != 0) return false;
|
2012-05-02 20:55:58 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// returns the size of the bitfield in bits.
|
2014-07-06 21:18:00 +02:00
|
|
|
int size() const
|
|
|
|
{
|
2015-04-18 04:33:39 +02:00
|
|
|
return m_buf == NULL ? 0 : int(m_buf[-1]);
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int num_words() const
|
|
|
|
{
|
|
|
|
return (size() + 31) / 32;
|
|
|
|
}
|
2013-09-06 04:29:21 +02:00
|
|
|
|
|
|
|
// returns true if the bitfield has zero size.
|
2014-07-06 21:18:00 +02:00
|
|
|
bool empty() const { return m_buf == NULL ? true : m_buf[-1] == 0; }
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// returns a pointer to the internal buffer of the bitfield.
|
2015-05-09 21:07:33 +02:00
|
|
|
char const* data() const { return reinterpret_cast<char const*>(m_buf); }
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2015-05-18 03:30:32 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
|
|
|
TORRENT_DEPRECATED
|
|
|
|
char const* bytes() const { return data(); }
|
|
|
|
#endif
|
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// copy operator
|
2008-05-28 04:35:02 +02:00
|
|
|
bitfield& operator=(bitfield const& rhs)
|
|
|
|
{
|
2015-05-09 21:07:33 +02:00
|
|
|
assign(rhs.data(), rhs.size());
|
2008-05-28 04:35:02 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// count the number of bits in the bitfield that are set to 1.
|
2008-05-28 04:35:02 +02:00
|
|
|
int count() const
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
const int words = num_words();
|
|
|
|
#if TORRENT_HAS_SSE
|
2015-04-26 20:46:45 +02:00
|
|
|
if (aux::mmx_support)
|
2008-05-28 04:35:02 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int i = 0; i < words; ++i)
|
|
|
|
{
|
|
|
|
#ifdef __GNUC__
|
|
|
|
ret += __builtin_popcount(m_buf[i]);
|
|
|
|
#else
|
|
|
|
ret += _mm_popcnt_u32(m_buf[i]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif // TORRENT_HAS_SSE
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int i = 0; i < words; ++i)
|
2008-05-28 04:35:02 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
boost::uint32_t v = m_buf[i];
|
|
|
|
// from:
|
|
|
|
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
|
|
|
|
static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers
|
|
|
|
static const int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
|
|
|
|
|
|
|
|
boost::uint32_t c = v - ((v >> 1) & B[0]);
|
|
|
|
c = ((c >> S[1]) & B[1]) + (c & B[1]);
|
|
|
|
c = ((c >> S[2]) + c) & B[2];
|
|
|
|
c = ((c >> S[3]) + c) & B[3];
|
|
|
|
c = ((c >> S[4]) + c) & B[4];
|
|
|
|
ret += c;
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(ret <= size());
|
2008-05-28 04:35:02 +02:00
|
|
|
TORRENT_ASSERT(ret >= 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct const_iterator
|
|
|
|
{
|
|
|
|
friend struct bitfield;
|
|
|
|
|
|
|
|
typedef bool value_type;
|
|
|
|
typedef ptrdiff_t difference_type;
|
|
|
|
typedef bool const* pointer;
|
|
|
|
typedef bool& reference;
|
|
|
|
typedef std::forward_iterator_tag iterator_category;
|
|
|
|
|
2015-08-02 01:55:28 +02:00
|
|
|
bool operator*() { return (*buf & host_to_network(bit)) != 0; }
|
2008-05-28 04:35:02 +02:00
|
|
|
const_iterator& operator++() { inc(); return *this; }
|
|
|
|
const_iterator operator++(int)
|
|
|
|
{ const_iterator ret(*this); inc(); return ret; }
|
|
|
|
const_iterator& operator--() { dec(); return *this; }
|
|
|
|
const_iterator operator--(int)
|
|
|
|
{ const_iterator ret(*this); dec(); return ret; }
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
const_iterator(): buf(0), bit(0x80000000) {}
|
2008-05-28 04:35:02 +02:00
|
|
|
bool operator==(const_iterator const& rhs) const
|
2014-07-06 21:18:00 +02:00
|
|
|
{ return buf == rhs.buf && bit == rhs.bit; }
|
2008-05-28 04:35:02 +02:00
|
|
|
|
|
|
|
bool operator!=(const_iterator const& rhs) const
|
2014-07-06 21:18:00 +02:00
|
|
|
{ return buf != rhs.buf || bit != rhs.bit; }
|
2008-05-28 04:35:02 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void inc()
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(buf);
|
2008-05-28 04:35:02 +02:00
|
|
|
if (bit == 0x01)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
bit = 0x80000000;
|
|
|
|
++buf;
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bit >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void dec()
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(buf);
|
|
|
|
if (bit == 0x80000000)
|
2008-05-28 04:35:02 +02:00
|
|
|
{
|
|
|
|
bit = 0x01;
|
2014-07-06 21:18:00 +02:00
|
|
|
--buf;
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bit <<= 1;
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
const_iterator(boost::uint32_t const* ptr, int offset)
|
|
|
|
: buf(ptr), bit(0x80000000 >> offset) {}
|
|
|
|
boost::uint32_t const* buf;
|
|
|
|
boost::uint32_t bit;
|
2008-05-28 04:35:02 +02:00
|
|
|
};
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
const_iterator begin() const { return const_iterator(m_buf, 0); }
|
|
|
|
const_iterator end() const { return const_iterator(
|
|
|
|
m_buf + num_words() - (((size() & 31) == 0) ? 0 : 1), size() & 31); }
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// set the size of the bitfield to ``bits`` length. If the bitfield is extended,
|
|
|
|
// the new bits are initialized to ``val``.
|
2008-05-28 04:35:02 +02:00
|
|
|
void resize(int bits, bool val)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (bits == size()) return;
|
|
|
|
|
|
|
|
int s = size();
|
|
|
|
int b = size() & 31;
|
2008-05-28 04:35:02 +02:00
|
|
|
resize(bits);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (s >= size()) return;
|
|
|
|
int old_size_words = (s + 31) / 32;
|
|
|
|
int new_size_words = num_words();
|
2008-08-29 14:19:21 +02:00
|
|
|
if (val)
|
|
|
|
{
|
2015-08-02 01:55:28 +02:00
|
|
|
if (old_size_words && b) m_buf[old_size_words - 1] |= host_to_network((0xffffffff >> b));
|
2014-07-06 21:18:00 +02:00
|
|
|
if (old_size_words < new_size_words)
|
2015-04-18 04:33:39 +02:00
|
|
|
std::memset(m_buf + old_size_words, 0xff
|
|
|
|
, size_t((new_size_words - old_size_words) * 4));
|
2009-02-06 09:51:25 +01:00
|
|
|
clear_trailing_bits();
|
2008-08-29 14:19:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (old_size_words < new_size_words)
|
2015-04-18 04:33:39 +02:00
|
|
|
std::memset(m_buf + old_size_words, 0x00
|
|
|
|
, size_t((new_size_words - old_size_words) * 4));
|
2008-08-29 14:19:21 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(size() == bits);
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
void resize(int bits)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (bits == size()) return;
|
|
|
|
|
2011-01-19 09:41:39 +01:00
|
|
|
TORRENT_ASSERT(bits >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
// +1 because the first word is the size (in bits)
|
|
|
|
const int b = (bits + 31) / 32;
|
|
|
|
if (m_buf)
|
2008-05-28 04:35:02 +02:00
|
|
|
{
|
2015-05-09 05:56:57 +02:00
|
|
|
boost::uint32_t* tmp = static_cast<boost::uint32_t*>(std::realloc(m_buf-1, (b+1) * 4));
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
if (tmp == NULL) throw std::bad_alloc();
|
|
|
|
#endif
|
|
|
|
m_buf = tmp + 1;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_buf[-1] = bits;
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
2011-01-19 09:41:39 +01:00
|
|
|
else if (bits > 0)
|
2008-05-28 04:35:02 +02:00
|
|
|
{
|
2015-05-09 05:56:57 +02:00
|
|
|
boost::uint32_t* tmp = static_cast<boost::uint32_t*>(std::malloc((b+1) * 4));
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
if (tmp == NULL) throw std::bad_alloc();
|
|
|
|
#endif
|
|
|
|
m_buf = tmp + 1;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_buf[-1] = bits;
|
|
|
|
}
|
|
|
|
else if (m_buf != NULL)
|
|
|
|
{
|
|
|
|
std::free(m_buf-1);
|
|
|
|
m_buf = NULL;
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
2009-02-06 09:51:25 +01:00
|
|
|
clear_trailing_bits();
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(size() == bits);
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// set all bits in the bitfield to 1 (set_all) or 0 (clear_all).
|
|
|
|
void set_all()
|
|
|
|
{
|
2015-04-18 04:33:39 +02:00
|
|
|
std::memset(m_buf, 0xff, size_t(num_words() * 4));
|
2013-09-06 04:29:21 +02:00
|
|
|
clear_trailing_bits();
|
|
|
|
}
|
|
|
|
void clear_all()
|
|
|
|
{
|
2015-04-18 04:33:39 +02:00
|
|
|
std::memset(m_buf, 0x00, size_t(num_words() * 4));
|
2013-09-06 04:29:21 +02:00
|
|
|
}
|
2015-05-18 07:06:07 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// make the bitfield empty, of zero size.
|
2014-07-06 21:18:00 +02:00
|
|
|
void clear() { dealloc(); }
|
2009-02-03 08:46:24 +01:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
private:
|
|
|
|
|
2009-02-06 09:51:25 +01:00
|
|
|
void clear_trailing_bits()
|
|
|
|
{
|
|
|
|
// clear the tail bits in the last byte
|
2015-08-02 01:55:28 +02:00
|
|
|
if (size() & 31) m_buf[num_words() - 1] &= host_to_network(0xffffffff << (32 - (size() & 31)));
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void dealloc()
|
|
|
|
{
|
|
|
|
if (m_buf) std::free(m_buf-1);
|
|
|
|
m_buf = NULL;
|
2009-02-06 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the first element is not part of the bitfield, it's the
|
|
|
|
// number of bits. For this purpose, the m_buf actually points
|
|
|
|
// the element 1, not 0. To access the size (in bits), access
|
|
|
|
// m_buf[-1]
|
|
|
|
boost::uint32_t* m_buf;
|
2008-05-28 04:35:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_BITFIELD_HPP_INCLUDED
|
|
|
|
|