mirror of https://github.com/aria2/aria2
2010-06-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Renamed member variable * src/BitfieldMan.cc * src/BitfieldMan.hpull/1/head
parent
8046476dfc
commit
75de9168bd
|
@ -1,3 +1,9 @@
|
||||||
|
2010-06-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Renamed member variable
|
||||||
|
* src/BitfieldMan.cc
|
||||||
|
* src/BitfieldMan.h
|
||||||
|
|
||||||
2010-06-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2010-06-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Renamed member variable
|
Renamed member variable
|
||||||
|
|
|
@ -46,51 +46,51 @@ using namespace aria2::expr;
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
BitfieldMan::BitfieldMan(size_t blockLength, uint64_t totalLength)
|
BitfieldMan::BitfieldMan(size_t blockLength, uint64_t totalLength)
|
||||||
:blockLength(blockLength),
|
:_blockLength(blockLength),
|
||||||
totalLength(totalLength),
|
_totalLength(totalLength),
|
||||||
bitfieldLength(0),
|
_bitfieldLength(0),
|
||||||
blocks(0),
|
_blocks(0),
|
||||||
filterEnabled(false),
|
_filterEnabled(false),
|
||||||
bitfield(0),
|
_bitfield(0),
|
||||||
useBitfield(0),
|
_useBitfield(0),
|
||||||
filterBitfield(0),
|
_filterBitfield(0),
|
||||||
cachedNumMissingBlock(0),
|
_cachedNumMissingBlock(0),
|
||||||
cachedNumFilteredBlock(0),
|
_cachedNumFilteredBlock(0),
|
||||||
cachedCompletedLength(0),
|
_cachedCompletedLength(0),
|
||||||
cachedFilteredComletedLength(0),
|
_cachedFilteredCompletedLength(0),
|
||||||
cachedFilteredTotalLength(0)
|
_cachedFilteredTotalLength(0)
|
||||||
{
|
{
|
||||||
if(blockLength > 0 && totalLength > 0) {
|
if(_blockLength > 0 && _totalLength > 0) {
|
||||||
blocks = totalLength/blockLength+(totalLength%blockLength ? 1 : 0);
|
_blocks = _totalLength/_blockLength+(_totalLength%_blockLength ? 1 : 0);
|
||||||
bitfieldLength = blocks/8+(blocks%8 ? 1 : 0);
|
_bitfieldLength = _blocks/8+(_blocks%8 ? 1 : 0);
|
||||||
bitfield = new unsigned char[bitfieldLength];
|
_bitfield = new unsigned char[_bitfieldLength];
|
||||||
useBitfield = new unsigned char[bitfieldLength];
|
_useBitfield = new unsigned char[_bitfieldLength];
|
||||||
memset(bitfield, 0, bitfieldLength);
|
memset(_bitfield, 0, _bitfieldLength);
|
||||||
memset(useBitfield, 0, bitfieldLength);
|
memset(_useBitfield, 0, _bitfieldLength);
|
||||||
updateCache();
|
updateCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan)
|
BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan)
|
||||||
:blockLength(bitfieldMan.blockLength),
|
:_blockLength(bitfieldMan._blockLength),
|
||||||
totalLength(bitfieldMan.totalLength),
|
_totalLength(bitfieldMan._totalLength),
|
||||||
bitfieldLength(bitfieldMan.bitfieldLength),
|
_bitfieldLength(bitfieldMan._bitfieldLength),
|
||||||
blocks(bitfieldMan.blocks),
|
_blocks(bitfieldMan._blocks),
|
||||||
filterEnabled(bitfieldMan.filterEnabled),
|
_filterEnabled(bitfieldMan._filterEnabled),
|
||||||
bitfield(new unsigned char[bitfieldLength]),
|
_bitfield(new unsigned char[_bitfieldLength]),
|
||||||
useBitfield(new unsigned char[bitfieldLength]),
|
_useBitfield(new unsigned char[_bitfieldLength]),
|
||||||
filterBitfield(0),
|
_filterBitfield(0),
|
||||||
cachedNumMissingBlock(0),
|
_cachedNumMissingBlock(0),
|
||||||
cachedNumFilteredBlock(0),
|
_cachedNumFilteredBlock(0),
|
||||||
cachedCompletedLength(0),
|
_cachedCompletedLength(0),
|
||||||
cachedFilteredComletedLength(0),
|
_cachedFilteredCompletedLength(0),
|
||||||
cachedFilteredTotalLength(0)
|
_cachedFilteredTotalLength(0)
|
||||||
{
|
{
|
||||||
memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
|
memcpy(_bitfield, bitfieldMan._bitfield, _bitfieldLength);
|
||||||
memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
|
memcpy(_useBitfield, bitfieldMan._useBitfield, _bitfieldLength);
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
filterBitfield = new unsigned char[bitfieldLength];
|
_filterBitfield = new unsigned char[_bitfieldLength];
|
||||||
memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
|
memcpy(_filterBitfield, bitfieldMan._filterBitfield, _bitfieldLength);
|
||||||
}
|
}
|
||||||
updateCache();
|
updateCache();
|
||||||
}
|
}
|
||||||
|
@ -98,26 +98,26 @@ BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan)
|
||||||
BitfieldMan& BitfieldMan::operator=(const BitfieldMan& bitfieldMan)
|
BitfieldMan& BitfieldMan::operator=(const BitfieldMan& bitfieldMan)
|
||||||
{
|
{
|
||||||
if(this != &bitfieldMan) {
|
if(this != &bitfieldMan) {
|
||||||
blockLength = bitfieldMan.blockLength;
|
_blockLength = bitfieldMan._blockLength;
|
||||||
totalLength = bitfieldMan.totalLength;
|
_totalLength = bitfieldMan._totalLength;
|
||||||
blocks = bitfieldMan.blocks;
|
_blocks = bitfieldMan._blocks;
|
||||||
bitfieldLength = bitfieldMan.bitfieldLength;
|
_bitfieldLength = bitfieldMan._bitfieldLength;
|
||||||
filterEnabled = bitfieldMan.filterEnabled;
|
_filterEnabled = bitfieldMan._filterEnabled;
|
||||||
|
|
||||||
delete [] bitfield;
|
delete [] _bitfield;
|
||||||
bitfield = new unsigned char[bitfieldLength];
|
_bitfield = new unsigned char[_bitfieldLength];
|
||||||
memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
|
memcpy(_bitfield, bitfieldMan._bitfield, _bitfieldLength);
|
||||||
|
|
||||||
delete [] useBitfield;
|
delete [] _useBitfield;
|
||||||
useBitfield = new unsigned char[bitfieldLength];
|
_useBitfield = new unsigned char[_bitfieldLength];
|
||||||
memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
|
memcpy(_useBitfield, bitfieldMan._useBitfield, _bitfieldLength);
|
||||||
|
|
||||||
delete [] filterBitfield;
|
delete [] _filterBitfield;
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
filterBitfield = new unsigned char[bitfieldLength];
|
_filterBitfield = new unsigned char[_bitfieldLength];
|
||||||
memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
|
memcpy(_filterBitfield, bitfieldMan._filterBitfield, _bitfieldLength);
|
||||||
} else {
|
} else {
|
||||||
filterBitfield = 0;
|
_filterBitfield = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCache();
|
updateCache();
|
||||||
|
@ -126,16 +126,16 @@ BitfieldMan& BitfieldMan::operator=(const BitfieldMan& bitfieldMan)
|
||||||
}
|
}
|
||||||
|
|
||||||
BitfieldMan::~BitfieldMan() {
|
BitfieldMan::~BitfieldMan() {
|
||||||
delete [] bitfield;
|
delete [] _bitfield;
|
||||||
delete [] useBitfield;
|
delete [] _useBitfield;
|
||||||
delete [] filterBitfield;
|
delete [] _filterBitfield;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BitfieldMan::getBlockLength(size_t index) const
|
size_t BitfieldMan::getBlockLength(size_t index) const
|
||||||
{
|
{
|
||||||
if(index == blocks-1) {
|
if(index == _blocks-1) {
|
||||||
return getLastBlockLength();
|
return getLastBlockLength();
|
||||||
} else if(index < blocks-1) {
|
} else if(index < _blocks-1) {
|
||||||
return getBlockLength();
|
return getBlockLength();
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -145,14 +145,14 @@ size_t BitfieldMan::getBlockLength(size_t index) const
|
||||||
bool BitfieldMan::hasMissingPiece
|
bool BitfieldMan::hasMissingPiece
|
||||||
(const unsigned char* peerBitfield, size_t length) const
|
(const unsigned char* peerBitfield, size_t length) const
|
||||||
{
|
{
|
||||||
if(bitfieldLength != length) {
|
if(_bitfieldLength != length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool retval = false;
|
bool retval = false;
|
||||||
for(size_t i = 0; i < bitfieldLength; ++i) {
|
for(size_t i = 0; i < _bitfieldLength; ++i) {
|
||||||
unsigned char temp = peerBitfield[i] & ~bitfield[i];
|
unsigned char temp = peerBitfield[i] & ~_bitfield[i];
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
temp &= filterBitfield[i];
|
temp &= _filterBitfield[i];
|
||||||
}
|
}
|
||||||
if(temp&0xff) {
|
if(temp&0xff) {
|
||||||
retval = true;
|
retval = true;
|
||||||
|
@ -164,37 +164,37 @@ bool BitfieldMan::hasMissingPiece
|
||||||
|
|
||||||
bool BitfieldMan::getFirstMissingUnusedIndex(size_t& index) const
|
bool BitfieldMan::getFirstMissingUnusedIndex(size_t& index) const
|
||||||
{
|
{
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
return bitfield::getFirstMissingIndex
|
return bitfield::getFirstMissingIndex
|
||||||
(index, ~array(bitfield)&~array(useBitfield)&array(filterBitfield),
|
(index, ~array(_bitfield)&~array(_useBitfield)&array(_filterBitfield),
|
||||||
blocks);
|
_blocks);
|
||||||
} else {
|
} else {
|
||||||
return bitfield::getFirstMissingIndex
|
return bitfield::getFirstMissingIndex
|
||||||
(index, ~array(bitfield)&~array(useBitfield), blocks);
|
(index, ~array(_bitfield)&~array(_useBitfield), _blocks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BitfieldMan::getFirstNMissingUnusedIndex
|
size_t BitfieldMan::getFirstNMissingUnusedIndex
|
||||||
(std::vector<size_t>& out, size_t n) const
|
(std::vector<size_t>& out, size_t n) const
|
||||||
{
|
{
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
return bitfield::getFirstNMissingIndex
|
return bitfield::getFirstNMissingIndex
|
||||||
(std::back_inserter(out), n,
|
(std::back_inserter(out), n,
|
||||||
~array(bitfield)&~array(useBitfield)&array(filterBitfield), blocks);
|
~array(_bitfield)&~array(_useBitfield)&array(_filterBitfield), _blocks);
|
||||||
} else {
|
} else {
|
||||||
return bitfield::getFirstNMissingIndex
|
return bitfield::getFirstNMissingIndex
|
||||||
(std::back_inserter(out), n,
|
(std::back_inserter(out), n,
|
||||||
~array(bitfield)&~array(useBitfield), blocks);
|
~array(_bitfield)&~array(_useBitfield), _blocks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::getFirstMissingIndex(size_t& index) const
|
bool BitfieldMan::getFirstMissingIndex(size_t& index) const
|
||||||
{
|
{
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
return bitfield::getFirstMissingIndex
|
return bitfield::getFirstMissingIndex
|
||||||
(index, ~array(bitfield)&array(filterBitfield), blocks);
|
(index, ~array(_bitfield)&array(_filterBitfield), _blocks);
|
||||||
} else {
|
} else {
|
||||||
return bitfield::getFirstMissingIndex(index, ~array(bitfield), blocks);
|
return bitfield::getFirstMissingIndex(index, ~array(_bitfield), _blocks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,14 +262,14 @@ bool BitfieldMan::getSparseMissingUnusedIndex
|
||||||
const unsigned char* ignoreBitfield,
|
const unsigned char* ignoreBitfield,
|
||||||
size_t ignoreBitfieldLength) const
|
size_t ignoreBitfieldLength) const
|
||||||
{
|
{
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
return aria2::getSparseMissingUnusedIndex
|
return aria2::getSparseMissingUnusedIndex
|
||||||
(index, array(ignoreBitfield)|~array(filterBitfield)|array(bitfield)|array(useBitfield),
|
(index, array(ignoreBitfield)|~array(_filterBitfield)|array(_bitfield)|array(_useBitfield),
|
||||||
useBitfield, blocks);
|
_useBitfield, _blocks);
|
||||||
} else {
|
} else {
|
||||||
return aria2::getSparseMissingUnusedIndex
|
return aria2::getSparseMissingUnusedIndex
|
||||||
(index, array(ignoreBitfield)|array(bitfield)|array(useBitfield),
|
(index, array(ignoreBitfield)|array(_bitfield)|array(_useBitfield),
|
||||||
useBitfield, blocks);
|
_useBitfield, _blocks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,12 +290,12 @@ static bool copyBitfield(unsigned char* dst, const Array& src, size_t blocks)
|
||||||
bool BitfieldMan::getAllMissingIndexes(unsigned char* misbitfield, size_t len)
|
bool BitfieldMan::getAllMissingIndexes(unsigned char* misbitfield, size_t len)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
assert(len == bitfieldLength);
|
assert(len == _bitfieldLength);
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
return copyBitfield
|
return copyBitfield
|
||||||
(misbitfield, ~array(bitfield)&array(filterBitfield), blocks);
|
(misbitfield, ~array(_bitfield)&array(_filterBitfield), _blocks);
|
||||||
} else {
|
} else {
|
||||||
return copyBitfield(misbitfield, ~array(bitfield), blocks);
|
return copyBitfield(misbitfield, ~array(_bitfield), _blocks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,18 +303,19 @@ bool BitfieldMan::getAllMissingIndexes(unsigned char* misbitfield, size_t len,
|
||||||
const unsigned char* peerBitfield,
|
const unsigned char* peerBitfield,
|
||||||
size_t peerBitfieldLength) const
|
size_t peerBitfieldLength) const
|
||||||
{
|
{
|
||||||
assert(len == bitfieldLength);
|
assert(len == _bitfieldLength);
|
||||||
if(bitfieldLength != peerBitfieldLength) {
|
if(_bitfieldLength != peerBitfieldLength) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
return copyBitfield
|
return copyBitfield
|
||||||
(misbitfield, ~array(bitfield)&array(peerBitfield)&array(filterBitfield),
|
(misbitfield,
|
||||||
blocks);
|
~array(_bitfield)&array(peerBitfield)&array(_filterBitfield),
|
||||||
|
_blocks);
|
||||||
} else {
|
} else {
|
||||||
return copyBitfield
|
return copyBitfield
|
||||||
(misbitfield, ~array(bitfield)&array(peerBitfield),
|
(misbitfield, ~array(_bitfield)&array(peerBitfield),
|
||||||
blocks);
|
_blocks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,51 +324,52 @@ bool BitfieldMan::getAllMissingUnusedIndexes(unsigned char* misbitfield,
|
||||||
const unsigned char* peerBitfield,
|
const unsigned char* peerBitfield,
|
||||||
size_t peerBitfieldLength) const
|
size_t peerBitfieldLength) const
|
||||||
{
|
{
|
||||||
assert(len == bitfieldLength);
|
assert(len == _bitfieldLength);
|
||||||
if(bitfieldLength != peerBitfieldLength) {
|
if(_bitfieldLength != peerBitfieldLength) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
return copyBitfield
|
return copyBitfield
|
||||||
(misbitfield,
|
(misbitfield,
|
||||||
~array(bitfield)&~array(useBitfield)&array(peerBitfield)&array(filterBitfield),
|
~array(_bitfield)&~array(_useBitfield)&array(peerBitfield)&
|
||||||
blocks);
|
array(_filterBitfield),
|
||||||
|
_blocks);
|
||||||
} else {
|
} else {
|
||||||
return copyBitfield
|
return copyBitfield
|
||||||
(misbitfield,
|
(misbitfield,
|
||||||
~array(bitfield)&~array(useBitfield)&array(peerBitfield),
|
~array(_bitfield)&~array(_useBitfield)&array(peerBitfield),
|
||||||
blocks);
|
_blocks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BitfieldMan::countMissingBlock() const {
|
size_t BitfieldMan::countMissingBlock() const {
|
||||||
return cachedNumMissingBlock;
|
return _cachedNumMissingBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BitfieldMan::countMissingBlockNow() const {
|
size_t BitfieldMan::countMissingBlockNow() const {
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
array_ptr<unsigned char> temp(new unsigned char[bitfieldLength]);
|
array_ptr<unsigned char> temp(new unsigned char[_bitfieldLength]);
|
||||||
for(size_t i = 0; i < bitfieldLength; ++i) {
|
for(size_t i = 0; i < _bitfieldLength; ++i) {
|
||||||
temp[i] = bitfield[i]&filterBitfield[i];
|
temp[i] = _bitfield[i]&_filterBitfield[i];
|
||||||
}
|
}
|
||||||
size_t count = bitfield::countSetBit(filterBitfield, blocks)-
|
size_t count = bitfield::countSetBit(_filterBitfield, _blocks)-
|
||||||
bitfield::countSetBit(temp, blocks);
|
bitfield::countSetBit(temp, _blocks);
|
||||||
return count;
|
return count;
|
||||||
} else {
|
} else {
|
||||||
return blocks-bitfield::countSetBit(bitfield, blocks);
|
return _blocks-bitfield::countSetBit(_bitfield, _blocks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BitfieldMan::countFilteredBlockNow() const {
|
size_t BitfieldMan::countFilteredBlockNow() const {
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
return bitfield::countSetBit(filterBitfield, blocks);
|
return bitfield::countSetBit(_filterBitfield, _blocks);
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::setBitInternal(unsigned char* bitfield, size_t index, bool on) {
|
bool BitfieldMan::setBitInternal(unsigned char* bitfield, size_t index, bool on) {
|
||||||
if(blocks <= index) { return false; }
|
if(_blocks <= index) { return false; }
|
||||||
unsigned char mask = 128 >> (index%8);
|
unsigned char mask = 128 >> (index%8);
|
||||||
if(on) {
|
if(on) {
|
||||||
bitfield[index/8] |= mask;
|
bitfield[index/8] |= mask;
|
||||||
|
@ -378,29 +380,29 @@ bool BitfieldMan::setBitInternal(unsigned char* bitfield, size_t index, bool on)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::setUseBit(size_t index) {
|
bool BitfieldMan::setUseBit(size_t index) {
|
||||||
return setBitInternal(useBitfield, index, true);
|
return setBitInternal(_useBitfield, index, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::unsetUseBit(size_t index) {
|
bool BitfieldMan::unsetUseBit(size_t index) {
|
||||||
return setBitInternal(useBitfield, index, false);
|
return setBitInternal(_useBitfield, index, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::setBit(size_t index) {
|
bool BitfieldMan::setBit(size_t index) {
|
||||||
bool b = setBitInternal(bitfield, index, true);
|
bool b = setBitInternal(_bitfield, index, true);
|
||||||
updateCache();
|
updateCache();
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::unsetBit(size_t index) {
|
bool BitfieldMan::unsetBit(size_t index) {
|
||||||
bool b = setBitInternal(bitfield, index, false);
|
bool b = setBitInternal(_bitfield, index, false);
|
||||||
updateCache();
|
updateCache();
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::isFilteredAllBitSet() const {
|
bool BitfieldMan::isFilteredAllBitSet() const {
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
for(size_t i = 0; i < bitfieldLength; ++i) {
|
for(size_t i = 0; i < _bitfieldLength; ++i) {
|
||||||
if((bitfield[i]&filterBitfield[i]) != filterBitfield[i]) {
|
if((_bitfield[i]&_filterBitfield[i]) != _filterBitfield[i]) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -426,77 +428,77 @@ static bool testAllBitSet
|
||||||
|
|
||||||
bool BitfieldMan::isAllBitSet() const
|
bool BitfieldMan::isAllBitSet() const
|
||||||
{
|
{
|
||||||
return testAllBitSet(bitfield, bitfieldLength, blocks);
|
return testAllBitSet(_bitfield, _bitfieldLength, _blocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::isAllFilterBitSet() const
|
bool BitfieldMan::isAllFilterBitSet() const
|
||||||
{
|
{
|
||||||
if(!filterBitfield) {
|
if(!_filterBitfield) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return testAllBitSet(filterBitfield, bitfieldLength, blocks);
|
return testAllBitSet(_filterBitfield, _bitfieldLength, _blocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::isBitSet(size_t index) const
|
bool BitfieldMan::isBitSet(size_t index) const
|
||||||
{
|
{
|
||||||
return bitfield::test(bitfield, blocks, index);
|
return bitfield::test(_bitfield, _blocks, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::isUseBitSet(size_t index) const
|
bool BitfieldMan::isUseBitSet(size_t index) const
|
||||||
{
|
{
|
||||||
return bitfield::test(useBitfield, blocks, index);
|
return bitfield::test(_useBitfield, _blocks, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::setBitfield(const unsigned char* bitfield, size_t bitfieldLength) {
|
void BitfieldMan::setBitfield(const unsigned char* bitfield, size_t bitfieldLength) {
|
||||||
if(this->bitfieldLength != bitfieldLength) {
|
if(_bitfieldLength != bitfieldLength) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
memcpy(this->bitfield, bitfield, this->bitfieldLength);
|
memcpy(_bitfield, bitfield, _bitfieldLength);
|
||||||
memset(this->useBitfield, 0, this->bitfieldLength);
|
memset(_useBitfield, 0, _bitfieldLength);
|
||||||
updateCache();
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::clearAllBit() {
|
void BitfieldMan::clearAllBit() {
|
||||||
memset(this->bitfield, 0, this->bitfieldLength);
|
memset(_bitfield, 0, _bitfieldLength);
|
||||||
updateCache();
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::setAllBit() {
|
void BitfieldMan::setAllBit() {
|
||||||
for(size_t i = 0; i < blocks; ++i) {
|
for(size_t i = 0; i < _blocks; ++i) {
|
||||||
setBitInternal(bitfield, i, true);
|
setBitInternal(_bitfield, i, true);
|
||||||
}
|
}
|
||||||
updateCache();
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::clearAllUseBit() {
|
void BitfieldMan::clearAllUseBit() {
|
||||||
memset(this->useBitfield, 0, this->bitfieldLength);
|
memset(_useBitfield, 0, _bitfieldLength);
|
||||||
updateCache();
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::setAllUseBit() {
|
void BitfieldMan::setAllUseBit() {
|
||||||
for(size_t i = 0; i < blocks; ++i) {
|
for(size_t i = 0; i < _blocks; ++i) {
|
||||||
setBitInternal(useBitfield, i, true);
|
setBitInternal(_useBitfield, i, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::setFilterBit(size_t index) {
|
bool BitfieldMan::setFilterBit(size_t index) {
|
||||||
return setBitInternal(filterBitfield, index, true);
|
return setBitInternal(_filterBitfield, index, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::ensureFilterBitfield()
|
void BitfieldMan::ensureFilterBitfield()
|
||||||
{
|
{
|
||||||
if(!filterBitfield) {
|
if(!_filterBitfield) {
|
||||||
filterBitfield = new unsigned char[bitfieldLength];
|
_filterBitfield = new unsigned char[_bitfieldLength];
|
||||||
memset(filterBitfield, 0, bitfieldLength);
|
memset(_filterBitfield, 0, _bitfieldLength);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::addFilter(uint64_t offset, uint64_t length) {
|
void BitfieldMan::addFilter(uint64_t offset, uint64_t length) {
|
||||||
ensureFilterBitfield();
|
ensureFilterBitfield();
|
||||||
if(length > 0) {
|
if(length > 0) {
|
||||||
size_t startBlock = offset/blockLength;
|
size_t startBlock = offset/_blockLength;
|
||||||
size_t endBlock = (offset+length-1)/blockLength;
|
size_t endBlock = (offset+length-1)/_blockLength;
|
||||||
for(size_t i = startBlock; i <= endBlock && i < blocks; i++) {
|
for(size_t i = startBlock; i <= endBlock && i < _blocks; i++) {
|
||||||
setFilterBit(i);
|
setFilterBit(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -506,10 +508,10 @@ void BitfieldMan::addFilter(uint64_t offset, uint64_t length) {
|
||||||
void BitfieldMan::removeFilter(uint64_t offset, uint64_t length) {
|
void BitfieldMan::removeFilter(uint64_t offset, uint64_t length) {
|
||||||
ensureFilterBitfield();
|
ensureFilterBitfield();
|
||||||
if(length > 0) {
|
if(length > 0) {
|
||||||
size_t startBlock = offset/blockLength;
|
size_t startBlock = offset/_blockLength;
|
||||||
size_t endBlock = (offset+length-1)/blockLength;
|
size_t endBlock = (offset+length-1)/_blockLength;
|
||||||
for(size_t i = startBlock; i <= endBlock && i < blocks; i++) {
|
for(size_t i = startBlock; i <= endBlock && i < _blocks; i++) {
|
||||||
setBitInternal(filterBitfield, i, false);
|
setBitInternal(_filterBitfield, i, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateCache();
|
updateCache();
|
||||||
|
@ -518,16 +520,16 @@ void BitfieldMan::removeFilter(uint64_t offset, uint64_t length) {
|
||||||
void BitfieldMan::addNotFilter(uint64_t offset, uint64_t length)
|
void BitfieldMan::addNotFilter(uint64_t offset, uint64_t length)
|
||||||
{
|
{
|
||||||
ensureFilterBitfield();
|
ensureFilterBitfield();
|
||||||
if(length > 0 && blocks > 0) {
|
if(length > 0 && _blocks > 0) {
|
||||||
size_t startBlock = offset/blockLength;
|
size_t startBlock = offset/_blockLength;
|
||||||
if(blocks <= startBlock) {
|
if(_blocks <= startBlock) {
|
||||||
startBlock = blocks;
|
startBlock = _blocks;
|
||||||
}
|
}
|
||||||
size_t endBlock = (offset+length-1)/blockLength;
|
size_t endBlock = (offset+length-1)/_blockLength;
|
||||||
for(size_t i = 0; i < startBlock; ++i) {
|
for(size_t i = 0; i < startBlock; ++i) {
|
||||||
setFilterBit(i);
|
setFilterBit(i);
|
||||||
}
|
}
|
||||||
for(size_t i = endBlock+1; i < blocks; ++i) {
|
for(size_t i = endBlock+1; i < _blocks; ++i) {
|
||||||
setFilterBit(i);
|
setFilterBit(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -536,61 +538,61 @@ void BitfieldMan::addNotFilter(uint64_t offset, uint64_t length)
|
||||||
|
|
||||||
void BitfieldMan::enableFilter() {
|
void BitfieldMan::enableFilter() {
|
||||||
ensureFilterBitfield();
|
ensureFilterBitfield();
|
||||||
filterEnabled = true;
|
_filterEnabled = true;
|
||||||
updateCache();
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::disableFilter() {
|
void BitfieldMan::disableFilter() {
|
||||||
filterEnabled = false;
|
_filterEnabled = false;
|
||||||
updateCache();
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::clearFilter() {
|
void BitfieldMan::clearFilter() {
|
||||||
if(filterBitfield) {
|
if(_filterBitfield) {
|
||||||
delete [] filterBitfield;
|
delete [] _filterBitfield;
|
||||||
filterBitfield = 0;
|
_filterBitfield = 0;
|
||||||
}
|
}
|
||||||
filterEnabled = false;
|
_filterEnabled = false;
|
||||||
updateCache();
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getFilteredTotalLengthNow() const {
|
uint64_t BitfieldMan::getFilteredTotalLengthNow() const {
|
||||||
if(!filterBitfield) {
|
if(!_filterBitfield) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
size_t filteredBlocks = bitfield::countSetBit(filterBitfield, blocks);
|
size_t filteredBlocks = bitfield::countSetBit(_filterBitfield, _blocks);
|
||||||
if(filteredBlocks == 0) {
|
if(filteredBlocks == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(bitfield::test(filterBitfield, blocks, blocks-1)) {
|
if(bitfield::test(_filterBitfield, _blocks, _blocks-1)) {
|
||||||
return ((uint64_t)filteredBlocks-1)*blockLength+getLastBlockLength();
|
return ((uint64_t)filteredBlocks-1)*_blockLength+getLastBlockLength();
|
||||||
} else {
|
} else {
|
||||||
return ((uint64_t)filteredBlocks)*blockLength;
|
return ((uint64_t)filteredBlocks)*_blockLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getCompletedLength(bool useFilter) const {
|
uint64_t BitfieldMan::getCompletedLength(bool useFilter) const {
|
||||||
unsigned char* temp;
|
unsigned char* temp;
|
||||||
if(useFilter) {
|
if(useFilter) {
|
||||||
temp = new unsigned char[bitfieldLength];
|
temp = new unsigned char[_bitfieldLength];
|
||||||
for(size_t i = 0; i < bitfieldLength; ++i) {
|
for(size_t i = 0; i < _bitfieldLength; ++i) {
|
||||||
temp[i] = bitfield[i];
|
temp[i] = _bitfield[i];
|
||||||
if(filterEnabled) {
|
if(_filterEnabled) {
|
||||||
temp[i] &= filterBitfield[i];
|
temp[i] &= _filterBitfield[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
temp = bitfield;
|
temp = _bitfield;
|
||||||
}
|
}
|
||||||
size_t completedBlocks = bitfield::countSetBit(temp, blocks);
|
size_t completedBlocks = bitfield::countSetBit(temp, _blocks);
|
||||||
uint64_t completedLength = 0;
|
uint64_t completedLength = 0;
|
||||||
if(completedBlocks == 0) {
|
if(completedBlocks == 0) {
|
||||||
completedLength = 0;
|
completedLength = 0;
|
||||||
} else {
|
} else {
|
||||||
if(bitfield::test(temp, blocks, blocks-1)) {
|
if(bitfield::test(temp, _blocks, _blocks-1)) {
|
||||||
completedLength = ((uint64_t)completedBlocks-1)*blockLength+getLastBlockLength();
|
completedLength = ((uint64_t)completedBlocks-1)*_blockLength+getLastBlockLength();
|
||||||
} else {
|
} else {
|
||||||
completedLength = ((uint64_t)completedBlocks)*blockLength;
|
completedLength = ((uint64_t)completedBlocks)*_blockLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(useFilter) {
|
if(useFilter) {
|
||||||
|
@ -609,11 +611,11 @@ uint64_t BitfieldMan::getFilteredCompletedLengthNow() const {
|
||||||
|
|
||||||
void BitfieldMan::updateCache()
|
void BitfieldMan::updateCache()
|
||||||
{
|
{
|
||||||
cachedNumMissingBlock = countMissingBlockNow();
|
_cachedNumMissingBlock = countMissingBlockNow();
|
||||||
cachedNumFilteredBlock = countFilteredBlockNow();
|
_cachedNumFilteredBlock = countFilteredBlockNow();
|
||||||
cachedFilteredTotalLength = getFilteredTotalLengthNow();
|
_cachedFilteredTotalLength = getFilteredTotalLengthNow();
|
||||||
cachedCompletedLength = getCompletedLengthNow();
|
_cachedCompletedLength = getCompletedLengthNow();
|
||||||
cachedFilteredComletedLength = getFilteredCompletedLengthNow();
|
_cachedFilteredCompletedLength = getFilteredCompletedLengthNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::isBitRangeSet(size_t startIndex, size_t endIndex) const
|
bool BitfieldMan::isBitRangeSet(size_t startIndex, size_t endIndex) const
|
||||||
|
@ -647,14 +649,14 @@ bool BitfieldMan::isBitSetOffsetRange(uint64_t offset, uint64_t length) const
|
||||||
if(length <= 0) {
|
if(length <= 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(totalLength <= offset) {
|
if(_totalLength <= offset) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(totalLength < offset+length) {
|
if(_totalLength < offset+length) {
|
||||||
length = totalLength-offset;
|
length = _totalLength-offset;
|
||||||
}
|
}
|
||||||
size_t startBlock = offset/blockLength;
|
size_t startBlock = offset/_blockLength;
|
||||||
size_t endBlock = (offset+length-1)/blockLength;
|
size_t endBlock = (offset+length-1)/_blockLength;
|
||||||
for(size_t i = startBlock; i <= endBlock; i++) {
|
for(size_t i = startBlock; i <= endBlock; i++) {
|
||||||
if(!isBitSet(i)) {
|
if(!isBitSet(i)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -665,11 +667,11 @@ bool BitfieldMan::isBitSetOffsetRange(uint64_t offset, uint64_t length) const
|
||||||
|
|
||||||
uint64_t BitfieldMan::getMissingUnusedLength(size_t startingIndex) const
|
uint64_t BitfieldMan::getMissingUnusedLength(size_t startingIndex) const
|
||||||
{
|
{
|
||||||
if(startingIndex < 0 || blocks <= startingIndex) {
|
if(startingIndex < 0 || _blocks <= startingIndex) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
uint64_t length = 0;
|
uint64_t length = 0;
|
||||||
for(size_t i = startingIndex; i < blocks; ++i) {
|
for(size_t i = startingIndex; i < _blocks; ++i) {
|
||||||
if(isBitSet(i) || isUseBitSet(i)) {
|
if(isBitSet(i) || isUseBitSet(i)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,21 +45,21 @@ namespace aria2 {
|
||||||
|
|
||||||
class BitfieldMan {
|
class BitfieldMan {
|
||||||
private:
|
private:
|
||||||
size_t blockLength;
|
size_t _blockLength;
|
||||||
uint64_t totalLength;
|
uint64_t _totalLength;
|
||||||
size_t bitfieldLength;
|
size_t _bitfieldLength;
|
||||||
size_t blocks;
|
size_t _blocks;
|
||||||
bool filterEnabled;
|
bool _filterEnabled;
|
||||||
unsigned char* bitfield;
|
unsigned char* _bitfield;
|
||||||
unsigned char* useBitfield;
|
unsigned char* _useBitfield;
|
||||||
unsigned char* filterBitfield;
|
unsigned char* _filterBitfield;
|
||||||
|
|
||||||
// for caching
|
// for caching
|
||||||
size_t cachedNumMissingBlock;
|
size_t _cachedNumMissingBlock;
|
||||||
size_t cachedNumFilteredBlock;
|
size_t _cachedNumFilteredBlock;
|
||||||
uint64_t cachedCompletedLength;
|
uint64_t _cachedCompletedLength;
|
||||||
uint64_t cachedFilteredComletedLength;
|
uint64_t _cachedFilteredCompletedLength;
|
||||||
uint64_t cachedFilteredTotalLength;
|
uint64_t _cachedFilteredTotalLength;
|
||||||
|
|
||||||
bool setBitInternal(unsigned char* bitfield, size_t index, bool on);
|
bool setBitInternal(unsigned char* bitfield, size_t index, bool on);
|
||||||
bool setFilterBit(size_t index);
|
bool setFilterBit(size_t index);
|
||||||
|
@ -69,7 +69,7 @@ private:
|
||||||
|
|
||||||
uint64_t getCompletedLength(bool useFilter) const;
|
uint64_t getCompletedLength(bool useFilter) const;
|
||||||
|
|
||||||
// If filterBitfield is 0, allocate bitfieldLength bytes to it and
|
// If _filterBitfield is 0, allocate _bitfieldLength bytes to it and
|
||||||
// set 0 to all bytes.
|
// set 0 to all bytes.
|
||||||
void ensureFilterBitfield();
|
void ensureFilterBitfield();
|
||||||
public:
|
public:
|
||||||
|
@ -102,19 +102,19 @@ public:
|
||||||
|
|
||||||
size_t getBlockLength() const
|
size_t getBlockLength() const
|
||||||
{
|
{
|
||||||
return blockLength;
|
return _blockLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t getLastBlockLength() const
|
size_t getLastBlockLength() const
|
||||||
{
|
{
|
||||||
return totalLength-blockLength*(blocks-1);
|
return _totalLength-_blockLength*(_blocks-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t getBlockLength(size_t index) const;
|
size_t getBlockLength(size_t index) const;
|
||||||
|
|
||||||
uint64_t getTotalLength() const { return totalLength; }
|
uint64_t getTotalLength() const { return _totalLength; }
|
||||||
|
|
||||||
// Returns true iff there is a bit index which is set in bitfield,
|
// Returns true iff there is a bit index which is set in _bitfield,
|
||||||
// but not set in this object.
|
// but not set in this object.
|
||||||
//
|
//
|
||||||
// affected by filter
|
// affected by filter
|
||||||
|
@ -181,23 +181,23 @@ public:
|
||||||
|
|
||||||
const unsigned char* getBitfield() const
|
const unsigned char* getBitfield() const
|
||||||
{
|
{
|
||||||
return bitfield;
|
return _bitfield;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t getBitfieldLength() const
|
size_t getBitfieldLength() const
|
||||||
{
|
{
|
||||||
return bitfieldLength;
|
return _bitfieldLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
// affected by filter
|
// affected by filter
|
||||||
size_t countFilteredBlock() const
|
size_t countFilteredBlock() const
|
||||||
{
|
{
|
||||||
return cachedNumFilteredBlock;
|
return _cachedNumFilteredBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t countBlock() const
|
size_t countBlock() const
|
||||||
{
|
{
|
||||||
return blocks;
|
return _blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
// affected by filter
|
// affected by filter
|
||||||
|
@ -205,7 +205,7 @@ public:
|
||||||
|
|
||||||
size_t getMaxIndex() const
|
size_t getMaxIndex() const
|
||||||
{
|
{
|
||||||
return blocks-1;
|
return _blocks-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
|
void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
|
||||||
|
@ -228,13 +228,13 @@ public:
|
||||||
void disableFilter();
|
void disableFilter();
|
||||||
bool isFilterEnabled() const
|
bool isFilterEnabled() const
|
||||||
{
|
{
|
||||||
return filterEnabled;
|
return _filterEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
// affected by filter
|
// affected by filter
|
||||||
uint64_t getFilteredTotalLength() const
|
uint64_t getFilteredTotalLength() const
|
||||||
{
|
{
|
||||||
return cachedFilteredTotalLength;
|
return _cachedFilteredTotalLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
// affected by filter
|
// affected by filter
|
||||||
|
@ -242,7 +242,7 @@ public:
|
||||||
|
|
||||||
uint64_t getCompletedLength() const
|
uint64_t getCompletedLength() const
|
||||||
{
|
{
|
||||||
return cachedCompletedLength;
|
return _cachedCompletedLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t getCompletedLengthNow() const;
|
uint64_t getCompletedLengthNow() const;
|
||||||
|
@ -250,7 +250,7 @@ public:
|
||||||
// affected by filter
|
// affected by filter
|
||||||
uint64_t getFilteredCompletedLength() const
|
uint64_t getFilteredCompletedLength() const
|
||||||
{
|
{
|
||||||
return cachedFilteredComletedLength;
|
return _cachedFilteredCompletedLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
// affected by filter
|
// affected by filter
|
||||||
|
@ -270,7 +270,7 @@ public:
|
||||||
|
|
||||||
const unsigned char* getFilterBitfield() const
|
const unsigned char* getFilterBitfield() const
|
||||||
{
|
{
|
||||||
return filterBitfield;
|
return _filterBitfield;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue