BitfieldMan.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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(size_t blockLength, uint64_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. cachedNumMissingBlock(0),
  51. cachedNumFilteredBlock(0),
  52. cachedCompletedLength(0),
  53. cachedFilteredComletedLength(0),
  54. cachedFilteredTotalLength(0)
  55. {
  56. if(blockLength > 0 && totalLength > 0) {
  57. blocks = totalLength/blockLength+(totalLength%blockLength ? 1 : 0);
  58. bitfieldLength = blocks/8+(blocks%8 ? 1 : 0);
  59. bitfield = new unsigned char[bitfieldLength];
  60. useBitfield = new unsigned char[bitfieldLength];
  61. memset(bitfield, 0, bitfieldLength);
  62. memset(useBitfield, 0, bitfieldLength);
  63. updateCache();
  64. }
  65. }
  66. BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan)
  67. :blockLength(0),
  68. totalLength(0),
  69. bitfield(0),
  70. useBitfield(0),
  71. filterBitfield(0),
  72. bitfieldLength(0),
  73. blocks(0),
  74. filterEnabled(false),
  75. cachedNumMissingBlock(0),
  76. cachedNumFilteredBlock(0),
  77. cachedCompletedLength(0),
  78. cachedFilteredComletedLength(0),
  79. cachedFilteredTotalLength(0)
  80. {
  81. blockLength = bitfieldMan.blockLength;
  82. totalLength = bitfieldMan.totalLength;
  83. blocks = bitfieldMan.blocks;
  84. bitfieldLength = bitfieldMan.bitfieldLength;
  85. bitfield = new unsigned char[bitfieldLength];
  86. useBitfield = new unsigned char[bitfieldLength];
  87. memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
  88. memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
  89. filterEnabled = bitfieldMan.filterEnabled;
  90. if(filterBitfield) {
  91. filterBitfield = new unsigned char[bitfieldLength];
  92. memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
  93. } else {
  94. filterBitfield = 0;
  95. }
  96. this->randomizer = bitfieldMan.randomizer;
  97. updateCache();
  98. }
  99. BitfieldMan& BitfieldMan::operator=(const BitfieldMan& bitfieldMan)
  100. {
  101. if(this != &bitfieldMan) {
  102. blockLength = bitfieldMan.blockLength;
  103. totalLength = bitfieldMan.totalLength;
  104. blocks = bitfieldMan.blocks;
  105. bitfieldLength = bitfieldMan.bitfieldLength;
  106. filterEnabled = bitfieldMan.filterEnabled;
  107. delete [] bitfield;
  108. bitfield = new unsigned char[bitfieldLength];
  109. memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
  110. delete [] useBitfield;
  111. useBitfield = new unsigned char[bitfieldLength];
  112. memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
  113. delete [] filterBitfield;
  114. if(filterEnabled) {
  115. filterBitfield = new unsigned char[bitfieldLength];
  116. memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
  117. } else {
  118. filterBitfield = 0;
  119. }
  120. updateCache();
  121. }
  122. return *this;
  123. }
  124. BitfieldMan::~BitfieldMan() {
  125. delete [] bitfield;
  126. delete [] useBitfield;
  127. delete [] filterBitfield;
  128. }
  129. size_t BitfieldMan::getBlockLength() const
  130. {
  131. return blockLength;
  132. }
  133. size_t BitfieldMan::getLastBlockLength() const
  134. {
  135. return totalLength-blockLength*(blocks-1);
  136. }
  137. size_t BitfieldMan::getBlockLength(size_t index) const
  138. {
  139. if(index == blocks-1) {
  140. return getLastBlockLength();
  141. } else if(index < blocks-1) {
  142. return getBlockLength();
  143. } else {
  144. return 0;
  145. }
  146. }
  147. size_t BitfieldMan::countSetBit(const unsigned char* bitfield, size_t len) const {
  148. size_t count = 0;
  149. size_t size = sizeof(uint32_t);
  150. size_t to = len/size;
  151. for(size_t i = 0; i < to; ++i) {
  152. count += Util::countBit(*reinterpret_cast<const uint32_t*>(&bitfield[i*size]));
  153. }
  154. for(size_t i = len-len%size; i < len; i++) {
  155. count += Util::countBit(static_cast<uint32_t>(bitfield[i]));
  156. }
  157. return count;
  158. }
  159. size_t
  160. BitfieldMan::getNthBitIndex(const unsigned char bitfield, size_t nth) const
  161. {
  162. size_t index = 0;
  163. for(size_t bs = 7; bs >= 0; bs--) {
  164. unsigned char mask = 1 << bs;
  165. if(bitfield & mask) {
  166. nth--;
  167. if(nth == 0) {
  168. index = 7-bs;
  169. break;
  170. }
  171. }
  172. }
  173. return index;
  174. }
  175. template<typename Array>
  176. bool BitfieldMan::getMissingIndexRandomly(size_t& index,
  177. const Array& bitfield,
  178. size_t bitfieldLength) const
  179. {
  180. size_t byte = randomizer->getRandomNumber(bitfieldLength);
  181. unsigned char lastMask = 0;
  182. // the number of bytes in the last byte of bitfield
  183. size_t lastByteLength = totalLength%(blockLength*8);
  184. // the number of block in the last byte of bitfield
  185. size_t lastBlockCount = DIV_FLOOR(lastByteLength, blockLength);
  186. for(size_t i = 0; i < lastBlockCount; ++i) {
  187. lastMask >>= 1;
  188. lastMask |= 0x80;
  189. }
  190. for(size_t i = 0; i < bitfieldLength; ++i) {
  191. unsigned char mask;
  192. if(byte == bitfieldLength-1) {
  193. mask = lastMask;
  194. } else {
  195. mask = 0xff;
  196. }
  197. if(bitfield[byte]&mask) {
  198. index = byte*8+getNthBitIndex(bitfield[byte], 1);
  199. return true;
  200. }
  201. byte++;
  202. if(byte == bitfieldLength) {
  203. byte = 0;
  204. }
  205. }
  206. return false;
  207. }
  208. bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, size_t length) const {
  209. if(bitfieldLength != length) {
  210. return false;
  211. }
  212. bool retval = false;
  213. for(size_t i = 0; i < bitfieldLength; ++i) {
  214. unsigned char temp = peerBitfield[i] & ~bitfield[i];
  215. if(filterEnabled) {
  216. temp &= filterBitfield[i];
  217. }
  218. if(temp&0xff) {
  219. retval = true;
  220. break;
  221. }
  222. }
  223. return retval;
  224. }
  225. bool BitfieldMan::getMissingIndex(size_t& index, const unsigned char* peerBitfield, size_t length) const {
  226. if(bitfieldLength != length) {
  227. return false;
  228. }
  229. array_fun<unsigned char> bf = array_and(array_negate(bitfield), peerBitfield);
  230. if(filterEnabled) {
  231. bf = array_and(bf, filterBitfield);
  232. }
  233. return getMissingIndexRandomly(index, bf, bitfieldLength);
  234. }
  235. bool BitfieldMan::getMissingUnusedIndex(size_t& index, const unsigned char* peerBitfield, size_t length) const {
  236. if(bitfieldLength != length) {
  237. return false;
  238. }
  239. array_fun<unsigned char> bf = array_and(array_and(array_negate(bitfield),
  240. array_negate(useBitfield)),
  241. peerBitfield);
  242. if(filterEnabled) {
  243. bf = array_and(bf, filterBitfield);
  244. }
  245. return getMissingIndexRandomly(index, bf, bitfieldLength);
  246. }
  247. template<typename Array>
  248. bool BitfieldMan::getFirstMissingIndex(size_t& index, const Array& bitfield, size_t bitfieldLength) const
  249. {
  250. for(size_t i = 0; i < bitfieldLength; ++i) {
  251. size_t base = i*8;
  252. for(size_t bi = 0; bi < 8 && base+bi < blocks; ++bi) {
  253. unsigned char mask = 128 >> bi;
  254. if(bitfield[i] & mask) {
  255. index = base+bi;
  256. return true;
  257. }
  258. }
  259. }
  260. return false;
  261. }
  262. bool BitfieldMan::getFirstMissingUnusedIndex(size_t& index) const
  263. {
  264. array_fun<unsigned char> bf = array_and(array_negate(bitfield),
  265. array_negate(useBitfield));
  266. if(filterEnabled) {
  267. bf = array_and(bf, filterBitfield);
  268. }
  269. return getFirstMissingIndex(index, bf, bitfieldLength);
  270. }
  271. bool BitfieldMan::getFirstMissingIndex(size_t& index) const
  272. {
  273. array_fun<unsigned char> bf = array_negate(bitfield);
  274. if(filterEnabled) {
  275. bf = array_and(bf, filterBitfield);
  276. }
  277. return getFirstMissingIndex(index, bf, bitfieldLength);
  278. }
  279. bool BitfieldMan::getMissingIndex(size_t& index) const {
  280. array_fun<unsigned char> bf = array_negate(bitfield);
  281. if(filterEnabled) {
  282. bf = array_and(bf, filterBitfield);
  283. }
  284. return getMissingIndexRandomly(index, bf, bitfieldLength);
  285. }
  286. bool BitfieldMan::getMissingUnusedIndex(size_t& index) const
  287. {
  288. array_fun<unsigned char> bf = array_and(array_negate(bitfield),
  289. array_negate(useBitfield));
  290. if(filterEnabled) {
  291. bf = array_and(bf, filterBitfield);
  292. }
  293. return getMissingIndexRandomly(index, bf, bitfieldLength);
  294. }
  295. // [startIndex, endIndex)
  296. class Range {
  297. public:
  298. size_t startIndex;
  299. size_t endIndex;
  300. Range(size_t startIndex = 0, size_t endIndex = 0):startIndex(startIndex),
  301. endIndex(endIndex) {}
  302. size_t getSize() const {
  303. return endIndex-startIndex;
  304. }
  305. size_t getMidIndex() const {
  306. return (endIndex-startIndex)/2+startIndex;
  307. }
  308. bool operator<(const Range& range) const {
  309. return getSize() < range.getSize();
  310. }
  311. };
  312. size_t BitfieldMan::getStartIndex(size_t index) const {
  313. while(index < blocks && (isUseBitSet(index) || isBitSet(index))) {
  314. index++;
  315. }
  316. if(blocks <= index) {
  317. return blocks;
  318. } else {
  319. return index;
  320. }
  321. }
  322. size_t BitfieldMan::getEndIndex(size_t index) const {
  323. while(index < blocks && (!isUseBitSet(index) && !isBitSet(index))) {
  324. index++;
  325. }
  326. return index;
  327. }
  328. bool BitfieldMan::getSparseMissingUnusedIndex(size_t& index) const {
  329. Range maxRange;
  330. Range currentRange;
  331. {
  332. size_t nextIndex = 0;
  333. while(nextIndex < blocks) {
  334. currentRange.startIndex = getStartIndex(nextIndex);
  335. if(currentRange.startIndex == blocks) {
  336. break;
  337. }
  338. currentRange.endIndex = getEndIndex(currentRange.startIndex);
  339. if(maxRange < currentRange) {
  340. maxRange = currentRange;
  341. }
  342. nextIndex = currentRange.endIndex;
  343. }
  344. }
  345. if(maxRange.getSize()) {
  346. if(maxRange.startIndex == 0) {
  347. index = 0;
  348. } else if(isUseBitSet(maxRange.startIndex-1)) {
  349. index = maxRange.getMidIndex();
  350. } else {
  351. index = maxRange.startIndex;
  352. }
  353. return true;
  354. } else {
  355. return false;
  356. }
  357. }
  358. template<typename Array>
  359. bool BitfieldMan::getAllMissingIndexes(std::deque<size_t>& indexes,
  360. const Array& bitfield,
  361. size_t bitfieldLength) const
  362. {
  363. for(size_t i = 0; i < bitfieldLength; ++i) {
  364. size_t base = i*8;
  365. for(size_t bi = 0; bi < 8 && base+bi < blocks; ++bi) {
  366. unsigned char mask = 128 >> bi;
  367. if(bitfield[i] & mask) {
  368. indexes.push_back(base+bi);
  369. }
  370. }
  371. }
  372. return !indexes.empty();
  373. }
  374. bool BitfieldMan::getAllMissingIndexes(std::deque<size_t>& indexes) const
  375. {
  376. array_fun<unsigned char> bf = array_negate(bitfield);
  377. if(filterEnabled) {
  378. bf = array_and(bf, filterBitfield);
  379. }
  380. return getAllMissingIndexes(indexes, bf, bitfieldLength);
  381. }
  382. bool BitfieldMan::getAllMissingIndexes(std::deque<size_t>& indexes,
  383. const unsigned char* peerBitfield,
  384. size_t peerBitfieldLength) const
  385. {
  386. if(bitfieldLength != peerBitfieldLength) {
  387. return false;
  388. }
  389. array_fun<unsigned char> bf = array_and(array_negate(bitfield),
  390. peerBitfield);
  391. if(filterEnabled) {
  392. bf = array_and(bf, filterBitfield);
  393. }
  394. return getAllMissingIndexes(indexes, bf, bitfieldLength);
  395. }
  396. bool BitfieldMan::getAllMissingUnusedIndexes(std::deque<size_t>& indexes,
  397. const unsigned char* peerBitfield,
  398. size_t peerBitfieldLength) const
  399. {
  400. if(bitfieldLength != peerBitfieldLength) {
  401. return false;
  402. }
  403. array_fun<unsigned char> bf = array_and(array_and(array_negate(bitfield),
  404. array_negate(useBitfield)),
  405. peerBitfield);
  406. if(filterEnabled) {
  407. bf = array_and(bf, filterBitfield);
  408. }
  409. return getAllMissingIndexes(indexes, bf, bitfieldLength);
  410. }
  411. size_t BitfieldMan::countMissingBlock() const {
  412. return cachedNumMissingBlock;
  413. }
  414. size_t BitfieldMan::countMissingBlockNow() const {
  415. if(filterEnabled) {
  416. unsigned char* temp = new unsigned char[bitfieldLength];
  417. for(size_t i = 0; i < bitfieldLength; ++i) {
  418. temp[i] = bitfield[i]&filterBitfield[i];
  419. }
  420. size_t count = countSetBit(filterBitfield, bitfieldLength)-
  421. countSetBit(temp, bitfieldLength);
  422. delete [] temp;
  423. return count;
  424. } else {
  425. return blocks-countSetBit(bitfield, bitfieldLength);
  426. }
  427. }
  428. size_t BitfieldMan::countFilteredBlock() const {
  429. return cachedNumFilteredBlock;
  430. }
  431. size_t BitfieldMan::countBlock() const {
  432. return blocks;
  433. }
  434. size_t BitfieldMan::countFilteredBlockNow() const {
  435. if(filterEnabled) {
  436. return countSetBit(filterBitfield, bitfieldLength);
  437. } else {
  438. return 0;
  439. }
  440. }
  441. size_t BitfieldMan::getMaxIndex() const
  442. {
  443. return blocks-1;
  444. }
  445. bool BitfieldMan::setBitInternal(unsigned char* bitfield, size_t index, bool on) {
  446. if(blocks <= index) { return false; }
  447. unsigned char mask = 128 >> index%8;
  448. if(on) {
  449. bitfield[index/8] |= mask;
  450. } else {
  451. bitfield[index/8] &= ~mask;
  452. }
  453. return true;
  454. }
  455. bool BitfieldMan::setUseBit(size_t index) {
  456. return setBitInternal(useBitfield, index, true);
  457. }
  458. bool BitfieldMan::unsetUseBit(size_t index) {
  459. return setBitInternal(useBitfield, index, false);
  460. }
  461. bool BitfieldMan::setBit(size_t index) {
  462. bool b = setBitInternal(bitfield, index, true);
  463. updateCache();
  464. return b;
  465. }
  466. bool BitfieldMan::unsetBit(size_t index) {
  467. bool b = setBitInternal(bitfield, index, false);
  468. updateCache();
  469. return b;
  470. }
  471. bool BitfieldMan::isFilteredAllBitSet() const {
  472. if(filterEnabled) {
  473. for(size_t i = 0; i < bitfieldLength; ++i) {
  474. if((bitfield[i]&filterBitfield[i]) != filterBitfield[i]) {
  475. return false;
  476. }
  477. }
  478. return true;
  479. } else {
  480. return isAllBitSet();
  481. }
  482. }
  483. bool BitfieldMan::isAllBitSet() const {
  484. if(bitfieldLength == 0) {
  485. return true;
  486. }
  487. for(size_t i = 0; i < bitfieldLength-1; ++i) {
  488. if(bitfield[i] != 0xff) {
  489. return false;
  490. }
  491. }
  492. unsigned char b = ~((128 >> (blocks-1)%8)-1);
  493. if(bitfield[bitfieldLength-1] != b) {
  494. return false;
  495. }
  496. return true;
  497. }
  498. bool BitfieldMan::isBitSetInternal(const unsigned char* bitfield, size_t index) const {
  499. if(index < 0 || blocks <= index) { return false; }
  500. unsigned char mask = 128 >> index%8;
  501. return (bitfield[index/8] & mask) != 0;
  502. }
  503. bool BitfieldMan::isBitSet(size_t index) const {
  504. return isBitSetInternal(bitfield, index);
  505. }
  506. bool BitfieldMan::isUseBitSet(size_t index) const {
  507. return isBitSetInternal(useBitfield, index);
  508. }
  509. void BitfieldMan::setBitfield(const unsigned char* bitfield, size_t bitfieldLength) {
  510. if(this->bitfieldLength != bitfieldLength) {
  511. return;
  512. }
  513. memcpy(this->bitfield, bitfield, this->bitfieldLength);
  514. memset(this->useBitfield, 0, this->bitfieldLength);
  515. updateCache();
  516. }
  517. const unsigned char* BitfieldMan::getBitfield() const
  518. {
  519. return bitfield;
  520. }
  521. size_t BitfieldMan::getBitfieldLength() const
  522. {
  523. return bitfieldLength;
  524. }
  525. void BitfieldMan::clearAllBit() {
  526. memset(this->bitfield, 0, this->bitfieldLength);
  527. updateCache();
  528. }
  529. void BitfieldMan::setAllBit() {
  530. for(size_t i = 0; i < blocks; ++i) {
  531. setBitInternal(bitfield, i, true);
  532. }
  533. updateCache();
  534. }
  535. void BitfieldMan::clearAllUseBit() {
  536. memset(this->useBitfield, 0, this->bitfieldLength);
  537. updateCache();
  538. }
  539. void BitfieldMan::setAllUseBit() {
  540. for(size_t i = 0; i < blocks; ++i) {
  541. setBitInternal(useBitfield, i, true);
  542. }
  543. }
  544. bool BitfieldMan::setFilterBit(size_t index) {
  545. return setBitInternal(filterBitfield, index, true);
  546. }
  547. void BitfieldMan::addFilter(uint64_t offset, uint64_t length) {
  548. if(!filterBitfield) {
  549. filterBitfield = new unsigned char[bitfieldLength];
  550. memset(filterBitfield, 0, bitfieldLength);
  551. }
  552. size_t startBlock = offset/blockLength;
  553. size_t endBlock = (offset+length-1)/blockLength;
  554. for(size_t i = startBlock; i <= endBlock && i < blocks; i++) {
  555. setFilterBit(i);
  556. }
  557. updateCache();
  558. }
  559. void BitfieldMan::enableFilter() {
  560. filterEnabled = true;
  561. updateCache();
  562. }
  563. void BitfieldMan::disableFilter() {
  564. filterEnabled = false;
  565. updateCache();
  566. }
  567. void BitfieldMan::clearFilter() {
  568. if(filterBitfield) {
  569. delete [] filterBitfield;
  570. filterBitfield = 0;
  571. }
  572. filterEnabled = false;
  573. updateCache();
  574. }
  575. bool BitfieldMan::isFilterEnabled() const {
  576. return filterEnabled;
  577. }
  578. uint64_t BitfieldMan::getFilteredTotalLength() const {
  579. return cachedFilteredTotalLength;
  580. }
  581. uint64_t BitfieldMan::getFilteredTotalLengthNow() const {
  582. if(!filterBitfield) {
  583. return 0;
  584. }
  585. size_t filteredBlocks = countSetBit(filterBitfield, bitfieldLength);
  586. if(filteredBlocks == 0) {
  587. return 0;
  588. }
  589. if(isBitSetInternal(filterBitfield, blocks-1)) {
  590. return ((uint64_t)filteredBlocks-1)*blockLength+getLastBlockLength();
  591. } else {
  592. return ((uint64_t)filteredBlocks)*blockLength;
  593. }
  594. }
  595. uint64_t BitfieldMan::getCompletedLength(bool useFilter) const {
  596. unsigned char* temp = new unsigned char[bitfieldLength];
  597. if(useFilter) {
  598. for(size_t i = 0; i < bitfieldLength; ++i) {
  599. temp[i] = bitfield[i];
  600. if(filterEnabled) {
  601. temp[i] &= filterBitfield[i];
  602. }
  603. }
  604. } else {
  605. memcpy(temp, bitfield, bitfieldLength);
  606. }
  607. size_t completedBlocks = countSetBit(temp, bitfieldLength);
  608. uint64_t completedLength = 0;
  609. if(completedBlocks == 0) {
  610. completedLength = 0;
  611. } else {
  612. if(isBitSetInternal(temp, blocks-1)) {
  613. completedLength = ((uint64_t)completedBlocks-1)*blockLength+getLastBlockLength();
  614. } else {
  615. completedLength = ((uint64_t)completedBlocks)*blockLength;
  616. }
  617. }
  618. delete [] temp;
  619. return completedLength;
  620. }
  621. uint64_t BitfieldMan::getCompletedLength() const {
  622. return cachedCompletedLength;
  623. }
  624. uint64_t BitfieldMan::getCompletedLengthNow() const {
  625. return getCompletedLength(false);
  626. }
  627. uint64_t BitfieldMan::getFilteredCompletedLength() const {
  628. return cachedFilteredComletedLength;
  629. }
  630. uint64_t BitfieldMan::getFilteredCompletedLengthNow() const {
  631. return getCompletedLength(true);
  632. }
  633. void BitfieldMan::updateCache()
  634. {
  635. cachedNumMissingBlock = countMissingBlockNow();
  636. cachedNumFilteredBlock = countFilteredBlockNow();
  637. cachedFilteredTotalLength = getFilteredTotalLengthNow();
  638. cachedCompletedLength = getCompletedLengthNow();
  639. cachedFilteredComletedLength = getFilteredCompletedLengthNow();
  640. }
  641. bool BitfieldMan::isBitRangeSet(size_t startIndex, size_t endIndex) const
  642. {
  643. for(size_t i = startIndex; i <= endIndex; ++i) {
  644. if(!isBitSet(i)) {
  645. return false;
  646. }
  647. }
  648. return true;
  649. }
  650. void BitfieldMan::unsetBitRange(size_t startIndex, size_t endIndex)
  651. {
  652. for(size_t i = startIndex; i <= endIndex; ++i) {
  653. unsetBit(i);
  654. }
  655. updateCache();
  656. }
  657. void BitfieldMan::setBitRange(size_t startIndex, size_t endIndex)
  658. {
  659. for(size_t i = startIndex; i <= endIndex; ++i) {
  660. setBit(i);
  661. }
  662. updateCache();
  663. }
  664. bool BitfieldMan::isBitSetOffsetRange(uint64_t offset, uint64_t length) const
  665. {
  666. if(length <= 0) {
  667. return false;
  668. }
  669. if(totalLength <= offset) {
  670. return false;
  671. }
  672. if(totalLength < offset+length) {
  673. length = totalLength-offset;
  674. }
  675. size_t startBlock = offset/blockLength;
  676. size_t endBlock = (offset+length-1)/blockLength;
  677. for(size_t i = startBlock; i <= endBlock; i++) {
  678. if(!isBitSet(i)) {
  679. return false;
  680. }
  681. }
  682. return true;
  683. }
  684. uint64_t BitfieldMan::getMissingUnusedLength(size_t startingIndex) const
  685. {
  686. if(startingIndex < 0 || blocks <= startingIndex) {
  687. return 0;
  688. }
  689. uint64_t length = 0;
  690. for(size_t i = startingIndex; i < blocks; ++i) {
  691. if(isBitSet(i) || isUseBitSet(i)) {
  692. break;
  693. }
  694. length += getBlockLength(i);
  695. }
  696. return length;
  697. }
  698. void BitfieldMan::setRandomizer(const SharedHandle<Randomizer>& randomizer)
  699. {
  700. this->randomizer = randomizer;
  701. }
  702. SharedHandle<Randomizer> BitfieldMan::getRandomizer() const
  703. {
  704. return randomizer;
  705. }
  706. } // namespace aria2