| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2011 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 #include "wtf/LeakAnnotations.h" | 29 #include "wtf/LeakAnnotations.h" |
| 30 #include "wtf/PartitionAlloc.h" | 30 #include "wtf/PartitionAlloc.h" |
| 31 #include "wtf/Partitions.h" | 31 #include "wtf/Partitions.h" |
| 32 #include "wtf/PrintStream.h" | 32 #include "wtf/PrintStream.h" |
| 33 #include <algorithm> | 33 #include <algorithm> |
| 34 #include <string.h> | 34 #include <string.h> |
| 35 | 35 |
| 36 namespace WTF { | 36 namespace WTF { |
| 37 | 37 |
| 38 void BitVector::setSlow(const BitVector& other) | 38 void BitVector::setSlow(const BitVector& other) { |
| 39 { | 39 uintptr_t newBitsOrPointer; |
| 40 uintptr_t newBitsOrPointer; | 40 if (other.isInline()) { |
| 41 if (other.isInline()) { | 41 newBitsOrPointer = other.m_bitsOrPointer; |
| 42 newBitsOrPointer = other.m_bitsOrPointer; | 42 } else { |
| 43 } else { | 43 OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(other.size()); |
| 44 OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(other.size()); | 44 memcpy(newOutOfLineBits->bits(), other.bits(), byteCount(other.size())); |
| 45 memcpy(newOutOfLineBits->bits(), other.bits(), byteCount(other.size())); | 45 newBitsOrPointer = bitwise_cast<uintptr_t>(newOutOfLineBits) >> 1; |
| 46 newBitsOrPointer = bitwise_cast<uintptr_t>(newOutOfLineBits) >> 1; | 46 } |
| 47 } | 47 if (!isInline()) |
| 48 if (!isInline()) | 48 OutOfLineBits::destroy(outOfLineBits()); |
| 49 OutOfLineBits::destroy(outOfLineBits()); | 49 m_bitsOrPointer = newBitsOrPointer; |
| 50 m_bitsOrPointer = newBitsOrPointer; | |
| 51 } | 50 } |
| 52 | 51 |
| 53 void BitVector::resize(size_t numBits) | 52 void BitVector::resize(size_t numBits) { |
| 54 { | 53 if (numBits <= maxInlineBits()) { |
| 55 if (numBits <= maxInlineBits()) { | 54 if (isInline()) |
| 56 if (isInline()) | 55 return; |
| 57 return; | |
| 58 | 56 |
| 59 OutOfLineBits* myOutOfLineBits = outOfLineBits(); | 57 OutOfLineBits* myOutOfLineBits = outOfLineBits(); |
| 60 m_bitsOrPointer = makeInlineBits(*myOutOfLineBits->bits()); | 58 m_bitsOrPointer = makeInlineBits(*myOutOfLineBits->bits()); |
| 61 OutOfLineBits::destroy(myOutOfLineBits); | 59 OutOfLineBits::destroy(myOutOfLineBits); |
| 62 return; | 60 return; |
| 63 } | 61 } |
| 64 | 62 |
| 65 resizeOutOfLine(numBits); | 63 resizeOutOfLine(numBits); |
| 66 } | 64 } |
| 67 | 65 |
| 68 void BitVector::clearAll() | 66 void BitVector::clearAll() { |
| 69 { | 67 if (isInline()) |
| 70 if (isInline()) | 68 m_bitsOrPointer = makeInlineBits(0); |
| 71 m_bitsOrPointer = makeInlineBits(0); | 69 else |
| 72 else | 70 memset(outOfLineBits()->bits(), 0, byteCount(size())); |
| 73 memset(outOfLineBits()->bits(), 0, byteCount(size())); | |
| 74 } | 71 } |
| 75 | 72 |
| 76 BitVector::OutOfLineBits* BitVector::OutOfLineBits::create(size_t numBits) | 73 BitVector::OutOfLineBits* BitVector::OutOfLineBits::create(size_t numBits) { |
| 77 { | 74 // Because of the way BitVector stores the pointer, memory tools |
| 78 // Because of the way BitVector stores the pointer, memory tools | 75 // will erroneously report a leak here. |
| 79 // will erroneously report a leak here. | 76 WTF_ANNOTATE_SCOPED_MEMORY_LEAK; |
| 80 WTF_ANNOTATE_SCOPED_MEMORY_LEAK; | 77 numBits = (numBits + bitsInPointer() - 1) & ~(bitsInPointer() - 1); |
| 81 numBits = (numBits + bitsInPointer() - 1) & ~(bitsInPointer() - 1); | 78 size_t size = sizeof(OutOfLineBits) + sizeof(uintptr_t) * (numBits / bitsInPoi
nter()); |
| 82 size_t size = sizeof(OutOfLineBits) + sizeof(uintptr_t) * (numBits / bitsInP
ointer()); | 79 void* allocation = Partitions::bufferMalloc(size); |
| 83 void* allocation = Partitions::bufferMalloc(size); | 80 OutOfLineBits* result = new (NotNull, allocation) OutOfLineBits(numBits); |
| 84 OutOfLineBits* result = new (NotNull, allocation) OutOfLineBits(numBits); | 81 return result; |
| 85 return result; | |
| 86 } | 82 } |
| 87 | 83 |
| 88 void BitVector::OutOfLineBits::destroy(OutOfLineBits* outOfLineBits) | 84 void BitVector::OutOfLineBits::destroy(OutOfLineBits* outOfLineBits) { |
| 89 { | 85 Partitions::bufferFree(outOfLineBits); |
| 90 Partitions::bufferFree(outOfLineBits); | |
| 91 } | 86 } |
| 92 | 87 |
| 93 void BitVector::resizeOutOfLine(size_t numBits) | 88 void BitVector::resizeOutOfLine(size_t numBits) { |
| 94 { | 89 ASSERT(numBits > maxInlineBits()); |
| 95 ASSERT(numBits > maxInlineBits()); | 90 OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(numBits); |
| 96 OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(numBits); | 91 size_t newNumWords = newOutOfLineBits->numWords(); |
| 97 size_t newNumWords = newOutOfLineBits->numWords(); | 92 if (isInline()) { |
| 98 if (isInline()) { | 93 // Make sure that all of the bits are zero in case we do a no-op resize. |
| 99 // Make sure that all of the bits are zero in case we do a no-op resize. | 94 *newOutOfLineBits->bits() = m_bitsOrPointer & ~(static_cast<uintptr_t>(1) <<
maxInlineBits()); |
| 100 *newOutOfLineBits->bits() = m_bitsOrPointer & ~(static_cast<uintptr_t>(1
) << maxInlineBits()); | 95 memset(newOutOfLineBits->bits() + 1, 0, (newNumWords - 1) * sizeof(void*)); |
| 101 memset(newOutOfLineBits->bits() + 1, 0, (newNumWords - 1) * sizeof(void*
)); | 96 } else { |
| 97 if (numBits > size()) { |
| 98 size_t oldNumWords = outOfLineBits()->numWords(); |
| 99 memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), oldNumWords * si
zeof(void*)); |
| 100 memset(newOutOfLineBits->bits() + oldNumWords, 0, (newNumWords - oldNumWor
ds) * sizeof(void*)); |
| 102 } else { | 101 } else { |
| 103 if (numBits > size()) { | 102 memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), newOutOfLineBits
->numWords() * sizeof(void*)); |
| 104 size_t oldNumWords = outOfLineBits()->numWords(); | |
| 105 memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), oldNumWord
s * sizeof(void*)); | |
| 106 memset(newOutOfLineBits->bits() + oldNumWords, 0, (newNumWords - old
NumWords) * sizeof(void*)); | |
| 107 } else { | |
| 108 memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), newOutOfLi
neBits->numWords() * sizeof(void*)); | |
| 109 } | |
| 110 OutOfLineBits::destroy(outOfLineBits()); | |
| 111 } | 103 } |
| 112 m_bitsOrPointer = bitwise_cast<uintptr_t>(newOutOfLineBits) >> 1; | 104 OutOfLineBits::destroy(outOfLineBits()); |
| 105 } |
| 106 m_bitsOrPointer = bitwise_cast<uintptr_t>(newOutOfLineBits) >> 1; |
| 113 } | 107 } |
| 114 | 108 |
| 115 void BitVector::dump(PrintStream& out) | 109 void BitVector::dump(PrintStream& out) { |
| 116 { | 110 for (size_t i = 0; i < size(); ++i) { |
| 117 for (size_t i = 0; i < size(); ++i) { | 111 if (get(i)) |
| 118 if (get(i)) | 112 out.printf("1"); |
| 119 out.printf("1"); | 113 else |
| 120 else | 114 out.printf("-"); |
| 121 out.printf("-"); | 115 } |
| 122 } | |
| 123 } | 116 } |
| 124 | 117 |
| 125 } // namespace WTF | 118 } // namespace WTF |
| OLD | NEW |