BitfieldMan.cc 14 KB

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