BitfieldMan.h 7.2 KB

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