123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /*
- * Copyright (C) 2011 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "config.h"
- #include "BitVector.h"
- #include <algorithm>
- #include <string.h>
- #include <wtf/Assertions.h>
- #include <wtf/FastMalloc.h>
- #include <wtf/StdLibExtras.h>
- namespace WTF {
- template <bool shared>
- void BitVector_base<shared>::setSlow(const BitVector_base<shared>& other)
- {
- uintptr_t newBitsOrPointer;
- if (other.isInline())
- newBitsOrPointer = other.m_bitsOrPointer;
- else {
- OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(other.size());
- memcpy(newOutOfLineBits->bits(), other.bits(), byteCount(other.size()));
- newBitsOrPointer = bitwise_cast<uintptr_t>(newOutOfLineBits) >> 1;
- }
- if (!isInline())
- OutOfLineBits::destroy(outOfLineBits());
- m_bitsOrPointer = newBitsOrPointer;
- }
- template <bool shared>
- void BitVector_base<shared>::resize(size_t numBits)
- {
- if (numBits <= maxInlineBits()) {
- if (isInline())
- return;
-
- OutOfLineBits* myOutOfLineBits = outOfLineBits();
- m_bitsOrPointer = makeInlineBits(*myOutOfLineBits->bits());
- OutOfLineBits::destroy(myOutOfLineBits);
- return;
- }
-
- resizeOutOfLine(numBits);
- }
- template <bool shared>
- void BitVector_base<shared>::clearAll()
- {
- if (isInline())
- m_bitsOrPointer = makeInlineBits(0);
- else
- memset(outOfLineBits()->bits(), 0, byteCount(size()));
- }
- template <>
- BitVector_base<false>::OutOfLineBits* BitVector_base<false>::OutOfLineBits::create(size_t numBits)
- {
- numBits = (numBits + bitsInPointer() - 1) & ~(bitsInPointer() - 1);
- size_t size = sizeof(OutOfLineBits) + sizeof(uintptr_t) * (numBits / bitsInPointer());
- OutOfLineBits* result = new (NotNull, fastMalloc(size)) OutOfLineBits(numBits);
- return result;
- }
- template <>
- void BitVector_base<false>::OutOfLineBits::destroy(OutOfLineBits* outOfLineBits)
- {
- fastFree(outOfLineBits);
- }
- #if ENABLE(DETACHED_JIT)
- template<>
- BitVector_base<true>::OutOfLineBits* BitVector_base<true>::OutOfLineBits::create(size_t numBits)
- {
- numBits = (numBits + bitsInPointer() - 1) & ~(bitsInPointer() - 1);
- size_t size = sizeof(OutOfLineBits) + sizeof(uintptr_t) * (numBits / bitsInPointer());
- OutOfLineBits* result = new (NotNull, JITSharedDataMemory::shared_malloc(size)) OutOfLineBits(numBits);
- return result;
- }
- template <>
- void BitVector_base<true>::OutOfLineBits::destroy(OutOfLineBits* outOfLineBits)
- {
- JITSharedDataMemory::shared_free(outOfLineBits);
- }
- #endif
- template <bool shared>
- void BitVector_base<shared>::resizeOutOfLine(size_t numBits)
- {
- ASSERT(numBits > maxInlineBits());
- OutOfLineBits* newOutOfLineBits = OutOfLineBits::create(numBits);
- size_t newNumWords = newOutOfLineBits->numWords();
- if (isInline()) {
- // Make sure that all of the bits are zero in case we do a no-op resize.
- *newOutOfLineBits->bits() = m_bitsOrPointer & ~(static_cast<uintptr_t>(1) << maxInlineBits());
- memset(newOutOfLineBits->bits() + 1, 0, (newNumWords - 1) * sizeof(void*));
- } else {
- if (numBits > size()) {
- size_t oldNumWords = outOfLineBits()->numWords();
- memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), oldNumWords * sizeof(void*));
- memset(newOutOfLineBits->bits() + oldNumWords, 0, (newNumWords - oldNumWords) * sizeof(void*));
- } else
- memcpy(newOutOfLineBits->bits(), outOfLineBits()->bits(), newOutOfLineBits->numWords() * sizeof(void*));
- OutOfLineBits::destroy(outOfLineBits());
- }
- m_bitsOrPointer = bitwise_cast<uintptr_t>(newOutOfLineBits) >> 1;
- }
- template <bool shared>
- void BitVector_base<shared>::dump(PrintStream& out)
- {
- for (size_t i = 0; i < size(); ++i) {
- if (get(i))
- out.printf("1");
- else
- out.printf("-");
- }
- }
- #if ENABLE(DETACHED_JIT)
- template void BitVector_base<true>::resizeOutOfLine(size_t numBits);
- template void BitVector_base<true>::setSlow(const BitVector_base<true>& other);
- #endif
- template void BitVector_base<false>::resize(size_t numBits);
- template void BitVector_base<false>::resizeOutOfLine(size_t numBits);
- template void BitVector_base<false>::clearAll();
- template void BitVector_base<false>::setSlow(const BitVector_base<false>& other);
- template void BitVector_base<false>::dump(PrintStream& out);
- } // namespace WTF
|