BitfieldMan.h 6.8 KB

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