BitfieldMan.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #ifndef _D_BITFIELD_MAN_H_
  36. #define _D_BITFIELD_MAN_H_
  37. #include "common.h"
  38. #include "Randomizer.h"
  39. #include <deque>
  40. typedef deque<int> BlockIndexes;
  41. class BitfieldMan {
  42. private:
  43. uint32_t blockLength;
  44. uint64_t totalLength;
  45. unsigned char* bitfield;
  46. unsigned char* useBitfield;
  47. unsigned char* filterBitfield;
  48. uint32_t bitfieldLength;
  49. uint32_t blocks;
  50. bool filterEnabled;
  51. RandomizerHandle randomizer;
  52. // for caching
  53. uint32_t cachedNumMissingBlock;
  54. uint32_t cachedNumFilteredBlock;
  55. uint64_t cachedCompletedLength;
  56. uint64_t cachedFilteredComletedLength;
  57. uint64_t cachedFilteredTotalLength;
  58. uint32_t countSetBit(const unsigned char* bitfield, uint32_t len) const;
  59. int32_t getNthBitIndex(const unsigned char bit, uint32_t nth) const;
  60. int32_t getMissingIndexRandomly(const unsigned char* bitfield, uint32_t len) const;
  61. bool isBitSetInternal(const unsigned char* bitfield, int32_t index) const;
  62. bool setBitInternal(unsigned char* bitfield, int32_t index, bool on);
  63. bool setFilterBit(int32_t index);
  64. int32_t getStartIndex(int32_t index) const;
  65. int32_t getEndIndex(int32_t index) const;
  66. uint64_t getCompletedLength(bool useFilter) const;
  67. public:
  68. BitfieldMan(uint32_t blockLength, uint64_t totalLength);
  69. BitfieldMan(const BitfieldMan& bitfieldMan);
  70. ~BitfieldMan();
  71. BitfieldMan& operator=(const BitfieldMan& bitfieldMan) {
  72. if(this != &bitfieldMan) {
  73. blockLength = bitfieldMan.blockLength;
  74. totalLength = bitfieldMan.totalLength;
  75. blocks = bitfieldMan.blocks;
  76. bitfieldLength = bitfieldMan.bitfieldLength;
  77. filterEnabled = bitfieldMan.filterEnabled;
  78. delete [] bitfield;
  79. bitfield = new unsigned char[bitfieldLength];
  80. memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
  81. delete [] useBitfield;
  82. useBitfield = new unsigned char[bitfieldLength];
  83. memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
  84. delete [] filterBitfield;
  85. if(filterEnabled) {
  86. filterBitfield = new unsigned char[bitfieldLength];
  87. memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
  88. } else {
  89. filterBitfield = 0;
  90. }
  91. updateCache();
  92. }
  93. return *this;
  94. }
  95. uint32_t getBlockLength() const { return blockLength; }
  96. uint32_t getLastBlockLength() const {
  97. return totalLength-blockLength*(blocks-1);
  98. }
  99. uint32_t getBlockLength(int32_t index) const {
  100. if(index == (int32_t)(blocks-1)) {
  101. return getLastBlockLength();
  102. } else if(0 <= index && index < (int32_t)(blocks-1)) {
  103. return getBlockLength();
  104. } else {
  105. return 0;
  106. }
  107. }
  108. uint64_t getTotalLength() const { return totalLength; }
  109. /**
  110. * affected by filter
  111. */
  112. bool hasMissingPiece(const unsigned char* bitfield, uint32_t len) const;
  113. /**
  114. * affected by filter
  115. */
  116. int32_t getMissingIndex(const unsigned char* bitfield, uint32_t len) const;
  117. /**
  118. * affected by filter
  119. */
  120. int32_t getMissingIndex() const;
  121. /**
  122. * affected by filter
  123. */
  124. int32_t getFirstMissingUnusedIndex(const unsigned char* bitfield, uint32_t len) const;
  125. /**
  126. * affected by filter
  127. */
  128. int32_t getFirstMissingUnusedIndex() const;
  129. /**
  130. * affected by filter
  131. */
  132. int32_t getMissingUnusedIndex(const unsigned char* bitfield, uint32_t len) const;
  133. /**
  134. * affected by filter
  135. */
  136. int32_t getMissingUnusedIndex() const;
  137. /**
  138. * affected by filter
  139. */
  140. int32_t getSparseMissingUnusedIndex() const;
  141. /**
  142. * affected by filter
  143. */
  144. BlockIndexes getAllMissingIndexes() const;
  145. /**
  146. * affected by filter
  147. */
  148. BlockIndexes getAllMissingIndexes(const unsigned char* bitfield, uint32_t len) const;
  149. /**
  150. * affected by filter
  151. */
  152. uint32_t countMissingBlock() const;
  153. /**
  154. * affected by filter
  155. */
  156. uint32_t countMissingBlockNow() const;
  157. bool setUseBit(int32_t index);
  158. bool unsetUseBit(int32_t index);
  159. bool setBit(int32_t index);
  160. bool unsetBit(int32_t index);
  161. bool isBitSet(int32_t index) const;
  162. bool isUseBitSet(int32_t index) const;
  163. /**
  164. * affected by filter
  165. */
  166. bool isAllBitSet() const;
  167. const unsigned char* getBitfield() const { return bitfield; }
  168. uint32_t getBitfieldLength() const { return bitfieldLength; }
  169. /**
  170. * affected by filter
  171. */
  172. uint32_t countBlock() const;
  173. /**
  174. * affected by filter
  175. */
  176. uint32_t countFilteredBlockNow() const;
  177. int32_t getMaxIndex() const { return blocks-1; }
  178. void setBitfield(const unsigned char* bitfield, uint32_t bitfieldLength);
  179. void clearAllBit();
  180. void setAllBit();
  181. void clearAllUseBit();
  182. void setAllUseBit();
  183. void addFilter(int64_t offset, uint64_t length);
  184. /**
  185. * Clears filter and disables filter
  186. */
  187. void clearFilter();
  188. void enableFilter();
  189. void disableFilter();
  190. bool isFilterEnabled() const;
  191. /**
  192. * affected by filter
  193. */
  194. uint64_t getFilteredTotalLength() const;
  195. /**
  196. * affected by filter
  197. */
  198. uint64_t getFilteredTotalLengthNow() const;
  199. uint64_t getCompletedLength() const;
  200. uint64_t getCompletedLengthNow() const;
  201. /**
  202. * affected by filter
  203. */
  204. uint64_t getFilteredCompletedLength() const;
  205. /**
  206. * affected by filter
  207. */
  208. uint64_t getFilteredCompletedLengthNow() const;
  209. void setRandomizer(const RandomizerHandle& randomizer) {
  210. this->randomizer = randomizer;
  211. }
  212. RandomizerHandle getRandomizer() const {
  213. return randomizer;
  214. }
  215. void updateCache();
  216. bool isBitRangeSet(int32_t startIndex, int32_t endIndex) const;
  217. void unsetBitRange(int32_t startIndex, int32_t endIndex);
  218. };
  219. #endif // _D_BITFIELD_MAN_H_