| OLD | NEW |
| (Empty) |
| 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 | |
| 26 #include "wtf/BitVector.h" | |
| 27 | |
| 28 #include "wtf/LeakAnnotations.h" | |
| 29 #include "wtf/PrintStream.h" | |
| 30 #include "wtf/allocator/Partitions.h" | |
| 31 #include <algorithm> | |
| 32 #include <string.h> | |
| 33 | |
| 34 namespace WTF { | |
| 35 | |
| 36 void BitVector::setSlow(const BitVector& other) { | |
| 37 uintptr_t newBitsOrPointer; | |
| 38 if (other.isInline()) { | |
| 39 newBitsOrPointer = other.m_bitsOrPointer; | |
| 40 } else { | |
| 41 OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(other.size()); | |
| 42 memcpy(newOutOfLineBits->bits(), other.bits(), byteCount(other.size())); | |
| 43 newBitsOrPointer = bitwiseCast<uintptr_t>(newOutOfLineBits) >> 1; | |
| 44 } | |
| 45 if (!isInline()) | |
| 46 OutOfLineBits::destroy(outOfLineBits()); | |
| 47 m_bitsOrPointer = newBitsOrPointer; | |
| 48 } | |
| 49 | |
| 50 void BitVector::resize(size_t numBits) { | |
| 51 if (numBits <= maxInlineBits()) { | |
| 52 if (isInline()) | |
| 53 return; | |
| 54 | |
| 55 OutOfLineBits* myOutOfLineBits = outOfLineBits(); | |
| 56 m_bitsOrPointer = makeInlineBits(*myOutOfLineBits->bits()); | |
| 57 OutOfLineBits::destroy(myOutOfLineBits); | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 resizeOutOfLine(numBits); | |
| 62 } | |
| 63 | |
| 64 void BitVector::clearAll() { | |
| 65 if (isInline()) | |
| 66 m_bitsOrPointer = makeInlineBits(0); | |
| 67 else | |
| 68 memset(outOfLineBits()->bits(), 0, byteCount(size())); | |
| 69 } | |
| 70 | |
| 71 BitVector::OutOfLineBits* BitVector::OutOfLineBits::create(size_t numBits) { | |
| 72 // Because of the way BitVector stores the pointer, memory tools | |
| 73 // will erroneously report a leak here. | |
| 74 WTF_INTERNAL_LEAK_SANITIZER_DISABLED_SCOPE; | |
| 75 numBits = (numBits + bitsInPointer() - 1) & | |
| 76 ~(bitsInPointer() - static_cast<size_t>(1)); | |
| 77 size_t size = | |
| 78 sizeof(OutOfLineBits) + sizeof(uintptr_t) * (numBits / bitsInPointer()); | |
| 79 void* allocation = Partitions::bufferMalloc( | |
| 80 size, WTF_HEAP_PROFILER_TYPE_NAME(OutOfLineBits)); | |
| 81 OutOfLineBits* result = new (NotNull, allocation) OutOfLineBits(numBits); | |
| 82 return result; | |
| 83 } | |
| 84 | |
| 85 void BitVector::OutOfLineBits::destroy(OutOfLineBits* outOfLineBits) { | |
| 86 Partitions::bufferFree(outOfLineBits); | |
| 87 } | |
| 88 | |
| 89 void BitVector::resizeOutOfLine(size_t numBits) { | |
| 90 DCHECK_GT(numBits, maxInlineBits()); | |
| 91 OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(numBits); | |
| 92 size_t newNumWords = newOutOfLineBits->numWords(); | |
| 93 if (isInline()) { | |
| 94 // Make sure that all of the bits are zero in case we do a no-op resize. | |
| 95 *newOutOfLineBits->bits() = | |
| 96 m_bitsOrPointer & ~(static_cast<uintptr_t>(1) << maxInlineBits()); | |
| 97 memset(newOutOfLineBits->bits() + 1, 0, (newNumWords - 1) * sizeof(void*)); | |
| 98 } else { | |
| 99 if (numBits > size()) { | |
| 100 size_t oldNumWords = outOfLineBits()->numWords(); | |
| 101 memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), | |
| 102 oldNumWords * sizeof(void*)); | |
| 103 memset(newOutOfLineBits->bits() + oldNumWords, 0, | |
| 104 (newNumWords - oldNumWords) * sizeof(void*)); | |
| 105 } else { | |
| 106 memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), | |
| 107 newOutOfLineBits->numWords() * sizeof(void*)); | |
| 108 } | |
| 109 OutOfLineBits::destroy(outOfLineBits()); | |
| 110 } | |
| 111 m_bitsOrPointer = bitwiseCast<uintptr_t>(newOutOfLineBits) >> 1; | |
| 112 } | |
| 113 | |
| 114 void BitVector::dump(PrintStream& out) { | |
| 115 for (size_t i = 0; i < size(); ++i) { | |
| 116 if (get(i)) | |
| 117 out.printf("1"); | |
| 118 else | |
| 119 out.printf("-"); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 } // namespace WTF | |
| OLD | NEW |