BitfieldMan.cc 19 KB

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