BitVector.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include "config.h"
  26. #include "BitVector.h"
  27. #include <algorithm>
  28. #include <string.h>
  29. #include <wtf/Assertions.h>
  30. #include <wtf/FastMalloc.h>
  31. #include <wtf/StdLibExtras.h>
  32. namespace WTF {
  33. template <bool shared>
  34. void BitVector_base<shared>::setSlow(const BitVector_base<shared>& other)
  35. {
  36. uintptr_t newBitsOrPointer;
  37. if (other.isInline())
  38. newBitsOrPointer = other.m_bitsOrPointer;
  39. else {
  40. OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(other.size());
  41. memcpy(newOutOfLineBits->bits(), other.bits(), byteCount(other.size()));
  42. newBitsOrPointer = bitwise_cast<uintptr_t>(newOutOfLineBits) >> 1;
  43. }
  44. if (!isInline())
  45. OutOfLineBits::destroy(outOfLineBits());
  46. m_bitsOrPointer = newBitsOrPointer;
  47. }
  48. template <bool shared>
  49. void BitVector_base<shared>::resize(size_t numBits)
  50. {
  51. if (numBits <= maxInlineBits()) {
  52. if (isInline())
  53. return;
  54. OutOfLineBits* myOutOfLineBits = outOfLineBits();
  55. m_bitsOrPointer = makeInlineBits(*myOutOfLineBits->bits());
  56. OutOfLineBits::destroy(myOutOfLineBits);
  57. return;
  58. }
  59. resizeOutOfLine(numBits);
  60. }
  61. template <bool shared>
  62. void BitVector_base<shared>::clearAll()
  63. {
  64. if (isInline())
  65. m_bitsOrPointer = makeInlineBits(0);
  66. else
  67. memset(outOfLineBits()->bits(), 0, byteCount(size()));
  68. }
  69. template <>
  70. BitVector_base<false>::OutOfLineBits* BitVector_base<false>::OutOfLineBits::create(size_t numBits)
  71. {
  72. numBits = (numBits + bitsInPointer() - 1) & ~(bitsInPointer() - 1);
  73. size_t size = sizeof(OutOfLineBits) + sizeof(uintptr_t) * (numBits / bitsInPointer());
  74. OutOfLineBits* result = new (NotNull, fastMalloc(size)) OutOfLineBits(numBits);
  75. return result;
  76. }
  77. template <>
  78. void BitVector_base<false>::OutOfLineBits::destroy(OutOfLineBits* outOfLineBits)
  79. {
  80. fastFree(outOfLineBits);
  81. }
  82. #if ENABLE(DETACHED_JIT)
  83. template<>
  84. BitVector_base<true>::OutOfLineBits* BitVector_base<true>::OutOfLineBits::create(size_t numBits)
  85. {
  86. numBits = (numBits + bitsInPointer() - 1) & ~(bitsInPointer() - 1);
  87. size_t size = sizeof(OutOfLineBits) + sizeof(uintptr_t) * (numBits / bitsInPointer());
  88. OutOfLineBits* result = new (NotNull, JITSharedDataMemory::shared_malloc(size)) OutOfLineBits(numBits);
  89. return result;
  90. }
  91. template <>
  92. void BitVector_base<true>::OutOfLineBits::destroy(OutOfLineBits* outOfLineBits)
  93. {
  94. JITSharedDataMemory::shared_free(outOfLineBits);
  95. }
  96. #endif
  97. template <bool shared>
  98. void BitVector_base<shared>::resizeOutOfLine(size_t numBits)
  99. {
  100. ASSERT(numBits > maxInlineBits());
  101. OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(numBits);
  102. size_t newNumWords = newOutOfLineBits->numWords();
  103. if (isInline()) {
  104. // Make sure that all of the bits are zero in case we do a no-op resize.
  105. *newOutOfLineBits->bits() = m_bitsOrPointer & ~(static_cast<uintptr_t>(1) << maxInlineBits());
  106. memset(newOutOfLineBits->bits() + 1, 0, (newNumWords - 1) * sizeof(void*));
  107. } else {
  108. if (numBits > size()) {
  109. size_t oldNumWords = outOfLineBits()->numWords();
  110. memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), oldNumWords * sizeof(void*));
  111. memset(newOutOfLineBits->bits() + oldNumWords, 0, (newNumWords - oldNumWords) * sizeof(void*));
  112. } else
  113. memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), newOutOfLineBits->numWords() * sizeof(void*));
  114. OutOfLineBits::destroy(outOfLineBits());
  115. }
  116. m_bitsOrPointer = bitwise_cast<uintptr_t>(newOutOfLineBits) >> 1;
  117. }
  118. template <bool shared>
  119. void BitVector_base<shared>::dump(PrintStream& out)
  120. {
  121. for (size_t i = 0; i < size(); ++i) {
  122. if (get(i))
  123. out.printf("1");
  124. else
  125. out.printf("-");
  126. }
  127. }
  128. #if ENABLE(DETACHED_JIT)
  129. template void BitVector_base<true>::resizeOutOfLine(size_t numBits);
  130. template void BitVector_base<true>::setSlow(const BitVector_base<true>& other);
  131. #endif
  132. template void BitVector_base<false>::resize(size_t numBits);
  133. template void BitVector_base<false>::resizeOutOfLine(size_t numBits);
  134. template void BitVector_base<false>::clearAll();
  135. template void BitVector_base<false>::setSlow(const BitVector_base<false>& other);
  136. template void BitVector_base<false>::dump(PrintStream& out);
  137. } // namespace WTF