BitfieldMan.cc 19 KB

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