BitfieldMan.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <vector>
  39. #include "SharedHandle.h"
  40. namespace aria2 {
  41. class BitfieldMan {
  42. private:
  43. size_t blockLength_;
  44. uint64_t totalLength_;
  45. size_t bitfieldLength_;
  46. size_t blocks_;
  47. bool filterEnabled_;
  48. unsigned char* bitfield_;
  49. unsigned char* useBitfield_;
  50. unsigned char* filterBitfield_;
  51. // for caching
  52. size_t cachedNumMissingBlock_;
  53. size_t cachedNumFilteredBlock_;
  54. uint64_t cachedCompletedLength_;
  55. uint64_t cachedFilteredCompletedLength_;
  56. uint64_t cachedFilteredTotalLength_;
  57. bool setBitInternal(unsigned char* bitfield, size_t index, bool on);
  58. bool setFilterBit(size_t index);
  59. size_t getStartIndex(size_t index) const;
  60. size_t getEndIndex(size_t index) const;
  61. uint64_t getCompletedLength(bool useFilter) const;
  62. // If filterBitfield_ is 0, allocate bitfieldLength_ bytes to it and
  63. // set 0 to all bytes.
  64. void ensureFilterBitfield();
  65. public:
  66. // [startIndex, endIndex)
  67. class Range {
  68. public:
  69. size_t startIndex;
  70. size_t endIndex;
  71. Range(size_t startIndex = 0, size_t endIndex = 0):startIndex(startIndex),
  72. endIndex(endIndex) {}
  73. size_t getSize() const {
  74. return endIndex-startIndex;
  75. }
  76. size_t getMidIndex() const {
  77. return (endIndex-startIndex)/2+startIndex;
  78. }
  79. bool operator<(const Range& range) const {
  80. return getSize() < range.getSize();
  81. }
  82. bool operator==(const Range& range) const
  83. {
  84. return getSize() == range.getSize();
  85. }
  86. };
  87. public:
  88. BitfieldMan(size_t blockLength, uint64_t totalLength);
  89. BitfieldMan(const BitfieldMan& bitfieldMan);
  90. ~BitfieldMan();
  91. BitfieldMan& operator=(const BitfieldMan& bitfieldMan);
  92. size_t getBlockLength() const
  93. {
  94. return blockLength_;
  95. }
  96. size_t getLastBlockLength() const
  97. {
  98. return totalLength_-blockLength_*(blocks_-1);
  99. }
  100. size_t getBlockLength(size_t index) const;
  101. uint64_t getTotalLength() const { return totalLength_; }
  102. // Returns true iff there is a bit index which is set in bitfield_,
  103. // but not set in this object.
  104. //
  105. // affected by filter
  106. bool hasMissingPiece(const unsigned char* bitfield, size_t len) const;
  107. // affected by filter
  108. bool getFirstMissingUnusedIndex(size_t& index) const;
  109. // Appends at most n missing unused index to out. This function
  110. // doesn't delete existing elements in out. Returns the number of
  111. // appended elements.
  112. //
  113. // affected by filter
  114. size_t getFirstNMissingUnusedIndex(std::vector<size_t>& out, size_t n) const;
  115. // Stores first missing bit index to index. Returns true if such bit
  116. // index is found. Otherwise returns false.
  117. //
  118. // affected by filter
  119. bool getFirstMissingIndex(size_t& index) const;
  120. // Stores missing bit index to index. index is selected so that it
  121. // divides longest missing bit subarray into 2 equally sized
  122. // subarray. Set bits in ignoreBitfield are excluded. Returns true
  123. // if such bit index is found. Otherwise returns false.
  124. //
  125. // affected by filter
  126. bool getSparseMissingUnusedIndex
  127. (size_t& index,
  128. size_t minSplitSize,
  129. const unsigned char* ignoreBitfield,
  130. size_t ignoreBitfieldLength) const;
  131. // affected by filter
  132. bool getAllMissingIndexes(unsigned char* misbitfield, size_t mislen) const;
  133. // affected by filter
  134. bool getAllMissingIndexes(unsigned char* misbitfield, size_t mislen,
  135. const unsigned char* bitfield, size_t len) const;
  136. // affected by filter
  137. bool getAllMissingUnusedIndexes(unsigned char* misbitfield, size_t mislen,
  138. const unsigned char* bitfield,
  139. size_t len) const;
  140. // affected by filter
  141. size_t countMissingBlock() const;
  142. // affected by filter
  143. size_t countMissingBlockNow() const;
  144. bool setUseBit(size_t index);
  145. bool unsetUseBit(size_t index);
  146. bool setBit(size_t index);
  147. bool unsetBit(size_t index);
  148. bool isBitSet(size_t index) const;
  149. bool isUseBitSet(size_t index) const;
  150. // affected by filter
  151. bool isFilteredAllBitSet() const;
  152. bool isAllBitSet() const;
  153. bool isAllFilterBitSet() const;
  154. const unsigned char* getBitfield() const
  155. {
  156. return bitfield_;
  157. }
  158. size_t getBitfieldLength() const
  159. {
  160. return bitfieldLength_;
  161. }
  162. // affected by filter
  163. size_t countFilteredBlock() const
  164. {
  165. return cachedNumFilteredBlock_;
  166. }
  167. size_t countBlock() const
  168. {
  169. return blocks_;
  170. }
  171. // affected by filter
  172. size_t countFilteredBlockNow() const;
  173. size_t getMaxIndex() const
  174. {
  175. return blocks_-1;
  176. }
  177. void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
  178. void clearAllBit();
  179. void setAllBit();
  180. void clearAllUseBit();
  181. void setAllUseBit();
  182. void addFilter(uint64_t offset, uint64_t length);
  183. void removeFilter(uint64_t offset, uint64_t length);
  184. // Add filter not in the range of [offset, offset+length) bytes
  185. void addNotFilter(uint64_t offset, uint64_t length);
  186. // Clears filter and disables filter
  187. void clearFilter();
  188. void enableFilter();
  189. void disableFilter();
  190. bool isFilterEnabled() const
  191. {
  192. return filterEnabled_;
  193. }
  194. // affected by filter
  195. uint64_t getFilteredTotalLength() const
  196. {
  197. return cachedFilteredTotalLength_;
  198. }
  199. // affected by filter
  200. uint64_t getFilteredTotalLengthNow() const;
  201. uint64_t getCompletedLength() const
  202. {
  203. return cachedCompletedLength_;
  204. }
  205. uint64_t getCompletedLengthNow() const;
  206. // affected by filter
  207. uint64_t getFilteredCompletedLength() const
  208. {
  209. return cachedFilteredCompletedLength_;
  210. }
  211. // affected by filter
  212. uint64_t getFilteredCompletedLengthNow() const;
  213. void updateCache();
  214. bool isBitRangeSet(size_t startIndex, size_t endIndex) const;
  215. void unsetBitRange(size_t startIndex, size_t endIndex);
  216. void setBitRange(size_t startIndex, size_t endIndex);
  217. bool isBitSetOffsetRange(uint64_t offset, uint64_t length) const;
  218. uint64_t getMissingUnusedLength(size_t startingIndex) const;
  219. const unsigned char* getFilterBitfield() const
  220. {
  221. return filterBitfield_;
  222. }
  223. };
  224. } // namespace aria2
  225. #endif // D_BITFIELD_MAN_H