BitfieldMan.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. size_t bitfieldLength;
  47. size_t blocks;
  48. bool filterEnabled;
  49. unsigned char* bitfield;
  50. unsigned char* useBitfield;
  51. unsigned char* filterBitfield;
  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. // If filterBitfield is 0, allocate bitfieldLength bytes to it and
  76. // set 0 to all bytes.
  77. void ensureFilterBitfield();
  78. public:
  79. // [startIndex, endIndex)
  80. class Range {
  81. public:
  82. size_t startIndex;
  83. size_t endIndex;
  84. Range(size_t startIndex = 0, size_t endIndex = 0):startIndex(startIndex),
  85. endIndex(endIndex) {}
  86. size_t getSize() const {
  87. return endIndex-startIndex;
  88. }
  89. size_t getMidIndex() const {
  90. return (endIndex-startIndex)/2+startIndex;
  91. }
  92. bool operator<(const Range& range) const {
  93. return getSize() < range.getSize();
  94. }
  95. };
  96. public:
  97. BitfieldMan(size_t blockLength, uint64_t totalLength);
  98. BitfieldMan(const BitfieldMan& bitfieldMan);
  99. ~BitfieldMan();
  100. BitfieldMan& operator=(const BitfieldMan& bitfieldMan);
  101. size_t getBlockLength() const
  102. {
  103. return blockLength;
  104. }
  105. size_t getLastBlockLength() const
  106. {
  107. return totalLength-blockLength*(blocks-1);
  108. }
  109. size_t getBlockLength(size_t index) const;
  110. uint64_t getTotalLength() const { return totalLength; }
  111. /**
  112. * affected by filter
  113. */
  114. bool hasMissingPiece(const unsigned char* bitfield, size_t len) const;
  115. /**
  116. * affected by filter
  117. */
  118. bool getMissingIndex(size_t& index, const unsigned char* bitfield, size_t len) const;
  119. /**
  120. * affected by filter
  121. */
  122. bool getMissingIndex(size_t& index) const;
  123. /**
  124. * affected by filter
  125. */
  126. bool getFirstMissingUnusedIndex(size_t& index, const unsigned char* bitfield, size_t len) const;
  127. /**
  128. * affected by filter
  129. */
  130. bool getFirstMissingUnusedIndex(size_t& index) const;
  131. /**
  132. * affected by filter
  133. */
  134. bool getFirstMissingIndex(size_t& index) const;
  135. /**
  136. * affected by filter
  137. */
  138. bool getMissingUnusedIndex(size_t& index, const unsigned char* bitfield, size_t len) const;
  139. /**
  140. * affected by filter
  141. */
  142. bool getMissingUnusedIndex(size_t& index) const;
  143. /**
  144. * affected by filter
  145. */
  146. bool getSparseMissingUnusedIndex
  147. (size_t& index,
  148. const unsigned char* ignoreBitfield,
  149. size_t ignoreBitfieldLength) const;
  150. /**
  151. * affected by filter
  152. */
  153. bool getAllMissingIndexes(unsigned char* misbitfield, size_t mislen) const;
  154. /**
  155. * affected by filter
  156. */
  157. bool getAllMissingIndexes(unsigned char* misbitfield, size_t mislen,
  158. const unsigned char* bitfield, size_t len) const;
  159. /**
  160. * affected by filter
  161. */
  162. bool getAllMissingUnusedIndexes(unsigned char* misbitfield, size_t mislen,
  163. const unsigned char* bitfield,
  164. size_t len) const;
  165. /**
  166. * affected by filter
  167. */
  168. size_t countMissingBlock() const;
  169. /**
  170. * affected by filter
  171. */
  172. size_t countMissingBlockNow() const;
  173. bool setUseBit(size_t index);
  174. bool unsetUseBit(size_t index);
  175. bool setBit(size_t index);
  176. bool unsetBit(size_t index);
  177. bool isBitSet(size_t index) const;
  178. bool isUseBitSet(size_t index) const;
  179. /**
  180. * affected by filter
  181. */
  182. bool isFilteredAllBitSet() const;
  183. bool isAllBitSet() const;
  184. bool isAllFilterBitSet() const;
  185. const unsigned char* getBitfield() const
  186. {
  187. return bitfield;
  188. }
  189. size_t getBitfieldLength() const
  190. {
  191. return bitfieldLength;
  192. }
  193. /**
  194. * affected by filter
  195. */
  196. size_t countFilteredBlock() const
  197. {
  198. return cachedNumFilteredBlock;
  199. }
  200. size_t countBlock() const
  201. {
  202. return blocks;
  203. }
  204. /**
  205. * affected by filter
  206. */
  207. size_t countFilteredBlockNow() const;
  208. size_t getMaxIndex() const
  209. {
  210. return blocks-1;
  211. }
  212. void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
  213. void clearAllBit();
  214. void setAllBit();
  215. void clearAllUseBit();
  216. void setAllUseBit();
  217. void addFilter(uint64_t offset, uint64_t length);
  218. void removeFilter(uint64_t offset, uint64_t length);
  219. // Add filter not in the range of [offset, offset+length) bytes
  220. void addNotFilter(uint64_t offset, uint64_t length);
  221. /**
  222. * Clears filter and disables filter
  223. */
  224. void clearFilter();
  225. void enableFilter();
  226. void disableFilter();
  227. bool isFilterEnabled() const
  228. {
  229. return filterEnabled;
  230. }
  231. /**
  232. * affected by filter
  233. */
  234. uint64_t getFilteredTotalLength() const
  235. {
  236. return cachedFilteredTotalLength;
  237. }
  238. /**
  239. * affected by filter
  240. */
  241. uint64_t getFilteredTotalLengthNow() const;
  242. uint64_t getCompletedLength() const
  243. {
  244. return cachedCompletedLength;
  245. }
  246. uint64_t getCompletedLengthNow() const;
  247. /**
  248. * affected by filter
  249. */
  250. uint64_t getFilteredCompletedLength() const
  251. {
  252. return cachedFilteredComletedLength;
  253. }
  254. /**
  255. * affected by filter
  256. */
  257. uint64_t getFilteredCompletedLengthNow() const;
  258. void setRandomizer(const SharedHandle<Randomizer>& randomizer);
  259. const SharedHandle<Randomizer>& getRandomizer() const
  260. {
  261. return randomizer;
  262. }
  263. void updateCache();
  264. bool isBitRangeSet(size_t startIndex, size_t endIndex) const;
  265. void unsetBitRange(size_t startIndex, size_t endIndex);
  266. void setBitRange(size_t startIndex, size_t endIndex);
  267. bool isBitSetOffsetRange(uint64_t offset, uint64_t length) const;
  268. uint64_t getMissingUnusedLength(size_t startingIndex) const;
  269. const unsigned char* getFilterBitfield() const
  270. {
  271. return filterBitfield;
  272. }
  273. };
  274. } // namespace aria2
  275. #endif // _D_BITFIELD_MAN_H_