BitfieldMan.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "BitfieldMan.h"
  23. #include "Util.h"
  24. #include <string.h>
  25. BitfieldMan::BitfieldMan(int blockLength, long long int totalLength)
  26. :blockLength(blockLength), totalLength(totalLength), filterBitfield(0),
  27. filterEnabled(false) {
  28. if(blockLength > 0 && totalLength > 0) {
  29. blocks = totalLength/blockLength+(totalLength%blockLength ? 1 : 0);
  30. bitfieldLength = blocks/8+(blocks%8 ? 1 : 0);
  31. bitfield = new unsigned char[bitfieldLength];
  32. useBitfield = new unsigned char[bitfieldLength];
  33. memset(bitfield, 0, bitfieldLength);
  34. memset(useBitfield, 0, bitfieldLength);
  35. }
  36. }
  37. BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan) {
  38. blockLength = bitfieldMan.blockLength;
  39. totalLength = bitfieldMan.totalLength;
  40. blocks = bitfieldMan.blocks;
  41. bitfieldLength = bitfieldMan.bitfieldLength;
  42. bitfield = new unsigned char[bitfieldLength];
  43. useBitfield = new unsigned char[bitfieldLength];
  44. memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
  45. memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
  46. filterEnabled = bitfieldMan.filterEnabled;
  47. if(bitfieldMan.filterBitfield) {
  48. filterBitfield = new unsigned char[bitfieldLength];
  49. memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
  50. } else {
  51. filterBitfield = 0;
  52. }
  53. }
  54. BitfieldMan::~BitfieldMan() {
  55. delete [] bitfield;
  56. delete [] useBitfield;
  57. if(filterBitfield) {
  58. delete [] filterBitfield;
  59. }
  60. }
  61. int BitfieldMan::countSetBit(const unsigned char* bitfield, int len) const {
  62. int count = 0;
  63. int size = sizeof(unsigned int);
  64. for(int i = 0; i < len/size; i++) {
  65. count += Util::countBit(*(unsigned int*)&bitfield[i*size]);
  66. }
  67. for(int i = len-len%size; i < len; i++) {
  68. count += Util::countBit((unsigned int)bitfield[i]);
  69. }
  70. return count;
  71. }
  72. int BitfieldMan::getNthBitIndex(const unsigned char bitfield, int nth) const {
  73. int index = -1;
  74. for(int bs = 7; bs >= 0; bs--) {
  75. unsigned char mask = 1 << bs;
  76. if(bitfield & mask) {
  77. nth--;
  78. if(nth == 0) {
  79. index = 7-bs;
  80. break;
  81. }
  82. }
  83. }
  84. return index;
  85. }
  86. int
  87. BitfieldMan::getMissingIndexRandomly(const unsigned char* bitfield,
  88. int bitfieldLength) const
  89. {
  90. int byte = (int)(((double)bitfieldLength)*random()/(RAND_MAX+1.0));
  91. unsigned char lastMask = 0;
  92. int lastByteLength = totalLength%(blockLength*8);
  93. int lastBlockCount = DIV_FLOOR(lastByteLength, blockLength);
  94. for(int i = 0; i < lastBlockCount; i++) {
  95. lastMask >>= 1;
  96. lastMask |= 0x80;
  97. }
  98. for(int i = 0; i < bitfieldLength; i++) {
  99. unsigned char mask;
  100. if(byte == bitfieldLength-1) {
  101. mask = lastMask;
  102. } else {
  103. mask = 0xff;
  104. }
  105. if(bitfield[byte]&mask) {
  106. int index = byte*8+getNthBitIndex(bitfield[byte], 1);
  107. return index;
  108. }
  109. byte++;
  110. if(byte == bitfieldLength) {
  111. byte = 0;
  112. }
  113. }
  114. return -1;
  115. }
  116. bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, int length) const {
  117. if(bitfieldLength != length) {
  118. return false;
  119. }
  120. bool retval = false;
  121. for(int i = 0; i < bitfieldLength; i++) {
  122. unsigned char temp = peerBitfield[i] & ~bitfield[i];
  123. if(filterEnabled) {
  124. temp &= filterBitfield[i];
  125. }
  126. if(temp&0xff) {
  127. retval = true;
  128. break;
  129. }
  130. }
  131. return retval;
  132. }
  133. int BitfieldMan::getMissingIndex(const unsigned char* peerBitfield, int length) const {
  134. if(bitfieldLength != length) {
  135. return -1;
  136. }
  137. unsigned char* tempBitfield = new unsigned char[bitfieldLength];
  138. for(int i = 0; i < bitfieldLength; i++) {
  139. tempBitfield[i] = peerBitfield[i] & ~bitfield[i];
  140. if(filterEnabled) {
  141. tempBitfield[i] &= filterBitfield[i];
  142. }
  143. }
  144. int index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
  145. delete [] tempBitfield;
  146. return index;
  147. }
  148. int BitfieldMan::getMissingUnusedIndex(const unsigned char* peerBitfield, int length) const {
  149. if(bitfieldLength != length) {
  150. return -1;
  151. }
  152. unsigned char* tempBitfield = new unsigned char[bitfieldLength];
  153. for(int i = 0; i < bitfieldLength; i++) {
  154. tempBitfield[i] = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
  155. if(filterEnabled) {
  156. tempBitfield[i] &= filterBitfield[i];
  157. }
  158. }
  159. int index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
  160. delete [] tempBitfield;
  161. return index;
  162. }
  163. int BitfieldMan::getFirstMissingUnusedIndex(const unsigned char* peerBitfield, int length) const {
  164. if(bitfieldLength != length) {
  165. return -1;
  166. }
  167. for(int i = 0; i < bitfieldLength; i++) {
  168. unsigned char bit = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
  169. if(filterEnabled) {
  170. bit &= filterBitfield[i];
  171. }
  172. for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
  173. unsigned char mask = 1 << bs;
  174. if(bit & mask) {
  175. return i*8+7-bs;
  176. }
  177. }
  178. }
  179. return -1;
  180. }
  181. int BitfieldMan::getFirstMissingUnusedIndex() const {
  182. for(int i = 0; i < bitfieldLength; i++) {
  183. unsigned char bit = ~bitfield[i] & ~useBitfield[i];
  184. if(filterEnabled) {
  185. bit &= filterBitfield[i];
  186. }
  187. for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
  188. unsigned char mask = 1 << bs;
  189. if(bit & mask) {
  190. return i*8+7-bs;
  191. }
  192. }
  193. }
  194. return -1;
  195. }
  196. int BitfieldMan::getMissingIndex() const {
  197. unsigned char* tempBitfield = new unsigned char[bitfieldLength];
  198. for(int i = 0; i < bitfieldLength; i++) {
  199. tempBitfield[i] = ~bitfield[i];
  200. if(filterEnabled) {
  201. tempBitfield[i] &= filterBitfield[i];
  202. }
  203. }
  204. int index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
  205. delete [] tempBitfield;
  206. return index;
  207. }
  208. int BitfieldMan::getMissingUnusedIndex() const {
  209. unsigned char* tempBitfield = new unsigned char[bitfieldLength];
  210. memset(tempBitfield, 0xff, bitfieldLength);
  211. int index = getMissingUnusedIndex(tempBitfield, bitfieldLength);
  212. delete [] tempBitfield;
  213. return index;
  214. }
  215. // [startIndex, endIndex)
  216. class Range {
  217. public:
  218. int startIndex;
  219. int endIndex;
  220. Range(int startIndex = 0, int endIndex = 0):startIndex(startIndex),
  221. endIndex(endIndex) {}
  222. int getSize() const {
  223. return endIndex-startIndex;
  224. }
  225. int getMidIndex() const {
  226. return (endIndex-startIndex)/2+startIndex;
  227. }
  228. bool operator<(const Range& range) const {
  229. return getSize() < range.getSize();
  230. }
  231. };
  232. int BitfieldMan::getStartIndex(int index) const {
  233. while(index < blocks &&
  234. (isUseBitSet(index) || isBitSet(index))) {
  235. index++;
  236. }
  237. if(blocks <= index) {
  238. return -1;
  239. } else {
  240. return index;
  241. }
  242. }
  243. int BitfieldMan::getEndIndex(int index) const {
  244. while(index < blocks &&
  245. (!isUseBitSet(index) && !isBitSet(index))) {
  246. index++;
  247. }
  248. return index;
  249. }
  250. int BitfieldMan::getSparseMissingUnusedIndex() const {
  251. Range maxRange;
  252. int index = 0;
  253. int blocks = countBlock();
  254. Range currentRange;
  255. while(index < blocks) {
  256. currentRange.startIndex = getStartIndex(index);
  257. if(currentRange.startIndex == -1) {
  258. break;
  259. }
  260. currentRange.endIndex = getEndIndex(currentRange.startIndex);
  261. if(maxRange < currentRange) {
  262. maxRange = currentRange;
  263. }
  264. index = currentRange.endIndex;
  265. }
  266. if(maxRange.getSize()) {
  267. if(maxRange.startIndex == 0) {
  268. return 0;
  269. } else {
  270. return maxRange.getMidIndex();
  271. }
  272. } else {
  273. return -1;
  274. }
  275. }
  276. BlockIndexes BitfieldMan::getAllMissingIndexes() const {
  277. BlockIndexes missingIndexes;
  278. for(int i = 0; i < bitfieldLength; i++) {
  279. unsigned char bit = ~bitfield[i];
  280. if(filterEnabled) {
  281. bit &= filterBitfield[i];
  282. }
  283. for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
  284. unsigned char mask = 1 << bs;
  285. if(bit & mask) {
  286. missingIndexes.push_back(i*8+7-bs);
  287. }
  288. }
  289. }
  290. return missingIndexes;
  291. }
  292. BlockIndexes BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield, int peerBitfieldLength) const {
  293. BlockIndexes missingIndexes;
  294. if(bitfieldLength != peerBitfieldLength) {
  295. return missingIndexes;
  296. }
  297. for(int i = 0; i < bitfieldLength; i++) {
  298. unsigned char bit = peerBitfield[i] & ~bitfield[i];
  299. if(filterEnabled) {
  300. bit &= filterBitfield[i];
  301. }
  302. for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
  303. unsigned char mask = 1 << bs;
  304. if(bit & mask) {
  305. missingIndexes.push_back(i*8+7-bs);
  306. }
  307. }
  308. }
  309. return missingIndexes;
  310. }
  311. int BitfieldMan::countMissingBlock() const {
  312. if(filterEnabled) {
  313. unsigned char* temp = new unsigned char[bitfieldLength];
  314. for(int i = 0; i < bitfieldLength; i++) {
  315. temp[i] = bitfield[i]&filterBitfield[i];
  316. }
  317. int count = countSetBit(filterBitfield, bitfieldLength)-
  318. countSetBit(temp, bitfieldLength);
  319. delete [] temp;
  320. return count;
  321. } else {
  322. return blocks-countSetBit(bitfield, bitfieldLength);
  323. }
  324. }
  325. int BitfieldMan::countBlock() const {
  326. if(filterEnabled) {
  327. return countSetBit(filterBitfield, bitfieldLength);
  328. } else {
  329. return blocks;
  330. }
  331. }
  332. bool BitfieldMan::setBitInternal(unsigned char* bitfield, int index, bool on) {
  333. if(blocks <= index) { return false; }
  334. unsigned char mask = 128 >> index%8;
  335. if(on) {
  336. bitfield[index/8] |= mask;
  337. } else {
  338. bitfield[index/8] &= ~mask;
  339. }
  340. return true;
  341. }
  342. bool BitfieldMan::setUseBit(int index) {
  343. return setBitInternal(useBitfield, index, true);
  344. }
  345. bool BitfieldMan::unsetUseBit(int index) {
  346. return setBitInternal(useBitfield, index, false);
  347. }
  348. bool BitfieldMan::setBit(int index) {
  349. return setBitInternal(bitfield, index, true);
  350. }
  351. bool BitfieldMan::unsetBit(int index) {
  352. return setBitInternal(bitfield, index, false);
  353. }
  354. bool BitfieldMan::isAllBitSet() const {
  355. if(filterEnabled) {
  356. for(int i = 0; i < bitfieldLength; i++) {
  357. if((bitfield[i]&filterBitfield[i]) != filterBitfield[i]) {
  358. return false;
  359. }
  360. }
  361. return true;
  362. } else {
  363. for(int i = 0; i < bitfieldLength-1; i++) {
  364. if(bitfield[i] != 0xff) {
  365. return false;
  366. }
  367. }
  368. unsigned char b = ~((128 >> (blocks-1)%8)-1);
  369. if(bitfield[bitfieldLength-1] != b) {
  370. return false;
  371. }
  372. return true;
  373. }
  374. }
  375. bool BitfieldMan::isBitSetInternal(const unsigned char* bitfield, int index) const {
  376. if(index < 0 || blocks <= index) { return false; }
  377. unsigned char mask = 128 >> index%8;
  378. return (bitfield[index/8] & mask) != 0;
  379. }
  380. bool BitfieldMan::isBitSet(int index) const {
  381. return isBitSetInternal(bitfield, index);
  382. }
  383. bool BitfieldMan::isUseBitSet(int index) const {
  384. return isBitSetInternal(useBitfield, index);
  385. }
  386. void BitfieldMan::setBitfield(const unsigned char* bitfield, int bitfieldLength) {
  387. if(this->bitfieldLength != bitfieldLength) {
  388. return;
  389. }
  390. memcpy(this->bitfield, bitfield, this->bitfieldLength);
  391. memset(this->useBitfield, 0, this->bitfieldLength);
  392. }
  393. void BitfieldMan::clearAllBit() {
  394. memset(this->bitfield, 0, this->bitfieldLength);
  395. }
  396. void BitfieldMan::setAllBit() {
  397. for(int i = 0; i < blocks; i++) {
  398. setBit(i);
  399. }
  400. }
  401. void BitfieldMan::clearAllUseBit() {
  402. memset(this->useBitfield, 0, this->bitfieldLength);
  403. }
  404. void BitfieldMan::setAllUseBit() {
  405. for(int i = 0; i < blocks; i++) {
  406. setUseBit(i);
  407. }
  408. }
  409. bool BitfieldMan::setFilterBit(int index) {
  410. return setBitInternal(filterBitfield, index, true);
  411. }
  412. void BitfieldMan::addFilter(long long int offset, long long int length) {
  413. if(!filterBitfield) {
  414. filterBitfield = new unsigned char[bitfieldLength];
  415. memset(filterBitfield, 0, bitfieldLength);
  416. }
  417. int startBlock = offset/blockLength;
  418. int endBlock = (offset+length-1)/blockLength;
  419. for(int i = startBlock; i <= endBlock && i < blocks; i++) {
  420. setFilterBit(i);
  421. }
  422. }
  423. void BitfieldMan::enableFilter() {
  424. filterEnabled = true;
  425. }
  426. void BitfieldMan::disableFilter() {
  427. filterEnabled = false;
  428. }
  429. void BitfieldMan::clearFilter() {
  430. if(filterBitfield) {
  431. delete [] filterBitfield;
  432. filterBitfield = 0;
  433. }
  434. filterEnabled = false;
  435. }
  436. bool BitfieldMan::isFilterEnabled() const {
  437. return filterEnabled;
  438. }
  439. long long int BitfieldMan::getFilteredTotalLength() const {
  440. if(!filterBitfield) {
  441. return 0;
  442. }
  443. int filteredBlocks = countSetBit(filterBitfield, bitfieldLength);
  444. if(filteredBlocks == 0) {
  445. return 0;
  446. }
  447. if(isBitSetInternal(filterBitfield, blocks-1)) {
  448. return ((long long int)filteredBlocks-1)*blockLength+getLastBlockLength();
  449. } else {
  450. return ((long long int)filteredBlocks)*blockLength;
  451. }
  452. }
  453. long long int BitfieldMan::getCompletedLength() const {
  454. unsigned char* temp = new unsigned char[bitfieldLength];
  455. for(int i = 0; i < bitfieldLength; i++) {
  456. temp[i] = bitfield[i];
  457. if(filterEnabled) {
  458. temp[i] &= filterBitfield[i];
  459. }
  460. }
  461. int completedBlocks = countSetBit(temp, bitfieldLength);
  462. long long int completedLength = 0;
  463. if(completedBlocks == 0) {
  464. completedLength = 0;
  465. } else {
  466. if(isBitSetInternal(temp, blocks-1)) {
  467. completedLength = ((long long int)completedBlocks-1)*blockLength+getLastBlockLength();
  468. } else {
  469. completedLength = ((long long int)completedBlocks)*blockLength;
  470. }
  471. }
  472. delete [] temp;
  473. return completedLength;
  474. }