Changeset 55093 in webkit for trunk/JavaScriptCore/wtf/Vector.h


Ignore:
Timestamp:
Feb 22, 2010, 11:12:01 AM (15 years ago)
Author:
[email protected]
Message:

JSStringBuilder should not CRASH if allocation fails, it should throw a JSException.

Reviewed by Oliver Hunt.

  • runtime/JSGlobalObjectFunctions.cpp:
  • runtime/JSStringBuilder.h:

(JSC::JSStringBuilder::JSStringBuilder):
(JSC::JSStringBuilder::append):
(JSC::JSStringBuilder::build):

  • runtime/StringBuilder.h:

(JSC::StringBuilder::build):

  • wtf/Vector.h:

(WTF::VectorBufferBase::tryAllocateBuffer):
(WTF::):
(WTF::VectorBuffer::tryAllocateBuffer):
(WTF::::tryExpandCapacity):
(WTF::::tryReserveCapacity):
(WTF::::tryAppend):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/Vector.h

    r54619 r55093  
    287287        }
    288288
     289        bool tryAllocateBuffer(size_t newCapacity)
     290        {
     291            if (newCapacity > std::numeric_limits<size_t>::max() / sizeof(T))
     292                return false;
     293
     294            T* newBuffer;
     295            if (tryFastMalloc(newCapacity * sizeof(T)).getValue(newBuffer)) {
     296                m_capacity = newCapacity;
     297                m_buffer = newBuffer;
     298                return true;
     299            }
     300            return false;
     301        }
     302
    289303        void deallocateBuffer(T* bufferToDeallocate)
    290304        {
     
    362376
    363377        using Base::allocateBuffer;
     378        using Base::tryAllocateBuffer;
    364379        using Base::deallocateBuffer;
    365380
     
    404419                m_capacity = inlineCapacity;
    405420            }
     421        }
     422
     423        bool tryAllocateBuffer(size_t newCapacity)
     424        {
     425            if (newCapacity > inlineCapacity)
     426                return Base::tryAllocateBuffer(newCapacity);
     427            m_buffer = inlineBuffer();
     428            m_capacity = inlineCapacity;
     429            return true;
    406430        }
    407431
     
    539563        void resize(size_t size);
    540564        void reserveCapacity(size_t newCapacity);
     565        bool tryReserveCapacity(size_t newCapacity);
    541566        void reserveInitialCapacity(size_t initialCapacity);
    542567        void shrinkCapacity(size_t newCapacity);
     
    549574        template<typename U> void uncheckedAppend(const U& val);
    550575        template<size_t otherCapacity> void append(const Vector<T, otherCapacity>&);
     576        template<typename U> bool tryAppend(const U*, size_t);
    551577
    552578        template<typename U> void insert(size_t position, const U*, size_t);
     
    593619        void expandCapacity(size_t newMinCapacity);
    594620        const T* expandCapacity(size_t newMinCapacity, const T*);
     621        bool tryExpandCapacity(size_t newMinCapacity);
     622        const T* tryExpandCapacity(size_t newMinCapacity, const T*);
    595623        template<typename U> U* expandCapacity(size_t newMinCapacity, U*);
    596624
     
    743771    }
    744772
     773    template<typename T, size_t inlineCapacity>
     774    bool Vector<T, inlineCapacity>::tryExpandCapacity(size_t newMinCapacity)
     775    {
     776        return tryReserveCapacity(max(newMinCapacity, max(static_cast<size_t>(16), capacity() + capacity() / 4 + 1)));
     777    }
     778   
     779    template<typename T, size_t inlineCapacity>
     780    const T* Vector<T, inlineCapacity>::tryExpandCapacity(size_t newMinCapacity, const T* ptr)
     781    {
     782        if (ptr < begin() || ptr >= end()) {
     783            if (!tryExpandCapacity(newMinCapacity))
     784                return 0;
     785            return ptr;
     786        }
     787        size_t index = ptr - begin();
     788        if (!tryExpandCapacity(newMinCapacity))
     789            return 0;
     790        return begin() + index;
     791    }
     792
    745793    template<typename T, size_t inlineCapacity> template<typename U>
    746794    inline U* Vector<T, inlineCapacity>::expandCapacity(size_t newMinCapacity, U* ptr)
     
    798846   
    799847    template<typename T, size_t inlineCapacity>
     848    bool Vector<T, inlineCapacity>::tryReserveCapacity(size_t newCapacity)
     849    {
     850        if (newCapacity <= capacity())
     851            return true;
     852        T* oldBuffer = begin();
     853        T* oldEnd = end();
     854        if (!m_buffer.tryAllocateBuffer(newCapacity))
     855            return false;
     856        ASSERT(begin());
     857        TypeOperations::move(oldBuffer, oldEnd, begin());
     858        m_buffer.deallocateBuffer(oldBuffer);
     859        return true;
     860    }
     861   
     862    template<typename T, size_t inlineCapacity>
    800863    inline void Vector<T, inlineCapacity>::reserveInitialCapacity(size_t initialCapacity)
    801864    {
     
    846909            new (&dest[i]) T(data[i]);
    847910        m_size = newSize;
     911    }
     912
     913    template<typename T, size_t inlineCapacity> template<typename U>
     914    bool Vector<T, inlineCapacity>::tryAppend(const U* data, size_t dataSize)
     915    {
     916        size_t newSize = m_size + dataSize;
     917        if (newSize > capacity()) {
     918            data = tryExpandCapacity(newSize, data);
     919            if (!data)
     920                return false;
     921            ASSERT(begin());
     922        }
     923        if (newSize < m_size)
     924            return false;
     925        T* dest = end();
     926        for (size_t i = 0; i < dataSize; ++i)
     927            new (&dest[i]) T(data[i]);
     928        m_size = newSize;
     929        return true;
    848930    }
    849931
Note: See TracChangeset for help on using the changeset viewer.