BitfieldMan.h 8.9 KB

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