BitfieldMan.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "BitfieldMan.h"
  36. #include "Randomizer.h"
  37. #include "Util.h"
  38. #include "array_fun.h"
  39. #include <cstring>
  40. namespace aria2 {
  41. BitfieldMan::BitfieldMan(int32_t blockLength, int64_t totalLength)
  42. :blockLength(blockLength),
  43. totalLength(totalLength),
  44. bitfield(0),
  45. useBitfield(0),
  46. filterBitfield(0),
  47. bitfieldLength(0),
  48. blocks(0),
  49. filterEnabled(false),
  50. randomizer(0),
  51. cachedNumMissingBlock(0),
  52. cachedNumFilteredBlock(0),
  53. cachedCompletedLength(0),
  54. cachedFilteredComletedLength(0),
  55. cachedFilteredTotalLength(0)
  56. {
  57. if(blockLength > 0 && totalLength > 0) {
  58. blocks = totalLength/blockLength+(totalLength%blockLength ? 1 : 0);
  59. bitfieldLength = blocks/8+(blocks%8 ? 1 : 0);
  60. bitfield = new unsigned char[bitfieldLength];
  61. useBitfield = new unsigned char[bitfieldLength];
  62. memset(bitfield, 0, bitfieldLength);
  63. memset(useBitfield, 0, bitfieldLength);
  64. updateCache();
  65. }
  66. }
  67. BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan)
  68. :blockLength(0),
  69. totalLength(0),
  70. bitfield(0),
  71. useBitfield(0),
  72. filterBitfield(0),
  73. bitfieldLength(0),
  74. blocks(0),
  75. filterEnabled(false),
  76. randomizer(0),
  77. cachedNumMissingBlock(0),
  78. cachedNumFilteredBlock(0),
  79. cachedCompletedLength(0),
  80. cachedFilteredComletedLength(0),
  81. cachedFilteredTotalLength(0)
  82. {
  83. blockLength = bitfieldMan.blockLength;
  84. totalLength = bitfieldMan.totalLength;
  85. blocks = bitfieldMan.blocks;
  86. bitfieldLength = bitfieldMan.bitfieldLength;
  87. bitfield = new unsigned char[bitfieldLength];
  88. useBitfield = new unsigned char[bitfieldLength];
  89. memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
  90. memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
  91. filterEnabled = bitfieldMan.filterEnabled;
  92. if(filterBitfield) {
  93. filterBitfield = new unsigned char[bitfieldLength];
  94. memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
  95. } else {
  96. filterBitfield = 0;
  97. }
  98. this->randomizer = bitfieldMan.randomizer;
  99. updateCache();
  100. }
  101. BitfieldMan& BitfieldMan::operator=(const BitfieldMan& bitfieldMan)
  102. {
  103. if(this != &bitfieldMan) {
  104. blockLength = bitfieldMan.blockLength;
  105. totalLength = bitfieldMan.totalLength;
  106. blocks = bitfieldMan.blocks;
  107. bitfieldLength = bitfieldMan.bitfieldLength;
  108. filterEnabled = bitfieldMan.filterEnabled;
  109. delete [] bitfield;
  110. bitfield = new unsigned char[bitfieldLength];
  111. memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
  112. delete [] useBitfield;
  113. useBitfield = new unsigned char[bitfieldLength];
  114. memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
  115. delete [] filterBitfield;
  116. if(filterEnabled) {
  117. filterBitfield = new unsigned char[bitfieldLength];
  118. memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
  119. } else {
  120. filterBitfield = 0;
  121. }
  122. updateCache();
  123. }
  124. return *this;
  125. }
  126. BitfieldMan::~BitfieldMan() {
  127. delete [] bitfield;
  128. delete [] useBitfield;
  129. delete [] filterBitfield;
  130. }
  131. int32_t BitfieldMan::getBlockLength() const
  132. {
  133. return blockLength;
  134. }
  135. int32_t BitfieldMan::getLastBlockLength() const
  136. {
  137. return totalLength-blockLength*(blocks-1);
  138. }
  139. int32_t BitfieldMan::getBlockLength(int32_t index) const
  140. {
  141. if(index == blocks-1) {
  142. return getLastBlockLength();
  143. } else if(0 <= index && index < blocks-1) {
  144. return getBlockLength();
  145. } else {
  146. return 0;
  147. }
  148. }
  149. int32_t BitfieldMan::countSetBit(const unsigned char* bitfield, int32_t len) const {
  150. int32_t count = 0;
  151. int32_t size = sizeof(int32_t);
  152. for(int32_t i = 0; i < len/size; ++i) {
  153. count += Util::countBit(*(uint32_t*)&bitfield[i*size]);
  154. }
  155. for(int32_t i = len-len%size; i < len; i++) {
  156. count += Util::countBit((uint32_t)bitfield[i]);
  157. }
  158. return count;
  159. }
  160. int32_t BitfieldMan::getNthBitIndex(const unsigned char bitfield, int32_t nth) const {
  161. int32_t index = -1;
  162. for(int bs = 7; bs >= 0; bs--) {
  163. unsigned char mask = 1 << bs;
  164. if(bitfield & mask) {
  165. nth--;
  166. if(nth == 0) {
  167. index = 7-bs;
  168. break;
  169. }
  170. }
  171. }
  172. return index;
  173. }
  174. template<typename Array>
  175. int32_t
  176. BitfieldMan::getMissingIndexRandomly(const Array& bitfield,
  177. int32_t bitfieldLength) const
  178. {
  179. /*
  180. int32_t byte = (int32_t)(((double)bitfieldLength)*
  181. randomizer->getRandomNumber()/
  182. (randomizer->getMaxRandomNumber()+1.0));
  183. */
  184. int32_t byte = randomizer->getRandomNumber(bitfieldLength);
  185. unsigned char lastMask = 0;
  186. // the number of bytes in the last byte of bitfield
  187. int32_t lastByteLength = totalLength%(blockLength*8);
  188. // the number of block in the last byte of bitfield
  189. int32_t lastBlockCount = DIV_FLOOR(lastByteLength, blockLength);
  190. for(int32_t i = 0; i < lastBlockCount; ++i) {
  191. lastMask >>= 1;
  192. lastMask |= 0x80;
  193. }
  194. for(int32_t i = 0; i < bitfieldLength; ++i) {
  195. unsigned char mask;
  196. if(byte == bitfieldLength-1) {
  197. mask = lastMask;
  198. } else {
  199. mask = 0xff;
  200. }
  201. if(bitfield[byte]&mask) {
  202. int32_t index = byte*8+getNthBitIndex(bitfield[byte], 1);
  203. return index;
  204. }
  205. byte++;
  206. if(byte == bitfieldLength) {
  207. byte = 0;
  208. }
  209. }
  210. return -1;
  211. }
  212. bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, int32_t length) const {
  213. if(bitfieldLength != length) {
  214. return false;
  215. }
  216. bool retval = false;
  217. for(int32_t i = 0; i < bitfieldLength; ++i) {
  218. unsigned char temp = peerBitfield[i] & ~bitfield[i];
  219. if(filterEnabled) {
  220. temp &= filterBitfield[i];
  221. }
  222. if(temp&0xff) {
  223. retval = true;
  224. break;
  225. }
  226. }
  227. return retval;
  228. }
  229. int32_t BitfieldMan::getMissingIndex(const unsigned char* peerBitfield, int32_t length) const {
  230. if(bitfieldLength != length) {
  231. return -1;
  232. }
  233. array_fun<unsigned char> bf = array_and(array_negate(bitfield), peerBitfield);
  234. if(filterEnabled) {
  235. bf = array_and(bf, filterBitfield);
  236. }
  237. return getMissingIndexRandomly(bf, bitfieldLength);
  238. }
  239. int32_t BitfieldMan::getMissingUnusedIndex(const unsigned char* peerBitfield, int32_t length) const {
  240. if(bitfieldLength != length) {
  241. return -1;
  242. }
  243. array_fun<unsigned char> bf = array_and(array_and(array_negate(bitfield),
  244. array_negate(useBitfield)),
  245. peerBitfield);
  246. if(filterEnabled) {
  247. bf = array_and(bf, filterBitfield);
  248. }
  249. return getMissingIndexRandomly(bf, bitfieldLength);
  250. }
  251. template<typename Array>
  252. int32_t BitfieldMan::getFirstMissingIndex(const Array& bitfield, int32_t bitfieldLength) const
  253. {
  254. for(int32_t i = 0; i < bitfieldLength; ++i) {
  255. int32_t base = i*8;
  256. for(int32_t bi = 0; bi < 8 && base+bi < blocks; ++bi) {
  257. unsigned char mask = 128 >> bi;
  258. if(bitfield[i] & mask) {
  259. return base+bi;
  260. }
  261. }
  262. }
  263. return -1;
  264. }
  265. int32_t BitfieldMan::getFirstMissingUnusedIndex() const {
  266. array_fun<unsigned char> bf = array_and(array_negate(bitfield),
  267. array_negate(useBitfield));
  268. if(filterEnabled) {
  269. bf = array_and(bf, filterBitfield);
  270. }
  271. return getFirstMissingIndex(bf, bitfieldLength);
  272. }
  273. int32_t BitfieldMan::getFirstMissingIndex() const
  274. {
  275. array_fun<unsigned char> bf = array_negate(bitfield);
  276. if(filterEnabled) {
  277. bf = array_and(bf, filterBitfield);
  278. }
  279. return getFirstMissingIndex(bf, bitfieldLength);
  280. }
  281. int32_t BitfieldMan::getMissingIndex() const {
  282. array_fun<unsigned char> bf = array_negate(bitfield);
  283. if(filterEnabled) {
  284. bf = array_and(bf, filterBitfield);
  285. }
  286. return getMissingIndexRandomly(bf, bitfieldLength);
  287. }
  288. int32_t BitfieldMan::getMissingUnusedIndex() const {
  289. array_fun<unsigned char> bf = array_and(array_negate(bitfield),
  290. array_negate(useBitfield));
  291. if(filterEnabled) {
  292. bf = array_and(bf, filterBitfield);
  293. }
  294. return getMissingIndexRandomly(bf, bitfieldLength);
  295. }
  296. // [startIndex, endIndex)
  297. class Range {
  298. public:
  299. int32_t startIndex;
  300. int32_t endIndex;
  301. Range(int32_t startIndex = 0, int32_t endIndex = 0):startIndex(startIndex),
  302. endIndex(endIndex) {}
  303. int32_t getSize() const {
  304. return endIndex-startIndex;
  305. }
  306. int32_t getMidIndex() const {
  307. return (endIndex-startIndex)/2+startIndex;
  308. }
  309. bool operator<(const Range& range) const {
  310. return getSize() < range.getSize();
  311. }
  312. };
  313. int32_t BitfieldMan::getStartIndex(int32_t index) const {
  314. while(index < blocks && (isUseBitSet(index) || isBitSet(index))) {
  315. index++;
  316. }
  317. if((int32_t)blocks <= index) {
  318. return -1;
  319. } else {
  320. return index;
  321. }
  322. }
  323. int32_t BitfieldMan::getEndIndex(int32_t index) const {
  324. while(index < blocks && (!isUseBitSet(index) && !isBitSet(index))) {
  325. index++;
  326. }
  327. return index;
  328. }
  329. int32_t BitfieldMan::getSparseMissingUnusedIndex() const {
  330. Range maxRange;
  331. int32_t index = 0;
  332. Range currentRange;
  333. while(index < blocks) {
  334. currentRange.startIndex = getStartIndex(index);
  335. if(currentRange.startIndex == -1) {
  336. break;
  337. }
  338. currentRange.endIndex = getEndIndex(currentRange.startIndex);
  339. if(maxRange < currentRange) {
  340. maxRange = currentRange;
  341. }
  342. index = currentRange.endIndex;
  343. }
  344. if(maxRange.getSize()) {
  345. if(maxRange.startIndex == 0) {
  346. return 0;
  347. } else if(isUseBitSet(maxRange.startIndex-1)) {
  348. return maxRange.getMidIndex();
  349. } else {
  350. return maxRange.startIndex;
  351. }
  352. } else {
  353. return -1;
  354. }
  355. }
  356. template<typename Array>
  357. std::deque<int32_t> BitfieldMan::getAllMissingIndexes(const Array& bitfield, int32_t bitfieldLength) const
  358. {
  359. std::deque<int32_t> missingIndexes;
  360. for(int32_t i = 0; i < bitfieldLength; ++i) {
  361. int32_t base = i*8;
  362. for(int32_t bi = 0; bi < 8 && base+bi < blocks; ++bi) {
  363. unsigned char mask = 128 >> bi;
  364. if(bitfield[i] & mask) {
  365. missingIndexes.push_back(base+bi);
  366. }
  367. }
  368. }
  369. return missingIndexes;
  370. }
  371. std::deque<int32_t> BitfieldMan::getAllMissingIndexes() const {
  372. array_fun<unsigned char> bf = array_negate(bitfield);
  373. if(filterEnabled) {
  374. bf = array_and(bf, filterBitfield);
  375. }
  376. return getAllMissingIndexes(bf, bitfieldLength);
  377. }
  378. std::deque<int32_t> BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield, int32_t peerBitfieldLength) const {
  379. if(bitfieldLength != peerBitfieldLength) {
  380. return std::deque<int32_t>();
  381. }
  382. array_fun<unsigned char> bf = array_and(array_negate(bitfield),
  383. peerBitfield);
  384. if(filterEnabled) {
  385. bf = array_and(bf, filterBitfield);
  386. }
  387. return getAllMissingIndexes(bf, bitfieldLength);
  388. }
  389. int32_t BitfieldMan::countMissingBlock() const {
  390. return cachedNumMissingBlock;
  391. }
  392. int32_t BitfieldMan::countMissingBlockNow() const {
  393. if(filterEnabled) {
  394. unsigned char* temp = new unsigned char[bitfieldLength];
  395. for(int32_t i = 0; i < bitfieldLength; ++i) {
  396. temp[i] = bitfield[i]&filterBitfield[i];
  397. }
  398. int32_t count = countSetBit(filterBitfield, bitfieldLength)-
  399. countSetBit(temp, bitfieldLength);
  400. delete [] temp;
  401. return count;
  402. } else {
  403. return blocks-countSetBit(bitfield, bitfieldLength);
  404. }
  405. }
  406. int32_t BitfieldMan::countFilteredBlock() const {
  407. return cachedNumFilteredBlock;
  408. }
  409. int32_t BitfieldMan::countBlock() const {
  410. return blocks;
  411. }
  412. int32_t BitfieldMan::countFilteredBlockNow() const {
  413. if(filterEnabled) {
  414. return countSetBit(filterBitfield, bitfieldLength);
  415. } else {
  416. return 0;
  417. }
  418. }
  419. int32_t BitfieldMan::getMaxIndex() const
  420. {
  421. return blocks-1;
  422. }
  423. bool BitfieldMan::setBitInternal(unsigned char* bitfield, int32_t index, bool on) {
  424. if(blocks <= index) { return false; }
  425. unsigned char mask = 128 >> index%8;
  426. if(on) {
  427. bitfield[index/8] |= mask;
  428. } else {
  429. bitfield[index/8] &= ~mask;
  430. }
  431. return true;
  432. }
  433. bool BitfieldMan::setUseBit(int32_t index) {
  434. return setBitInternal(useBitfield, index, true);
  435. }
  436. bool BitfieldMan::unsetUseBit(int32_t index) {
  437. return setBitInternal(useBitfield, index, false);
  438. }
  439. bool BitfieldMan::setBit(int32_t index) {
  440. bool b = setBitInternal(bitfield, index, true);
  441. updateCache();
  442. return b;
  443. }
  444. bool BitfieldMan::unsetBit(int32_t index) {
  445. bool b = setBitInternal(bitfield, index, false);
  446. updateCache();
  447. return b;
  448. }
  449. bool BitfieldMan::isFilteredAllBitSet() const {
  450. if(filterEnabled) {
  451. for(int32_t i = 0; i < bitfieldLength; ++i) {
  452. if((bitfield[i]&filterBitfield[i]) != filterBitfield[i]) {
  453. return false;
  454. }
  455. }
  456. return true;
  457. } else {
  458. return isAllBitSet();
  459. }
  460. }
  461. bool BitfieldMan::isAllBitSet() const {
  462. if(bitfieldLength == 0) {
  463. return true;
  464. }
  465. for(int32_t i = 0; i < bitfieldLength-1; ++i) {
  466. if(bitfield[i] != 0xff) {
  467. return false;
  468. }
  469. }
  470. unsigned char b = ~((128 >> (blocks-1)%8)-1);
  471. if(bitfield[bitfieldLength-1] != b) {
  472. return false;
  473. }
  474. return true;
  475. }
  476. bool BitfieldMan::isBitSetInternal(const unsigned char* bitfield, int32_t index) const {
  477. if(index < 0 || blocks <= index) { return false; }
  478. unsigned char mask = 128 >> index%8;
  479. return (bitfield[index/8] & mask) != 0;
  480. }
  481. bool BitfieldMan::isBitSet(int32_t index) const {
  482. return isBitSetInternal(bitfield, index);
  483. }
  484. bool BitfieldMan::isUseBitSet(int32_t index) const {
  485. return isBitSetInternal(useBitfield, index);
  486. }
  487. void BitfieldMan::setBitfield(const unsigned char* bitfield, int32_t bitfieldLength) {
  488. if(this->bitfieldLength != bitfieldLength) {
  489. return;
  490. }
  491. memcpy(this->bitfield, bitfield, this->bitfieldLength);
  492. memset(this->useBitfield, 0, this->bitfieldLength);
  493. updateCache();
  494. }
  495. const unsigned char* BitfieldMan::getBitfield() const
  496. {
  497. return bitfield;
  498. }
  499. int32_t BitfieldMan::getBitfieldLength() const
  500. {
  501. return bitfieldLength;
  502. }
  503. void BitfieldMan::clearAllBit() {
  504. memset(this->bitfield, 0, this->bitfieldLength);
  505. updateCache();
  506. }
  507. void BitfieldMan::setAllBit() {
  508. for(int32_t i = 0; i < blocks; ++i) {
  509. setBitInternal(bitfield, i, true);
  510. }
  511. updateCache();
  512. }
  513. void BitfieldMan::clearAllUseBit() {
  514. memset(this->useBitfield, 0, this->bitfieldLength);
  515. updateCache();
  516. }
  517. void BitfieldMan::setAllUseBit() {
  518. for(int32_t i = 0; i < blocks; ++i) {
  519. setBitInternal(useBitfield, i, true);
  520. }
  521. }
  522. bool BitfieldMan::setFilterBit(int32_t index) {
  523. return setBitInternal(filterBitfield, index, true);
  524. }
  525. void BitfieldMan::addFilter(int64_t offset, int64_t length) {
  526. if(!filterBitfield) {
  527. filterBitfield = new unsigned char[bitfieldLength];
  528. memset(filterBitfield, 0, bitfieldLength);
  529. }
  530. int32_t startBlock = offset/blockLength;
  531. int32_t endBlock = (offset+length-1)/blockLength;
  532. for(int i = startBlock; i <= endBlock && i < blocks; i++) {
  533. setFilterBit(i);
  534. }
  535. updateCache();
  536. }
  537. void BitfieldMan::enableFilter() {
  538. filterEnabled = true;
  539. updateCache();
  540. }
  541. void BitfieldMan::disableFilter() {
  542. filterEnabled = false;
  543. updateCache();
  544. }
  545. void BitfieldMan::clearFilter() {
  546. if(filterBitfield) {
  547. delete [] filterBitfield;
  548. filterBitfield = 0;
  549. }
  550. filterEnabled = false;
  551. updateCache();
  552. }
  553. bool BitfieldMan::isFilterEnabled() const {
  554. return filterEnabled;
  555. }
  556. int64_t BitfieldMan::getFilteredTotalLength() const {
  557. return cachedFilteredTotalLength;
  558. }
  559. int64_t BitfieldMan::getFilteredTotalLengthNow() const {
  560. if(!filterBitfield) {
  561. return 0;
  562. }
  563. int32_t filteredBlocks = countSetBit(filterBitfield, bitfieldLength);
  564. if(filteredBlocks == 0) {
  565. return 0;
  566. }
  567. if(isBitSetInternal(filterBitfield, blocks-1)) {
  568. return ((int64_t)filteredBlocks-1)*blockLength+getLastBlockLength();
  569. } else {
  570. return ((int64_t)filteredBlocks)*blockLength;
  571. }
  572. }
  573. int64_t BitfieldMan::getCompletedLength(bool useFilter) const {
  574. unsigned char* temp = new unsigned char[bitfieldLength];
  575. if(useFilter) {
  576. for(int32_t i = 0; i < bitfieldLength; ++i) {
  577. temp[i] = bitfield[i];
  578. if(filterEnabled) {
  579. temp[i] &= filterBitfield[i];
  580. }
  581. }
  582. } else {
  583. memcpy(temp, bitfield, bitfieldLength);
  584. }
  585. int32_t completedBlocks = countSetBit(temp, bitfieldLength);
  586. int64_t completedLength = 0;
  587. if(completedBlocks == 0) {
  588. completedLength = 0;
  589. } else {
  590. if(isBitSetInternal(temp, blocks-1)) {
  591. completedLength = ((int64_t)completedBlocks-1)*blockLength+getLastBlockLength();
  592. } else {
  593. completedLength = ((int64_t)completedBlocks)*blockLength;
  594. }
  595. }
  596. delete [] temp;
  597. return completedLength;
  598. }
  599. int64_t BitfieldMan::getCompletedLength() const {
  600. return cachedCompletedLength;
  601. }
  602. int64_t BitfieldMan::getCompletedLengthNow() const {
  603. return getCompletedLength(false);
  604. }
  605. int64_t BitfieldMan::getFilteredCompletedLength() const {
  606. return cachedFilteredComletedLength;
  607. }
  608. int64_t BitfieldMan::getFilteredCompletedLengthNow() const {
  609. return getCompletedLength(true);
  610. }
  611. void BitfieldMan::updateCache()
  612. {
  613. cachedNumMissingBlock = countMissingBlockNow();
  614. cachedNumFilteredBlock = countFilteredBlockNow();
  615. cachedFilteredTotalLength = getFilteredTotalLengthNow();
  616. cachedCompletedLength = getCompletedLengthNow();
  617. cachedFilteredComletedLength = getFilteredCompletedLengthNow();
  618. }
  619. bool BitfieldMan::isBitRangeSet(int32_t startIndex, int32_t endIndex) const
  620. {
  621. for(int32_t i = startIndex; i <= endIndex; ++i) {
  622. if(!isBitSet(i)) {
  623. return false;
  624. }
  625. }
  626. return true;
  627. }
  628. void BitfieldMan::unsetBitRange(int32_t startIndex, int32_t endIndex)
  629. {
  630. for(int32_t i = startIndex; i <= endIndex; ++i) {
  631. unsetBit(i);
  632. }
  633. updateCache();
  634. }
  635. void BitfieldMan::setBitRange(int32_t startIndex, int32_t endIndex)
  636. {
  637. for(int32_t i = startIndex; i <= endIndex; ++i) {
  638. setBit(i);
  639. }
  640. updateCache();
  641. }
  642. bool BitfieldMan::isBitSetOffsetRange(int64_t offset, int64_t length) const
  643. {
  644. if(length <= 0) {
  645. return false;
  646. }
  647. if(totalLength <= offset) {
  648. return false;
  649. }
  650. if(totalLength < offset+length) {
  651. length = totalLength-offset;
  652. }
  653. int32_t startBlock = offset/blockLength;
  654. int32_t endBlock = (offset+length-1)/blockLength;
  655. for(int32_t i = startBlock; i <= endBlock; i++) {
  656. if(!isBitSet(i)) {
  657. return false;
  658. }
  659. }
  660. return true;
  661. }
  662. int64_t BitfieldMan::getMissingUnusedLength(int32_t startingIndex) const
  663. {
  664. if(startingIndex < 0 || blocks <= startingIndex) {
  665. return 0;
  666. }
  667. int64_t length = 0;
  668. for(int32_t i = startingIndex; i < blocks; ++i) {
  669. if(isBitSet(i) || isUseBitSet(i)) {
  670. break;
  671. }
  672. length += getBlockLength(i);
  673. }
  674. return length;
  675. }
  676. void BitfieldMan::setRandomizer(const SharedHandle<Randomizer>& randomizer)
  677. {
  678. this->randomizer = randomizer;
  679. }
  680. SharedHandle<Randomizer> BitfieldMan::getRandomizer() const
  681. {
  682. return randomizer;
  683. }
  684. } // namespace aria2