BitVector.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef BitVector_h
  26. #define BitVector_h
  27. #include <stdio.h>
  28. #include <wtf/Assertions.h>
  29. #include <wtf/PrintStream.h>
  30. #include <wtf/StdLibExtras.h>
  31. namespace WTF {
  32. // This is a space-efficient, resizeable bitvector class. In the common case it
  33. // occupies one word, but if necessary, it will inflate this one word to point
  34. // to a single chunk of out-of-line allocated storage to store an arbitrary number
  35. // of bits.
  36. //
  37. // - The bitvector remembers the bound of how many bits can be stored, but this
  38. // may be slightly greater (by as much as some platform-specific constant)
  39. // than the last argument passed to ensureSize().
  40. //
  41. // - The bitvector can resize itself automatically (set, clear, get) or can be used
  42. // in a manual mode, which is faster (quickSet, quickClear, quickGet, ensureSize).
  43. //
  44. // - Accesses ASSERT that you are within bounds.
  45. //
  46. // - Bits are automatically initialized to zero.
  47. //
  48. // On the other hand, this BitVector class may not be the fastest around, since
  49. // it does conditionals on every get/set/clear. But it is great if you need to
  50. // juggle a lot of variable-length BitVectors and you're worried about wasting
  51. // space.
  52. template <bool shared = false>
  53. class BitVector_base {
  54. public:
  55. BitVector_base()
  56. : m_bitsOrPointer(makeInlineBits(0))
  57. {
  58. }
  59. explicit BitVector_base(size_t numBits)
  60. : m_bitsOrPointer(makeInlineBits(0))
  61. {
  62. ensureSize(numBits);
  63. }
  64. BitVector_base(const BitVector_base& other)
  65. : m_bitsOrPointer(makeInlineBits(0))
  66. {
  67. (*this) = other;
  68. }
  69. ~BitVector_base()
  70. {
  71. if (isInline())
  72. return;
  73. OutOfLineBits::destroy(outOfLineBits());
  74. }
  75. BitVector_base<shared>& operator=(const BitVector_base<shared>& other)
  76. {
  77. if (isInline() && other.isInline())
  78. m_bitsOrPointer = other.m_bitsOrPointer;
  79. else
  80. setSlow(other);
  81. return *this;
  82. }
  83. size_t size() const
  84. {
  85. if (isInline())
  86. return maxInlineBits();
  87. return outOfLineBits()->numBits();
  88. }
  89. void ensureSize(size_t numBits)
  90. {
  91. if (numBits <= size())
  92. return;
  93. resizeOutOfLine(numBits);
  94. }
  95. // Like ensureSize(), but supports reducing the size of the bitvector.
  96. WTF_EXPORT_PRIVATE void resize(size_t numBits);
  97. WTF_EXPORT_PRIVATE void clearAll();
  98. bool quickGet(size_t bit) const
  99. {
  100. ASSERT_WITH_SECURITY_IMPLICATION(bit < size());
  101. return !!(bits()[bit / bitsInPointer()] & (static_cast<uintptr_t>(1) << (bit & (bitsInPointer() - 1))));
  102. }
  103. void quickSet(size_t bit)
  104. {
  105. ASSERT_WITH_SECURITY_IMPLICATION(bit < size());
  106. bits()[bit / bitsInPointer()] |= (static_cast<uintptr_t>(1) << (bit & (bitsInPointer() - 1)));
  107. }
  108. void quickClear(size_t bit)
  109. {
  110. ASSERT_WITH_SECURITY_IMPLICATION(bit < size());
  111. bits()[bit / bitsInPointer()] &= ~(static_cast<uintptr_t>(1) << (bit & (bitsInPointer() - 1)));
  112. }
  113. void quickSet(size_t bit, bool value)
  114. {
  115. if (value)
  116. quickSet(bit);
  117. else
  118. quickClear(bit);
  119. }
  120. bool get(size_t bit) const
  121. {
  122. if (bit >= size())
  123. return false;
  124. return quickGet(bit);
  125. }
  126. void set(size_t bit)
  127. {
  128. ensureSize(bit + 1);
  129. quickSet(bit);
  130. }
  131. void ensureSizeAndSet(size_t bit, size_t size)
  132. {
  133. ensureSize(size);
  134. quickSet(bit);
  135. }
  136. void clear(size_t bit)
  137. {
  138. if (bit >= size())
  139. return;
  140. quickClear(bit);
  141. }
  142. void set(size_t bit, bool value)
  143. {
  144. if (value)
  145. set(bit);
  146. else
  147. clear(bit);
  148. }
  149. void dump(PrintStream& out);
  150. private:
  151. static unsigned bitsInPointer()
  152. {
  153. return sizeof(void*) << 3;
  154. }
  155. static unsigned maxInlineBits()
  156. {
  157. return bitsInPointer() - 1;
  158. }
  159. static size_t byteCount(size_t bitCount)
  160. {
  161. return (bitCount + 7) >> 3;
  162. }
  163. static uintptr_t makeInlineBits(uintptr_t bits)
  164. {
  165. ASSERT(!(bits & (static_cast<uintptr_t>(1) << maxInlineBits())));
  166. return bits | (static_cast<uintptr_t>(1) << maxInlineBits());
  167. }
  168. class OutOfLineBits {
  169. public:
  170. size_t numBits() const { return m_numBits; }
  171. size_t numWords() const { return (m_numBits + bitsInPointer() - 1) / bitsInPointer(); }
  172. uintptr_t* bits() { return bitwise_cast<uintptr_t*>(this + 1); }
  173. const uintptr_t* bits() const { return bitwise_cast<const uintptr_t*>(this + 1); }
  174. static WTF_EXPORT_PRIVATE OutOfLineBits* create(size_t numBits);
  175. static WTF_EXPORT_PRIVATE void destroy(OutOfLineBits*);
  176. private:
  177. OutOfLineBits(size_t numBits)
  178. : m_numBits(numBits)
  179. {
  180. }
  181. size_t m_numBits;
  182. };
  183. bool isInline() const { return m_bitsOrPointer >> maxInlineBits(); }
  184. const OutOfLineBits* outOfLineBits() const { return bitwise_cast<const OutOfLineBits*>(m_bitsOrPointer << 1); }
  185. OutOfLineBits* outOfLineBits() { return bitwise_cast<OutOfLineBits*>(m_bitsOrPointer << 1); }
  186. WTF_EXPORT_PRIVATE void resizeOutOfLine(size_t numBits);
  187. WTF_EXPORT_PRIVATE void setSlow(const BitVector_base& other);
  188. uintptr_t* bits()
  189. {
  190. if (isInline())
  191. return &m_bitsOrPointer;
  192. return outOfLineBits()->bits();
  193. }
  194. const uintptr_t* bits() const
  195. {
  196. if (isInline())
  197. return &m_bitsOrPointer;
  198. return outOfLineBits()->bits();
  199. }
  200. uintptr_t m_bitsOrPointer;
  201. };
  202. typedef BitVector_base<false> BitVector;
  203. #if ENABLE(DETACHED_JIT)
  204. typedef BitVector_base<true> BitVector_shared;
  205. #else
  206. typedef BitVector_base<false> BitVector_shared;
  207. #endif
  208. } // namespace WTF
  209. using WTF::BitVector;
  210. using WTF::BitVector_shared;
  211. #endif // BitVector_h